OpenGL 和 SDL_Image 纹理问题
OpenGL and SDL_Image texture issue
我正在使用 glew 1.1.0、SDL_image 2.0 和 mingw(Code::Blocks, Windows)。我正在尝试使用 SDL_Image 导入一个 .png 文件并使其成为纹理并显示到我使用 OpenGL 的屏幕上。当我 运行 程序显示纯白色正方形时,它与我尝试导入的图像具有相同的宽度和高度,但它是纯白色的。我连续查看了大约 30 分钟的代码,代码看起来不错,而且它从来没有给我任何错误 execpt "Error initializing OpenGL! invalid value".
我的main.cpp:
#include <GL/glew.h>
#include <SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include <stdio.h>
#include <SDL_image.h>
#include <SDL_opengl.h>
using namespace std;
void init();
void Render();
bool ex;
bool akey;
SDL_Event e;
SDL_Window *win = NULL;
GLuint txID;
int x, y, w, h;
int main(int argc, char* args[])
{
init();
while(!ex)
{
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
{
ex = true;
}
Render();
}
}
}
void init()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
win = SDL_CreateWindow("Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL);
SDL_GLContext cont = SDL_GL_CreateContext(win);
SDL_GL_SetSwapInterval(1);
glewInit();
glClear( GL_COLOR_BUFFER_BIT );
glViewport( 0.f, 0.f, 640, 480);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, 640, 480, 0.0, 1.0, -1.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glClearColor( 0.f, 0.f, 0.f, 1.f );
glEnable( GL_TEXTURE_2D );
glEnable(GL_BLEND);
SDL_Surface *sur = IMG_Load("ef.png");
w = 40;
h = 60;
glGenTextures( 1, &txID );
glBindTexture(GL_TEXTURE_2D, txID);
glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, sur->pixels);
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
}
//Set texture parameters
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
//Unbind texture
glBindTexture(GL_TEXTURE_2D, NULL);
}
void Render()
{
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glTranslatef(20, 20, 0);
glBindTexture(GL_TEXTURE_2D, txID);
glBegin( GL_QUADS );
glTexCoord2f( 0.f, 0.f ); glVertex2f(0.f, 0.f);
glTexCoord2f( 1.f, 0.f ); glVertex2f(w, 0.f);
glTexCoord2f( 1.f, 1.f ); glVertex2f(w, h);
glTexCoord2f( 0.f, 1.f ); glVertex2f(0.f, h);
glEnd();
SDL_GL_SwapWindow(win);
}
这是我要加载的图像:
程序和错误,如您所见,白色框是图像应在的位置。
你得到一个 invalid value
因为你将 4
作为 internalFormat
传递。传递 4
表示 GL_TRIANGLES
不是有效格式。
Check the documentation for valid formats.
您可能打算通过 GL_RGBA
。
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, sur->pixels);
The weirdest part about this is i got to work on my old laptop
它之所以能在你的旧笔记本电脑上运行,是因为驱动程序很聪明。
It still gives me the same error and still renders a pure white square
首先,您可以从 SDL_Surface
.
获取它们,而不是自己设置 w
和 h
SDL_Surface *sur = IMG_Load("someimage.jpg");
GLuint txID = 0;
glGenTextures(1, &txID);
glBindTexture(GL_TEXTURE_2D, txID);
int mode = GL_RGB;
if(sur->format->BytesPerPixel == 4)
mode = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, mode, sur->w, sur->h, 0, mode, GL_UNSIGNED_BYTE, sur->pixels);
我现在注意到您的目标是 OpenGL 4.0。因此 invalid value
也可能是由于您尝试使用固定功能管道,同时具有 OpenGL 4.0 上下文。
虽然我通常建议远离固定功能管道。尝试请求 OpenGL 2.0 上下文,看看是否能解决问题。
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);
我正在使用 glew 1.1.0、SDL_image 2.0 和 mingw(Code::Blocks, Windows)。我正在尝试使用 SDL_Image 导入一个 .png 文件并使其成为纹理并显示到我使用 OpenGL 的屏幕上。当我 运行 程序显示纯白色正方形时,它与我尝试导入的图像具有相同的宽度和高度,但它是纯白色的。我连续查看了大约 30 分钟的代码,代码看起来不错,而且它从来没有给我任何错误 execpt "Error initializing OpenGL! invalid value".
我的main.cpp:
#include <GL/glew.h>
#include <SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include <stdio.h>
#include <SDL_image.h>
#include <SDL_opengl.h>
using namespace std;
void init();
void Render();
bool ex;
bool akey;
SDL_Event e;
SDL_Window *win = NULL;
GLuint txID;
int x, y, w, h;
int main(int argc, char* args[])
{
init();
while(!ex)
{
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
{
ex = true;
}
Render();
}
}
}
void init()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
win = SDL_CreateWindow("Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL);
SDL_GLContext cont = SDL_GL_CreateContext(win);
SDL_GL_SetSwapInterval(1);
glewInit();
glClear( GL_COLOR_BUFFER_BIT );
glViewport( 0.f, 0.f, 640, 480);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, 640, 480, 0.0, 1.0, -1.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glClearColor( 0.f, 0.f, 0.f, 1.f );
glEnable( GL_TEXTURE_2D );
glEnable(GL_BLEND);
SDL_Surface *sur = IMG_Load("ef.png");
w = 40;
h = 60;
glGenTextures( 1, &txID );
glBindTexture(GL_TEXTURE_2D, txID);
glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, sur->pixels);
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
}
//Set texture parameters
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
//Unbind texture
glBindTexture(GL_TEXTURE_2D, NULL);
}
void Render()
{
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glTranslatef(20, 20, 0);
glBindTexture(GL_TEXTURE_2D, txID);
glBegin( GL_QUADS );
glTexCoord2f( 0.f, 0.f ); glVertex2f(0.f, 0.f);
glTexCoord2f( 1.f, 0.f ); glVertex2f(w, 0.f);
glTexCoord2f( 1.f, 1.f ); glVertex2f(w, h);
glTexCoord2f( 0.f, 1.f ); glVertex2f(0.f, h);
glEnd();
SDL_GL_SwapWindow(win);
}
这是我要加载的图像:
程序和错误,如您所见,白色框是图像应在的位置。
你得到一个 invalid value
因为你将 4
作为 internalFormat
传递。传递 4
表示 GL_TRIANGLES
不是有效格式。
Check the documentation for valid formats.
您可能打算通过 GL_RGBA
。
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, sur->pixels);
The weirdest part about this is i got to work on my old laptop
它之所以能在你的旧笔记本电脑上运行,是因为驱动程序很聪明。
It still gives me the same error and still renders a pure white square
首先,您可以从 SDL_Surface
.
w
和 h
SDL_Surface *sur = IMG_Load("someimage.jpg");
GLuint txID = 0;
glGenTextures(1, &txID);
glBindTexture(GL_TEXTURE_2D, txID);
int mode = GL_RGB;
if(sur->format->BytesPerPixel == 4)
mode = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, mode, sur->w, sur->h, 0, mode, GL_UNSIGNED_BYTE, sur->pixels);
我现在注意到您的目标是 OpenGL 4.0。因此 invalid value
也可能是由于您尝试使用固定功能管道,同时具有 OpenGL 4.0 上下文。
虽然我通常建议远离固定功能管道。尝试请求 OpenGL 2.0 上下文,看看是否能解决问题。
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);