我正在尝试制作游戏,它是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;
}
任何想法?
答案 0 :(得分:2)
我必须承认,我嘲笑这个:)你使用原始 PNG 文件数据作为RGB像素数据。这是不正确的。 PNG文件已压缩。在将它们提供给OpenGL之前,首先需要解码它们。 Slick2D是一个很好地完成工作的库。