2011-04-16 145 views
0

尝试将.bmp文件转换为数组时遇到一些问题,因此我可以将它们用于纹理。无法加载多个.bmp文件中的纹理OpenGL C++

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

我也有我的纹理

#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; 
} 

我所有的错误与此代码Display.cpp做:

Bitmap image[2]; 

    image[0].loadBMP("myTexture.bmp"); 
    image[1].loadBMP("myTexture2.bmp"); 

个错误:

错误C2040:图像: '位图[2]' 在 '位图'
错误C2088的间接的电平不同: '[' 类非法(两次)
错误C2228:左的。 BMP必须有类/结构/联合(两次)
没有运营商 “[]” 匹配这些操作数(两次)

+0

您的第一个方法不会使用传递的文件名,而您的第二个不能编译......请发布您目前拥有的(完全)以及您获得的错误(编译时间或运行时间) 。 – Mat 2011-04-16 13:21:19

+0

我已更新我的帖子,包含错误和涉及纹理的所有代码 – 2011-04-16 14:00:37

回答

0
Bitmap image; 

Bitmap image[2]; 

你声明image的两倍,这使得没有SENS。删除第一个,如果你想image是一个位图。

如果使用数组,则需要参考实际位图bitmap[0]bitmap[1]。它不适合你的其他代码 - 你打电话loadTexture两次,所以加载两个位图没有任何敏感。所以坚持只有一个位图。

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); 
} 
+0

位图图像; image.loadBMP(“myTexture.bmp”); image.loadBMP(“myTexture2.bmp”); Bitmap [1]; 位图[2]; 像这样的东西? – 2011-04-16 14:19:14

+0

不,你期望'Bitmap [1];'完成什么? – Mat 2011-04-16 14:27:01

+0

好吧,所以我把上面的代码放在里面,我仍然只能看到一个纹理,如果我启用GL_TEXTURE_2D它只是应用第一个纹理。 – 2011-04-16 14:59:43