Hi folks,
I am following in2gpu's tutorials on modern OpenGL. The goal is to render a triangle. Everything worked fine so far, except for the color. The authors triangle has three color defined which makes the fragment shader render the triangle in different colors. Although I strictly followed the tutorial my triangle is colored in a flat white.
The project now contains several classes which are responsible for freeglut and glew initialization. I also have a Vertex- and a Fragmentshader which look like this:
Vertex_Shader.glsl:
#version 330 core
layout(location = 0) in vec3 in_position;
layout(location = 1) in vec4 in_color;
out vec4 color;
void main(){
color = in_color;
gl_Position = vec4(in_position, 1);
}
Fragment_Shader.glsl:
#version 330 core
layout(location = 0) out vec4 out_color;
in vec4 color;
void main(){
out_color = color;
}
So the first thing to mention here is I am using version 330 while the author uses version 450 and I am not sure whether this is crucial here. Also there might be another source for the problem. I am using Visual Studio 2015 which does not seem to know .glsl-files. I created the shaders by adding a new Item. Here I chose a Pixel Shader File (.hlsl) and renamed it to .glsl. This did raise the following Error:
The "ConfigurationCustomBuildTool" rule is missing the "ShaderType" property.
I am able to build and run the project though, without errors. Also here is the Triangle class itself:
Triangle.cpp:
#include "Triangle.h"
Triangle::Triangle()
{
}
Triangle::~Triangle()
{
//is going to be deleted in Models.cpp (inheritance)
}
void Triangle::Create()
{
GLuint vao;
GLuint vbo;
glGenVertexArrays(1,&vao);
glBindVertexArray(vao);
std::vector<VertexFormat> vertices;
vertices.push_back(VertexFormat(glm::vec3(0.25,-0.25,0.0),
glm::vec4(1,0,0,1)));
vertices.push_back(VertexFormat(glm::vec3(-0.25,-0.25,0.0),
glm::vec4(0,1,0,1)));
vertices.push_back(VertexFormat(glm::vec3(0.25,0.25,0.0),
glm::vec4(0,0,1,1)));
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,sizeof(VertexFormat) * 3, &vertices[0],GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(VertexFormat),(void*)0);
glEnableVertexAttribArray(1);
//you can use offsetof to get the offset of an attribute
glVertexAttribPointer(1,4,GL_FLOAT,GL_FALSE,sizeof(VertexFormat),(void*)(offsetof(VertexFormat,VertexFormat::color)));
glBindVertexArray(0);
//here we assign the values
this->vao = vao;
this->vbos.push_back(vbo);
}
void Triangle::Update()
{
//Triangle does not have to be updated
}
void Triangle::Draw()
{
glUseProgram(program);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES,0,3);
}
Although it seems that I followed the tutorial all the way, my triangle is still rendered white only. The are of course a lot more classes but I guess I should not post the entire project here. I can always post some additional information if it is needed. At the end it seems to me that something is wrong with the fragment shader. I also described my problem to the author. he could not have a look into my code/project but he suspected that there is something wrong with my attributes.
I am very new to both C++ and OpenGL therefore it is very difficult for me to debug (if it is even possible for shaders).
Glad for any help and thanks in advance!