OpenGL 3.3纹理映射三角形

时间:2013-08-12 21:12:53

标签: c++ opengl glsl texture-mapping opengl-3

在过去的几天里,我在Opengl 3.3(核心)中简单地纹理三角形的运气很少。我可以成功渲染顶点和颜色,但纹理似乎有问题。我当前的任务/目标只是顶点和纹理坐标(没有颜色)。

由于我手工创建测试纹理,它应该显示为16x16白色/红色方格三角形。

奇怪的行为:如果我使用GL_CLAMP_TO_EDGE,那就是白色。如果我使用GL_REPEAT,它会变成粉红色(白色+红色)......

顶点着色器:

#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;
};

片段着色器:

#version 330
out vec4 Color;
in vec2 tex;
uniform sampler2D texsampler;
void main() {
  Color = texture(texsampler, tex);
};

状态初始化代码(这些按顺序显示):

  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

纹理初始化:

  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);

着色器初始化:

  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;

缓冲区数据初始化:

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);

渲染:

  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);

2 个答案:

答案 0 :(得分:2)

问题1:

您应该将片段着色器输出变量Color绑定到片段数据位置:

glBindFragDataLocation( shader1, 0, "Color" );

否则OpenGL不知道着色器程序的结果位于何处。

问题2:

为纹理坐标创建第二个缓冲区对象:glGenBuffers(1, &tboid);但是,所有内容都应放在一个VBO中。

问题3:

您将null传递给glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,sizeof(float)*5, null);但是,纹理坐标的开头不是0.它是sizeof(float)*3

第4期:

在顶点着色器中,您有out vec2 texCoordOut;。但是,在片段着色器中,您使用 in vec2 tex;。名称应该匹配。

答案 1 :(得分:1)

在顶点着色器中,您有

out vec2 texCoordOut;

但在片段着色器中,您使用

in vec2 tex;

这不起作用,因为您需要使用相同的名称,以便GL可以匹配它。