Open GL - ES 2.0:多顶点缓冲区对象

发布于 2025-01-04 01:01:19 字数 2384 浏览 2 评论 0原文

我有一个关于 openGL ES 2.0 的快速问题,我正在尝试在屏幕上创建一些绘图,这将需要多个几何图形。这就是我创建几何图形的方式。

(void)setupBoxGeometry{

   GLuint vertexBuffer;
   glGenBuffers(1, &vertexBuffer);
   glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
   glBufferData(GL_ARRAY_BUFFER, sizeof(BoxVertices), BoxVertices, GL_STATIC_DRAW);

   GLuint indexBuffer;
   glGenBuffers(1, &indexBuffer);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
   glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(BoxIndices), BoxIndices, GL_STATIC_DRAW);
}

- (void)setupLineGeometry{

   GLuint vertexBuffer;
   glGenBuffers(1, &vertexBuffer);
   glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
   glBufferData(GL_ARRAY_BUFFER, sizeof(LineVertices), LineVertices, GL_STATIC_DRAW);

   GLuint indexBuffer;
   glGenBuffers(1, &indexBuffer);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
   glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(LineIndices), LineIndices, GL_STATIC_DRAW);
}

这是我的渲染函数,

- (void)render:(CADisplayLink*)displayLink {
   glClearColor(0.0/255.0, 0.0/255.0, 0.0/255.0, 1.0);
   glClear(GL_COLOR_BUFFER_BIT);

   float h = 4.0f * self.frame.size.height / self.frame.size.width;
   glViewport(0, 0, self.frame.size.width, self.frame.size.height);

   glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 
                      sizeof(Vertex), 0);
   glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, 
                      sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));


   // Do some thing to the projection    
   CC3GLMatrix *projection = [CC3GLMatrix matrix];    
   [projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
   glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);


   // Create the base for the square to be drawn on the screen    
   _baseMatrix = [CC3GLMatrix matrix];


   [_baseMatrix populateFromTranslation:CC3VectorMake(-3.0, -5.2 + _yIncrease, -10)];    
   [_baseMatrix scaleByX:0.5];
   [_baseMatrix scaleByY:_yIncrease];
   [_baseMatrix rotateBy:CC3VectorMake(0, _rotationAngle, 0)];
   glUniformMatrix4fv(_modelViewUniform, 1, 0, _baseMatrix.glMatrix);
   glDrawElements(GL_TRIANGLES, sizeof(BoxIndices)/sizeof(BoxIndices[0]), 
               GL_UNSIGNED_BYTE, 0);
}

我在调用线几何函数之前调用框几何函数。现在的问题是我的渲染函数只绘制线条。它不绘制任何框。我做错了什么。如果问题不清楚,我可以提供更多说明/

I had a quick question on openGL ES 2.0 I am trying to create some drawings on the screen which will require multiple geometries. This is how I am creating the geometries.

(void)setupBoxGeometry{

   GLuint vertexBuffer;
   glGenBuffers(1, &vertexBuffer);
   glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
   glBufferData(GL_ARRAY_BUFFER, sizeof(BoxVertices), BoxVertices, GL_STATIC_DRAW);

   GLuint indexBuffer;
   glGenBuffers(1, &indexBuffer);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
   glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(BoxIndices), BoxIndices, GL_STATIC_DRAW);
}

- (void)setupLineGeometry{

   GLuint vertexBuffer;
   glGenBuffers(1, &vertexBuffer);
   glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
   glBufferData(GL_ARRAY_BUFFER, sizeof(LineVertices), LineVertices, GL_STATIC_DRAW);

   GLuint indexBuffer;
   glGenBuffers(1, &indexBuffer);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
   glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(LineIndices), LineIndices, GL_STATIC_DRAW);
}

This is my render function

- (void)render:(CADisplayLink*)displayLink {
   glClearColor(0.0/255.0, 0.0/255.0, 0.0/255.0, 1.0);
   glClear(GL_COLOR_BUFFER_BIT);

   float h = 4.0f * self.frame.size.height / self.frame.size.width;
   glViewport(0, 0, self.frame.size.width, self.frame.size.height);

   glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 
                      sizeof(Vertex), 0);
   glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, 
                      sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));


   // Do some thing to the projection    
   CC3GLMatrix *projection = [CC3GLMatrix matrix];    
   [projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
   glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);


   // Create the base for the square to be drawn on the screen    
   _baseMatrix = [CC3GLMatrix matrix];


   [_baseMatrix populateFromTranslation:CC3VectorMake(-3.0, -5.2 + _yIncrease, -10)];    
   [_baseMatrix scaleByX:0.5];
   [_baseMatrix scaleByY:_yIncrease];
   [_baseMatrix rotateBy:CC3VectorMake(0, _rotationAngle, 0)];
   glUniformMatrix4fv(_modelViewUniform, 1, 0, _baseMatrix.glMatrix);
   glDrawElements(GL_TRIANGLES, sizeof(BoxIndices)/sizeof(BoxIndices[0]), 
               GL_UNSIGNED_BYTE, 0);
}

I call the box geometry function before I call the line geometry function. The problem is now my render function only draws lines. It does not draw any boxes. What am I doing wrong. If the question is not clear I can give more clarifications/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝戈者 2025-01-11 01:01:19

绘制之前在哪里绑定缓冲区?
调用

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

之前

glVertexAttribPointer(...);

并调用

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

之前

glDrawElements(...);

您还需要启用您正在使用的顶点指针:

glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);

Where are you binding buffers before drawing?
Call

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

before

glVertexAttribPointer(...);

And call

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

before

glDrawElements(...);

Also you need to enable vertex pointers you are using:

glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文