OpenGL fill rect

Hi All,

Maybe this isn’t the proper forum for this question, but since I’m using
SDL to setup the video and play music and sounds I thought it wouldn’t
be a problem. :slight_smile:

I’m trying to draw a filled rectangle using OpenGL but it’s not working.
I already can draw images without problems. The code is:

void
SDLEngine::Render(Texture* texture, Quad* quad, float x, float y,
BlendMode blend_mode, float angle, float scale_x, float scale_y)
{
glMatrixMode(GL_MODELVIEW);
CheckGLError();
glPushMatrix();
CheckGLError();
glLoadIdentity();
CheckGLError();
glTranslatef(x, y, 0);
CheckGLError();
glRotatef(angle, 0, 0, 1);
CheckGLError();
glScalef(scale_x, scale_y != 0 ? scale_y : scale_x, 1);
CheckGLError();

switch (blend_mode)
{
	case kColorBlend:
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		break;
	case kColorAdd:
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
		break;
}
CheckGLError();

if (texture != NULL)
{
	glBindTexture(GL_TEXTURE_2D, ((GLTexture*)texture)->GetID());
	CheckGLError();
}

glBegin(GL_QUADS);
Vertex* vertex = quad->m_Vertex;
Vertex* end = vertex + 4;
while (vertex < end)
{
	glVertex2f(
		vertex->m_X,
		vertex->m_Y
	);
	glColor4ub(
		vertex->m_Color.GetRed(),
		vertex->m_Color.GetGreen(),
		vertex->m_Color.GetBlue(),
		vertex->m_Color.GetAlpha()
	);
	if (texture != NULL)
	{
		glTexCoord2f(
			vertex->m_TextureX,
			vertex->m_TextureY
		);
	}
	vertex++;
}
glEnd();
CheckGLError();
glPopMatrix();
CheckGLError();

}

If texture isn’t NULL, an image is drawn. If it is NULL, a rectangle
should be drawn but it’s not. Does anyone see problems with my code? The
quad parameter is being setup the same way to draw both images and
rectangles.

Cheers,

Andre

I’m pretty sure it’s as simple as: ‘glDisable(TEXTURE_2D);’ before
’glBegin(GL_QUADS);’, and then re-enabling it the next time you use a
texture.

  • StevenOn Sat, Oct 3, 2009 at 6:44 PM, Andre de Leiradella wrote:

Hi All,

Maybe this isn’t the proper forum for this question, but since I’m using SDL
to setup the video and play music and sounds I thought it wouldn’t be a
problem. :slight_smile:

I’m trying to draw a filled rectangle using OpenGL but it’s not working. I
already can draw images without problems. The code is:

void
SDLEngine::Render(Texture* texture, Quad* quad, float x, float y, BlendMode
blend_mode, float angle, float scale_x, float scale_y)
{
? ? ? ?glMatrixMode(GL_MODELVIEW);
? ? ? ?CheckGLError();
? ? ? ?glPushMatrix();
? ? ? ?CheckGLError();
? ? ? ?glLoadIdentity();
? ? ? ?CheckGLError();
? ? ? ?glTranslatef(x, y, 0);
? ? ? ?CheckGLError();
? ? ? ?glRotatef(angle, 0, 0, 1);
? ? ? ?CheckGLError();
? ? ? ?glScalef(scale_x, scale_y != 0 ? scale_y : scale_x, 1);
? ? ? ?CheckGLError();

? ? ? ?switch (blend_mode)
? ? ? ?{
? ? ? ? ? ? ? ?case kColorBlend:
? ? ? ? ? ? ? ? ? ? ? ?glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?case kColorAdd:
? ? ? ? ? ? ? ? ? ? ? ?glBlendFunc(GL_SRC_ALPHA, GL_ONE);
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ?}
? ? ? ?CheckGLError();

? ? ? ?if (texture != NULL)
? ? ? ?{
? ? ? ? ? ? ? ?glBindTexture(GL_TEXTURE_2D, ((GLTexture*)texture)->GetID());
? ? ? ? ? ? ? ?CheckGLError();
? ? ? ?}

? ? ? ?glBegin(GL_QUADS);
? ? ? ?Vertex* vertex = quad->m_Vertex;
? ? ? ?Vertex* end = vertex + 4;
? ? ? ?while (vertex < end)
? ? ? ?{
? ? ? ? ? ? ? ?glVertex2f(
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_X,
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_Y
? ? ? ? ? ? ? ?);
? ? ? ? ? ? ? ?glColor4ub(
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_Color.GetRed(),
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_Color.GetGreen(),
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_Color.GetBlue(),
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_Color.GetAlpha()
? ? ? ? ? ? ? ?);
? ? ? ? ? ? ? ?if (texture != NULL)
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ?glTexCoord2f(
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?vertex->m_TextureX,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?vertex->m_TextureY
? ? ? ? ? ? ? ? ? ? ? ?);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?vertex++;
? ? ? ?}
? ? ? ?glEnd();
? ? ? ?CheckGLError();
? ? ? ?glPopMatrix();
? ? ? ?CheckGLError();
}

If texture isn’t NULL, an image is drawn. If it is NULL, a rectangle should
be drawn but it’s not. Does anyone see problems with my code? The quad
parameter is being setup the same way to draw both images and rectangles.

Er, I meant ‘GL_TEXTURE_2D’, of course…On Sat, Oct 3, 2009 at 7:02 PM, Steven Noonan <@Steven_Noonan> wrote:

On Sat, Oct 3, 2009 at 6:44 PM, Andre de Leiradella wrote:

Hi All,

Maybe this isn’t the proper forum for this question, but since I’m using SDL
to setup the video and play music and sounds I thought it wouldn’t be a
problem. :slight_smile:

