[英]SDL2 with OpenGL texture displaying incorrectly
I have searched for this but unfortunately the only answers I find are those that deal with the issue of textures not being mapped, in this case the texture is mapped but does not look like the loaded surface. 我已经进行了搜索,但是不幸的是,我找到的唯一答案是那些处理纹理未映射问题的答案,在这种情况下,纹理已映射但看上去不像加载的表面。
Anyway, ignoring the fact that power of 2 square images aren't required (which I've yet to properly look into yet) I've made a functional script for expanding the surface to power of 2 dimensions and a structure for storing the resized surface, the old dimensions of the surface and the ID for the texture. 无论如何,忽略了不需要2方图像的幂的事实(我尚未适当研究过),我制作了一个功能脚本,用于将表面扩展为2维的幂,并编写了用于存储调整大小的结构表面,表面的旧尺寸和纹理ID。
My surface to texture code is (arguments are SDL_Surface* surf, antTex* tex): 我的表面到纹理的代码是(参数为SDL_Surface * surf和antTex * tex):
tex->w=surf->w;
tex->h=surf->h;
tex->surf=resizeToNearestPow2(surf);
glGenTextures(1, &tex->id);
glBindTexture(GL_TEXTURE_2D, tex->id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->surf->w, tex->surf->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, tex->surf->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, NULL);
The antTex struct is: antTex结构为:
GLuint id; //Texture ID
int w, h; //Original Image Size
int rw, rh; //Resized Canvas Size
SDL_Surface* surf; //Resized Surface
Everything about resizing the image appears to be working fine (having used SDL's SDL_SaveBMP function to check the resulting image), I used SDL2_image.h's IMG_Load for loading the original image. 关于调整图像大小的所有操作似乎都正常(使用SDL的SDL_SaveBMP函数检查生成的图像),我使用SDL2_image.h的IMG_Load加载了原始图像。
When I draw a plane with the texture mapped to it while GL_BLEND is enabled nothing shows up at all, when I disable it I get this (the red is the window's background colour): 在启用GL_BLEND的情况下绘制映射有纹理的平面时,什么都没有显示,当禁用它时,我得到了这个(红色是窗口的背景色):
http://i1200.photobucket.com/albums/bb336/DarknessXeagle/error_zps03e1e5a1.png http://i1200.photobucket.com/albums/bb336/DarknessXeagle/error_zps03e1e5a1.png
The original image was this: 原始图像是这样的:
http://i1200.photobucket.com/albums/bb336/DarknessXeagle/d_zpsc8b9af6f.png http://i1200.photobucket.com/albums/bb336/DarknessXeagle/d_zpsc8b9af6f.png
This is my function for drawing plane with the texture mapped to it (arguments are antTex* tex, int x, int y): 这是我的功能,用于绘制映射有纹理的平面(参数为antTex * tex,int x,int y):
glBindTexture(GL_TEXTURE_2D, tex->id);
int w, h;
w=tex->w;
h=tex->h;
GLfloat tw, th;
th=(GLfloat)tex->h/(GLfloat)tex->rh;
tw=(GLfloat)tex->w/(GLfloat)tex->rw;
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
glNormal3f(0, 0, 1);
glTexCoord2f(0, th); glVertex2f(0, h);
glTexCoord2f(tw, th); glVertex2f(w, h);
glTexCoord2f(tw, 0); glVertex2f(w, 0);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glEnd();
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, NULL);
You have a problem with u and v coordinates passed to render the texture into the quad using glTexCoord2f. 您使用u和v坐标传递问题,使用glTexCoord2f将纹理渲染到四边形中。
You need to change: 您需要更改:
glTexCoord2f(0, th); glVertex2f(0, h);
glTexCoord2f(tw, th); glVertex2f(w, h);
glTexCoord2f(tw, 0); glVertex2f(w, 0);
glTexCoord2f(0, 0); glVertex2f(0, 0);
into: 变成:
glTexCoord2f(0, 1); glVertex2f(0, h);
glTexCoord2f(1, 1); glVertex2f(w, h);
glTexCoord2f(1, 0); glVertex2f(w, 0);
glTexCoord2f(0, 0); glVertex2f(0, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.