I’m having a little problem with a very basic texturing program.
The program is supposed to display 2 textured polygons (squares in this case).
Unfortunately, I only seem to get the top part of each square showing the texture.
http://photos-c.ak.fbcdn.net/hphotos-ak-…0_6442172_n.jpg
Here is the code.
#include <GLUT/GLUT.h>
#include <stdio.h>
GLuint texture;
GLuint LoadTexture( const char * filename, int width, int height, int wrap )
{
GLuint texture;
unsigned char * data;
FILE * file;
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP );
gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );
free( data );
return texture;
}
void FreeTexture( GLuint texture )
{ glDeleteTextures( 1, &texture ); }
void drawplane()
{
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(1.0,0.0); glVertex3f(2.0,-2.0,-2.0);
glTexCoord2f(0.0,0.0); glVertex3f(-2.0,-2.0,-2.0);
glTexCoord2f(1.0,1.0); glVertex3f(2.0,-2.0,2.0);
glTexCoord2f(0.0,1.0); glVertex3f(-2.0,-2.0,2.0);
glEnd();
}
void drawPlanes()
{
glPushMatrix();
glTranslated(-2.5,0.0,0.0);
glRotatef(90.0, 1.0, 0.0, 0.0);
drawplane();
glPopMatrix();
glPushMatrix();
glTranslated(2.5,0.0,0.0);
glRotatef(90.0, 1.0, 0.0, 0.0);
drawplane();
glPopMatrix();
}
void display (void)
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
texture = LoadTexture( "texture.raw", 256, 256, TRUE);
glEnable(GL_TEXTURE_2D); //enable 2D texturing
glBindTexture(GL_TEXTURE_2D, texture);
drawPlanes();
FreeTexture(texture);
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w/(GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
int main (int argc, char **argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (600, 500);
glutInitWindowPosition (20, 100);
glutCreateWindow ("Texturing");
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutMainLoop ();
return 0;
}
You may recognize some of the code, most of it is taken from Swiftless’ Tutorial.
Any advice is appreciated.