fredvam

07-10-2013, 07: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::vec3(m_pos),

view*model,

proj,

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.

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::vec3(m_pos),

view*model,

proj,

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.