Skip to main content
gamedev.net
🔒 Locked

Texture doesn't show

Started by Odiee Sep 1, 2011 at 12:07 PM 4 replies 1.8k views
Original Post
Odiee
Odiee
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

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.
Brother Bob
Brother Bob
You set the minification filter to a mipmap filter, but you never supply a complete set of mipmaps, only the base level. Your texture is thus incomplete and disabled when you try to use it. Either set the minification filter to a non-mipmap filter, or make sure that all mipmap levels correctly defined.
Odiee
Odiee
My.God. The.Horror.
Thanks.

I now See the OGL SuberBible has chapters that go from 8:Texture Mapping Basics to 9: Texture Mapping Beyond the Basics and one of the last thing that ch8 talks about is MipMapping. So when i finished this chapter I restructured my code from the beginning and thus failed to notice the mipmapping bit. I thought nothing would happen.

Now I feel like playing the adventure game and I cheated for going to net for solution when it was right at front of me this whole time.
Thanks again.
UltimaX
UltimaX
Also... I am not 100% sure with SDL, but shouldn't you use nOfColors so OpenGL knows the correct number of channels / components to use?

This
glTexImage2D( GL_TEXTURE_2D, 0, 3,image[0]->w,image[0]->h, 0, texture_format, GL_UNSIGNED_BYTE,image[0]->pixels );

Should be

glTexImage2D( GL_TEXTURE_2D, 0, nOfColors,image[0]->w,image[0]->h, 0, texture_format, GL_UNSIGNED_BYTE,image[0]->pixels );
Brother Bob
Brother Bob
The third parameter is the internal format of the texture; the seventh and eighth parameters describe the source format.
V-man
V-man
So this
glTexImage2D( GL_TEXTURE_2D, 0, 3,image[0]->w,image[0]->h, 0, texture_format, GL_UNSIGNED_BYTE,image[0]->pixels );

or this
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors,image[0]->w,image[0]->h, 0, texture_format, GL_UNSIGNED_BYTE,image[0]->pixels );

would turn into this
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB8,image[0]->w,image[0]->h, 0, texture_format, GL_UNSIGNED_BYTE,image[0]->pixels );

where GL_RGB8 is an OpenGL internal format while "3" is used in classical OpenGL version 1.0.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.