Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have been trying to get a simple triangle to render on the screen but an unable to using the Projection, View and Transformation Matrix's but an unable to get anything to the screen.

Main loop

...
shader->start();

Matrix4f transformation = Math::createTransformationMatrix(Vec3<float>(0,0,-25),0,0,0, 1);
shader->loadTransformationMatrix(transformation);

Matrix4f camera = Math::createViewMatrix(Vec3<float>(0, 0, 0), 1, 0, 0);
shader->loadViewMatrix(camera);

Matrix4f projection = Math::getProjectionMatrix(1080,720);
shader->loadProjectionMatrix(projection);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
    0, 
    3,
    GL_FLOAT,
    GL_FALSE,
    0,
    (void*)0
);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);

shader->stop();
...

Math.h

static Matrix4f createTransformationMatrix(Vec3<float> translation, float rx, float ry, float rz, float scale) {
    Matrix4f matrix;
    Matrix4f::translate(translation, matrix,matrix);
    Matrix4f::rotate(toRadians(rx), Vec3<float>(1, 0, 0), matrix, matrix);
    Matrix4f::rotate(toRadians(ry), Vec3<float>(0, 1, 0), matrix, matrix);
    Matrix4f::rotate(toRadians(rz), Vec3<float>(0, 0, 1), matrix, matrix);
    Matrix4f::scale(Vec3<float>(scale, scale, scale), matrix, matrix);
    return matrix;
}
static Matrix4f createViewMatrix(Vec3<float> pos, float pitch, float yaw, float roll) {
    Matrix4f matrix;
    Matrix4f::rotate(toRadians(pitch), Vec3<float>(1, 0, 0), matrix, matrix);
    Matrix4f::rotate(toRadians(yaw), Vec3<float>(0, 1, 0), matrix, matrix);
    Matrix4f::rotate(toRadians(roll), Vec3<float>(0, 0, 1), matrix, matrix);

    Vec3<float> negativeCam(-pos.x, -pos.y, -pos.z);
    Matrix4f::translate(negativeCam, matrix, matrix);

    return matrix;
}
static Matrix4f getProjectionMatrix(int width, int height) {
    float aspectRatio = (float)width / (float)height;
    float y_scale = (float)((1 / tan(toRadians(FOV / 2)))*aspectRatio);
    float x_scale = y_scale / aspectRatio;
    float frustum_length = FAR_PLANE - NEAR_PLANE;

    Matrix4f projectionMatrix;
    projectionMatrix[0] = x_scale;
    projectionMatrix[5] = y_scale;
    projectionMatrix[10] = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
    projectionMatrix[11] = -1;
    projectionMatrix[14] = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
    projectionMatrix[15] = 0;
    return projectionMatrix;
}

Vertex Shader

    #version 330 core

    layout(location = 0) in vec3 position;

    uniform mat4 transformationMatrix;
    uniform mat4 projectionMatrix;
    uniform mat4 viewMatrix;

    void main() {
        //Works but renders without any camera or projection applied
        //gl_Position.xyz = position;
        //gl_Position.w = 1.0;

        vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
        vec4 positionRelitiveToCam = viewMatrix * worldPosition;
        gl_Position = projectionMatrix * positionRelitiveToCam;
    }
share|improve this question

First make sure that your matrices are column-major. Second this call:

glVertexAttribPointer(
    0, 
    3,
    GL_FLOAT,
    GL_FALSE,
    0,
    (void*)0
);

Should look like this:

glVertexAttribPointer(
    0, 
    3,
    GL_FLOAT,
    GL_FALSE,
    sizeof(float) * 3,
    (void*)0
);

Because you want to move vertex attrib pointer by 3 floats (I assume) each time you draw new vertex.

share|improve this answer
1  
If I remember correctly stride == 0 for glVertexAttribPointer designates tightly packed data, which is in op's case same as sizeof (float) * 3. – HolyBlackCat Oct 23 at 10:27

There are many things that can be wrong, check the follows:

    • Use quaternions, because then you can normalize them and be sure that all is correct.
    • gl_position should look like this: ViewProjection * cameraRotation * cameraTranslation * Object transformation * vec4(Position,1.0);

    Where object transformation is: Object Translation * Object Rotation * Object Scale

    • Stop using glUseProgram(0) Opengl is sequential, it wont do anything good for you.
    • Make a buffer at the init to initializate the vertex buffer and save it for later use it. It should look something like this:

      glGenVertexArrays(1, &m_VAO); glBindVertexArray(m_VAO); glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[POS_VB]); glBufferData(GL_ARRAY_BUFFER, sizeof(Positions[0]) * Positions.size(), &Positions[0], GL_STATIC_DRAW); glEnableVertexAttribArray(POSITION_LOCATION); glVertexAttribPointer(POSITION_LOCATION, 3, GL_FLOAT, GL_FALSE, 0, 0);

    share|improve this answer
        
    I have to disagree about calling glUseProgram(0). If you don't call that, then the state remains set and future drawing will use the same program. If that isn't what you intended, you now have a bug in your program and don't realize it. – user1118321 Oct 23 at 14:50
        
    He is using the whole time the same shader, it is no point on deleting it every frame. Also if you want to use another shader it is no point on calling glUseProgram(0), the new one will remplace the last one. – Haruko Oct 23 at 15:01
        
    A few points: 1) glUseProgram(0) does not delete the program. It just sets the current gl state to not use any program. 2) We don't know what other drawing happens in this code. This is just a snippet. 3) You're assuming that the developer will always remember to set a new program. My point is that if they don't they won't have any indication that the wrong program is being used. If you set it back to 0 when you're done, then there's a very obvious thing that's different when you go to draw the next piece of geometry. Most gl bugs I encounter are due to state not being set correctly. – user1118321 Oct 23 at 15:46
        
    I see your point. You are right, but in this case we know that this user just want to draw a triangle, there is not more geometry to draw. Anyways, we just have different ways to see it, when i want to draw without using a shader i just use glUseProgram(0) or i use a shader that do nothing, when i want to use a shader i just use it, it will just replace the last one. It is just different ways to do it, seting whole time the shader to 0 can generate problems also, for example you might think that you are using a shader but you aren´t (it happened to me). – Haruko Oct 23 at 16:20

    Your Answer

     
    discard

    By posting your answer, you agree to the privacy policy and terms of service.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.