2014-03-03 80 views
-1

我想制作一个游戏,它是2D并且有一个移动的玩家。我之前没有使用纹理,正确显示这个纹理对我来说是非常令人沮丧的。显示加载的纹理

电流输出:http://puu.sh/7hTEL/64ac1529b1.jpg

我的图像:http://puu.sh/7hUlM/3be59d6497.jpg

我只是想该图像在屏幕上的背景重演(x和y)。目前我只有一个固定的高度/宽度的游戏。它专注于玩家,玩家总是在x方向上移动!

我已经剥了下来,因为大多数我可以:

#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; 
} 

什么想法?

回答

2

我必须承认,我笑了这个:)你正在使用原始的文件数据作为RGB像素数据的PNG。这是不正确的。 PNG文件被压缩。在将它们输入到OpenGL之前,您首先需要解码它们。 Slick2D是一个很好地完成这项工作的图书馆。

+0

完全忘了那个。哈哈什么是一些原始图像格式? BMP? – MysteryDev

+0

无。它们都必须至少指定宽度和高度(除非像素数量是两个素数的乘积:P)。只要看看光滑。 –