2
votes

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:

Result -> I dont Like too because the origin should be in the upper left corner and is in the center of the window

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 );
}

2
Please don't call your functions returning void "voids".thokra
Also, with a near-plane at (z == 0.1f) you won't see a thing. Have you tried moving the camera "back" a little, i.e. some distance along the positive z-axis? (Hint: glm::lookAt)thokra
Hello thokra, yes I tried and nothing happensczapata91
What are your vertex postions data of the triangle? And where do you define your ModelViewMatrix? Is it an identify matrix? It seems like your triagnle is out of the view frustum.Wayne Wang
Somehow that got lost in my previous comment: you won't see a thing with a near-plane at z == 0.1f and vertices which are all at z == 0.thokra

2 Answers

2
votes

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 */
0
votes

There are two issues in your code,

  1. Your triangle is in z=0 plane, but you set near/far to be 0.1 and 100, so your triangle is out of the view frustum, you need to set z value in the range of (-0.1 < z < -100)
  2. If w, h is the size of your window, then your triangle after rasterization will only occupy one pixel on your screen, in other words you will hardly see anything on your screen. You can consider you are in window space with pixel units, so you need to have a reasonable size triangle. So, the fix for this issue, either you change you vertex postion larger, e.g. (w/2, h/2, -1, w, h/2, -1, w, h, -1) or you can change the orth to be glm :: ortho(-1, 1, -1, 1, 0.1f, 100.0f);