Results 1 to 3 of 3

Thread: Basic question: Background image hiding polygons!

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    2

    Basic question: Background image hiding polygons!

    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!
    Code :
    #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, &amp;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, &amp;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

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Basic question: Background image hiding polygons!

    The order in which you draw is important. Draw the background first then the other items on top.

  3. #3
    Member Contributor
    Join Date
    Feb 2011
    Posts
    72

    Re: Basic question: Background image hiding polygons!

    duh

Similar Threads

  1. Replies: 1
    Last Post: 07-09-2017, 07:07 AM
  2. Anti-aliasing polygons on a white background
    By alexb in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 09-27-2003, 04:56 AM
  3. 3D image hiding
    By Reaper4774 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 10-23-2001, 10:25 AM
  4. cut off background of image
    By dan-leech in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 03-07-2001, 01:29 AM
  5. An image for the background....
    By Boubou in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-30-2001, 12:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean