GLuint texture[2];
int texnumber=0;
vector<char *>vtex;
GLvoid InitTextures(GLvoid)
{
vtex.push_back("textur001.bmp");
vtex.push_back("textur2.bmp");
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
xrot+=0.5f;
return TRUE;
}
AUX_RGBImageRec *LoadBMP(char *Filename) //lädt eine bitmap
{
FILE *File=NULL; //unser zeiger auf ne file
if(!Filename)
{return NULL;}
File=fopen(Filename,"r");
if(File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}
int LoadGLTextures(int texno)
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);
if(TextureImage[0]=LoadBMP(vtex[texno]))
{
Status=TRUE;
glGenTextures(vtex.size(),&texture[texno]);
glBindTexture(GL_TEXTURE_2D,texture[texno]);
glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage[0]->sizeX,TextureImage[0]->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Lineare Filterung bei Objekten, die weiter entfernt liegen
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Lineare Filterung bei Objekten, die näher an den betrachter kommen.
}
if(TextureImage[0])
{
if(TextureImage[0]->data)
{free(TextureImage[0]->data);}
free(TextureImage[0]);
}
return Status;
}
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
InitTextures();
for(texnumber=vtex.size()-1;texnumber>=0;texnumber--)
{
if(!LoadGLTextures(texnumber))
{return FALSE;}
}
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return TRUE; // Initialization Went OK
}
loading more than one texture
Hello everyone.
I'm new to opengl and learn it with help of nehe's tutorials. currently i'm trying to write a programm that's able to load more than one texture, coz' nehe's tutorial only shows how to load one texture..
but I'm sure my code is very bad and there are a few better ways to realize what i'm trying. I even get an error-message after quitting the program.
...
[Edited by - phantom on December 19, 2005 7:18:41 AM]
You never call the LoadBitmap function. You only fill in a vector with the names. You should load those textures at startup of your program in order to render them.
jeroenb: He does load the textures, in the loop immediately following the call to InitTextures().
matthoz: The only error that immediately jumps out at me is:
The first time this statement executes, with texno == 1, it tries to generate two texture ids and place them in locations texture[1] and texture[2]. texture[2] doesn't exist, so writing to this location is undefined behaviour - anything could legitimately happen (usually what happens is that it overwrites some other variable in your program). Your code then loads the second texture and associates it with the texture id in texture[1].
Next time the statement executes, with texno == 0, it again tries to generate two texture ids but this time places them in texture[0] and texture[1]. The latter overwrites the previous texture id which was associated with your second texture image. Your code then loads the first texture image and associates it with the texture id in texture[0]. The end result is that you have a valid texture id which maps to your first texture in texture[0], a valid texture id which maps to an uninitialised texture in texture[1] and a valid texture id which maps to your second texture but is no longer stored anywhere.
Enigma
matthoz: The only error that immediately jumps out at me is:
glGenTextures(vtex.size(),&texture[texno]);
The first time this statement executes, with texno == 1, it tries to generate two texture ids and place them in locations texture[1] and texture[2]. texture[2] doesn't exist, so writing to this location is undefined behaviour - anything could legitimately happen (usually what happens is that it overwrites some other variable in your program). Your code then loads the second texture and associates it with the texture id in texture[1].
Next time the statement executes, with texno == 0, it again tries to generate two texture ids but this time places them in texture[0] and texture[1]. The latter overwrites the previous texture id which was associated with your second texture image. Your code then loads the first texture image and associates it with the texture id in texture[0]. The end result is that you have a valid texture id which maps to your first texture in texture[0], a valid texture id which maps to an uninitialised texture in texture[1] and a valid texture id which maps to your second texture but is no longer stored anywhere.
Enigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement