这是我的代码:
#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调试x64上进行了测试。
教程页面:https://open.gl
我检查了着色器是否编译成功,并且glGetError
在绘制函数之前返回0。