Original Post
Hi.
I am unable to see loaded textures no matter what I try to do. I followed all the steps thoroughly but I'm still unable to see any textures on the object I draw.
I'm using SDL and the opengl SuperBible v4 code for practice. Here are some specific code excerpts in order of function call.
Init SDL
Init OGL
I am unable to see loaded textures no matter what I try to do. I followed all the steps thoroughly but I'm still unable to see any textures on the object I draw.
I'm using SDL and the opengl SuperBible v4 code for practice. Here are some specific code excerpts in order of function call.
Init SDL
int system_InitSDL(int w, int h)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
gData->AppendLog("I_cEngine::system_InitSDL: SDL_Init(SDL_INIT_VIDEO): -1");
return -1;
}
const SDL_VideoInfo* info = NULL;
info = SDL_GetVideoInfo( );
int bpp = info->vfmt->BitsPerPixel;
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, bpp );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
screen_ = SDL_SetVideoMode(w,h, bpp , SDL_HWSURFACE | SDL_OPENGL);
if (!screen_)
{
gData->AppendLog("I_cEngine::system_InitSDL: SDL_SetVideoMode(%d,%d,%d, SDL_HWSURFACE | SDL_OPENGL): NULL",w,h,bpp);
return -1;
}
return 0;
}
Init OGL
void system_InitGL(int w, int h)
{
GLfloat aspectRatio;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)w / (GLfloat)h;
gluPerspective(45,aspectRatio,1.0,400);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
system_LoadTextures();
// Enable Texture Mapping ( NEW )
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.25f, 0.25f, 0.25f, 1.0f ); // grey backsurface
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
}
Load textures is called from initOGL function above
int system_LoadTextures(void)
{
GLint nOfColors;
GLenum texture_format;
gData->AppendLog(" I_cEngine::system_LoadTextures:");
// Load this texture map
SDL_Surface *image[1];
image[0] = SDL_LoadBMP("res/nehe.bmp");
// get the number of channels in the SDL surface
nOfColors = image[0]->format->BytesPerPixel;
if (nOfColors == 4)
{
if (image[0]->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
} else if (nOfColors == 3) // no alpha channel
{
if (image[0]->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
} else {
printf("warning: the image is not truecolor.. this will probably break\n");
}
glGenTextures( 1, &textureObject[0] );
glBindTexture(GL_TEXTURE_2D, textureObject[0]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
GLint iWidth, iHeight, iComponents;
GLenum eFormat;
glTexImage2D( GL_TEXTURE_2D, 0, 3,image[0]->w,image[0]->h, 0, texture_format, GL_UNSIGNED_BYTE,image[0]->pixels );
SDL_FreeSurface(image[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
return 0;
return 0;
}
Render function is standard bind tex and draw box stuff
glBindTexture(GL_TEXTURE_2D, textureObject[0]);
glBegin(GL_QUADS);
/* Front Face */
/* Bottom Left Of The Texture and Quad */
glTexCoord2f( 0.0f, 1.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( 1.0f, 0.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 );
/* Back Face */
/* Bottom Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 0.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 */
...
...
Now, what did I messed up. Because I really want to know. I've been 5 days on this code restructuring it and rewriting it and stuff.
Thanks in advance.