在树莓派上打开GL,索引顶点
openGL on raspberry pi, indexed vertices
我正在尝试学习一些关于树莓派的基本opengl。我喜欢这些教程https://solarianprogrammer.com/2013/05/13/opengl-101-drawing-primitives/http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-9-vbo-indexing/
我碰到了一个我无法解决的问题。我成功地通过列出数组中的顶点来绘制三角形,然后我尝试做同样的事情,但使用索引,它不起作用,我不知道为什么。它成功编译并运行,但不显示三角形。
这两个教程都使它变得如此简单,添加一个缓冲区并将"glDrawArrays"替换为"glDrawElements"。 但我一定错过了什么..
#include <stdio.h>
#include <unistd.h>
#include "../common/startScreen.h"
#include "../common/LoadShaders.h"
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
int main(int argc, const char **argv)
{
float r=0.0;
InitGraphics();
printf("Screen startedn");
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "simpletransformvertshader.glsl", "simplefragshader.glsl" );
printf("Shaders loadedn");
// Get a handle for our "MVP" uniform
GLuint MatrixID = glGetUniformLocation(programID, "MVP");
// Get a handle for our buffers
GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
// Projection matrix
glm::mat4 Projection = glm::perspective(45.0f, 16.0f / 9.0f, 0.1f, 100.0f);
// Camera matrix
glm::mat4 View = glm::lookAt(
glm::vec3(0,0,5),
glm::vec3(0,0,0),
glm::vec3(0,1,0),
);
//glm::mat4 myMatrix = glm::translate(10.0f, 0.0f, 0.0f);
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model = glm::mat4(1.0f);
// Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = Projection * View * Model; // Remember, matrix$
//glm:mat4 MVP = Prpkection * View * Model;
GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
// -1.0, 1.0f, 0.0f,
};
GLuint indices[3] = {
0,1,2,
// 2,3,0
};
// Set the viewport
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_bu$
GLuint eab;
glGenBuffers(1, &eab);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STAT$
do{
// Model = glm::rotate(Model, r, glm::vec3(0.0f, 0.0f, 1.0f));
glClear( GL_COLOR_BUFFER_BIT );
// Use our shader
glUseProgram(programID);
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(vertexPosition_modelspaceID);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
vertexPosition_modelspaceID,
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// see above glEnableVertexAttribArray(vertexPosition_modelspaceID);
// Draw the triangle !
// glDrawArrays(GL_TRIANGLES, 0, 3);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab);
glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(vertexPosition_modelspaceID);
updateScreen();
}
while(1);
// Cleanup VBO
glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &eab);
glDeleteProgram(programID);
}
GLES 1.x/2.x 默认不支持 32 位索引。您最多可以使用 16 位索引,因此应将代码更改为:
GLushort indices[3] = {...}
[...]
glDrawElements(..., 3, GL_UNSIGNED_SHORT, ...);
请注意,尝试在不支持GL_OES_element_index_uint
的情况下将 GL_UNSIGNED_INT
用作实现上的类型参数只会导致GL_INVALID_ENUM
错误。至少应该添加一些错误检查以进行调试。
相关文章:
- 数组索引的值没有增加
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 查找最接近的大于当前数字的数字的索引
- 在C++中调整向量中的索引
- 重载元组索引运算符-C++
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 为std::string的某个索引赋值
- 并行用于C++17中数组索引范围内的循环
- 跟随整数索引列表的自定义类迭代器
- 如何在for循环中包含两个索引值的测试条件
- D3D11-将混合权重和索引传递到顶点着色器
- 将转换字符键入 int 以用作向量C++的索引
- 在 C++ 中访问数组负索引处的内存不会返回垃圾
- 如何为圆环创建索引
- 在子集化后将包含索引号的列表列表映射到标准索引序列
- 查找字符在两个索引之间出现的次数
- Azure Kinect 使用正文索引映射裁剪正文
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 数组索引重载错误
- 在树莓派上打开GL,索引顶点