1

我正在学习 OpenGL ES。我已经成功地绘制了一些立方体,了解相机位置背后的基本矩阵逻辑等。现在我正在尝试为 VBO 构建一个示例,但它没有在屏幕上绘制任何内容。我将粘贴我的代码,可能有人会意识到我做错了什么。

片段着色器代码

precision mediump float;
varying vec2 v_texCoord;
varying vec3 v_colour; 
uniform sampler2D s_texture;    
void main() 
{ 
 gl_FragColor = texture2D(s_texture, v_texCoord);
}

顶点着色器

attribute vec3 av3position;
attribute vec2 a_texCoord; 
attribute vec3 av3colour;

uniform mat4 PerspectiveMatrix;
uniform mat4 ModelViewMatrix;

varying vec2 v_texCoord; 
varying vec3 v_colour;

void main() 
{ 
v_texCoord = a_texCoord; 
v_colour =av3colour;
vec4 pos = ModelViewMatrix * vec4(av3position,1.0);
gl_Position = PerspectiveMatrix * pos;
}

纹理加载

Texture01RGBA->boLoadTextureFromfile("BitmapData/cubeLayout_02.png" );

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);         // Use tightly packed data
glGenTextures(1, &gluiTextureID);              // Generate a texture object
glBindTexture(GL_TEXTURE_2D, gluiTextureID);   // Bind the texture object
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Texture01RGBA->getWidth(), Texture01RGBA->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, Texture01RGBA->getPixmapPointer()); 
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);     
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
glActiveTexture ( GL_TEXTURE0 );
glBindTexture ( GL_TEXTURE_2D, gluiTextureID );
// Set the sampler texture unit to 0
glUniform1i ( iLoc2DSampler, 0 );

顶点对象结构

const float aCubeVertices[] =
{ // x, y, z, nx, ny, nz, u, v
 1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.500000, 0.500000,
 1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.500000, 0.250000,
-1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.250000, 0.250000,
-1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.250000, 0.500000,
 1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.500000, 0.750000,
-1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.250000, 0.750000,
-1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.250000, 1.000000,
 1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.500000, 1.000000,
 1.000000,1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.750000, 0.250000,
 1.000000,1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.750000, 0.000000,
 1.000000,1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.500000, 0.000000,
 1.000000,1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.500000, 0.250000,
 1.000000,1.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.500000, 0.250000,
 1.000000,1.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.500000, 0.000000,
-1.000000,1.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.250000, 0.000000,
-1.000000,1.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.250000, 0.250000,
-1.000000,1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.250000, 0.250000,
-1.000000,1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.250000, 0.000000,
-1.000000,1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000,
-1.000000,1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.250000,
 1.000000,1.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.500000, 0.750000,
 1.000000,1.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.500000, 0.500000,
-1.000000,1.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.250000, 0.500000,
-1.000000,1.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.250000, 0.750000
};

unsigned short aCubeIndices[]= // 36 Elements. 3 Groups
{  0,1,2,0,2,3,4,5,6,4,6,7,8,9,10,8,10,11,12,13,14,12,14,15,16,17,18,16,18,19,20,21,22,20,22,23 };

VBO创作

glGenBuffers (1, &vao);
glBindBuffer  (GL_ARRAY_BUFFER, vao);

glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float)*8, aCubeVertices, GL_STATIC_DRAW);
glBindBuffer (GL_ARRAY_BUFFER, 0);

glGenBuffers(1, &vinx);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vinx);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36* sizeof(GLushort), aCubeIndices, GL_STATIC_DRAW);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);

VBO 绘图

glBindBuffer  (GL_ARRAY_BUFFER, vao);
  // set up vertex attributes
  glEnableVertexAttribArray(iLocPosition);
  glEnableVertexAttribArray(iLocTexCoord);
  glVertexAttribPointer(iLocPosition, 3, GL_FLOAT, GL_FALSE, sizeof(float)*8, 0);
  glVertexAttribPointer(iLocTexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(float)*8,  (const void *)24  );
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vinx);
  glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, (void*)0);
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  glDisableVertexAttribArray(iLocPosition);
  glDisableVertexAttribArray(iLocTexCoord);

正如我之前指出的那样,对于我之前完成的更简单的示例(纹理、转换矩阵),我一直使用基本相同的代码。我还检查了错误,并且没有代码错误存在。

如果有人强调我的代码可能有什么问题,我将不胜感激。

4

2 回答 2

0

Your vertex buffer (or at least your index buffer) seems incorrect. For instance, the first triangle is defined by the indices 0, 1, 2, which correspond to the following vertices:

 1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.500000, 0.500000,
 1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.500000, 0.250000,
-1.000000,1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.250000, 0.250000,

As you can see, the position of the first and the second vertices is the same (1,1,1). The result is a degenerate triangle (a triangle with no area) which can't be rendered.

于 2012-10-29T20:38:33.117 回答
0

您在此处显示的代码看起来是正确的。问题可能出在代码的其他部分。错误来源之一可能是您如何设置模型视图和透视矩阵。另一个可能是您设置制服 glUniform*() 的方式有问题(可能是行/列多数与您的矩阵不匹配以及 OpenGL 的预期方式)。如果您可以粘贴该代码,那么我们将拥有完整的图片我假设立方体的顶点和索引数据是正确的,因为您提到您之前已经绘制了立方体。

于 2013-05-07T10:56:46.257 回答