////////////////// Setup VBO ///////////////////
float points[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
uint8_t colors[] = {
100, 100, 0,
100, 100, 0,
100, 100, 0
};
uint8_t colors_fbo[] = {
100, 0, 100,
100, 0, 100,
100, 0, 100
};
uint32_t indices[] = {0, 1, 2}; // one triangle
GLuint vao = 0;
std::vector<GLuint> vbos;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
vbos.resize(3); // xyz, colors, indices
glGenBuffers(3, vbos.data());
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(uint8_t), colors, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * sizeof(uint32_t), indices, GL_STATIC_DRAW);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
/////////////////////////////////////////
//////////////////// Setup FBO //////////
GLuint fbo = 0;
GLuint color = 0;
GLuint depth = 0;
GLuint fbo_vao = 0;
std::vector<GLuint> fbo_vbos;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenTextures(1, &color);
glBindTexture(GL_TEXTURE_2D, color);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
color, 0);
glGenRenderbuffers(1, &depth);
glBindRenderbuffer(GL_RENDERBUFFER, depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE){
std::cerr<<"Framebuffer fail, :"<<status<<std::endl;
}
// same points, different colors
glGenVertexArrays(1, &fbo_vao);
glBindVertexArray(fbo_vao);
fbo_vbos.resize(3);
glGenBuffers(3, fbo_vbos.data());
glBindBuffer(GL_ARRAY_BUFFER, fbo_vbos[0]);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, fbo_vbos[1]);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(uint8_t), colors_fbo, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fbo_vbos[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * sizeof(uint32_t), indices, GL_STATIC_DRAW);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
////////////////////////////////////////
/////////////////// Setup Program ///////
const char* vertex_shader =
"#version 330 core\n"
"layout (location = 0) in vec3 position;"
"layout (location = 1) in vec3 color;"
"flat out vec3 vColor;"
"void main() {"
" vColor = color;"
" gl_Position = vec4(vp, 1.0);"
"}";
const char* fragment_shader =
"#version 330 core\n"
"flat in vec3 vColor;"
"out vec4 fColor;"
"void main() {"
" fColor = vec4(vColor, 1.0f);"
"}";
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);
GLuint shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
///////////////////////////////////////////