OpenGL VBO Drawing
OpenGL VBO Drawing
我似乎在使用VBO在OpenGL中绘制对象时遇到了一些问题。我试图从以下位置复制此示例:http://www.opengl.org/wiki/VBO_-_just_examples(数字2)但我无法让飞机出现在屏幕上。
顶点.h:
#include <freeglut>
struct Vertex {
GLfloat position[3];
GLfloat normal[3];
GLfloat *uvs[2];
unsigned short uvCount;
};
三角形.h:
#include <GL/glew.h>
#include "Vertex.h"
class Triangles {
public:
Triangles(GLuint program, Vertex *vertices, unsigned int vertexCount, unsigned int *indices[3], unsigned int indiceCount);
~Triangles();
void Draw();
private:
GLuint program;
GLuint VertexVBOID;
GLuint IndexVBOID;
GLuint VaoID;
unsigned int *indices[3];
unsigned int indiceCount;
};
三角形.cpp:
#include "Triangles.h"
#include <stdio.h>
#include <stddef.h>
Triangles::Triangles(GLuint program, unsigned int *indices[3], unsigned int indiceCount) {
memcpy(this->indices, indices, sizeof(int) * indiceCount * 3);
this->indiceCount = indiceCount;
this->program = program;
glGenVertexArrays(1, &VaoID);
glBindVertexArray(VaoID);
glGenBuffers(1, &VertexVBOID);
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertexCount, vertices, GL_STATIC_DRAW);
GLuint attributeLocation = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(attributeLocation);
glVertexAttribPointer(attributeLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)(offsetof(Vertex, position)));
attributeLocation = glGetAttribLocation(program, "normal");
glEnableVertexAttribArray(attributeLocation);
glVertexAttribPointer(attributeLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)(offsetof(Vertex, normal)));
glGenBuffers(1, &IndexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 3 * indiceCount, indices, GL_STATIC_DRAW);
};
Triangles::~Triangles() {
glDisableVertexAttribArray(glGetAttribLocation(program, "position"));
glDisableVertexAttribArray(glGetAttribLocation(program, "normal"));
glDeleteBuffers(1, &VertexVBOID);
glDeleteBuffers(1, &IndexVBOID);
glDeleteVertexArrays(1, &VaoID);
}
void Triangles::Draw() {
glBindVertexArray(VaoID);
glDrawElements(GL_TRIANGLES, indiceCount, GL_UNSIGNED_INT, 0);
};
摘录自main.cpp(创建triangle对象):
Vertex vertices[4];
vertices[0].position[0] = -1;
vertices[0].position[1] = 1;
vertices[0].position[2] = 0;
vertices[0].normal[0] = 0;
vertices[0].normal[0] = 0;
vertices[0].normal[0] = 1;
vertices[0].uvCount = 0;
vertices[1].position[0] = 1;
vertices[1].position[1] = 1;
vertices[1].position[2] = 0;
vertices[1].normal[0] = 0;
vertices[1].normal[0] = 0;
vertices[1].normal[0] = 1;
vertices[1].uvCount = 0;
vertices[2].position[0] = 1;
vertices[2].position[1] = -1;
vertices[2].position[2] = 0;
vertices[2].normal[0] = 0;
vertices[2].normal[0] = 0;
vertices[2].normal[0] = 1;
vertices[2].uvCount = 0;
vertices[3].position[0] = -1;
vertices[3].position[1] = -1;
vertices[3].position[2] = 0;
vertices[3].normal[0] = 0;
vertices[3].normal[0] = 0;
vertices[3].normal[0] = 1;
vertices[3].uvCount = 0;
unsigned int **indices;
indices = new unsigned int*[2];
indices[0] = new unsigned int[3];
indices[0][0] = 0;
indices[0][1] = 1;
indices[0][2] = 2;
indices[1] = new unsigned int[3];
indices[1][0] = 2;
indices[1][1] = 3;
indices[1][2] = 0;
Triangles *t = new Triangles(program, vertices, 4 indices, 2);
createShader(GLenum,char*):
GLuint createShader(GLenum type, char *file) {
GLuint shader = glCreateShader(type);
const char *fileData = textFileRead(file);
glShaderSource(shader, 1, &fileData, NULL);
glCompileShader(shader);
return shader;
}
着色器加载:
GLuint v = createShader(GL_VERTEX_SHADER);
GLuint f = createShader(GL_FRAGMENT_SHADER, "fragmentShader.frag");
program = glCreateProgram();
glAttachShader(program, v);
glAttachShader(program, f);
glLinkProgram(program);
glUseProgram(program);
vertxShader.vert:
in vec3 position;
in vec3 normal;
out vec3 a_normal;
void main() {
gl_Position = vec4(position, 1.0);
}
fragmentShader.frag:
in vec3 a_normal;
out vec4 out_color;
void main() {
out_color = vec4(1.0, 1.0, 1.0, 1.0);
}
如果需要更多的代码,请告诉我。顺便说一句,一切都很好,我只是看不到我在屏幕上构建的飞机(也许是因为我没有使用颜色?)
我的OpenGL信息如下:
- 供应商:ATI Technologies股份有限公司
- 渲染器:ATI Radeon HD 5700系列
- 版本:3.2.9756兼容性配置文件上下文
- 扩展:扩展=GL_AMDX_name_gen_delete GL_AMDX_random_access_target GL_AMDX-vertex_shader_thesterator GL_AMD_conservative_depth GL_AMD_draw_buffers_blend GL_AMD_performance_monitor GL_AMD_seamles_cubemap_per_texture GL_AMD_shader_stencil_export GL_AMD _texture
回复您的评论:
不幸的是,我不做错误检查
您应该始终添加一些OpenGL错误检查,这将使您免受许多问题的困扰。它应该看起来像以下内容:
int err = glGetError();
if(err != 0) {
//throw exception or log message or die or something
}
我使用矩阵函数,因为我没有意识到顶点着色器会影响它。我假设矩阵设置为堆栈顶部的矩阵(在绘制之前我推送的矩阵)
这是一个错误的假设。唯一引用已弃用的矩阵堆栈的变量是特殊(尽管已弃用)变量gl_ModelViewProjectionMatrix
。您当前拥有的只是一个未使用、未初始化的矩阵,它完全忽略了您的矩阵堆栈。
至于指数,我不太清楚你的意思。我只是在纸上画了顶点,并以此为基础决定了索引。
我指的不是索引缓冲区中三角形的索引,而是glAttrib*函数的第一个参数。我认为"属性位置"是一个比索引更正确的术语。
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, ... //attrib location 0
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, ... //attrib location 1
你似乎只是随机地假设"0"answers"1"映射到"位置"answers"法线"。这不是一个安全的假设。您应该使用glGetAttribLocation查询"position"answers"normal"的属性位置值,然后将该值用于glEnableVertexAttribArray和glVertexAttribPointer。
- OpenGL VBO Indexing ( How to compute Index Array)
- QT QOpenGLWidget:如何在不使用数据块复制的情况下修改VBO中的单个顶点值?
- 将单个未唱的 int 发送到 VBO
- VBO必须在VAO之后绑定
- C++ OpenCV 3D Drawing
- 如何在顶点着色器中使用VBO数据作为位置数据,在片段着色器中使用统一数据作为颜色数据
- OpenGL:第二个VBO破坏了第一个VBO
- 如何告诉 VBO 处理给定的数据?(布料模拟)
- 我们是否需要序列化 VAO 和 VBO
- OpenGL VAO VBO resize
- glVertexAttribDivisor 存储在哪里 - VAO、VBO 或全局状态?
- 将 OpenGL VBO 与自定义类/数据结构一起使用
- Qtopengl,为什么不能用不同的vbo画两个立方体
- OpenGL C++:VBO 的结构包装器不会初始化成员
- 为什么VAO可以使用VBO数据绑定它?
- 可以在同一 VBO 中存储不同的顶点属性和索引
- 两个不同的对象 OpenGL.VAO VBO IBO 网格失真问题
- c++ cuda opengl not rendering vbo
- 在 OpenGL 中,在保持良好帧速率的同时,VBO 中有多少个顶点的好目标是什么
- OpenGL VBO Drawing