Jump to content
  • Advertisement
Sign in to follow this  
Phil15

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites

glm::perspective takes fov as radians nowadays. So you probably want to use glm::radians(60.0f).

Share this post


Link to post
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 this post


Link to post
Share on other sites

 

 

glm::perspective takes fov as radians nowadays. So you probably want to use glm::radians(60.0f).

 

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

It's not radians, its degrees.

 

 

Check better, that's probably outdated. See http://glm.g-truc.net/0.9.7/api/a00174.html#gac3613dcb6c6916465ad5b7ad5a786175

 

 

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.  dry.png

Edited by Glass_Knife

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!