我正在学习来自第6版的opengl,我似乎无法从书中给出的以下代码中画出三角形。
#include <iostream>
#include "externalErrorFix.h"
#include "sb6.h"
GLuint compile_shaders(void)
{
GLuint vertex_shader;
GLuint fragment_shader;
GLuint program;
//GLSL shader sources
static const GLchar *vertex_shader_source[] =
{
"#version 430 core \n"
" \n"
"void main(void) \n"
"{ \n"
" const vec4 vertices[3] = vec4[3](vec4( 0.25, -0.25, 0.5, 1.0), \n"
" vec4(-0.25, -0.25, 0.5, 1.0), \n"
" vec4(0.25, 0.25, 0.5, 1.0)); \n"
" \n"
" gl_postion = vertices[gl_VertexID]; \n"
"} \n"
};
static const GLchar *fragment_shader_source[] =
{
"#version 430 core \n"
" \n"
"out vec4 color; \n"
"void main(void) \n"
"{ \n"
" color = vec4(0.0, 1.0, 0.0, 1.0); \n"
"} \n"
};
//create and compile vertex shaders
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
glCompileShader(vertex_shader);
//create and compile fragment shader
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
glCompileShader(fragment_shader);
//create a program attach shaders and link it
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return program;
}
class my_application : public sb6::application
{
private:
GLuint render_program;
GLuint vertex_array_object;
public:
void startup()
{
render_program = compile_shaders();
glGenVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
}
void shutdown()
{
glDeleteVertexArrays(1, &vertex_array_object);
glDeleteProgram(render_program);
glDeleteVertexArrays(1, &vertex_array_object);
}
void render(double currentTime)
{
const GLfloat color[] = { 0.0f,0.2f,0.0f,1.0f };
glClearBufferfv(GL_COLOR, 0, color);
glUseProgram(render_program);
glDrawArrays(GL_TRIANGLES, 0, 3);
//glPointSize(10.0f);
}
/*
// First program
void render(double currentTime)
{
const GLfloat color[] = {
(float)sin(currentTime) * 0.5f + 0.5f,
(float)cos(currentTime) * 0.5f + 0.5f,
0.0f,1.0f
};
glClearBufferfv(GL_COLOR, 0, color);
}*/
};
DECLARE_MAIN(my_application);
答案 0 :(得分:0)
似乎有一些拼写错误:
gl_postion
代替gl_Position
out vec4 color
缺少分号out vec4 color;
glDeleteVertexArrays(1, &vertex_array_object);
在关闭功能中出现两次此外,类my_application中缺少init()
函数。它应该特别呼叫sb6::application::init()
。
如果它仍然无效,那么该书似乎维护了一个包含所有样本here的存储库。也许他们会纠正那里的一切。