我正在尝试将图像加载到我的游戏中。它是用 C++ 编写的,带有 SDL 和 OpenGL,以及 SDL_Image 框架。我已经获得了图像,并根据我的需要对其进行了旋转/反转,但是有两个主要问题。
首先,图像的颜色完全反转。(即蓝色-> 红色)我的格式为“GL_RGB”或“GL_RGBA”,具体取决于每个像素的字节数。我尝试过使用 PNG、JPEG 和 BMP,但它们都一样。我迷路了!
--感谢 datenwolf 纠正我的愚蠢错误!
其次,图像绘制一次,仅此而已。它不会在另一个缓冲区上重新绘制,它只会显示一次,然后在绘制下一帧时立即消失。
GLuint loadTexture()
{
SDL_Surface* image = IMG_Load( "/Users/<My name>/Pictures/pacman board.jpeg" );
SDL_DisplayFormatAlpha(image);
unsigned object(0);
glGenTextures(1, &object);
glBindTexture(GL_TEXTURE_2D, object);
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_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
int Mode = NULL;
if(image->format->BytesPerPixel == 3) {Mode = GL_RGB;}
else if(image->format->BytesPerPixel == 4) {Mode = GL_RGBA;}
glTexImage2D(GL_TEXTURE_2D, 0, Mode, image->w, image->h, 0, Mode, GL_UNSIGNED_BYTE, image->pixels);
SDL_FreeSurface(image);
return object;
}
int main(int argc, char * argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_WM_SetCaption("HI", NULL);
SDL_SetVideoMode(600, 600, 32, SDL_OPENGL);
glClearColor(0,0,0,1);
glViewport(0, 0, 600, 600);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
unsigned int pad_texture = 0;
pad_texture = loadTexture();
//---MAIN LOOP---\\
bool isRunning = true;
SDL_Event event;
while (isRunning)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT) { isRunning = false; }
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE) {isRunning = false;}
}
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0, 600, 0, 600); // For some reason, I prefer having '0,0' at the bottom left.
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,pad_texture);
glBegin(GL_QUADS);
glTexCoord2d(0, 1); glVertex2f(0, 0);
glTexCoord2d(1, 1); glVertex2f(600, 0);
glTexCoord2d(1, 0); glVertex2f(600, 600);
glTexCoord2d(0, 0); glVertex2f(0, 600);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix(); //Stop drawing
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}