C++ SDL,OpenGL:分段错误
当我尝试使用openGL生成纹理时,我发现该方法存在分割错误:C++ SDL,OpenGL:分段错误,c++,opengl,sdl,sdl-image,C++,Opengl,Sdl,Sdl Image,当我尝试使用openGL生成纹理时,我发现该方法存在分割错误: void RendererGL::create_gl(SDL_Surface * surf, GLuint * tex ) { GLenum format; GLint colors_amount = surf->format->BytesPerPixel; if (colors_amount == 4) { if (surf->format->Rmask == 0
void RendererGL::create_gl(SDL_Surface * surf, GLuint * tex ) {
GLenum format; GLint colors_amount = surf->format->BytesPerPixel;
if (colors_amount == 4) {
if (surf->format->Rmask == 0x000000ff)
format = GL_RGBA;
else
format = GL_BGRA;
}
else if (colors_amount == 3) {
if (surf->format->Rmask == 0x000000ff)
format = GL_RGB;
else
format = GL_BGR;
}
else {
gCritical("Image is not truecolor");
}
glGenTextures( 1, tex );
glBindTexture( GL_TEXTURE_2D, *tex );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D( GL_TEXTURE_2D, 0, colors_amount, surf->w, surf->h, 0, format,
GL_UNSIGNED_BYTE, surf->pixels ); }
我只在debian和nvidia驱动程序上遇到这个问题。使用开源驱动程序,应用程序可以正常运行。Valgrind测试返回:
[ Info ] RendererGL::Init GL
==13033== Thread 3:
==13033== Invalid read of size 8
==13033== at 0x51293C9: ??? (in /usr/lib/x86_64-linux-gnu/nvidia/current/libGL.so.304.64)
==13033== by 0x419B0F: RendererGL::initGL() (RendererGL.cpp:12)
==13033== by 0x41036C: Renderer::Renderer() (Renderer.cpp:27)
==13033== by 0x4058EA: Renderer::getInstance() (Renderer.hpp:17)
==13033== by 0x40B648: StandardReferences::StandardReferences() (StandardReferences.hpp:13)
==13033== by 0x414C26: Map::Map(short**, unsigned short, unsigned int, unsigned int) (Map.cpp:8)
==13033== by 0x4136E4: MapManager::loadMapFromFile(std::string, short) (MapManager.cpp:82)
==13033== by 0x4138A0: MapManager::load() (MapManager.cpp:97)
==13033== by 0x4084BA: Resource::load() (Resource.cpp:36)
==13033== by 0x4196AF: Splash::initThread(void*) (Splash.cpp:99)
==13033== by 0x53D0405: ??? (in /usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0.11.4)
==13033== by 0x5413898: ??? (in /usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0.11.4)
==13033== Address 0x658 is not stack'd, malloc'd or (recently) free'd
==13033==
==13033==
==13033== HEAP SUMMARY:
==13033== in use at exit: 22,104,451 bytes in 31,729 blocks
==13033== total heap usage: 47,499 allocs, 15,770 frees, 54,869,707 bytes allocated
==13033==
==13033== LEAK SUMMARY:
==13033== definitely lost: 833 bytes in 8 blocks
==13033== indirectly lost: 1,728 bytes in 38 blocks
==13033== possibly lost: 269,732 bytes in 99 blocks
==13033== still reachable: 21,832,158 bytes in 31,584 blocks
==13033== suppressed: 0 bytes in 0 blocks
==13033== Rerun with --leak-check=full to see details of leaked memory
==13033==
==13033== For counts of detected and suppressed errors, rerun with: -v
==13033== Use --track-origins=yes to see where uninitialised values come from
==13033== ERROR SUMMARY: 12 errors from 2 contexts (suppressed: 6 from 6)
我打赌它是用RGB曲面分割的,但不是RGBA曲面 请在调用
glTexImage2D()
之前重试
GL\u UNPACK\u ALIGNMENT
默认为4,这对RGBA数据很好,但会导致OpenGL读取RGB曲面的surf->pixels
末尾
如果
surf->pitch
为非零,则必须使用。通过变量“color\u amount”输入的glTexImage2D的第三个参数不接受任意值。根据规范,它必须是glTexImage2D参考中的格式标记之一;但是,使用无效令牌不应导致segfault。请尝试将其添加到main的最开始处:setenv(“MESA_DEBUG”,0)代码>。它将启用来自Mesa/OpenGL的调试/警告消息(它将这些消息写入stdout/stderr)。我通常只是将其包装在#ifdef DEBUG
中,并将其放入每个GL程序中。我唯一能想到的是,您的纹理可能不支持二维,而您的nvidia卡不支持它。不过我只是在猜测。谢谢你的回复:)我的图像大小是2048x1024像素,我不认为这是个问题。我发现segm在这一行失败了:glGenTextures(1,tex);GDB会话以此结束:程序收到信号SIGSEGV,分段故障。[切换到线程0x7fffe85f2700(LWP 3888)]从/usr/lib/x86_64-linux-gnu/libGL.so.1(gdb)的glGenTextures()中的0x00007ffff7db91a9,然后单步执行,直到退出没有行号信息的函数glGenTextures。0x00007FF75C3E40英寸??()来自/usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0