I’m new to opengl. I just learnt how to implement textures. I tried to use one as a background image, but it seems to cover up all my other polygon. How do I force it to stay in the back.
Please HELP!
#include <stdio.h>
#include <stdlib.h>
#include <gl/glut.h>
#include <math.h>
float ballX = 0.0f;
float ballY = 0.0f;
float ballZ = -1.0f;
GLfloat angle=0.0;
GLuint texture;
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100,100,-100,100,-100,100);
glMatrixMode(GL_MODELVIEW);
}
void reshape(int width,int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
if(width>=height)
gluOrtho2D(-100*width/height,100*width/height,-100,100);
else
gluOrtho2D(-100,100,-100*height/width,100*height/width);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void myreshape (int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, width / height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
//glutPostRedisplay();
}
GLuint LoadTexture( const char * filename, int width, int height )
{
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 );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,GL_RGB, GL_UNSIGNED_BYTE, data);
free( data );
return texture;
}
void FreeTexture( GLuint texture )
{
glDeleteTextures( 1, &texture );
}
void rocket()
{
glColor3f(0.0,1.0,0.0);
glBegin(GL_POLYGON); //fuselage
glVertex2f(-5.0,-10.0);
glVertex2f(5.0,-10.0);
glVertex2f(5.0,15.0);
glVertex2f(-5.0,15.0);
glEnd();
glColor3f(.2,.5,0.7);
glBegin(GL_POLYGON); //tip
glVertex2f(-5.0,15.0);
glVertex2f(5.0,15.0);
glVertex2f(0.0,20.0);
glEnd();
glBegin(GL_POLYGON); //righttwing
glVertex2f(5.0,-10.0);
glVertex2f(7.0,-10.0);
glVertex2f(7.0,0.0);
glVertex2f(5.0,2.0);
glEnd();
glBegin(GL_POLYGON); //leftwing
glVertex2f(-5.0,-10.0);
glVertex2f(-7.0,-10.0);
glVertex2f(-7.0,0.0);
glVertex2f(-5.0,2.0);
glEnd();
}
void drawsphere()
{
glPushMatrix();
glTranslatef(ballX,ballY,ballZ);
glRotatef(angle,0.0,0.0,1.0);
rocket();
//glutSolidCube(20);
glPopMatrix();
}
void display(void)
{
glClearColor(1,1,1,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,1);
glEnable(GL_ALPHA_TEST);
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture );
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2d(-100.0,-100.0);
glTexCoord2d(1.0,0.0); glVertex2d(-100.0,100.0);
glTexCoord2d(1.0,1.0); glVertex2d(+100.0,100.0);
glTexCoord2d(0.0,1.0); glVertex2d(100.0,-100.0);
glEnd();
drawsphere();
glutSwapBuffers();
}
void mykey(unsigned char key, int x, int y)
{
switch (key)
{
case 'a' : ballX -= 1.0f;break;
case 'd' : ballX += 1.0f;break;
case 'w' : ballY += 1.0f;break;
case 's' : ballY -= 1.0f;break;
case 'r' : angle += 1.0f;break;
case 'f' : angle -= 1.0f;break;
default: ;
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("GAME!!1");
myinit();
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(mykey);
texture = LoadTexture( "space.raw", 512, 512 );
//glutReshapeFunc(myreshape);
glutMainLoop();
}
Here’s the image too if you require it…
http://dl.dropbox.com/u/7916754/space.raw