Orthogonal Projection for 2D Rendering with Modern OpenGL

senex

I'm trying to port an old SDL game I wrote some time ago to 3.3+ OpenGL and I'm having a few issues getting a proper orthogonal matrix and it's corresponding view.

    glm::mat4 proj = glm::ortho( 0.0f, static_cast<float>(win_W), static_cast<float>(win_H), 0.0f,-5.0f, 5.0f);

If I simply use this projection and try to render say a quad inside the boundaries of win_W and win_H, it works. However, if instead I try to simulate a camera using:

   glm::mat4 view = glm::lookAt(
    glm::vec3(0.0f, 0.0f, 1.0f),//cam pos
    glm::vec3(0.0f, 0.0f, 0.0f),//looking at
    glm::vec3(0.0f, 0.0f, 1.0f)//floored
);

I get nothing. Even when positioning the quad in the center, same if instead I center the view:

  glm::mat4 view = glm::lookAt(
    glm::vec3(static_cast<float>(win_W), static_cast<float>(winH), 1.0f),//cam pos
    glm::vec3(static_cast<float>(win_W), static_cast<float>(winH), 0.0f),//looking at
    glm::vec3(0.0f, 0.0f, 1.0f)//floored
);

Considering that SDL usually subtracts the camera values from the vertices in the game to simulate a view, is it simply better to replace my MVP operation in the shader like this:

#version 150

in vec2 position;
in vec3 color;

out vec3 Color;

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

void main()
{
Color = color;
 //faulty view
//gl_Position = projection *view * model * vec4(position, 0.0, 1.0);
//new SDL style camera
  mat4 newPosition = projection * model * vec4(position, 0.0, 1.0);
 gl_Position = newPosition - camera;
} 

Unfortunately the source I'm learning from (ArcSynthesis Book) doesn't cover a view that involves a coordinate system other than the default NDC. And for some reason even today most discussion about opengl is about deprecated versions. So in short, whats the best way of setting up the view to an orthogonal projection matrix for simple 2D rendering in modern opengl?

derhass

Your lookup call actually does not make sense at all, since your up vector (0,0,1) is colinear to the viewing direction (0,0,-1). This will not produce a useable matrix.

You probably want (0,1,0) as up vector. However, even if you change that, you might be surprised about the result. In combination with the projection matrix you set up, the point you specified as the lookat target will not appear in the center, but at the corner of the screen. Your projection does not map (0,0) to the center, which one typically assumes when using some LookAt function.

I agree to Colonel Thirty Two's comment: Don't use LookAt in this scenario, unless you have a very good reason to.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

how to use JOML to simulate OpenGL like Model, View Matrices on 3D projection to 2D plane?

From Dev

How to manage the Model, View and Projection matrices in modern OpenGL

From Dev

Converting perspective projection to orthogonal projection

From Dev

Modern OpenGL problems with Orthographic Projection

From Dev

OpenGL/SharpGL: Rendering 2D image is stretched horizontally

From Dev

How to draw polygon with 3D points in modern openGL?

From Dev

Rendering multiple 2D images in OpenGL-ES 2.0

From Dev

Can I use both deprecated OpenGL and modern OpenGL in a single rendering window?

From Dev

Is the projection matrix in OpenGL really a "projection matrix"?

From Dev

How to draw cylinder in modern opengl

From Dev

SDF text rendering in perspective projection

From Dev

Will using DirectX or OpenGL speed up 2D Rendering on a windows form?

From Dev

Orthogonal Projection of Point onto Line

From Dev

Orthogonal projection of a point onto a line in processing?

From Dev

OpenGL Orthographic Projection and Translate

From Dev

Rendering Textures with Modern OpenGL

From Dev

OpenGL FreeType2 bitmap not rendering

From Dev

OpenGL ES 2 Projection Switching Foregroung and Background

From Dev

OpenGL ES 2 Projection Switching Foregroung and Background

From Dev

orthogonal projection with numpy

From Dev

OpenGL Projection/Matrix confusion

From Dev

Outside of projection matrix? (OpenGL)

From Dev

Modern OpenGL problems with Orthographic Projection

From Dev

Can I use both deprecated OpenGL and modern OpenGL in a single rendering window?

From Dev

Getting ModelView and Projection matrices in Opengl 2.x [C++]

From Dev

Orthogonal Projection of Point onto Line

From Dev

OpenGL 2D projection stretched by aspect ratio

From Dev

Point cloud projection to 2D

From Dev

How to make 2D OpenGL rendering efficient in C++

Related Related

  1. 1

    how to use JOML to simulate OpenGL like Model, View Matrices on 3D projection to 2D plane?

  2. 2

    How to manage the Model, View and Projection matrices in modern OpenGL

  3. 3

    Converting perspective projection to orthogonal projection

  4. 4

    Modern OpenGL problems with Orthographic Projection

  5. 5

    OpenGL/SharpGL: Rendering 2D image is stretched horizontally

  6. 6

    How to draw polygon with 3D points in modern openGL?

  7. 7

    Rendering multiple 2D images in OpenGL-ES 2.0

  8. 8

    Can I use both deprecated OpenGL and modern OpenGL in a single rendering window?

  9. 9

    Is the projection matrix in OpenGL really a "projection matrix"?

  10. 10

    How to draw cylinder in modern opengl

  11. 11

    SDF text rendering in perspective projection

  12. 12

    Will using DirectX or OpenGL speed up 2D Rendering on a windows form?

  13. 13

    Orthogonal Projection of Point onto Line

  14. 14

    Orthogonal projection of a point onto a line in processing?

  15. 15

    OpenGL Orthographic Projection and Translate

  16. 16

    Rendering Textures with Modern OpenGL

  17. 17

    OpenGL FreeType2 bitmap not rendering

  18. 18

    OpenGL ES 2 Projection Switching Foregroung and Background

  19. 19

    OpenGL ES 2 Projection Switching Foregroung and Background

  20. 20

    orthogonal projection with numpy

  21. 21

    OpenGL Projection/Matrix confusion

  22. 22

    Outside of projection matrix? (OpenGL)

  23. 23

    Modern OpenGL problems with Orthographic Projection

  24. 24

    Can I use both deprecated OpenGL and modern OpenGL in a single rendering window?

  25. 25

    Getting ModelView and Projection matrices in Opengl 2.x [C++]

  26. 26

    Orthogonal Projection of Point onto Line

  27. 27

    OpenGL 2D projection stretched by aspect ratio

  28. 28

    Point cloud projection to 2D

  29. 29

    How to make 2D OpenGL rendering efficient in C++

HotTag

Archive