I am trying to use OpenGL in order to build some rendering soft,I have already used OpenGL in the past but i can't find where i am wrong in my code. so i have implemented:
this is my mesh class function & constructor init and display :
Mesh::Mesh(char* meshSource)
{
//Creation d'un triangle pour quelques debug...
struct Vertex v0;
struct Vertex v1;
struct Vertex v2;
//Position local
v0.coord = vec3(-1.0f,-1.0f,0.0f);
v1.coord = vec3(1.0f,-1.0f,0.0f);
v2.coord = vec3(0.0f,1.0f,0.0f);
//Coouleur des points
v0.color = vec3(1.0,0.0,0.0);
v1.color = vec3(0.0,1.0,0.0);
v2.color = vec3(0.0,0.0,1.0);
//normals des points
v0.normal = vec3(0.0,0.0,-1.0);
v1.normal = vec3(0.0,0.0,-1.0);
v2.normal = vec3(0.0,0.0,-1.0);
Vertices.push_back(v0);
Vertices.push_back(v1);
Vertices.push_back(v2);
//sert a la premiere initialisation...
_ready = false;
}
void Mesh::init(Shader *_shader)
{
glGenVertexArrays(1,&_vao);
glGenBuffers(1,&_vbo);
checkGLError();
//bind des caracteristiques du mesh...
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER,_vbo);
checkGLError();
//On donne nos données au VBO.
glBufferData(GL_ARRAY_BUFFER, sizeof(vec3) * Vertices.size(), Vertices.data(), GL_STATIC_DRAW);
checkGLError();
int vertex_loc = _shader->getAttribLocation("V_position");
std::cout << "vertex_loc = " << vertex_loc << std::endl;
if(vertex_loc>=0)
{
glEnableVertexAttribArray(vertex_loc);
glVertexAttribPointer(vertex_loc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
}
int color_loc = _shader->getAttribLocation("V_color");
std::cout << "color_loc = " << color_loc << std::endl;
if(color_loc>=0)
{
glEnableVertexAttribArray(color_loc);
glVertexAttribPointer(color_loc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)sizeof(vec3));
}
int normal_loc = _shader->getAttribLocation("V_normal");
std::cout << "normal_loc = " << normal_loc << std::endl;
if(normal_loc>=0)
{
glEnableVertexAttribArray(normal_loc);
glVertexAttribPointer(normal_loc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(2*sizeof(vec3)));
}
if(vertex_loc>=0)
glDisableVertexAttribArray(vertex_loc);
if(color_loc>=0)
glDisableVertexAttribArray(color_loc);
if(normal_loc>=0)
glDisableVertexAttribArray(normal_loc);
glBindVertexArray(0);
this->_ready = true;
}
void Mesh::draw(Shader *_shader)
{
if(!_ready)
{
init(_shader);
std::cout << "Initialisation du mesh terminer" << std::endl;
}
glBindVertexArray(_vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0,3);
glBindVertexArray(0);
}
And this is my viewer with my rendering loop.. :
void S2Viewer::runLoop()
{
/* Loop until the user closes the window */
glClearColor(1.0, 1.0, 1.0, 0.0);
glEnable (GL_DEPTH_TEST);
while (!glfwWindowShouldClose(_S2viewer))
{
Shaders[0]->use();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (std::vector<Mesh*>::iterator mesh = Meshs.begin(); mesh < Meshs.end(); mesh++)
{
(*mesh)->draw(Shaders[0]);
}
glfwSwapBuffers(_S2viewer);
glfwPollEvents();
Shaders[0]->desactivate();
}
glfwTerminate();
}
and my main.cpp
int main(int argc,char** argv)
{
std::cout << "Hello fuck**g World" <<std::endl;
S2Viewer viewer;
viewer.init();
//initialisation des shaders et des mesh...
Mesh mesh = Mesh("mesh");
Shader shader("/Users/benz/Documents/projPerso/Moteur_S2_Engine/data/shader/simple.vert","/Users/benz/Documents/projPerso/Moteur_S2_Engine/data/shader/simple.frag");
viewer.addMesh(&mesh);
viewer.addShader(&shader);
viewer.runLoop();
}
Here my Vertex shader :
#version 410 core
layout(location = 0) in vec3 V_position;
layout(location = 1) in vec3 V_color;
layout(location = 2) in vec3 V_normal;
void main()
{
gl_Position = vec4(V_position, 1.);
}
Here my Fragment Shader :
#version 410 core
out vec4 out_color;
void main(void) {
out_color = vec4(1.0,0.0,0.0,1.0);
}
When i run my code it compile perfectly but it display nothing.. The problem do not come from the shader because i used in the past my class Shader in previous soft.. Plus glGetError does not show any error, i do not understant where is my problem ..
PS :I am on macOS
The state if vertex attribute is enabled is stored in the Vertex Array Object.
In your code the vertex attributes get enabled, but you disable them right after again. Finally the state of the vertex attributes which is stored in the vertex array object state vector is "disabled".
Skip the disabling of the vertex attributes
glBindVertexArray(_vao);
....
if(vertex_loc>=0)
{
glEnableVertexAttribArray(vertex_loc);
glVertexAttribPointer(vertex_loc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
}
....
if(vertex_loc>=0)
glDisableVertexAttribArray(vertex_loc); // <---- delete this
....
glBindVertexArray(0);
The 2nd paramter of glBufferData
has to be the size of the entire buffer in bytes.
Your buffer has Vertices.size()
elements and and the size of each element is sizeof(Vertex)
, so the buffer size in bytes is sizeof(Vertex) * Vertices.size()
:
glBufferData(GL_ARRAY_BUFFER,
sizeof(Vertex) * Vertices.size(), // <---- sizeof(Vertex) istead of sizeof(vec3)
Vertices.data(), GL_STATIC_DRAW);