I was trying to implement normal mapping on a simple cube but since i had troubles with my normals i wanted to try to display them using a geometry shader. Following learnopengl tutorial, it basically calls mesh.render() twice, the first time to draw the model and the second time to display normals. When i try to do the same i get this
The cube seems well drawn but it has a weird white rectangle in front of it and i don't know why. I don't know if it's a problem of drawings or if it's the geometry shader, so i'll post both.
My code:
glutDisplayFunc(MyRenderScene);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glGenVertexArrays(1,&global.gVAO);
glGenBuffers(1, &global.VBO);
glBindVertexArray(global.VAO);
glBindBuffer(GL_ARRAY_BUFFER, global.VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &global.IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glGenVertexArrays(1,&global.gVAO);
glGenBuffers(1, &global.gVBO);
glBindVertexArray(global.gVAO);
glBindBuffer(GL_ARRAY_BUFFER, global.gVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &global.gIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.gIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
Here i'm basically passing the same structure to both buffers.
Buffers are istantiated in a global struct.
This is MyRenderScene() :
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
global.shaders.enable(); // glUseProgram
global.shaders.set_sampler(0); // setting textures
global.shaders.set_sampler(1);
global.sceneT.set_camera(
global.camera.position(),
global.camera.lookat(),
global.camera.up()
);
glm::mat4 model = glm::mat4(1.0f);
glm::vec3 vaxis = glm::vec3(0.0,1.0,0.0);
glm::vec3 haxis = glm::vec3(1.0,0.0,0.0);
model = glm::rotate(model,glm::radians(global.gradX),haxis);
model = glm::rotate(model,glm::radians(global.gradY),vaxis);
glm::mat4 projection = glm::perspective(glm::radians(40.0f), (float)global.WINDOW_WIDTH/(float)global.WINDOW_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = glm::lookAt(global.camera.position(),global.camera.lookat()+ global.camera.position(),global.camera.up());
global.shaders.set_model(model);
global.shaders.set_view(view);
global.shaders.set_projection(projection);
global.shaders.set_viewPos(global.camera.position());
global.shaders.set_lightPos(lightPos);
global.shaders.update_uniforms();
glBindVertexArray(global.VAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, position)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, textcoord)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, normal)));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, tangent)));
glBindBuffer(GL_ARRAY_BUFFER, global.VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.IBO);
global.brickwall.Bind(GL_TEXTURE0+0); // binding textures
global.brickwall_normals.Bind(GL_TEXTURE0+1);
glDrawElements(GL_TRIANGLES,36,GL_UNSIGNED_INT,0);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
global.geometryShader.enable(); // setting up geometry shader
global.geometryShader.set_projection(projection);
global.geometryShader.set_model(model);
global.geometryShader.set_view(view);
global.geometryShader.update_uniforms();
glBindVertexArray(global.gVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, position)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, textcoord)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, normal)));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, tangent)));
glBindBuffer(GL_ARRAY_BUFFER, global.gVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.gIBO);
glDrawElements(GL_TRIANGLES,36,GL_UNSIGNED_INT,0);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
glutSwapBuffers();
glutPostRedisplay();***
I even tried calling the same vertexArrays and vertexArrayBuffer but i get the same result.
Here are vertex shader:
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 textcoord;
layout (location = 2) in vec3 normal;
layout (location = 3) in vec3 tangent;
out VS_OUT {
vec3 newNormal;
} vs_out;
uniform mat4 view;
uniform mat4 model;
void main() {
mat3 normalMatrix = mat3(transpose(inverse(view * model)));
newNormal = vec3(vec4(normalMatrix * normal, 0.0));
gl_Position = view * model * vec4(position, 1.0);
}
And geometryShader:
#version 330 core
layout (triangles) in;
layout (line_strip, max_vertices = 6) out;
in VS_OUT {
vec3 newNormal;
} gs_in[];
const float MAGNITUDE = 0.2;
uniform mat4 projection;
void GenerateLine(int index) {
gl_Position = projection * gl_in[index].gl_Position;
EmitVertex();
gl_Position = projection * (gl_in[index].gl_Position + vec4(gs_in[index].newNormal,0.0) * MAGNITUDE);
EmitVertex();
EndPrimitive();
}
void main() {
GenerateLine(0); // first vertex normal
GenerateLine(1); // second vertex normal
GenerateLine(2); // third vertex normal
}
Feel free to correct me on everything possible and imaginable.
The reason for the issue is, that the shader program with the geometry shader fails to compile or link. Hence the geometry is drawn by the default shader program instead of the program with the geometry shader.
There is (at least) one error in the vertex shader:
newNormal = vec3(vec4(normalMatrix * normal, 0.0));
vs_out.newNormal = vec3(vec4(normalMatrix * normal, 0.0));
Related
I have this vertex shader. When i remove in vec3 LVertexNorm and everything related to it. It render fine. But if i add it in, nothing renders anymore.
#version 140
in vec3 LVertexNorm;
in vec3 LVertexPos2D;
uniform mat4 MVP;
out vec3 norm;
void main() {
norm = LVertexNorm;
gl_Position = MVP * vec4( LVertexPos2D.x, LVertexPos2D.y, LVertexPos2D.z, 1 );
}
Fragment shader
#version 140
in vec3 norm;
out vec4 LFragment;
void main() {
LFragment = vec4( 1.0,1.0,1.0, 1.0 );
}
And code for building the VAO
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, verticesCount * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(program->getAttribute("LVertexPos2D"), 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, nbo);
glBufferData(GL_ARRAY_BUFFER, normalCount * sizeof(GLfloat), normals, GL_STATIC_DRAW);
glVertexAttribPointer(program->getAttribute("LVertexNorm"), 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesCount * sizeof(GLuint), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBindVertexArray(0);
I tryd different ways. And always the same result. when LVertexNorm gets added, shader stops working. I cant ifgure out why. What might be wrong?
The argument to glEnableVertexAttribArray has to be the vertex attribute index:
GLuint pos_inx = program->getAttribute("LVertexPos2D")
glVertexAttribPointer(pos_inx, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(pos_inx);
GLuint norm_inx = program->getAttribute("LVertexNorm");
glVertexAttribPointer(norm_inx, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(norm_inx);
When you have the vertex shader input variables in vec3 LVertexNorm; and in vec3 LVertexPos2D;, then possibly LVertexNorm gets the attribute index 0 and LVertexPos2D gets the attribute index 1. Since the vertex attribute 1 is not enabled, the vertex positions are not specified.
In fact, the attribute indexes are not specified and can be any number. Most drivers, however, use ascending indexes that start at 0.
I'm working on a 3D object viewer, a really basic opengl program i think. But i'm new to opengl and i find a problem that i can't resolve.
The last tutorial that i used is here : LearnOpenGL
So the code than i'll show is from there (Model Loading part), i use Assimp to load the object.
There is some code:
Mesh Setup:
struct Vertex {
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texCoords;
};
glGenVertexArrays(1, &this->m_VAO);
glGenBuffers(1, &this->m_VBO);
glGenBuffers(1, &this->m_EBO);
glBindVertexArray(this->m_VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->m_VBO);
glBufferData(GL_ARRAY_BUFFER, this->m_vertices.size() * sizeof(Vertex),
&this->m_vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->m_EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->m_indices.size() * sizeof(GLuint),
&this->m_indices[0], GL_STATIC_DRAW);
// Vertex Positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)0);
// Vertex Normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)offsetof(Vertex, normal));
// Vertex Texture Coords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)offsetof(Vertex, texCoords));
glBindVertexArray(0);
You can check the whole code on the site.
Draw :
// Draw mesh
glBindVertexArray(model.get_VAO());
glDrawElements(GL_TRIANGLES, model.get_Indices_Size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
Shaders :
Vertex ->
#version 130
in vec4 gxl3d_Position;
in vec4 gxl3d_Color;
smooth out vec4 VertexColor;
void main() {
gl_Position = gxl3d_Position;
VertexColor = vec4(0.5f, 0.0f, 0.0f, 1.0f);
}
Fragment ->
#version 130
smooth in vec4 VertexColor;
void main() {
gl_FragColor = VertexColor;
}
When i run this, i can check than the last vertex from Assimp is not the same when i read the .obj directly, maybe it's normal?
So in the end i reach a "nouveau failed idel channel 0xcccc0000 Xorg 677"
If someone have any idea, Thanks!
One thing is wrong in the mesh setup.
This
// Vertex Positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)0);
// Vertex Normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)offsetof(Vertex, normal));
// Vertex Texture Coords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)offsetof(Vertex, texCoords));
Should be after:
glBindBuffer(GL_ARRAY_BUFFER, this->m_VBO);
glBufferData(GL_ARRAY_BUFFER, this->m_vertices.size() * sizeof(Vertex),
&this->m_vertices[0], GL_STATIC_DRAW);
while the VBO is bound, otherwise you're setting the attrib pointers the EBO instead if the VBO.
And if you have no reason to use that old GLSL version, you should really bump it up to like 330 or so and use layout(location = 0) in vec3 position.
For instance to access to normals:
#version 330
layout(location = 1) in vec3 normals;
etc
About assimp I have no idea.
Good luck :)
I want to pass arrays of vertices, UV's and normals to the shader and transform them using MVP matrix, so I wrote a simple shader program:
#version 330 core
//Vertex shader
layout(location=0)in vec3 vertexPosition_modelspace;
layout(location=1)in vec2 vertexUV;
out vec2 UV;
uniform mat4 MVP;
void main(){
gl_Position = MVP * vec4(vertexPosition_modelspace,1);
UV = vertexUV;
}
#version 330
//Fragment Shader
in vec2 UV;
out vec3 color;
uniform sampler2D color_texture;
void main(void) {
color = texture(color_texture, UV).rgb;
}
Then I needed to pass an array of vertices, which is being initialized like that:
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], model_usage);
Same with UV's and normals, the type is still GL_ARRAY_BUFFER for them.
Then a draw loop:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (Model* mdl : baseShader->getModels()) {
glUseProgram(baseShader->getShaderProgram());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mdl->getTextureBuffer());
glUniform1i(texture_location, 0);
glm::mat4 mvp = RootEngine->projection_matrix * RootEngine->view_matrix * mdl->getModelMatrix();
glUniformMatrix4fv(baseShader->getMVPlocation(), 1, GL_FALSE, &mvp[0][0]);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mdl->getVertexBuffer());
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1); // Matches layout (location = 1)
glBindBuffer(GL_ARRAY_BUFFER, mdl->getUVsBuffer());
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, mdl->getVertices()->size());
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
SDL_GL_SwapWindow(RootEngine->getMainWindow());
BaseShader and Model are my own classes which do a simple initialization and VBO handling.
The problem is nothing actually being rendered. I tried to add glEnableClientState(GL_VERTEX_ARRAY);
and
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
...
glDeleteVertexArrays(1, &VertexArrayID);
But still nothing. When I don't use layout and pass the data with glVertexPointer everything seems to work fine.
UPDATE 1: I found out what prevents vertices from being rendered. It's uniform variable in the VertexShader. If it's being removed the vertices are being rendered, but then there is no way to pass a matrix to the shader.
I've just passed the wrong texture, so the model was colored black and thus invisible. Thanks everybody for your answers.
I am trying to print a simple cube using GLSL but I only get an empty screen. I don't know what I am doing wrong. The vertices, normals, triangles are exported from Blender.
void InitBuffers() {
// monkey vertices, normals
readVerticesNormals();
// cube vertices
glGenVertexArraysAPPLE(1, &CubeVao);
glBindVertexArrayAPPLE(CubeVao);
glGenBuffers(1, &CubeVboPositions);
glBindBuffer(GL_ARRAY_BUFFER,CubeVboPositions);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glGenBuffers(1,&CubeVboColors);
glBindBuffer(GL_ARRAY_BUFFER, CubeVboColors);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glGenBuffers(1, &CubeNormals);
glBindBuffer(GL_ARRAY_BUFFER, CubeNormals);
glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
glGenBuffers(1, &CubeIbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, CubeIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangles), triangles, GL_STATIC_DRAW);
}
I bind the vertex data to the shader.
glBindAttribLocation(ProgramShader, 0, "position");
glBindAttribLocation(ProgramShader, 1, "color");
glBindAttribLocation(ProgramShader, 2, "normal");
Camera is positioned in (0,0,0) looking towards (0,0,-1). The object, in this case the cube, it positioned at (0,0,-4).
The render function is:
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
// set view matrix
ViewMatrix.setView(0,0,0,0,0,-1,0,1,0);
// use shader program
glUseProgram(ProgramShader);
// send uniforms to shader
glUniformMatrix4fv(ProjectionMatrixLocation, 1, false, ProjectionMatrix.m);
glUniformMatrix4fv(ViewMatrixLocation, 1, false, ViewMatrix.m);
glUniformMatrix4fv(ModelMatrixLocation, 1, false, ModelMatrix.m);
glBindVertexArrayAPPLE(CubeVao);
glDrawElements(GL_TRIANGLES, 3*tri_num, GL_UNSIGNED_INT, (void*)0);
glutSwapBuffers();
}
Vertex shader:
attribute vec3 position;
attribute vec3 color;
attribute vec3 normal;
uniform mat4 modelMatrix,viewMatrix,projMatrix;
varying vec4 Normal;
varying vec4 Position;
varying vec4 Color;
void main() {
// position in view space
Position = viewMatrix * modelMatrix * vec4(position, 1.0);
// normal in view space
Normal = normalize(viewMatrix * modelMatrix * vec4(normal, 1.0));
Color = vec4(color, 1.0);
// final position
gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}
Fragment shader:
varying vec4 Normal;
varying vec4 Position;
varying vec4 Color;
void main() {
gl_FragColor = Color;
}
Depending how vertices, colors, etc are defined, sizeof(vertices) may just return the size of the pointer. Try:
3*numVertices*sizeof(float)
I'm working on a rendering engine and so far it's been coming great, but I can not figure out why when I make two drawing calls to render different models, only 1 will show up.
I'm using wxWidgets to handle the window system, the code in question is pasted below. Any suggestions?
Main Rendering Loop
TestShader.Activate();
glUseProgram(TestShader.Program);
ProjectionMatrix = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
int projectionMatrixLocation = glGetUniformLocation(TestShader.Program, "ProjectionMatrix");
glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, glm::value_ptr(ProjectionMatrix));
glm::mat4 ViewMatrix = glm::lookAt(
glm::vec3(position),
glm::vec3(position+direction),
glm::vec3(up)
);
int viewMatrixLocation = glGetUniformLocation(TestShader.Program, "ViewMatrix");
glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, glm::value_ptr(ViewMatrix));
TestModel[1].Draw(TestShader, glm::vec3(0,0,-11));
TestModel[0].Draw(TestShader, glm::vec3(0,0,-1));
Refresh(false);
Model Drawing Function
void E_MODEL::Draw(EShader Shader, glm::vec3 Location)
{
if (!Registered) glGenVertexArrays(1, &VAO[0]);
glBindVertexArray(VAO[0]);
if (!Registered) glGenBuffers(1, &VBO[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
if (!Registered) glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(glm::vec3), &Vertices[0], GL_STATIC_DRAW);
if (!Registered) glGenBuffers(1, &VBO[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
if (!Registered) glBufferData(GL_ARRAY_BUFFER, Normals.size() * sizeof(glm::vec3), &Normals[0], GL_STATIC_DRAW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), Location);
int modelMatrixLocation = glGetUniformLocation(Shader.Program, "modelMatrix");
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));
glDrawArrays( GL_TRIANGLES, 0, Vertices.size() );
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Registered = true;
}
Vertex Shader
#version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertexColor;
// Output data ; will be interpolated for each fragment.
out vec3 fragmentColor;
// Values that stay constant for the whole mesh.
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;
uniform mat4 modelMatrix;
void main(){
gl_Position = ProjectionMatrix * ViewMatrix * modelMatrix * vec4(vertexPosition_modelspace,1);
// The color of each vertex will be interpolated
// to produce the color of each fragment
fragmentColor = vertexColor;
}
Fragment Shader
#version 330 core
// Interpolated values from the vertex shaders
in vec3 fragmentColor;
// Ouput data
out vec3 color;
void main(){
// Output color = color specified in the vertex shader,
// interpolated between all 3 surrounding vertices
color = fragmentColor;
}
glClear clears a buffer. With the GL_COLOR_BUFFER_BIT flag, you are clearing the color buffer. With the GL_DEPTH_BUFFER_BIT flag, you are clearing the depth buffer. By doing this each time you draw a model, the color and depth information written previously is cleared. This means you are erasing the image every time you draw a model.
You typically clear the buffers once per "paint" or "present". That is, you clear once, you draw N times, you present once. What you are doing now is clear, draw, clear, draw... present.
TL;DR: Call glClear once, before all of your drawing, not before each draw.
is it the glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); inside the draw function? maybe move that out to before you make the calls the draw functions?