I’m trying to draw a filled rectangle using OpenGL but it’s not working. I
already can draw images without problems. The code is:

void
SDLEngine::Render(Texture* texture, Quad* quad, float x, float y, BlendMode
blend_mode, float angle, float scale_x, float scale_y)
{
? ? ? ?glMatrixMode(GL_MODELVIEW);
? ? ? ?CheckGLError();
? ? ? ?glPushMatrix();
? ? ? ?CheckGLError();
? ? ? ?glLoadIdentity();
? ? ? ?CheckGLError();
? ? ? ?glTranslatef(x, y, 0);
? ? ? ?CheckGLError();
? ? ? ?glRotatef(angle, 0, 0, 1);
? ? ? ?CheckGLError();
? ? ? ?glScalef(scale_x, scale_y != 0 ? scale_y : scale_x, 1);
? ? ? ?CheckGLError();

? ? ? ?switch (blend_mode)
? ? ? ?{
? ? ? ? ? ? ? ?case kColorBlend:
? ? ? ? ? ? ? ? ? ? ? ?glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?case kColorAdd:
? ? ? ? ? ? ? ? ? ? ? ?glBlendFunc(GL_SRC_ALPHA, GL_ONE);
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ?}
? ? ? ?CheckGLError();

? ? ? ?if (texture != NULL)
? ? ? ?{
? ? ? ? ? ? ? ?glBindTexture(GL_TEXTURE_2D, ((GLTexture*)texture)->GetID());
? ? ? ? ? ? ? ?CheckGLError();
? ? ? ?}

? ? ? ?glBegin(GL_QUADS);
? ? ? ?Vertex* vertex = quad->m_Vertex;
? ? ? ?Vertex* end = vertex + 4;
? ? ? ?while (vertex < end)
? ? ? ?{
? ? ? ? ? ? ? ?glVertex2f(
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_X,
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_Y
? ? ? ? ? ? ? ?);
? ? ? ? ? ? ? ?glColor4ub(
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_Color.GetRed(),
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_Color.GetGreen(),
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_Color.GetBlue(),
? ? ? ? ? ? ? ? ? ? ? ?vertex->m_Color.GetAlpha()
? ? ? ? ? ? ? ?);
? ? ? ? ? ? ? ?if (texture != NULL)
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ?glTexCoord2f(
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?vertex->m_TextureX,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?vertex->m_TextureY
? ? ? ? ? ? ? ? ? ? ? ?);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?vertex++;
? ? ? ?}
? ? ? ?glEnd();
? ? ? ?CheckGLError();
? ? ? ?glPopMatrix();
? ? ? ?CheckGLError();
}

If texture isn’t NULL, an image is drawn. If it is NULL, a rectangle should
be drawn but it’s not. Does anyone see problems with my code? The quad
parameter is being setup the same way to draw both images and rectangles.

I’m pretty sure it’s as simple as: ‘glDisable(TEXTURE_2D);’ before
’glBegin(GL_QUADS);’, and then re-enabling it the next time you use a
texture.

Yep, that made it. Thanks a lot!

Steven Noonan wrote:> On Sat, Oct 3, 2009 at 7:02 PM, Steven Noonan wrote:

On Sat, Oct 3, 2009 at 6:44 PM, Andre de Leiradella wrote:

Hi All,

Maybe this isn’t the proper forum for this question, but since I’m using SDL
to setup the video and play music and sounds I thought it wouldn’t be a
problem. :slight_smile:

I’m trying to draw a filled rectangle using OpenGL but it’s not working. I
already can draw images without problems. The code is:

void
SDLEngine::Render(Texture* texture, Quad* quad, float x, float y, BlendMode
blend_mode, float angle, float scale_x, float scale_y)
{
glMatrixMode(GL_MODELVIEW);
CheckGLError();
glPushMatrix();
CheckGLError();
glLoadIdentity();
CheckGLError();
glTranslatef(x, y, 0);
CheckGLError();
glRotatef(angle, 0, 0, 1);
CheckGLError();
glScalef(scale_x, scale_y != 0 ? scale_y : scale_x, 1);
CheckGLError();

   switch (blend_mode)
   {
           case kColorBlend:
                   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                   break;
           case kColorAdd:
                   glBlendFunc(GL_SRC_ALPHA, GL_ONE);
                   break;
   }
   CheckGLError();

   if (texture != NULL)
   {
           glBindTexture(GL_TEXTURE_2D, ((GLTexture*)texture)->GetID());
           CheckGLError();
   }

   glBegin(GL_QUADS);
   Vertex* vertex = quad->m_Vertex;
   Vertex* end = vertex + 4;
   while (vertex < end)
   {
           glVertex2f(
                   vertex->m_X,
                   vertex->m_Y
           );
           glColor4ub(
                   vertex->m_Color.GetRed(),
                   vertex->m_Color.GetGreen(),
                   vertex->m_Color.GetBlue(),
                   vertex->m_Color.GetAlpha()
           );
           if (texture != NULL)
           {
                   glTexCoord2f(
                           vertex->m_TextureX,
                           vertex->m_TextureY
                   );
           }
           vertex++;
   }
   glEnd();
   CheckGLError();
   glPopMatrix();
   CheckGLError();

}

If texture isn’t NULL, an image is drawn. If it is NULL, a rectangle should
be drawn but it’s not. Does anyone see problems with my code? The quad
parameter is being setup the same way to draw both images and rectangles.

I’m pretty sure it’s as simple as: ‘glDisable(TEXTURE_2D);’ before
’glBegin(GL_QUADS);’, and then re-enabling it the next time you use a
texture.

Er, I meant ‘GL_TEXTURE_2D’, of course…


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