【发布时间】:2014-11-04 11:24:16
【问题描述】:
我正在尝试使用 SDL2 为波前对象的 OpenGL 渲染加载纹理(目前我正在使用固定管道进行测试,但我最终计划转向着色器)。问题是加载的纹理应用于四边形(以及使用纹理右下角一小部分的模型)看起来像这样:
(来源:image-upload.de)
当使用 SDL 函数绘制图像时,图像加载良好并且看起来非常正常,因此可能是转换为 OGL 纹理时损坏了。请注意,我启用了 alpha 混合,并且纹理仍然完全不透明 - 所以这些值不是完全随机的,也可能不是未初始化的内存。 这是我用于转换表面的代码(由本网站上的各种教程和问题拼凑而成):
GLuint glMakeTexture(bool mipmap = false, int request_size = 0) { // Only works on 32 Bit Surfaces
GLuint texture = 0;
if ((bool)_surface) {
int w,h;
if (request_size) { // NPOT and rectangular textures are widely supported since at least a decade now; you should never need this...
w = h = request_size;
if (w<_surface->w || h<_surface->h) return 0; // No can do.
} else {
w = _surface->w;
h = _surface->h;
}
SDL_LockSurface(&*_surface);
std::cout<<"Bits: "<<(int)_surface->format->BytesPerPixel<<std::endl;
Uint8 *temp = (Uint8*)malloc(w*h*sizeof(Uint32)); // Yes, I know it's 4...
if (!temp) return 0;
// Optimized code
/*for (int y = 0; y<h; y++) { // Pitch is given in bytes, so we need to cast to 8 bit here!
memcpy(temp+y*w*sizeof(Uint32),(Uint8*)_surface->pixels+y*_surface->pitch,_surface->w*sizeof(Uint32));
if (w>_surface->w) memset(temp+y*w*sizeof(Uint32)+_surface->w,0,(w-_surface->w)*sizeof(Uint32));
}
for (int y = _surface->h; y<h; y++) memset(temp+y*w*sizeof(Uint32),0,w*sizeof(Uint32));
GLenum format = (_surface->format->Rmask==0xFF)?GL_RGBA:GL_BGRA;*/
// Naive code for testing
for (int y = 0; y<_surface->h; y++)
for (int x = 0; x<_surface->w; x++) {
int mempos = (x+y*w)*4;
SDL_Color pcol = get_pixel(x,y);
temp[mempos] = pcol.r;
temp[mempos+1] = pcol.g;
temp[mempos+2] = pcol.b;
temp[mempos+3] = pcol.a;
}
GLenum format = GL_RGBA;
SDL_UnlockSurface(&*_surface);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
if (mipmap) glTexParameteri(texture, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, temp);
if (mipmap) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
else glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
free(temp); // Always clean up...
}
return texture;
}
编辑:_surface 实际上是 SDL_Surface 的 std::shared_ptr。因此,&* 当(取消)锁定它时。
顺便说一句,SDL 声称表面在我的机器上被格式化为 32 位 RGBA,我已经检查过了。
绑定纹理和绘制四边形的代码在这里:
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D,_texture[MAP_KD]);
static bool once = true;
if (once) {
int tex;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &tex);
bool valid = glIsTexture(tex);
std::cout<<tex<<" "<<valid<<std::endl;
once = false;
}
glBegin(GL_TRIANGLE_STRIP);
//glColor3f(1.f,1.f,1.f);
glNormal3f(0,1,0);
glTexCoord2f(0.f,0.f); glVertex3f(0,0,0);
glTexCoord2f(0.f,1.f); glVertex3f(0,0,1);
glTexCoord2f(1.f,0.f); glVertex3f(1,0,0);
glTexCoord2f(1.f,1.f); glVertex3f(1,0,1);
glEnd();
斧头是稍后从索引列表中绘制的;代码太长了,不能在这里分享(此外,除了纹理之外,它工作得很好)。
我还尝试了许多将_surface->pixels 传递给glTexImage2D() 的教程中的简单方法,但这也无济于事(而且我听说这是错误的方法,因为pitch!=width* BytesPerPixel)。 顺便说一句(正如预期的那样),注释掉的“优化”代码看起来完全相同。为了更好的测试,我写了下半部分。将所有像素设置为特定颜色或使纹理部分透明按预期工作,因此我假设 OpenGL 正确加载 temp 中的值。可能是我对 SDL2 Surfaces 内存布局的理解搞砸了。
最终编辑:解决方案(重置 GL_UNPACK_ROW_LENGTH 是关键,感谢 Peter Clark):
GLuint glTexture(bool mipmap = false) {
GLuint texture = 0;
if ((bool)_surface) {
GLenum texture_format, internal_format, tex_type;
if (_surface->format->BytesPerPixel == 4) {
if (_surface->format->Rmask == 0x000000ff) {
texture_format = GL_RGBA;
tex_type = GL_UNSIGNED_INT_8_8_8_8_REV;
} else {
texture_format = GL_BGRA;
tex_type = GL_UNSIGNED_INT_8_8_8_8;
}
internal_format = GL_RGBA8;
} else {
if (_surface->format->Rmask == 0x000000ff) {
texture_format = GL_RGB;
tex_type = GL_UNSIGNED_BYTE;
} else {
texture_format = GL_BGR;
tex_type = GL_UNSIGNED_BYTE;
}
internal_format = GL_RGB8;
}
int alignment = 8;
while (_surface->pitch%alignment) alignment>>=1; // x%1==0 for any x
glPixelStorei(GL_UNPACK_ALIGNMENT,alignment);
int expected_pitch = (_surface->w*_surface->format->BytesPerPixel+alignment-1)/alignment*alignment;
if (_surface->pitch-expected_pitch>=alignment) // Alignment alone wont't solve it now
glPixelStorei(GL_UNPACK_ROW_LENGTH,_surface->pitch/_surface->format->BytesPerPixel);
else glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, _surface->w, _surface->h, 0, texture_format, tex_type, _surface->pixels);
if (mipmap) {
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
}
return texture;
}
【问题讨论】:
-
你有关于直接发送像素的“pitch!=width*BytesPerPixel”的参考吗?我以前没听说过。
-
我从这个讨论中得知:gamedev.net/topic/184477-sdl_surface-to-opengl-texture - 可能是错的,但这并不能解释崩溃。
-
我的回答中有错字 - 对齐应该是 2 的 最大 次方,而不是最小的(否则它总是 1)。不应该修复崩溃,而是修复性能的重要事项。
-
不,这正是 NVidia 的信息。它说的唯一另一件事是“错误代码1”(这可能没有帮助)。在调试器中遍历代码时,它不会立即崩溃。确切的情况是屏幕开始闪烁并且应用程序在大约半秒内没有反应,然后出现 NVidia 错误消息。
-
用关于创建“完整”纹理的详细信息更新了我的答案。
标签: c++ opengl textures loading sdl-2