使用 OpenGL 绘制基本三角形
Basic triangle drawing with OpenGL
>我有以下代码
#include <iostream>
using namespace std;
#include <GLglew.h>
#include <GLfreeglut.h>
#define BUFFER_OFFSET(a) ((void*)(a))
enum eAttributesIDs
{
Position = 0,
Color = 1
};
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glFlush();
}
void init()
{
GLfloat vertices[] =
{
0.f, 1.f,
-1.f, -1.f,
1.f, -1.f
};
GLuint bufferId;
glGenBuffers(1, &bufferId);
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(Position);
glVertexAttribPointer(Position, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glClearColor(0, 1, 1, 1);
}
int main(int argc, char** argv)
{
// configure and open window via glut
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(640, 480);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]);
if (glewInit())
{
cerr << "Unable to initialize GLEW" << endl;
exit(EXIT_FAILURE);
}
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
但是我不知道为什么,即使它编译并运行更改窗口的背景颜色,屏幕上也没有出现三角形。
我认为你缺少着色器程序和VAO。使用核心 OpenGL 上下文时必须使用它们。
相关文章:
- 绘制旋转的三角形
- 绘制顺时针三角形,重新排序点
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 是否可以使用 DirectX 3D 11 绘制由三角形组成的圆?
- OpenGL绘制三角形而不是正方形
- 在 OpenGL 中使用三角形绘制字母?
- 为什么这个在 opengl 中绘制三角形的着色器没有多次运行
- 使用 glDrawArray 函数绘制三角形
- 三角形未绘制.C++/GLEW/SDL.
- GLFW程序不绘制三角形
- OpenGL不会绘制三角形,遵循确切的格式
- 使用 C++ 库 GLEW 在 openGl 中绘制一个红色三角形
- 用OpenGL绘制三角形
- 绘制三角形时只有空白屏幕
- OpenGL绘制三角形只有白色
- 使用 OpenGL 绘制基本三角形
- 计算机图形:我自己绘制三角形
- 如何使用OSG API在Osgearth上绘制三角形
- 理解在OpenGL 3.3+内核中使用三角形绘制四边形的代码
- OpenGL:使用退化三角形绘制线条