# Perspective projection makes triangle disapear

This topic is 1218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys,

I have been learning Model View Projection matrices. When I multiply the coordinates by the Model_View_Projection matrix the  W  is always zero and I can not see the triangle.


void renderScene()
{

glm::mat4 ModelViewProject;

glm::mat4 View;

glm::mat4 Model(1.0);

glm::mat4 Projection = glm::perspective(60.0f, 1024.0f / 768.0f, 0.1f, 10.0f);

View = glm::lookAt(glm::vec3(0, 0, 0), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0));

ModelViewProject = Projection *  View * Model;

GLint transform = glGetUniformLocation(programId, "ModelViewProject");

glUniformMatrix4fv(transform, 1, false, glm::value_ptr(ModelViewProject));;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glClearColor(0.0, 0.0, 0.0, 1.0);

glDrawArrays(GL_TRIANGLES, 0, 3);

glutSwapBuffers();
}



if I however manually set the last coordinate in ModelViewProject matrix as 1 (which is W) I can see the triangle.

the vertex shader code is :

#version 410

attribute vec3 position;

attribute vec3 ColorIn;

varying vec3 theColor;

uniform mat4 ModelViewProject;

void main()
{

gl_Position =  ModelViewProject * vec4(position, 1.0f);
theColor = ColorIn;

};


If I use Identity matrix as ModelViewProject I can also see the triangle.
What am I missing ??

##### Share on other sites

Well your view matrix is at (0, 0, 0) looking at (0, 0, -1) on the z with y up, wheres your triangle data and indices? Im guessing not in view between 0 and -1 on the z. Your model matrix is identity so its not moving the vertices from what they were defined as.

##### Share on other sites

I just checked the glm docs.  http://glm.g-truc.net/glm.pdf

##### Share on other sites

if I however manually set the last coordinate in ModelViewProject matrix as 1 (which is W) I can see the triangle.

This is exactly what should happen.

This comes down to understanding how the translation matrix works on data.  The extra row and column are added to the matrix so that transformation works.  When you multiply a vector by a translation matrix, you end up with this:

x = x + tx * w

y = y + ty * w

z = z + tz * w

If the w value is zero, then no translation will be applied.  This is why points, that can have a position, have a w = 1.0, so they can be moved around, while vectors, which do not have a position, have a w = 0, so the do not move around.

Sometimes there is already a w component in the data.  Most of my shaders set w = 1.  That's what you do if your vertex data doesn't contain a w component.  If you leave it zero, it can't be translated.

Edited by Glass_Knife

##### Share on other sites

I just checked the glm docs.  http://glm.g-truc.net/glm.pdf

I was looking the current version.  It is not outdated, and still not in radians.

So it looks like between 0.96 and 0.97 they switched to radians, but didn't update any of the example code in the manual, which still shows degrees in every code example.

Silly me, I should have ignored the code examples and just read the documentation.

Edited by Glass_Knife

##### Share on other sites

Thank you everyone for your replies. My problem was the view matrix as was pointed out. I always thought that the W coordinate must be >0 just before I pass it to shaders and the pipeline normalises the coordinates based on W. But looks like I still dont quiet understand it. When I adjusted the camera using lookat it worked fine. I am using an older glm api so I think it is still in degrees but thanks for the hint.

##### Share on other sites

So not only the example code in the manual, but the example code source files also have degrees?  Really?

http://glm.g-truc.net/0.9.7/code.html

I filed a bug report, but man, what a terrible decision.

##### Share on other sites
Good catch. I haven't even paid any attention to those example codes because I've been just using the library for years and checked for the release notes. And the release notes say that it was changed in 0.9.6.0.

Stupid internet points though, not sure why I get -6 for bringing that up.

1. 1
2. 2
Rutin
20
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633738
• Total Posts
3013616
×