简体   繁体   English

openGL-正交投影矩阵

[英]openGL - orthogonal projection matrix

I'm very new to openGL and I am doing a mini project where I experiment with the depth buffer. 我是openGL的新手,我正在做一个小型项目,在这里尝试深度缓冲区。 I got to the stage of displaying it to the screen. 我到了将其显示在屏幕上的阶段。 However I want to draw it as screen coordinates instead of converting to floats. 但是我想将其绘制为屏幕坐标,而不是转换为浮点数。 I read somewhere that I need to use a projection matrix. 我读到某处需要使用投影矩阵。 I have looked for ages and tested loads of different options but I can't seem to get it right. 我已经寻找了年龄并测试了不同选项的负载,但是我似乎无法正确地做到这一点。

Can anyone point me to a useful resource or explain how I would go about doing this? 谁能指出我有用的资源或解释我将如何去做?

EDIT At the moment my matrix looks like this: 编辑目前,我的矩阵如下所示:

projectionMat = glm::ortho(0.0f, (float)_cols, 0.0f, (float)_rows, 0.0f, (float)_maxDepthVal);

projection = glGetUniformLocation(_program, "Projection");  

glUniformMatrix4fv(projection, 1, GL_FALSE, glm::value_ptr(projectionMat));

EDIT 2 编辑2

With some fiddling I found that cols had to be negative for some strange reason before it would display. 经过一番摆弄,我发现cols在显示之前必须出于某种奇怪的原因为负。 I twill now display correctly on the screen but for some reason it his a gap around the sides opposite the origin, why is this? 我现在将在屏幕上正确显示,但是由于某种原因,他在与原点相对的两侧之间留有间隙,这是为什么? Even a small move in the camera position and target cause all of it to vanish so I don't think that would be the problem. 即使相机位置和目标的微小移动也会导致所有图像消失,所以我认为这不是问题。

Pixel Art Representation!! 像素艺术表现!!

OOOO!! 哦!
OOOO!! 哦!
OOOO!! 哦!
!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!

New code 新密码

glm::mat4 Projection = glm::ortho(0.0f, -static_cast<float>(_cols), 0.0f, static_cast<float>(_rows), 0.0f, static_cast<float>(_maxDepthVal));
projection = glGetUniformLocation(_program, "Projection");

glm::mat4 View       = glm::lookAt(
                            glm::vec3(0.0f, 0.0f, -0.1f),
                            glm::vec3(0.0f , 0.0f, 0.0f), // and looks at the origin
                            glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
                       );
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model      = glm::mat4(1.0f);

projectionMat        = Projection * View * Model;
glUniformMatrix4fv(projection, 1, GL_FALSE, glm::value_ptr(projectionMat));

EDIT 3 编辑3

I can translate it using the Model matrix but it has a gap of 5 pixels around it that I can't get rid of, any help on that would be appreciated but thanks for taken an interest. 我可以使用Model矩阵来翻译它,但是它周围有5个像素的间距,我无法摆脱,对此我们将不胜感激,但是感谢您的关注。

UPDATE 更新

As per request my draw code 根据要求我的绘画代码

glUseProgram(_program); glUseProgram(_program);

glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
SDL_GL_SwapWindow(_window);

glPointSize(1);

glEnableVertexAttribArray(0);

//Insert matrix here

glVertexAttribPointer(0, 3, GL_UNSIGNED_INT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, _dataCount)
glDisableVertexAttribArray(0);

my vbo: 我的vbo:

    glGenBuffers(1, &_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, _vbo);
    glBufferData(GL_ARRAY_BUFFER, _dataCount * 4 * sizeof(unsigned int), NULL, GL_STATIC_DRAW);
    if(_vbo == 0 || glGetError() != GL_NO_ERROR)
    {
        _errorMessage = "VBO COULD NOT BE CREATED";
        error();
    }
    checkCudaErrors(cudaGraphicsGLRegisterBuffer(&vbo, _vbo, cudaGraphicsMapFlagsNone));
    glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);

I'm also having issues with the write as when it converts to floats(for drawing) it loses precision so if I read the value out again it rounds to the nearest factor(0, 256, 512 etc.). 我还遇到写问题,因为当它转换为浮点数(用于绘制)时,它会失去精度,因此,如果我再次读取该值,它会四舍五入到最接近的因子(0、256、512等)。 Is there another way to do it that stores it as unsigned int. 还有另一种方法将其存储为unsigned int。 (I realize this is getting slightly off topic but any help would be appreciated) (我意识到这已经脱离话题了,但是可以提供任何帮助)

问题似乎与cols变量有关,需要反转才能起作用,否则它将不在屏幕上。

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

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