[1.2] SDL /OpenGL texture and primitive drawing problem

Hi

I have a problem where I can’t set the color of the drawing primitive when I load some texture before drawing.
e.g.
LoadBackTex();
DrawBack();
DrawSquare();

where LoadBackTex() loads the *.png image, sets texture format, bind texture, calls glTexImage2D with appropriate parameters. (format is GL_RGB)

DrawBack:

Code:

void drawBackground(void)
{
glBindTexture( GL_TEXTURE_2D, ID_BACK_TEXTURE );
glBegin( GL_QUADS );
glTexCoord2i( 0, 0 );
glVertex3f( 0.f, 0.f, 0.0f );

glTexCoord2i( 1, 0 );
glVertex3f( 640, 0, 0.f );

glTexCoord2i( 1, 1 );
glVertex3f( 640, 480.f, 0.f );

glTexCoord2i( 0, 1 );
glVertex3f( 0, 480, 0.f );

glEnd();
SDL_GL_SwapBuffers();
}

DrawSquare:

Code:

void drawSquare(void)
{
glColor3f(1,0,0);
glBegin(GL_QUADS);
glVertex3f(100, 100, 0.0f);
glVertex3f( 150, 100, 0.0f);
glVertex3f( 150,150, 0.0f);
glVertex3f( 100,150, 0.0f);
glEnd();
SDL_GL_SwapBuffers();
}

The problem is I can’t set the color of the square. It draws right, but the color remains unchanged.
It has something with glTexImage2D function because when I disable it, it draws fine. I figured it’s something with the pixel format but I just can’t get my finger on it.

opengl is initialized with

Code:

void InitGL(void)
{
glEnable( GL_TEXTURE_2D );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glViewport( 0, 0, 640, 480 );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}

and SDL with

Code:

int InitSDL(void)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
return -1;
}

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

screen = SDL_SetVideoMode(640,480, bpp, SDL_HWSURFACE | SDL_OPENGL);
if (!screen)
{
gData->AppendLog(“set video mode fail”);
return -1;
}

return 0;
}

Your problem is likely that you’re in GL_REPLACE mode, which means the
texture color completely overwrites the primitive color. Look here:
http://www.opengl.org/sdk/docs/man/xhtml/glTexEnv.xml for information on
other modes (you “probably” want GL_MODULATE).On Thu, Jul 14, 2011 at 7:22 AM, Odiee wrote:

**
Hi

I have a problem where I can’t set the color of the drawing primitive when
I load some texture before drawing.
e.g.
LoadBackTex();
DrawBack();
DrawSquare();

where LoadBackTex() loads the *.png image, sets texture format, bind
texture, calls glTexImage2D with appropriate parameters. (format is GL_RGB)

DrawBack:

Code:

void drawBackground(void)
{
glBindTexture( GL_TEXTURE_2D, ID_BACK_TEXTURE );
glBegin( GL_QUADS );
glTexCoord2i( 0, 0 );
glVertex3f( 0.f, 0.f, 0.0f );

glTexCoord2i( 1, 0 );
glVertex3f( 640, 0, 0.f );

glTexCoord2i( 1, 1 );
glVertex3f( 640, 480.f, 0.f );

glTexCoord2i( 0, 1 );
glVertex3f( 0, 480, 0.f );
glEnd();
SDL_GL_SwapBuffers();
}

DrawSquare:

Code:

void drawSquare(void)
{
glColor3f(1,0,0);
glBegin(GL_QUADS);
glVertex3f(100, 100, 0.0f);
glVertex3f( 150, 100, 0.0f);
glVertex3f( 150,150, 0.0f);
glVertex3f( 100,150, 0.0f);
glEnd();
SDL_GL_SwapBuffers();
}

The problem is I can’t set the color of the square. It draws right, but the
color remains unchanged.
It has something with glTexImage2D function because when I disable it, it
draws fine. I figured it’s something with the pixel format but I just can’t
get my finger on it.

opengl is initialized with

Code:

void InitGL(void)
{
glEnable( GL_TEXTURE_2D );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glViewport( 0, 0, 640, 480 );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}

and SDL with

Code:

int InitSDL(void)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
return -1;
}

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

screen = SDL_SetVideoMode(640,480, bpp, SDL_HWSURFACE | SDL_OPENGL);
if (!screen)
{
gData->AppendLog(“set video mode fail”);
return -1;
}

return 0;
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hello,

This question has nothing to do with SDL, specifically. Please consider
using an OpenGL oriented mailing list or forum, or a general one, for
example http://www.gamedev.net.

Regards,
– BrianOn 14 July 2011 12:22, Odiee wrote:

**
Hi

I have a problem where I can’t set the color of the drawing primitive when
I load some texture before drawing.

Brian Barrett wrote:

Hello,

This question has nothing to do with SDL, specifically. Please consider using an OpenGL oriented mailing list or forum, or a general one, for example http://www.gamedev.net (http://www.gamedev.net).

Regards,
? ? ? ? ? – Brian

Yes Brian, sorry 'bout that. I figured that SDL users encountered this problem before and didn’t think before posting. I was all about the variables man, variables. [Shocked]