Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: GLM::Projection questions

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2013
    Location
    Norway
    Posts
    19

    GLM::Projection questions

    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

    Code :
    		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.
    Code :
    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.


    Code :
    screenCoords = glm :: normalize(screenCoords);
     
    m_engine->postFXShader().setUniform("lightPositionOnScreen", vec2(screenCoords.x, screenCoords.y));


    Shader code (Fragment)

    Code :
     
    #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.

  2. #2
    Member Regular Contributor Nowhere-01's Avatar
    Join Date
    Feb 2011
    Location
    Novosibirsk
    Posts
    251
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2013
    Location
    Norway
    Posts
    19
    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!

    Code :
    #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;  
    }

  4. #4
    Member Regular Contributor Nowhere-01's Avatar
    Join Date
    Feb 2011
    Location
    Novosibirsk
    Posts
    251
    calling to glm:erspective 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, it looks like you don't understand, how modern graphics pipeline works;

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2013
    Location
    Norway
    Posts
    19
    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 explains what the fixed pipeline does.

  6. #6
    Member Regular Contributor Nowhere-01's Avatar
    Join Date
    Feb 2011
    Location
    Novosibirsk
    Posts
    251
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •