Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a regular coordinate system while drawing vertices in OpenGL using the GLM library

I'm currently practicing OpenGL and C++ by trying to make a 2D game. Just something very simple. Right now I've managed to use the GLM library to set up a 'camera' (with perspective, which isn't what I want), but I've run into a few problems when trying to display a piece of pixel art I made.

For one, there's the coordinate system. It's clearly made for 3D and drawing things in the 'world space', and NOT for drawing pixel art onto the screen. I just want to be able to set the size and position of each of the vertices I'm drawing using a regular coordinate system. This is why I took to Stack Exchange.

From what I've been able to tell, I should be using the orthographic view, but setting my projection matrix to glm::ortho() doesn't appear to do the trick. In fact, nothing appears. I get a blank screen. At first, I thought this was due to my vertices still having the 1 pixel size, but even after changing their positions to form the appropriate size rectangle (two triangles, technically, but still), nothing appeared on the screen.

Could I perhaps be using glm::ortho() incorrectly, or is it some other issue.


Setting the projection matrix to glm::ortho().

ProjectionMatrix = glm::ortho(0, 640, 480, 0);


Setting the vertices.
Vertex vertices[] = {
    // Position                     // Color                        // Texcoord
    glm::vec3(0.0f, 80.0f, 0.0f),   glm::vec3(1.0f, 0.0f, 0.0f),    glm::vec2(0.0f, 1.0f),  // bottom left
    glm::vec3(0.0f, 0.0f, 0.0f),    glm::vec3(0.0f, 1.0f, 0.0f),    glm::vec2(0.0f, 0.0f),  // top left
    glm::vec3(40.0f, 0.0f, 0.0f),   glm::vec3(0.0f, 0.0f, 1.0f),    glm::vec2(1.0f, 0.0f),  // top right
    glm::vec3(40.0f, 80.0f, 0.0f),  glm::vec3(1.0f, 1.0f, 0.0f),    glm::vec2(1.0f, 1.0f)   // bottom right
};

If any further code/information is required, feel free to let me know. I am a bit unsure of what to show, so I decided to stick with the obvious.

like image 324
MadCreativity Avatar asked Nov 07 '22 23:11

MadCreativity


1 Answers

The functions in the glm library are template functions. The type of the arguments effects the type of the operations. If you pass integral constants to the functions, then the inner operations are integer operations and the result of a division is integer, too.

See the implementation of glm::ortho:

template<typename T>
GLM_FUNC_QUALIFIER mat<4, 4, T, defaultp> ortho(T left, T right, T bottom, T top)
{
    mat<4, 4, T, defaultp> Result(static_cast<T>(1));
    Result[0][0] = static_cast<T>(2) / (right - left);
    Result[1][1] = static_cast<T>(2) / (top - bottom);
    Result[2][2] = - static_cast<T>(1);
    Result[3][0] = - (right + left) / (right - left);
    Result[3][1] = - (top + bottom) / (top - bottom);
    return Result;
}

Pass floating point constants to the function, to solve your issue:

ProjectionMatrix = glm::ortho(0.0f, 640.0f, 480.0f, 0.0f);
like image 118
Rabbid76 Avatar answered Nov 12 '22 16:11

Rabbid76