简体   繁体   English

使用GLM库在OpenGL中绘制顶点时如何使用正则坐标系

[英]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. 我目前正在通过尝试制作2D游戏来练习OpenGL和C ++。 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. 现在,我已经设法使用GLM库来设置“相机”(使用透视图,这不是我想要的),但是在尝试显示像素艺术时遇到了一些问题制作。

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. 显然,它是为3D和在“世界空间”中绘制事物而制作的,而不是为在屏幕上绘制像素画而制作的。 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. 这就是为什么我选择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. 据我所知,我应该使用正交视图,但是将投影矩阵设置为glm::ortho()似乎并不能解决问题。 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. 起初,我认为这是由于我的顶点仍具有1像素大小,但是即使更改了它们的位置以形成适当大小的矩形(从技术上来说,两个三角形,但仍然是三角形),屏幕上仍然没有任何显示。

Could I perhaps be using glm::ortho() incorrectly, or is it some other issue. 我可能会错误地使用glm::ortho() ,还是其他问题。


Setting the projection matrix to glm::ortho() . 将投影矩阵设置为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. 我不确定该显示什么,因此我决定坚持使用显而易见的内容。

The functions in the glm library are template functions. glm库中的函数是模板函数。 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 : 参见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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM