PDA

View Full Version : Drawing a pyramid and a cube



Defettins
11-03-2017, 01:25 PM
EDIT: doesn't matter I figured it out by myself

Hi, I'm trying to draw two shapes: a not-textured pyramid next to a textured cube. To do it I thought to use two different shaders and vaos, but I keep getting errors and not drawing anything. I think most of my code is correct because if I try to draw them separately they render correctly. These are my shaders:
const GLchar* vertexSource =
#if defined(__APPLE_CC__)
"#version 150 core\n"
#else
"#version 130\n"
#endif
"in vec3 position;"
"in vec3 color;"
"out vec3 Color;"
"uniform mat4 model;"
"uniform mat4 view;"
"uniform mat4 projection;"
"void main() {"
" Color = color;"
" gl_Position = projection * view * model * vec4(position, 1.0);"
"}";

const GLchar* fragmentSource =
#if defined(__APPLE_CC__)
"#version 150 core\n"
#else
"#version 130\n"
#endif
"in vec3 Color;"
"out vec4 outColor;"
"void main() {"
" outColor = vec4(Color, 1.0);"
"}";

const GLchar* vertexSource2 =
#if defined(__APPLE_CC__)
"#version 150 core\n"
#else
"#version 130\n"
#endif
"in vec3 position2;"
"in vec3 color2;"
"in vec2 coord2;"
"out vec3 Color2;"
"out vec2 Coord2;"
"uniform mat4 model2;"
"uniform mat4 view2;"
"uniform mat4 projection2;"
"void main() {"
" Color2 = color2;"
" Coord2 = coord2;"
" gl_Position = projection2 * view2 * model2 * vec4(position2, 1.0);"
"}";
const GLchar* fragmentSource2 =
#if defined(__APPLE_CC__)
"#version 150 core\n"
#else
"#version 130\n"
#endif
"in vec3 Color2;"
"in vec2 Coord2;"
"out vec4 outColor2;"
"uniform sampler2D textureSampler;"
"void main() {"
" outColor2 = vec4(Color2, 1.0)*texture(textureSampler, Coord2);"
"}"; this is where I create vaos
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesp), verticesp, GL_STATIC_DRAW);

glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementsp), elementsp, GL_STATIC_DRAW);


GLint posAttrib1 = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib1);
glVertexAttribPointer(posAttrib1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0);

GLint colAttrib1 = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(colAttrib1);
glVertexAttribPointer(colAttrib1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));

glGenVertexArrays(1, &vao2);
glBindVertexArray(vao2);

glGenBuffers(1, &vbo2);
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesc), verticesc, GL_STATIC_DRAW);


glGenBuffers(1, &ibo2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo2);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementsc), elementsc, GL_STATIC_DRAW);

GLint posAttrib2 = glGetAttribLocation(shaderProgram2, "position2");
glEnableVertexAttribArray(posAttrib2);
glVertexAttribPointer(posAttrib2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);

GLint colAttrib2 = glGetAttribLocation(shaderProgram2, "color2");
glEnableVertexAttribArray(colAttrib2);
glVertexAttribPointer(colAttrib2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));

GLint cooAttrib2 = glGetAttribLocation(shaderProgram2, "coord2");
glEnableVertexAttribArray(cooAttrib2);
glVertexAttribPointer(cooAttrib2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat))); this is where I draw the shapes
int width, height;
glfwGetFramebufferSize(window, &width, &height);

glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glUseProgram(shaderProgram);

glm::mat4 projection = glm::perspective(PI/4, 1.f/1.f, 1.0f, 10.0f);
glm::mat4 view = glm::lookAt(glm::vec3(2.5f, 2.5f, 2.5f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 model = glm::mat4(1.f);


glUniformMatrix4fv(glGetUniformLocation(shaderProg ram, "projection"), 1, GL_FALSE, &projection[0][0]);
glUniformMatrix4fv(glGetUniformLocation(shaderProg ram, "view"), 1, GL_FALSE, &view[0][0]);
glUniformMatrix4fv(glGetUniformLocation(shaderProg ram, "model"), 1, GL_FALSE, &model[0][0]);


glBindVertexArray(vao);

glDrawElements(GL_TRIANGLES, 3*6 , GL_UNSIGNED_INT, 0);

glBindVertexArray(vao2);

glViewport(width/2, 0, width/2, height);
glUseProgram(shaderProgram2);
glUniform1i(glGetUniformLocation(shaderProgram2, "textureSampler"), 0);
glUniformMatrix4fv(glGetUniformLocation(shaderProg ram2, "projection2"), 1, GL_FALSE, &projection[0][0]);
glUniformMatrix4fv(glGetUniformLocation(shaderProg ram2, "view2"), 1, GL_FALSE, &view[0][0]);
glUniformMatrix4fv(glGetUniformLocation(shaderProg ram2, "model2"), 1, GL_FALSE, &model[0][0]);

glDrawElements(GL_TRIANGLES, 6*2*3, GL_UNSIGNED_INT, 0);
can someone please tell me what I'm doing wrong or if there's a simpler way of doing what I'm trying to?