OPENGL:正方形 Class 使用 VBO
OPENGL: Square Class Using VBO
所以,我正在尝试制作一个基本的 "Drawable" class 来在后台为我处理大量绘图,我想使用现代 OpenGL(没有开始和结束语句) .当我 运行 draw().
时,我总是出现空白屏幕
我有 运行 调试器并进行了检查,我的数组已正确初始化,位置为 3xFLOAT,颜色为 4xFLOAT。知道出了什么问题吗?我对这个图书馆很陌生。我的示例尝试在 (+-0.5, +-0.5, 0.0) 处绘制一个红色立方体,因此 indexData 数组只是 { 0, 1, 2, 3 }。
#define DRAWABLE_VERTEX_DEPTH 3
#define SIZE_OF_VERTEX_ELEMENT sizeof(GLfloat)
#define VERTEX_SIZE (DRAWABLE_VERTEX_DEPTH * SIZE_OF_VERTEX_ELEMENT)
#define DRAWABLE_COLOR_DEPTH 4
#define SIZE_OF_COLOR_ELEMENT sizeof(GLfloat)
#define COLOR_SIZE (DRAWABLE_COLOR_DEPTH * SIZE_OF_COLOR_ELEMENT)
#define INDEX_SIZE sizeof(GLushort)
#define DRAWABLE_STRIDE (VERTEX_SIZE + COLOR_SIZE)
inline Drawable(/*Arguments omitted for brevity...*/)
{
//Standard initialization omitted....
glGenBuffers(1, &vboID);
glGenBuffers(1, &vioID);
glGenVertexArrays(1, &vaoID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vioID);
glBufferData(GL_ARRAY_BUFFER, (VERTEX_SIZE + COLOR_SIZE) * vertexCount, vertexData, drawType);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, INDEX_SIZE * indexCount, indexData, drawType);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//Generate Vertex Array
glBindVertexArray(vaoID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, DRAWABLE_VERTEX_DEPTH, GL_FLOAT, GL_FALSE, DRAWABLE_STRIDE, 0);
glVertexAttribPointer(1, DRAWABLE_COLOR_DEPTH, GL_FLOAT, GL_FALSE, DRAWABLE_STRIDE, (GLbyte*)VERTEX_SIZE);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vioID);
glBindVertexArray(0);
}
inline void draw()
{
glBindVertexArray(vaoID);
glDrawElements(drawMode, indexCount, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
}
GLSL 顶点着色器:
#version 430\r\n
in layout(location=0) vec3 inPosition;
in layout(location=1) vec4 inColor;
out vec4 outVertexColor;
void main()
{
gl_Position = vec4(inPosition, 1.0);
outVertexColor = inColor;
}
GLSL 片段着色器:
#version 430\r\n
in vec4 outVertexColor;
out vec4 outFragmentcolor;
void main()
{
outFragmentcolor = outVertexColor;
}
除了评论中提到的问题外,您的索引数组是 GLushort(无符号 16 位),而您的绘图调用指定 GL_UNSIGNED_INT(无符号 32 位)。替换为 GL_UNSIGNED_SHORT.
所以,我正在尝试制作一个基本的 "Drawable" class 来在后台为我处理大量绘图,我想使用现代 OpenGL(没有开始和结束语句) .当我 运行 draw().
时,我总是出现空白屏幕我有 运行 调试器并进行了检查,我的数组已正确初始化,位置为 3xFLOAT,颜色为 4xFLOAT。知道出了什么问题吗?我对这个图书馆很陌生。我的示例尝试在 (+-0.5, +-0.5, 0.0) 处绘制一个红色立方体,因此 indexData 数组只是 { 0, 1, 2, 3 }。
#define DRAWABLE_VERTEX_DEPTH 3
#define SIZE_OF_VERTEX_ELEMENT sizeof(GLfloat)
#define VERTEX_SIZE (DRAWABLE_VERTEX_DEPTH * SIZE_OF_VERTEX_ELEMENT)
#define DRAWABLE_COLOR_DEPTH 4
#define SIZE_OF_COLOR_ELEMENT sizeof(GLfloat)
#define COLOR_SIZE (DRAWABLE_COLOR_DEPTH * SIZE_OF_COLOR_ELEMENT)
#define INDEX_SIZE sizeof(GLushort)
#define DRAWABLE_STRIDE (VERTEX_SIZE + COLOR_SIZE)
inline Drawable(/*Arguments omitted for brevity...*/)
{
//Standard initialization omitted....
glGenBuffers(1, &vboID);
glGenBuffers(1, &vioID);
glGenVertexArrays(1, &vaoID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vioID);
glBufferData(GL_ARRAY_BUFFER, (VERTEX_SIZE + COLOR_SIZE) * vertexCount, vertexData, drawType);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, INDEX_SIZE * indexCount, indexData, drawType);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//Generate Vertex Array
glBindVertexArray(vaoID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, DRAWABLE_VERTEX_DEPTH, GL_FLOAT, GL_FALSE, DRAWABLE_STRIDE, 0);
glVertexAttribPointer(1, DRAWABLE_COLOR_DEPTH, GL_FLOAT, GL_FALSE, DRAWABLE_STRIDE, (GLbyte*)VERTEX_SIZE);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vioID);
glBindVertexArray(0);
}
inline void draw()
{
glBindVertexArray(vaoID);
glDrawElements(drawMode, indexCount, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
}
GLSL 顶点着色器:
#version 430\r\n
in layout(location=0) vec3 inPosition;
in layout(location=1) vec4 inColor;
out vec4 outVertexColor;
void main()
{
gl_Position = vec4(inPosition, 1.0);
outVertexColor = inColor;
}
GLSL 片段着色器:
#version 430\r\n
in vec4 outVertexColor;
out vec4 outFragmentcolor;
void main()
{
outFragmentcolor = outVertexColor;
}
除了评论中提到的问题外,您的索引数组是 GLushort(无符号 16 位),而您的绘图调用指定 GL_UNSIGNED_INT(无符号 32 位)。替换为 GL_UNSIGNED_SHORT.