Reputation: 51
SOLUTION
The problem was in my z interval I was considering negative values on z like (-0.1f < z < 100f), instead I used a positive interval for near/far plane ex : (0.0f < z < 100.0f), I arranged other stuffs with deprecated void's in OpenGL, and I arranged my bad position on my glm :: lookAt.
Note : Dont mix the old render way with the new render way (is a good practice but could be a problem), Btw :You must understand your position in the space.
Thanks for everyone guys :)
I'm a beginner graphic programmer and I was trying to draw a very simple triangle using a basic vertex/fragment shaders. But I've one error with my projection matrix and I can't find it. Also, Im trying to do a reshape void and I want that reshape callback works well with the triangle drawn by the shader. When I write these products in my render void happens this:
MVP = ProjectionMatrix * ModelViewMatrix;
Result -> I dont Like
And if I only assign to MVP the ModelView Matrix I got this:
These are my Reshape Voids
void OpenGL_Initialization(int w, int h){
ProjectionMatrix = glm :: ortho(0.0f,(GLfloat)w,(GLfloat)h,0.0f,0.1f, 100.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadMatrixf(glm :: value_ptr(ProjectionMatrix));
glMatrixMode(GL_MODELVIEW);
}
void Reshape(int w, int h){
width = w;
height = h;
glViewport(0,0,w,h);
OpenGL_Initialization(w,h);
}
And this is my render callback :
void Render(){
glClearColor(0.75,0.75,0.75,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glGetFloatv(GL_MODELVIEW_MATRIX,glm :: value_ptr(ModelViewMatrix));
MVP = ProjectionMatrix * ModelViewMatrix;
glUniformMatrix4fv(glGetUniformLocation(ProgramHandler,"MVP"),1,GL_FALSE,glm :: value_ptr(MVP));
glBindVertexArray(vaoHandle);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glutSwapBuffers();
}
Also these are my vertex and fragment shader
#version 400
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexColor;
uniform mat4 MVP;
out vec3 Color;
void main(){
Color = VertexColor;
gl_Position = MVP*vec4(VertexPosition,1.0);
}
#version 400
in vec3 Color;
out vec4 FragColor;
void main(){
FragColor = vec4(Color,1.0);
}
The VBOs and VAO initialization:
void InitVBO(GLuint programHandler){
glBindAttribLocation(programHandler,0,"VertexPosition");
glBindAttribLocation(programHandler,1,"VertexColor");
GLfloat positionData[] = {-0.5f,0.5f,0.0f,0.0f,-.5f,0.0f,0,0,0};
GLfloat colorData[] = {1.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,1.0f};
GLuint vboHandles[2];
glGenBuffers(2,vboHandles);
glBindBuffer(GL_ARRAY_BUFFER,vboHandles[0]);
glBufferData(GL_ARRAY_BUFFER,9*sizeof(GLfloat),positionData,GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,vboHandles[1]);
glBufferData(GL_ARRAY_BUFFER,9*sizeof(GLfloat),colorData,GL_STATIC_DRAW);
glGenVertexArrays(1, &vaoHandle);
glBindVertexArray(vaoHandle);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, vboHandles[0]);
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0,(GLubyte *)NULL );
glBindBuffer(GL_ARRAY_BUFFER, vboHandles[1]);
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0,(GLubyte *)NULL );
}
Upvotes: 2
Views: 3001
Reputation: 286
It appears you are using a mix of fixed-function pipeline (ie. GL1.x) calls and programmable shader pipeline calls. While it probably isn't causing the problem you have described, it's generally considered good practice to use either one or the other, but not both at the same time.
The correct way to upload a matrix to be used by a shader program is to call glUniformMatrix4fv
(as you have done). However, the glMatrixMode
, glLoadIdentity
and glLoadMatrix
calls in the OpenGL_Initialization(int w, int h)
function will actually have no bearing on the state of the shader program you're using; instead they're altering the state of the now-deprecated matrix stack.
Similarly, theglGetFloatv(GL_MODELVIEW_MATRIX,glm :: value_ptr(ModelViewMatrix))
call is getting the modelview matrix data used by the built-in matrix stack, which could contain garbage data.
Basically, what I suggest you do is ditch all the deprecated functionality and reimplement the OpenGL_Initialization
function quite simply like this:
void OpenGL_Initialization(int w, int h){
ProjectionMatrix = glm::ortho(0.0, (GLfloat)w, (GLfloat)h, 0.0, -1.0, 1.0);
}
and in your render callback you would just do
glClearColor(0.75, 0.75, 0.75, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 ModelViewMatrix; // default constructor => identity
MVP = ProjectionMatrix * ModelViewMatrix;
glUseProgram(ProgramHandler); // possibly redundant
GLuint MVP_loc = glGetUniformLocation(ProgramHandler, "MVP");
glUniformMatrix4fv(MVP_loc, 1, GL_FALSE, glm::value_ptr(MVP));
/* draw, swap buffers */
Upvotes: 2
Reputation: 1307
There are two issues in your code,
Upvotes: 0