c++ - 修复 glDrawArrays() 的访问冲突?
问题描述
这是我的代码:
#include <map>
#include <vector>
#include "App/Includes/glew.h"
#include "App/Includes/glfw3.h"
struct ShaderPipeline
{
unsigned int program;
unsigned int vao;
};
void createShaderPipeline(ShaderPipeline& pipeline, const char* vertexContent, const char* fragmentContent)
{
pipeline.program = glCreateProgram();
unsigned int id;
// Create vertex shader
{
id = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(id, 1, &vertexContent, NULL);
glCompileShader(id);
glAttachShader(pipeline.program, id);
}
// Create fragment shader
{
id = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(id, 1, &fragmentContent, NULL);
glCompileShader(id);
glAttachShader(pipeline.program, id);
}
glLinkProgram(pipeline.program);
glGenVertexArrays(1, &pipeline.vao);
glBindVertexArray(pipeline.vao);
}
int main()
{
glfwInit();
GLFWwindow* window;
// Create window
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
glfwWindowHint(GLFW_RESIZABLE, false);
window = glfwCreateWindow(800, 600, "Pixel Madness", nullptr, nullptr);
// Fullscren: window = glfwCreateWindow(800, 600, "Pixel Madness", glfwGetPrimaryMonitor(), nullptr);
glfwMakeContextCurrent(window);
}
glewExperimental = true;
glewInit();
std::map<std::string, ShaderPipeline> shaderPipelines;
// Load shader pipelines
{
createShaderPipeline
(
shaderPipelines["p"],
R"glsl(
#version 150 core
in vec2 pos;
void main()
{
gl_Position = vec4(pos, 0.0, 1.0);
}
)glsl",
R"glsl(
#version 150 core
out vec4 color;
void main()
{
color = vec4(1.0, 1.0, 1.0, 1.0);
}
)glsl"
);
// Setup input
{
unsigned int id = glGetAttribLocation(shaderPipelines["p"].program, "pos");
glVertexAttribPointer(id, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(id);
}
}
// Create vertex buffer
{
unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
}
// Test
{
std::vector<float> vertices
({
0.0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f
});
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW); ///
glUseProgram(shaderPipelines["p"].program);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_ESCAPE))
{
glfwSetWindowShouldClose(window, true);
}
}
glfwTerminate();
return 0;
}
我最近从 DirectX 切换到 OpenGL,但我不知道可能是什么问题。该代码应该绘制一个白色三角形。我正在关注一些在线教程,我是如何使用 DirectX 的......它在 VS 2017 debug x64 上进行了测试。
教程页面:https ://open.gl
我检查了着色器是否编译成功,并glGetError
在绘制函数之前返回 0。
解决方案
推荐阅读
- split - directx11如何实现分屏效果
- java - 我正在解析 json 数组形式的 api,但它显示错误为“(url)的意外响应代码 403”
- python - 如何将我的supervisord配置从python2转换为python3?
- azure-active-directory - 为托管在 IIS 中的 Asp.net MVC 网站从 Windows 身份验证迁移到 Azure AD 身份验证
- vue.js - 我在使用 npm 或 yarn 构建 vue.js 项目时遇到问题
- android - 在 github 操作工作流中下载操作存储库“r0adkll/sign-android-release@v1”时出错
- comments - 我的 Stata do 文件中有多少行代码,不包括注释?
- c - 如何正确遍历命令行参数以检查每个字符是否是 C 中的数字?
- c# - 将类中的属性列表与字符串列表匹配
- javascript - 每当有人从计算机中选择文件时,如何自动将图像重新缩放为 12 x 12