我已经尝试获取一个数组对象来渲染到窗口有一段时间了。 GLEW 功能似乎没有运行。结果应该是渲染到输出窗口的三角形。可能是这些功能已被弃用或缺少某些功能。
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <Vector>
#include <fstream>
#include <string>
#include <string_view>
#include <windows.h>
#include <winuser.h>
#include <stdio.h>
using namespace std;
GLuint vao = 0;
GLuint vbo = 0;
int main(void) {
if (!glfwInit()) {
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
GLFWwindow* window = glfwCreateWindow(640, 640, "OpenGL Example", NULL, NULL);
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glewInit();
static const GLfloat g_vertex_buffer_data[] = {
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), &g_vertex_buffer_data[0], GL_STATIC_DRAW);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
const float DEG2RAD = 3.14159 / 180;
float r = 0.0;
float g = 0.3;
float b = 0.6;
while (!glfwWindowShouldClose(window)) {
//Setup View
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
//Drawing
glColor3f(r, g, b);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(3, GL_FLOAT, 0, &g_vertex_buffer_data[0]);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
//Swap buffer and check for events
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate;
exit(EXIT_SUCCESS);
}
应将三角形渲染到窗口。
VAO 自 OpenGL 3.0 版起就已纳入标准,因此它们不在 OpenGL 2 版中。另请参阅顶点数组对象。一个选择可能是使用 OpenGL 扩展
GL_ARB_vertex_array_object
。