[英]OpenGL 3.3 texture mapping triangle
I have had little luck simply texturing a triangle in Opengl 3.3 (core) over the last few days. 在过去的几天里,我只是在Opengl 3.3(核心)中为一个三角形纹理化了一点运气。 I can successfully render the vertices and colors, but texturing seems to be problematic.
我可以成功渲染顶点和颜色,但纹理似乎有问题。 My current task/goal is vertices and texture coordinates only (no colors).
我当前的任务/目标只是顶点和纹理坐标(没有颜色)。
Since I create the test texture by hand, it should show up as a 16x16 white/red checkered triangle. 由于我手工创建测试纹理,它应该显示为16x16白色/红色方格三角形。
Strange behavior: If I use GL_CLAMP_TO_EDGE, it's all WHITE. 奇怪的行为:如果我使用GL_CLAMP_TO_EDGE,那就是白色。 If I use GL_REPEAT, it comes out pink (white + red)...
如果我使用GL_REPEAT,它会出现粉红色(白色+红色)......
vertex shader: 顶点着色器:
#version 330
layout (location = 0) in vec3 vertPos;
layout (location = 1) in vec2 texCoord;
out vec2 tex;
uniform mat4 viewMatrix, projMatrix;
void main()
{
gl_Position = projMatrix * viewMatrix * vec4(vertPos,1.0);
tex = texCoord;
};
fragment shader: 片段着色器:
#version 330
out vec4 Color;
in vec2 tex;
uniform sampler2D texsampler;
void main() {
Color = texture(texsampler, tex);
};
state initialization code (these are in order as shown): 状态初始化代码(这些按顺序如图所示):
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
//glPixelStorei(GL_PACK_ALIGNMENT,1);
glFrontFace(GL_CW); // clockwise oriented polys are front faces
glCullFace(GL_BACK); // cull out the inner polygons... Set Culling property appropriately
glLineWidth(1.0f); // for any wireframe
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); // color 1.0f is fully opaque, 0.0f is transparent
glDepthFunc(GL_LEQUAL);
glDepthRange(0,1); // default
glDisable(GL_CULL_FACE);
glClearColor(0.0f,0.0f,0.0f,1.0f); // set clear color to black
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); // filled polys
texture initialization: 纹理初始化:
glGenTextures(1,&texid1);
glBindTexture (GL_TEXTURE_2D, texid1);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
uint32 buf[16*16];
uint32 x;
for (x=0;x<16*16;) {
buf[x++] = 0xFFFFFFFF;
buf[x++] = 0xFFFF0000;
}
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8 , 16, 16, 0, GL_BGRA, GL_UNSIGNED_BYTE, (void*)buf);
glBindTexture (GL_TEXTURE_2D, 0);
shader initialization: 着色器初始化:
shader1 = new_gl_shader_program(vert_src,frag_src);
if (shader1) {
if (link_gl_shader_program(shader1)) {
projMatrixLoc = glGetUniformLocation(shader1, "projMatrix");
viewMatrixLoc = glGetUniformLocation(shader1, "viewMatrix");
sampler2dLoc = glGetUniformLocation(shader1, "texsampler");
glBindFragDataLocation( shader1, 0, "Color" ); // ADDED.
}
else {
print("LINK ERROR: %s\n",get_gl_shader_msg());
delete_gl_shader_program(shader1);
shader1 = 0;
}
}
else
print("COMPILE/MISC ERROR: %s\n",get_gl_shader_msg());
if (!shader1)
return;
buffer data initialization: 缓冲数据初始化:
typedef struct MESH_VERT_3D {
float x,y,z;
float u,v;
}MESH_VERT_3D;
MESH_VERT_3D meshbuf[3]; // total of 15 floats
meshbuf[0].x = 0.0f;
meshbuf[0].y = 0.0f;
meshbuf[0].z = 0.0f;
meshbuf[1].x = 1.0f;
meshbuf[1].y = 0.0f;
meshbuf[1].z = 0.0f;
meshbuf[2].x = 1.0f;
meshbuf[2].y = 0.0f;
meshbuf[2].z = 1.0f;
meshbuf[0].u = 0.0f;
meshbuf[0].v = 1.0f;
meshbuf[1].u = 1.0f;
meshbuf[1].v = 1.0f;
meshbuf[2].u = 1.0f;
meshbuf[2].v = 0.0f;
glGenBuffers(1, &vboid);
glBindBuffer(GL_ARRAY_BUFFER, vboid);
glBufferData(GL_ARRAY_BUFFER, sizeof(meshbuf), meshbuf, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &vaoHandle);
glBindVertexArray(vaoHandle);
glBindBuffer(GL_ARRAY_BUFFER, vboid);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*5, null);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,sizeof(float)*5, (void*)(sizeof(float)*3));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
render: 渲染:
gls_begin_3d(&glscene);
use_gl_shader_program(shader1);
glUniformMatrix4fv(projMatrixLoc, 1, GL_FALSE, gls_get_projection_matrix());
glUniformMatrix4fv(viewMatrixLoc, 1, GL_FALSE, gls_get_modelview_matrix());
glActiveTexture(GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, texid1);
glUniform1i(sampler2dLoc, 0);
glBindVertexArray(vaoHandle);
glDrawArrays(GL_TRIANGLES, 0, 3 );
glBindTexture (GL_TEXTURE_2D, 0);
use_gl_shader_program(0);
gls_end_3d(&glscene);
Issue 1: 问题1:
You should bind your fragment shader output variable Color
to a fragment data location: 您应该将片段着色器输出变量
Color
绑定到片段数据位置:
glBindFragDataLocation( shader1, 0, "Color" );
Otherwise OpenGL does not know where the results of your shader program are located. 否则,OpenGL不知道着色器程序的结果位于何处。
Issue 2: 问题2:
You create a second buffer object for your texture coordinates: glGenBuffers(1, &tboid);
您为纹理坐标创建第二个缓冲区对象:
glGenBuffers(1, &tboid);
However, everything should be put into a single VBO. 但是,一切都应该放在一个VBO中。
Issue 3: 问题3:
You pass null
to glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,sizeof(float)*5, null);
您将
null
传递给glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,sizeof(float)*5, null);
However, the beginning of texture coordinates is not 0. It is sizeof(float)*3
. 但是,纹理坐标的开头不是0.它是
sizeof(float)*3
。
Issue 4: 问题4:
In the vertex shader, you have out vec2 texCoordOut;
在顶点着色器中,你有
out vec2 texCoordOut;
. 。 However, in the fragment shader, you use
in vec2 tex;
但是,在片段着色器中,您
in vec2 tex;
使用in vec2 tex;
. 。 The names should match.
名称应该匹配。
In the vertex shader, you have 在顶点着色器中,你有
out vec2 texCoordOut;
but in the fragment shader, you use 但在片段着色器中,您使用
in vec2 tex;
This is not going to work, since you are required to use the same name so the GL can match it up. 这不起作用,因为您需要使用相同的名称,以便GL可以匹配它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.