OpenGL does not display simple triangle
up vote
-1
down vote
favorite
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
c++ opengl cmake glsl glfw
add a comment |
up vote
-1
down vote
favorite
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
c++ opengl cmake glsl glfw
You seem to glDisableVertexAttribArray all the vertex attributes... how do you expect anything to be rendered then? You must leave them enabled in the VAO.
– httpdigest
Nov 11 at 22:27
Sorry i edit the post, it does not work even with the depth buffer active
– Soof Benzaii
Nov 11 at 22:27
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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
c++ opengl cmake glsl glfw
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
c++ opengl cmake glsl glfw
c++ opengl cmake glsl glfw
edited Nov 12 at 15:28
genpfault
41.4k95197
41.4k95197
asked Nov 11 at 21:41
Soof Benzaii
66
66
You seem to glDisableVertexAttribArray all the vertex attributes... how do you expect anything to be rendered then? You must leave them enabled in the VAO.
– httpdigest
Nov 11 at 22:27
Sorry i edit the post, it does not work even with the depth buffer active
– Soof Benzaii
Nov 11 at 22:27
add a comment |
You seem to glDisableVertexAttribArray all the vertex attributes... how do you expect anything to be rendered then? You must leave them enabled in the VAO.
– httpdigest
Nov 11 at 22:27
Sorry i edit the post, it does not work even with the depth buffer active
– Soof Benzaii
Nov 11 at 22:27
You seem to glDisableVertexAttribArray all the vertex attributes... how do you expect anything to be rendered then? You must leave them enabled in the VAO.
– httpdigest
Nov 11 at 22:27
You seem to glDisableVertexAttribArray all the vertex attributes... how do you expect anything to be rendered then? You must leave them enabled in the VAO.
– httpdigest
Nov 11 at 22:27
Sorry i edit the post, it does not work even with the depth buffer active
– Soof Benzaii
Nov 11 at 22:27
Sorry i edit the post, it does not work even with the depth buffer active
– Soof Benzaii
Nov 11 at 22:27
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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);
Even when i delete the 3 glDisableVertexAttribArray(...) lines, noting is rendered..
– Soof Benzaii
Nov 11 at 22:32
it return me vertex_loc = 0. the color and normal loation are both egal to -1 because i am not using them in the shader...
– Soof Benzaii
Nov 11 at 22:36
@SoofBenzaii The calculation of the buffer size is wrong, see the last part of the answer.
– Rabbid76
Nov 11 at 22:44
I am so stupid. I forgot how this function worked thank you very much ..
– Soof Benzaii
Nov 11 at 22:48
@SoofBenzaii You're welcome.
– Rabbid76
Nov 11 at 22:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253521%2fopengl-does-not-display-simple-triangle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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);
Even when i delete the 3 glDisableVertexAttribArray(...) lines, noting is rendered..
– Soof Benzaii
Nov 11 at 22:32
it return me vertex_loc = 0. the color and normal loation are both egal to -1 because i am not using them in the shader...
– Soof Benzaii
Nov 11 at 22:36
@SoofBenzaii The calculation of the buffer size is wrong, see the last part of the answer.
– Rabbid76
Nov 11 at 22:44
I am so stupid. I forgot how this function worked thank you very much ..
– Soof Benzaii
Nov 11 at 22:48
@SoofBenzaii You're welcome.
– Rabbid76
Nov 11 at 22:50
add a comment |
up vote
1
down vote
accepted
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);
Even when i delete the 3 glDisableVertexAttribArray(...) lines, noting is rendered..
– Soof Benzaii
Nov 11 at 22:32
it return me vertex_loc = 0. the color and normal loation are both egal to -1 because i am not using them in the shader...
– Soof Benzaii
Nov 11 at 22:36
@SoofBenzaii The calculation of the buffer size is wrong, see the last part of the answer.
– Rabbid76
Nov 11 at 22:44
I am so stupid. I forgot how this function worked thank you very much ..
– Soof Benzaii
Nov 11 at 22:48
@SoofBenzaii You're welcome.
– Rabbid76
Nov 11 at 22:50
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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);
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);
edited Nov 11 at 22:43
answered Nov 11 at 22:27
Rabbid76
32.2k112842
32.2k112842
Even when i delete the 3 glDisableVertexAttribArray(...) lines, noting is rendered..
– Soof Benzaii
Nov 11 at 22:32
it return me vertex_loc = 0. the color and normal loation are both egal to -1 because i am not using them in the shader...
– Soof Benzaii
Nov 11 at 22:36
@SoofBenzaii The calculation of the buffer size is wrong, see the last part of the answer.
– Rabbid76
Nov 11 at 22:44
I am so stupid. I forgot how this function worked thank you very much ..
– Soof Benzaii
Nov 11 at 22:48
@SoofBenzaii You're welcome.
– Rabbid76
Nov 11 at 22:50
add a comment |
Even when i delete the 3 glDisableVertexAttribArray(...) lines, noting is rendered..
– Soof Benzaii
Nov 11 at 22:32
it return me vertex_loc = 0. the color and normal loation are both egal to -1 because i am not using them in the shader...
– Soof Benzaii
Nov 11 at 22:36
@SoofBenzaii The calculation of the buffer size is wrong, see the last part of the answer.
– Rabbid76
Nov 11 at 22:44
I am so stupid. I forgot how this function worked thank you very much ..
– Soof Benzaii
Nov 11 at 22:48
@SoofBenzaii You're welcome.
– Rabbid76
Nov 11 at 22:50
Even when i delete the 3 glDisableVertexAttribArray(...) lines, noting is rendered..
– Soof Benzaii
Nov 11 at 22:32
Even when i delete the 3 glDisableVertexAttribArray(...) lines, noting is rendered..
– Soof Benzaii
Nov 11 at 22:32
it return me vertex_loc = 0. the color and normal loation are both egal to -1 because i am not using them in the shader...
– Soof Benzaii
Nov 11 at 22:36
it return me vertex_loc = 0. the color and normal loation are both egal to -1 because i am not using them in the shader...
– Soof Benzaii
Nov 11 at 22:36
@SoofBenzaii The calculation of the buffer size is wrong, see the last part of the answer.
– Rabbid76
Nov 11 at 22:44
@SoofBenzaii The calculation of the buffer size is wrong, see the last part of the answer.
– Rabbid76
Nov 11 at 22:44
I am so stupid. I forgot how this function worked thank you very much ..
– Soof Benzaii
Nov 11 at 22:48
I am so stupid. I forgot how this function worked thank you very much ..
– Soof Benzaii
Nov 11 at 22:48
@SoofBenzaii You're welcome.
– Rabbid76
Nov 11 at 22:50
@SoofBenzaii You're welcome.
– Rabbid76
Nov 11 at 22:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253521%2fopengl-does-not-display-simple-triangle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You seem to glDisableVertexAttribArray all the vertex attributes... how do you expect anything to be rendered then? You must leave them enabled in the VAO.
– httpdigest
Nov 11 at 22:27
Sorry i edit the post, it does not work even with the depth buffer active
– Soof Benzaii
Nov 11 at 22:27