Having trouble loading in multiple .bmp files for textures OpenGL C++
Having some trouble trying to get to .bmp files into an array so I can use them for textures.
EDIT
Display.h
#pragma once
#include "Ball.h"
#include "Bitmap.h"
class Display
{
private :
static Bitmap m_HeightMap;
static unsigned int textures;
// static float TableX, TableY, TableZ;
static float eyeZ, eyeY, eyeX;
static float lightX, lightY, lightZ;
static float Position[4];
static float translateZ, translateX;
//static Timer m_Timer;
public:
static void Init(int argc, char ** argv);
static void DisplayScene();
static void Resize(int w, int h);
static void KeyboardInput(unsigned char pKey, int pX, int pY);
static void Idle();
static void DrawLight(int pLightNumber, float * pPos);
static void Table(float zStart, float xStart, float zEnd, float xEnd);
static void BallCollisions(float pTimestep, Vector3f New_position, Vector3f New_velocity);
};
void main(int argc, char ** argv)
{
Display::Init(argc, argv);
}
Display.cpp
#include "Display.h"
#include "Vector3f.h"
#include "Ball.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include <math.h>
#include "Bitmap.h"
static float TableWidth = 4; // Z axis normal = 4
float Display::eyeX = -7.5; //-7.5
float Display::eyeY = 3; //3
float Display::eyeZ = 5; //5
float Display::Position[4] = { 1.0f, 0.0f, -3.5, 1.0f };
float Display::translateZ = -3.5;
float Display::translateX = 0.0;
//Timer Display::m_Timer = Timer();
float Display::lightX = 5.0; //5 2.5
float Display::lightY = 5.0;
float Display::lightZ = 2.5;
float m_TableX = -5.0f;
float m_TableZ = -2.5f;
float m_TableWidth = 2.5f;
float m_TableLength = 5.0f;
float ballx = 0.7;
float bally = 0.1;
float ballz = -0.7;
Ball Redball;
float BALL_RED_START = 0;
float RADIUS_OF_BALL = 0.3;
float BALL_RED_END = 8;
Ball Yellowball;
float BALL_YELLOW_START = 0;
float BALL_YELLOW_END = 8;
Bitmap Display::m_HeightMap;
unsigned int Display:: textures;
GLuint textures[2];
void loadTexture(GLuint texture, const char* filename)
{
Bitmap image;
Bitmap image[2];
image[0].loadBMP("myTexture.bmp");
image[1].loadBMP("myTexture2.bmp");
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data);
}
void Display::Init(int argc, char ** argv)
{
glutInit(&argc, argv); // initializes glut
// sets display mode. These parameter set RGB colour model
// and double buffering.
glutInitWindowSize(500,500);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Pool Version 1.0");
// Set glut callback functions
glutDisplayFunc(Display::DisplayScene);
glutIdleFunc(Display::Idle);
glutReshapeFunc(Display::Resize);
glutKeyboardFunc(Display::KeyboardInput);
//m_Timer.getSeconds();
glEnable(GL_DEPTH_TEST);
glPointSize(5);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glClearColor(0,0,0,1);
glEnable(GL_COLOR_MATERIAL);
float white[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glEnable(GL_LIGHT0);
Redball.SetPos(Vector3f(0.0,0.3,0.0));
Ball Redball[8];
for(int i = BALL_RED_START; i < BALL_RED_START; i++)
{
glColor3f(1,0,0);
Redball[i].SetPos(Vector3f (i+128,RADIUS_OF_BALL,45));
}
Ball Yellowball[8];
for(int i = BALL_YELLOW_START; i < BALL_YELLOW_START; i++)
{
glColor3f(2,1,0);
Yellowball[i].SetPos(Vector3f (i+128,RADIUS_OF_BALL,45));
}
GLuint *textures = new GLuint[2];
glGenTextures(2, textures);
loadTexture(textures[0], "myTexture.bmp");
loadTexture(textures[1], "myTexture2.bmp");
// Begin glut main loop
glutMainLoop();
}
#pragma region Table Drawing Code
void drawTable()
{
glBegin(GL_QUADS); // RIGHT
glNormal3f(0,0,1);
glEnable(GL_TEXTURE_2D);
//glColor3d(0.5,0.35,0.05);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(m_TableX, 0.0f, m_TableWidth); //bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(m_TableLength, 0.0f, m_TableWidth);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(m_TableLength, 0.0f, m_TableWidth);//top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(m_TableX, 0.0f, m_TableWidth); //top left
glEnd();
glBegin(GL_QUADS); //BACK
glNormal3f(1,0,0);
glEnable(GL_TEXTURE_2D);
glColor4f(1,1,1,0);
//glColor3d(0.5,0.35,0.05);
glTexCoord2f(0.0f,0.0f);
glVertex3f(m_TableLength, 0.0f, m_TableWidth);
glTexCoord2f(1.0f,0.0f);
glVertex3f(m_TableLength, 0.0f, m_TableZ);
glTexCoord2f(1.0f,1.0f);
glVertex3f(m_TableLength, 0.0f, m_TableZ);
glTexCoord2f(0.0f,1.0f);
glVertex3f(m_TableLength, 0.0f, m_TableWidth);
glEnd();
glBegin(GL_QUADS); //FRONT
glNormal3f(-1,0,0);
//glColor3d(0.5,0.35,0.05);
glVertex3f(m_TableX, 0.0f, m_TableZ);
glVertex3f(m_TableX, 0.0f, m_TableWidth);
glVertex3f(m_TableX, 0.0f, m_TableWidth);
glVertex3f(m_TableX, 0.0f, m_TableZ);
glEnd();
glBegin(GL_QUADS); //lEFT
glNormal3f(0,0,-1);
glColor3d(0.5,0.35,0.05);
glVertex3f(m_TableX, 0.0f, m_TableZ);
glVertex3f(m_TableLength, 0.0f, m_TableZ);
glVertex3f(m_TableX, 0.0f, m_TableZ);
glVertex3f(m_TableLength, 0.0f, m_TableZ);
glEnd();
glBegin(GL_QUADS); //BOTTOM
glNormal3f(0,-1,0);
glColor3d(0.5,0.35,0.05);
glVertex3f(m_TableX, -0.001f, m_TableWidth);
glVertex3f(m_TableLength, -0.001f, m_TableWidth);
glVertex3f(m_TableLength, -0.001f, m_TableZ);
glVertex3f(m_TableX, -0.001f, m_TableZ);
glEnd();
}
#pragma endregion
#pragma region Cushion Drawing Code
// width 0.5 = dividing table width (5) by 9 to get the proportional size of 10cm
// height 0.3 = dividing the width by 10 to find the value of 1cm = 0.05. 0.05 x 6 = 0.3
// starting at 0.4 to leave for the pocket. (1cm x 8 = pocket size = 0.05 x 8 = 0.4)
//right cushion
void drawCushions()
{
glBegin(GL_QUADS); //BOTTOM
glNormal3f(0,-1,0);
glColor3d(0.5,0.40,0.05);
glVertex3f(-4.5, 0.01, 2);//bottom left
glVertex3f(-4.5, 0.01, 2.5);//bottom right
glVertex3f(4.5, 0.01, 2.5); //top right
glVertex3f(4.5, 0.01, 2);//top left
glEnd();
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //TOP
glBindTexture(GL_TEXTURE_2D, textures[0]);
glNormal3f(0,1,0);
glColor4f(1,1,1,0);
//glColor3d(0.5,0.40,0.05);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-4.5, 0.3, 2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(-4.5, 0.3, 2.5);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.5, 0.3, 2.5);//top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(4.5, 0.3, 2);//top left
glBindTexture(GL_TEXTURE_2D, 0); // Set the GL texture to NULL, standard cleanup
glEnd();
glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //RIGHT
glNormal3f(0,0,1);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-4.5, 0, 2.5);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(4.5, 0, 2.5);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.5, 0.3, 2.5); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(-4.5, 0.3, 2.5);//top left
glBindTexture(GL_TEXTURE_2D, 0); // Set the GL texture to NULL, standard cleanup
glEnd();
glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //LEFT
glNormal3f(0,0,-1);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-4.5, 0, 2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(4.5, 0, 2);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.5, 0.3, 2); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(-4.5, 0.3, 2);//top left
glEnd();
glDisable(GL_TEXTURE_2D);
//Left Cushion
glBegin(GL_QUADS); //BOTTOM
glNormal3f(0,-1,0);
glColor3d(0.5,0.40,0.05);
glVertex3f(-4.5, 0.01, -2.5);//bottom left
glVertex3f(-4.5, 0.01, -2);//bottom right
glVertex3f(4.5, 0.01, -2); //top right
glVertex3f(4.5, 0.01, -2.5);//top left
glEnd();
//glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //TOP
glNormal3f(0,1,0);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-4.5, 0.3, -2.5);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(-4.5, 0.3, -2);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.5, 0.3, -2); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(4.5, 0.3, -2.5);//top left
glEnd();
//glDisable(GL_TEXTURE_2D);
//glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //RIGHT
glNormal3f(0,0,1);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-4.5, 0, -2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(4.5, 0, -2);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.5, 0.3, -2); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(-4.5, 0.3, -2);//top left
glEnd();
//glDisable(GL_TEXTURE_2D);
//glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //LEFT
glNormal3f(0,0,-1);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-4.5, 0, -2.5);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(4.5, 0, -2.5);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.5, 0.3, -2.5); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(-4.5, 0.3, -2.5);//top left
glEnd();
//glDisable(GL_TEXTURE_2D);
//Bottom Cushion
glBegin(GL_QUADS); //BOTTOM
glNormal3f(0,-1,0);
glColor3d(0.5,0.40,0.05);
glVertex3f(-5, 0.01, -2);//bottom left
glVertex3f(-5, 0.01, 2);//bottom right
glVertex3f(-4.5, 0.01, 2); //top right
glVertex3f(-4.5, 0.01, -2);//top left
glEnd();
//glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //TOP
glNormal3f(0,1,0);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-5, 0.3, -2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(-5, 0.3, 2);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(-4.5, 0.3, 2); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(-4.5, 0.3, -2);//top left
glEnd();
//glDisable(GL_TEXTURE_2D);
//glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //RIGHT
glNormal3f(0,0,1);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-5, 0, -2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(-5, 0, 2);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(-5, 0.3, 2); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(-5, 0.3, -2);//top left
glEnd();
//glDisable(GL_TEXTURE_2D);
//glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //LEFT
glNormal3f(0,0,-1);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-5, 0.0,-2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(-5, 0.0, 2);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(-5, 0.3, 2); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(-5, 0.3,-2);//top left
glEnd();
//glDisable(GL_TEXTURE_2D);
//top cushion
glBegin(GL_QUADS); //BOTTOM
glNormal3f(0,-1,0);
glColor3d(0.5,0.40,0.05);
glVertex3f(4.5, 0.01, -2);//bottom left
glVertex3f(4.5, 0.01, 2);//bottom right
glVertex3f(5, 0.01, 2); //top right
glVertex3f(5, 0.01, -2);//top left
glEnd();
//glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //TOP
glNormal3f(0,1,0);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(4.5,0.3,-2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(4.5,0.3, 2);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(5, 0.3, 2); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(5, 0.3, -2);//top left
glEnd();
//glDisable(GL_TEXTURE_2D);
//glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //RIGHT
glNormal3f(0,0,1);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(5, 0, -2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(5, 0, 2);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(5, 0.3, 2); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(5, 0.3, -2);//top left
glEnd();
//glDisable(GL_TEXTURE_2D);
//glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //LEFT
glNormal3f(0,0,-1);
glColor4f(1,1,1,0);
glTexCoord2f(0.0f,0.0f);
glVertex3f(4.5, 0, -2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(4.5, 0, 2);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.5, 0.3, 2); //top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(4.5, 0.3, -2);//top left
glEnd();
//glDisable(GL_TEXTURE_2D);
}
void Display::DisplayScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the back buffer
glPushMatrix();
glLoadIdentity();
glNormal3f(0,1,0);
Vector3f Pos = Redball.GetPos();
gluLookAt(eyeX, eyeY, eyeZ, // 开发者_运维问答eye position
0, 0, 0, // what I'm looking at
0.0, 1.0, 0); // Up direction
float Position[] = {lightX, lightY, lightZ, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, Position);
DrawLight(0, Position);
/* Rendering code goes here */
glPushMatrix();
Ball Redball[8];
for (int i = BALL_RED_START; i<BALL_RED_END;i++)
{
float x = 0;
glTranslatef(x+1,1,0);
glColor3f(1,0,0);
Redball[i].DrawRed();
}
glPopMatrix();
glPushMatrix();
Ball Yellowball[8];
for (int i = BALL_YELLOW_START; i<BALL_YELLOW_END;i++)
{
float x = 0;
glTranslatef(x+1,0,1);
glColor3f(2,1,0);
Yellowball[i].DrawYellow();
}
glPopMatrix();
drawTable();
drawCushions();
Table(-2,-4.5,2,4.5); // Draws the table top in trianglestrip -4.5, 0.5, -0.5, 9.5
glPopMatrix();
glutSwapBuffers(); // Swap the front and back buffers
}
void Display::Resize(int w, int h)
{
/* Resize is called when window is resized */
glMatrixMode(GL_PROJECTION); // set matrix mode to profection
// this dictates how the 3d scene is "squashed" onto the 2d screen
glLoadIdentity();
glViewport(0, 0, w, h); // Set the part of the window to use.
gluPerspective(45, // field of view
(float)w/(float)h, // ration of window
1, // front clipping plane
1000 // back clipping plane
); // set the area in the 3d scene to draw
glMatrixMode(GL_MODELVIEW); // Setthe matrix mode to model view
// the matrix specifies how the 3d scene is viewed
/*glLoadIdentity();
gluLookAt(-3.5, 2, eyeZ, // eye position
1, 1, 0, // what I'm looking at
0.0, 1.0, 0); // Up direction*/
}
void Display::Idle()
{
/* When nothing else is happening, idle is called.
* Simulation should be done here and then
* the display method should be called
*/
BallMovement();
glutPostRedisplay();
}
Bitmap.h
I also have a class for my textures
#ifndef _BITMAP_H
#define _BITMAP_H
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
const short BITMAP_MAGIC_NUMBER=19778;
const int RGB_BYTE_SIZE=3;
#pragma pack(push,bitmap_data,1)
typedef struct tagRGBQuad {
char rgbBlue;
char rgbGreen;
char rgbRed;
char rgbReserved;
} RGBQuad;
typedef struct tagBitmapFileHeader {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} BitmapFileHeader;
typedef struct tagBitmapInfoHeader {
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BitmapInfoHeader;
#pragma pack(pop,bitmap_data)
class Bitmap {
public:
//variables
RGBQuad *colours;
char *data;
bool loaded;
int width,height;
unsigned short bpp;
string error;
//methods
Bitmap(void);
Bitmap(char *);
~Bitmap();
bool loadBMP(char *);
private:
//variables
BitmapFileHeader bmfh;
BitmapInfoHeader bmih;
int byteWidth; //the width in bytes of the image
int padWidth; //the width in bytes of the added image
unsigned int dataSize; //size of the data in the file
//methods
void reset(void);
bool convert24(char *); //convert to 24bit RGB bottom up data
bool convert8(char *); //convert to 24bit RGB bottom up data
};
#endif //_BITMAP_H
Bitmap.cpp
#include "Bitmap.h"
//basic constructor
Bitmap::Bitmap(){
reset();
}
//constructor loads the bitmap when it is created
Bitmap::Bitmap(char *file){
reset();
loadBMP(file);
}
//destructor
Bitmap::~Bitmap(){
if(colours!=0) {
delete[] colours;
}
if(data!=0) {
delete[] data;
}
}
//load a bitmap from a file and represent it correctly
//in memory
bool Bitmap::loadBMP(char *file) {
FILE *in; //file stream for reading
char *tempData; //temp storage for image data
int numColours; //total available colours
//bitmap is not loaded yet
loaded=false;
//make sure memory is not lost
if(colours!=0) {
delete[] colours;
}
if(data!=0) {
delete[] data;
}
//open the file for reading in binary mode
in=fopen(file,"rb");
//if the file does not exist return in error
if(in==NULL) {
error="File not found";
fclose(in);
return false;
}
//read in the entire BITMAPFILEHEADER
fread(&bmfh,sizeof(BitmapFileHeader),1,in);
//check for the magic number that says this is a bitmap
if(bmfh.bfType!=BITMAP_MAGIC_NUMBER) {
error="File is not in DIB format";
fclose(in);
return false;
}
//read in the entire BITMAPINFOHEADER
fread(&bmih,sizeof(BitmapInfoHeader),1,in);
//save the width, height and bits per pixel for external use
width=bmih.biWidth;
height=bmih.biHeight;
bpp=bmih.biBitCount;
//calculate the size of the image data with padding
dataSize=(width*height*(unsigned int)(bmih.biBitCount/8.0));
//calculate the number of available colours
numColours=1<<bmih.biBitCount;
//if the bitmap is not 8 bits per pixel or more
//return in error
if(bpp<8) {
error="File is not 8 or 24 bits per pixel";
fclose(in);
return false;
}
//load the palette for 8 bits per pixel
if(bpp==8) {
colours=new RGBQuad[numColours];
fread(colours,sizeof(RGBQuad),numColours,in);
}
//set up the temporary buffer for the image data
tempData=new char[dataSize];
//exit if there is not enough memory
if(tempData==NULL) {
error="Not enough memory to allocate a temporary buffer";
fclose(in);
return false;
}
//read in the entire image
fread(tempData,sizeof(char),dataSize,in);
//close the file now that we have all the info
fclose(in);
//calculate the witdh of the final image in bytes
byteWidth=padWidth=(int)((float)width*(float)bpp/8.0);
//adjust the width for padding as necessary
while(padWidth%4!=0) {
padWidth++;
}
//change format from GBR to RGB
if(bpp==8) {
loaded=convert8(tempData);
}
else if(bpp==24) {
loaded=convert24(tempData);
}
//clean up memory
delete[] tempData;
//bitmap is now loaded
error="Bitmap loaded";
//return success
return loaded;
}
//function to set the inital values
void Bitmap::reset(void) {
loaded=false;
colours=0;
data=0;
error="";
}
bool Bitmap::convert24(char* tempData) {
int offset,diff;
diff=width*height*RGB_BYTE_SIZE;
//allocate the buffer for the final image data
data=new char[diff];
//exit if there is not enough memory
if(data==NULL) {
error="Not enough memory to allocate an image buffer";
delete[] data;
return false;
}
if(height>0) {
offset=padWidth-byteWidth;
//count backwards so you start at the front of the image
for(int i=0;i<dataSize;i+=3) {
//jump over the padding at the start of a new line
if((i+1)%padWidth==0) {
i+=offset;
}
//transfer the data
*(data+i+2)=*(tempData+i);
*(data+i+1)=*(tempData+i+1);
*(data+i)=*(tempData+i+2);
}
}
//image parser for a forward image
else {
offset=padWidth-byteWidth;
int j=dataSize-3;
//count backwards so you start at the front of the image
//here you can start from the back of the file or the front,
//after the header The only problem is that some programs
//will pad not only the data, but also the file size to
//be divisible by 4 bytes.
for(int i=0;i<dataSize;i+=3) {
//jump over the padding at the start of a new line
if((i+1)%padWidth==0) {
i+=offset;
}
//transfer the data
*(data+j+2)=*(tempData+i);
*(data+j+1)=*(tempData+i+1);
*(data+j)=*(tempData+i+2);
j-=3;
}
}
return true;
}
bool Bitmap::convert8(char* tempData) {
int offset,diff;
diff=width*height*RGB_BYTE_SIZE;
//allocate the buffer for the final image data
data=new char[diff];
//exit if there is not enough memory
if(data==NULL) {
error="Not enough memory to allocate an image buffer";
delete[] data;
return false;
}
if(height>0) {
offset=padWidth-byteWidth;
int j=0;
//count backwards so you start at the front of the image
for(int i=0;i<dataSize*RGB_BYTE_SIZE;i+=3) {
//jump over the padding at the start of a new line
if((i+1)%padWidth==0) {
i+=offset;
}
//transfer the data
*(data+i)=colours[*(tempData+j)].rgbRed;
*(data+i+1)=colours[*(tempData+j)].rgbGreen;
*(data+i+2)=colours[*(tempData+j)].rgbBlue;
j++;
}
}
//image parser for a forward image
else {
offset=padWidth-byteWidth;
int j=dataSize-1;
//count backwards so you start at the front of the image
for(int i=0;i<dataSize*RGB_BYTE_SIZE;i+=3) {
//jump over the padding at the start of a new line
if((i+1)%padWidth==0) {
i+=offset;
}
//transfer the data
*(data+i)=colours[*(tempData+j)].rgbRed;
*(data+i+1)=colours[*(tempData+j)].rgbGreen;
*(data+i+2)=colours[*(tempData+j)].rgbBlue;
j--;
}
}
return true;
}
All my errors are to do with this code in Display.cpp:
Bitmap image[2];
image[0].loadBMP("myTexture.bmp");
image[1].loadBMP("myTexture2.bmp");
Errors:
error C2040: image:'Bitmap [2]' differs in level of indirection of 'Bitmap'
error C2088: '[' illegal for class (twice) error C2228: left of .BMP must have class/struct/union (twice) no operator "[]" matches these operands (twice)Bitmap image;
Bitmap image[2];
You're declaring image
twice, that makes no sens. Remove the first one if you want image
to be one bitmap.
If you go with the array, you'll need to refer to the actual bitmaps as bitmap[0]
and bitmap[1]
. And it doesn't fit with the rest of your code - you're calling loadTexture
twice, so loading two bitmaps in there makes no sens. So stick with just one bitmap in there.
void loadTexture(GLuint texture, const char* filename)
{
Bitmap image;
image.loadBMP(filename);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data);
}
精彩评论