2014-03-03 121 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)。只要看看光滑。 –