Advertisement

Newb: Texturing onto a rectangle

Started by May 10, 2003 03:21 PM
2 comments, last by Xiachunyi 21 years, 9 months ago
Hello, I am having problems texturing onto a rectangle, and I can''t seem to get it to work out for me. Here is my current syntax:

    glLoadIdentity();
	gluLookAt(0, 0, 6,     0, 0, 0,     0, 1, 0);
	glTranslatef(placex,placey,placez);
    glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture[pic]);
    glRectf(0.0,0.0,5.0,5.0);
 
If glBegin()/glEnd() is my problem, what do I pass into the argument? Thanks in advance
I''ve personally never seen glRectf() before, but it probably doesn''t support texturing if it''s not working. If it''s a function you made, then I''d need to see the code for it.

A simple textured rectangle can be coded as:

glBindTexture(GL_TEXTURE_2D, textureIndex);
glEnable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLE_FAN); // this is the fastest way to draw a rectangle
glTexCoord2f(0.0,0.0); glVertex3f(0.0,0.0,0.0);
glTexCoord2f(1.0,0.0); glVertex3f(1.0,0.0,0.0);
glTexCoord2f(1.0,1.0); glVertex3f(1.0,1.0,0.0);
glTexCoord2f(0.0,1.0); glVertex3f(0.0,1.0,0.0);
glEnd();
glDisable(GL_TEXTURE_2D);

Make sure you actually CREATE the texture as well. Binding a non-existent texture makes the texture all white I believe.

Legends Development Team
Legends Development Team
Advertisement
Thanks a lot!

I found the function glRectf(,,,) in Dev C++''s Opengl API reference book. Here is some data about it:

  These functions draw a rectangle.void glRectd(    GLdouble x1, 	    GLdouble y1, 	    GLdouble x2, 	    GLdouble y2 	   );	 void glRectf(    GLfloat x1, 	    GLfloat y1, 	    GLfloat x2, 	    GLfloat y2 	   );	 void glRecti(    GLint x1, 	    GLint y1, 	    GLint x2, 	    GLint y2 	   );	 void glRects(    GLshort x1, 	    GLshort y1, 	    GLshort x2, 	    GLshort y2 	   );	 Parametersx1, y1One vertex of a rectangle. x2, y2The opposite vertex of the rectangle. void glRectdv(    const GLdouble *v1, 	    const GLdouble *v2 	   );	 void glRectfv(    const GLfloat *v1, 	    const GLfloat *v2 	   );	 void glRectiv(    const GLint *v1, 	    const GLint *v2 	   );	 void glRectsv(    const GLshort *v1, 	    const GLshort *v2 	   );	 Parametersv1A pointer to one vertex of a rectangle. v2A pointer to the opposite vertex of the rectangle. RemarksThe glRect function supports efficient specification of rectangles as two corner points. Each rectangle command takes four arguments, organized either as two consecutive pairs of (x, y) coordinates, or as two pointers to arrays, each containing an (x,y) pair. The resulting rectangle is defined in the z = 0 plane.The glRect(x1, y1, x2, y2) function is exactly equivalent to the following sequence:glBegin(GL_POLYGON); glVertex2(x1, y1);glVertex2(x2, y1); glVertex2(x2, y2);glVertex2(x1, y2); glEnd( ); Notice that if the second vertex is above and to the right of the first vertex, the rectangle is constructed with a counterclockwise winding.Error CodesThe following are the error codes generated and their conditions.Error Code	ConditionGL_INVALID_OPERATION 	glRect was called between a call to glBegin and the corresponding call to glEnd.  


The rectangle draws, but I guess its been outdated or something. Thanks again!
Yeah, the problem is that it never specifies texture coordinates on each vertex. If you don''t tell OpenGL where on the texture each vertex is, then it will spread one of the texels over the entire surface (being as all of the vertices lie on the same point on the texture).

Legends Development Team
Legends Development Team

This topic is closed to new replies.

Advertisement