How to setup Ortho graphical view with glm?

Hello I’m trying to setup viewport with Ortho graphic but I can’t find real good tutorial for glm.
Can anyone please help me to achieve it?

I’ve tried something like below just to see whether it can project something but can’t see anything

glWindow.mvMatrix = glm::mat4(1.0f);
glWindow.mvMatrix = glm::translate(glWindow.mvMatrix, glm::vec3(0.0f, 0.0f, 0.0f));
glWindow.mvMatrix = glm::scale(glWindow.mvMatrix, glm::vec3(1.0f, 1.0f, 1.0f));

shader.setMat4("view", glWindow.view);
shader.setMat4("model", glWindow.mvMatrix);

below are perspective projection code and shaders I’m using which can project viewport perfectly

void Draw_Scene() { 
    .
    .
    .
    glWindow.prjMatrix = glm::perspective(glm::radians(glWindow.camera.Zoom), (float)glWindow.Width / (float)glWindow.Height, 0.1f, 100.0f);
    glWindow.view = glWindow.camera.GetViewMatrix();
    glWindow.mvMatrix = glm::mat4(1.0f);
    
    shader.use();
    shader.setMat4("projection", glWindow.prjMatrix);
    shader.setMat4("view", glWindow.view);
    .
    .
    .
}		

--------------------------------------------------------- vertex shader ---------------------------------------------------------

#version 450 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;

out VS_OUT {
    vec3 FragPos;
    vec3 Normal;
    vec2 TexCoords;
} vs_out;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

void main() {
    vs_out.FragPos = vec3(model * vec4(aPos, 1.0));   
    vs_out.TexCoords = aTexCoords;
        
    mat3 normalMatrix = transpose(inverse(mat3(model)));
    vs_out.Normal = normalize(normalMatrix * aNormal);

    gl_Position = projection * view * model * vec4(aPos, 1.0);
}

------------------------------------------------------- fragment shader -------------------------------------------------------

#version 450 core
layout (location = 0) out vec4 FragColor;
layout (location = 1) out vec4 BrightColor;

in VS_OUT {
    vec3 FragPos;
    vec3 Normal;
    vec2 TexCoords;
} fs_in;

struct Light {
    vec3 Position;
    vec3 Color;
};

uniform Light lights[4];
uniform sampler2D diffuseTexture;
uniform vec3 viewPos;

out vec4 FragColor;

void main() {
    vec3 color = texture(diffuseTexture, fs_in.TexCoords).rgb;
    vec3 normal = normalize(fs_in.Normal);

    // Ambient
    vec3 ambient = 0.0 * color;

    // Lighting
    vec3 lighting = vec3(0.0);
    vec3 viewDir = normalize(viewPos - fs_in.FragPos);

    for (int i = 0; i < 4; i++) {
        // Diffuse
        vec3 lightDir = normalize(lights[i].Position - fs_in.FragPos);
        float diff = max(dot(lightDir, normal), 0.0);
        vec3 result = lights[i].Color * diff * color;
    
        // Attenuation (use quadratic with gamma correction)
        float distance = length(fs_in.FragPos - lights[i].Position);
        result *= 1.0 / (distance * distance);
        lighting += result;          
    }

    vec3 result = ambient + lighting;

    // Check whether result is higher than some threshold, if so, output as threshold color
    float brightness = dot(result, vec3(0.2126, 0.7152, 0.0722));

    if (brightness > 1.0)
        BrightColor = vec4(result, 1.0);

    else
        BrightColor = vec4(0.0, 0.0, 0.0, 1.0);

    FragColor = vec4(result, 1.0);
}

Is there something I need to modify/update in shaders?

If you want an orthographic projection, peplace the glm::perspective call with glm::ortho.

1 Like

Thank you very much for your advice