三角形不渲染
Triangle doesn't render
本文关键字:三角形 更新时间:2023-10-16
我在渲染一个简单的三角形时遇到了麻烦。下面的代码编译并运行,除了没有任何三角形;只有黑色的背景。
GLuint VBO;
static void RenderSceneCB()
{
//glClear sets the bitplane area of the window to values previously selected by glClearColor, glClearDepth, and glClearStencil.
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
//swaps the buffers of the current window if double buffered.
glutSwapBuffers();
}
static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
}
static void CreateVertexBuffer()
{
Vector3f Vertices[3];
Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(1024, 768);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 02");
InitializeGlutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
glutMainLoop();
return 0;
}
因为你没有着色器,OpenGL不知道如何解释你的顶点属性0,因为它只知道位置,颜色等,而不是通用属性。请注意,这可能适用于某些gpu,因为它们将通用属性映射到相同的"插槽",然后零将被解释为位置(通常NVidia对这些问题不太严格)。
您可以使用MiniShader,只需将以下代码放在CreateVertexBuffer();
后面:
minish::InitializeGLExts(); // initialize shading extensions
const char *vs =
"layout(location = 0) in vec3 pos;n"
// the layout specifier binds this variable to vertex attribute 0
"void main()n"
"{n"
" gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);n"
"}n";
const char *fs = "void main() { gl_FragColor=vec4(.0, 1.0, .0, 1.0); }"; // green
minish::CompileShader(vs, fs);
请注意,我写的是我的头脑,如果有什么错误,请评论
相关文章:
- 绘制旋转的三角形
- 如何用for循环在c++中生成单词三角形
- 如何使用递归打印修改后的星号三角形图案
- C++ 创建带有 for 循环的三角形
- 绘制顺时针三角形,重新排序点
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Eigen c++ 三角形来自
- 为我的 c++ 类介绍制作一个三角形分类器.我有几个问题
- 使用对象文件读取三角形数据网格
- 从捕获的帧中裁剪三角形 - OpenCV 和 C++
- 如何从 3D 曲面网格中删除自相交三角形?
- 以C++输出一个数字三角形
- 打印对立三角形在C++中起作用
- 确定C++中的等腰直角三角形
- 是否可以使用 DirectX 3D 11 绘制由三角形组成的圆?
- 形成一个斐波那契三角形,使得每个数字是上面左对角线或右对角线上两个数字的总和
- OpenGL绘制三角形而不是正方形
- 将数字表示为两个三角形数字的总和
- OpenGL程序不显示三角形
- 在 OpenGL 中使用三角形绘制字母?