Question

I'm trying to make a game, it's 2D and has a moving player. I haven't worked with textures before and getting to display this texture correctly is very frustrating for me.

Current Output: http://puu.sh/7hTEL/64ac1529b1.jpg

My Image: http://puu.sh/7hUlM/3be59d6497.jpg

I just want that image to repeat itself (both x and y) over the background of the screen. at the moment I just have a fixed height / width for the game. It focuses on the player and the player is always moving in the x direction!

I have stripped down as most I can:

#include <iostream>
#include "snake.h"

#include <GL\glut.h>

GLuint texture = 0;
snake_t* snake = new snake_t();

GLuint LoadTexture( const char * filename, int width, int height )
{
    GLuint texture;
    unsigned char * data;
    FILE * file;

    //The following code will read in our RAW 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 ); 


    //even better quality, but this will do for now.
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );


    //to the edge of our shape. 
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

    //Generate the texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,GL_RGB, GL_UNSIGNED_BYTE, data);
    free( data ); //free the texture
    return texture; //return whether it was successful
}

void display( void )
{
    //set view of scene
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluOrtho2D( snake->x - start_x, (640 + snake->x) - start_x, 0.0, 480);

    //gluOrtho2D(0, 640, 0, 480);



    //draw texture on background
    glBindTexture( GL_TEXTURE_2D, texture ); 

    glPushMatrix();
    glBegin(GL_QUADS);
        glTexCoord2i(0,0); glVertex2f(snake->x - start_x, 480);
        glTexCoord2i(1,0); glVertex2f(640 + snake->x, 480);
        glTexCoord2i(1,1); glVertex2f(640 + snake->x, 0);
        glTexCoord2i(0,1); glVertex2f(snake->x - start_x, 0);
    glEnd();
    glPopMatrix();


    glColor3f( 1.0, 1.0, 1.0 );

    //render scene
    glutSwapBuffers();
}

int main( int argc, char** argv )
{
   glutInit( &argc, argv );
   glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
   glutInitWindowSize (640, 480);
   glutInitWindowPosition (100, 100);
   glutCreateWindow( "Slidey Snake" );
   glutDisplayFunc( display );
   //glutKeyboardFunc( buttonmap );
   //glutReshapeFunc(reshape);

   //init();
   //Timer(0);

   texture = LoadTexture("sky.png", 256, 256);

   glutMainLoop();

   return 0;
}

any ideas?

Was it helpful?

Solution

I must admit, I laughed at this one :) You are using the raw PNG file data as being RGB pixel data. That is not correct. PNG files are compressed. You first need to decode them before you feed them into OpenGL. Slick2D is a library that does the job nicely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top