# OpenGL fill rect

**URL:** <https://discourse.libsdl.org/t/opengl-fill-rect/16662>\
**Category:** SDL Development\
**Created:** [October 4, 2009, 1:44am UTC](https://discourse.libsdl.org/t/opengl-fill-rect/16662 "2009-10-04T01:44:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andre\_Leiradella](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/andre_leiradella/32/785_2.png) [@Andre\_Leiradella](https://discourse.libsdl.org/u/Andre_Leiradella)\
**Post date:** [October 4, 2009, 1:44am UTC](https://discourse.libsdl.org/t/opengl-fill-rect/16662/1 "2009-10-04T01:44:48Z")

</div>

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. 🙂

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

---

<div class="post-metadata">

**Author:** ![Steven\_Noonan](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/steven_noonan/32/828_2.png) [@Steven\_Noonan](https://discourse.libsdl.org/u/Steven_Noonan)\
**Post date:** [October 4, 2009, 2:02am UTC](https://discourse.libsdl.org/t/opengl-fill-rect/16662/2 "2009-10-04T02:02:24Z")

</div>

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. 🙂
> 
> 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.

---

<div class="post-metadata">

**Author:** ![Steven\_Noonan](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/steven_noonan/32/828_2.png) [@Steven\_Noonan](https://discourse.libsdl.org/u/Steven_Noonan)\
**Post date:** [October 4, 2009, 2:02am UTC](https://discourse.libsdl.org/t/opengl-fill-rect/16662/3 "2009-10-04T02:02:44Z")

</div>

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. 🙂
> > 
> > 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.

---

<div class="post-metadata">

**Author:** ![Andre\_Leiradella](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/andre_leiradella/32/785_2.png) [@Andre\_Leiradella](https://discourse.libsdl.org/u/Andre_Leiradella)\
**Post date:** [October 4, 2009, 2:05am UTC](https://discourse.libsdl.org/t/opengl-fill-rect/16662/4 "2009-10-04T02:05:42Z")

</div>

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. 🙂
> > > 
> > > 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)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
