View Full Version : GLM::Projection questions

07-10-2013, 06:23 PM
I am trying to understand projection with glm, the textbooks and references I use refers to gluProject, however I use GLM for everything and it is deprecated for the version of OpenGL (4) I use.

I am writing a shader that renders a volumetric light (Final post FX where i draw a quad), so i need to project into 2D space from 3D.

This is my attempt to use glm :: project over gluProject

glm::vec3 screenCoords = glm::project(
glm::vec4( 0, 0, m_windowWidth, m_windowHeight)

m_pos is the position of the light.
view is the view of the camera (from glm::lookAt)
model is mat4(1), i was unsure about this one
proj is the view projection of the camera.
glm::perspective<float>(70.0f, (float)m_windowWidth/
m_windowHeight, 0.1f, 1000.0f);

then final code before i draw the scene, normalize the vectors then update the vec2 uniform for light position.

screenCoords = glm :: normalize(screenCoords);

m_engine->postFXShader().setUniform("lightPositionOnScreen", vec2(screenCoords.x, screenCoords.y));

Shader code (Fragment)

#version 420

in vec2 UV;

out vec3 color;

uniform sampler2D renderedTexture;

uniform vec2 lightPositionOnScreen;

int NUM_SAMPLES = 100;
float density = 1.0;
float illuminationDecay = 0.75;
float weight = 0.010;
float decay = 0.99;

vec3 LightBeam()
vec3 sampler;
vec2 uvLocal = UV.xy;
vec2 light = lightPositionOnScreen;
//light.xy = (light.xy + vec2(1.0, 1.0)) / 2.0;
vec2 deltaTexCoord = uvLocal.xy - light.xy;

deltaTexCoord.xy *= 1.0 / (float(NUM_SAMPLES) * density);

vec3 color = texture2D(renderedTexture, uvLocal.xy ).xyz;

for(int i=0; i<NUM_SAMPLES; ++i)
uvLocal.xy -= deltaTexCoord.xy;
sampler = texture2D(renderedTexture, uvLocal.xy ).xyz;
sampler *= illuminationDecay * weight;
color += sampler;
illuminationDecay *= decay;
return color;

void main() {
color = LightBeam();

The result i see is good, but when i rotate the camera it appears like the rays rotate incorrectly in terms of camera rotation, so i wanted to post my code here, in case i made any obvious mistakes using glm.

07-11-2013, 08:49 PM
i don't see you applying(multiplying with it) that projection matrix anywhere in your code, probably cause you didn't post vertex shader or wherever you're using this matrix. but projection matrix has nothing to do with rotataion. this kind of misbehaviour happens when one of the previous matrix transformations was incorrect. i can't say more, there's no relevant code.

07-12-2013, 04:43 AM
ok posting the vertex shader as well, I just draw a quad in this post processing pass, so this part should stay unchanged, however, should i perhaps multiply the projection as you say, on the C++ side? If so then how. Any help appreciated!

#version 420

layout (location = 0) in vec3 vp_loc;

// Output data ; will be interpolated for each fragment.
out vec2 UV;

void main() {
gl_Position = vec4(vp_loc,1);
UV = (vp_loc.xy+vec2(1,1))/2.0;

07-12-2013, 05:58 AM
calling to glm::perspective doesn't apply projection to the scene by itself. you need to pass resulting matrix as a uniform to your vertex shader and multiply vertex by it. same about any matrix in modern OpenGL. you completely replace fixed pipeline with your own math and shaders.

i think, you need to read that (http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Chapter-1:-The-Graphics-Pipeline.html), it looks like you don't understand, how modern graphics pipeline works;

07-12-2013, 08:55 AM
i think you misunderstood my question, i do mvp in the passes before posteffect. As i stated before, this is a purely a 2D pass. I need to transform 3D vectors into screen cords. But anyway, thanks for your effort. Anyone else?

Edit: This (http://www.opengl.org/sdk/docs/man2/xhtml/gluProject.xml)explains what the fixed pipeline does.

07-12-2013, 09:08 AM
ok, i re-read your 1st post. it confused me, cause you concentrate on projection. but it almost certainly has nothing to do with projection. the problem is most likely inside the view-matrix. try to inverse it first. and post, how you derive it.

07-12-2013, 09:18 AM
alright, as for the word "project", think of a TV-projector, projecting to a 2D screen on the wall. That's what this function does (figures out the point X, Y). So i want to project :p