PDA

View Full Version : OpenGL beginner. Following Tutorial - Got a bunch of questions



RobertW
04-19-2015, 03:33 AM
Greetings OpenGL-Community!

I am following the tutorial at opengl-tutorial .org at the moment, but using my own classes to implement what that tutorial is teaching. (I did use code from another tutorial for the shader creation that might be one of the reasons im getting different results?)

My progress so far can be found at:

GitHub Link (https://github.com/rjwestman/thi_openglmac)

1) Why do i have to define and create the VAO before creating a shader-program?

If i create the shader (main.cpp | line 53) before the VAO (main.cpp | lines 26-30),
my validate program gets a warning. (However the triangle still gets drawn).

2) Part three in the tutorial i linked talks about transformation matrices. I do understand the math part of it, however when it comes to the implementation, it doesn't seem to work for me.

After the shader creation (main.cpp | line 53) I would add the following from the tutorial:



// Projection matrix : 45 Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
// Camera matrix
glm::mat4 View = glm::lookAt(
glm::vec3(4,3,3), // Camera is at (4,3,3), in World Space
glm::vec3(0,0,0), // and looks at the origin
glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
);
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model = glm::mat4(1.0f);* // Changes for each model !
// Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around

GLuint MatrixID = glGetUniformLocation(shader.getShaderProgramID(), "MVP");
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);


and change the vertex shader code to this:


#version 330 core

layout(location = 0) in vec3 vertexPosition;
uniform mat4 MVP;

void main() {
vec4 v = vec4(vertexPosition, 1);
gl_Position = MVP * v;
}


However i don't get the result the tutorial does (The triangle isn't visible at all).

I'd appreciate any help a lot!

Greetings, Robert

Result without transformation (GitHub Version):
1753

Expected result with transformation:
1751

Current result with transformation:
1752

RobertW
04-19-2015, 05:53 AM
UPDATE:

I just tried to pass an identity matrix to the shader, but the triangle disappears.
Pushed the code that i used to github.

It seems like the matrix isn't passed on correctly. Can somebody tell me what im doing wrong?

The error should be somewhere between main.cpp | line 55-61 and/or the basicShader.vertexshader file

EDIT:
By identifying what was wrong, then using google I found out what i was doing wrong.

I need to call the glUniform* after glUseProgram (in my case shader.Bind())

New result:
1754

Alfonse Reinheart
04-19-2015, 08:09 AM
Why do i have to define and create the VAO before creating a shader-program?

If i create the shader (main.cpp | line 53) before the VAO (main.cpp | lines 26-30),
my validate program gets a warning. (However the triangle still gets drawn).

The glValidateProgram function answers the question, "Given the current state of the rendering pipeline, can I render with this program?" Since you're not allowed to render without a VAO (in core OpenGL), the answer to that question is "no" if there's no VAO bound.

RobertW
04-19-2015, 10:50 AM
The glValidateProgram function answers the question, "Given the current state of the rendering pipeline, can I render with this program?" Since you're not allowed to render without a VAO (in core OpenGL), the answer to that question is "no" if there's no VAO bound.

Ah thank you very much!

So to get the correct answer i check the program validation after i made all the "preps".

Thank you very much!
Made a lot of progress today :)