1

在过去的几天里,我只是在 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);
4

2 回答 2

1

在顶点着色器中,您有

out vec2 texCoordOut;

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

in vec2 tex;

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

于 2013-08-12T22:21:22.230 回答
1

问题一:

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

glBindFragDataLocation( shader1, 0, "Color" );

否则 OpenGL 不知道你的着色器程序的结果在哪里。

问题 2:

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

问题 3:

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

问题 4:

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

于 2013-08-12T21:17:19.210 回答