0
votes

I'm very new to openGL and I am doing a mini project where I experiment with the depth buffer. 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

With some fiddling I found that cols had to be negative for some strange reason before it would display. 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

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.

UPDATE

As per request my draw code

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:

    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.). Is there another way to do it that stores it as unsigned int. (I realize this is getting slightly off topic but any help would be appreciated)

1
Which OpenGL? A programmable pipeline (2.0 on the desktop or ES; any web GL) or the old, fixed pipeline? Are you using things like glPushMatrix? Also: all coordinates you supply are going to be floats regardless, it's just a question of range.Tommy
No I'm using vertex shader and glm for the matrixDast 99
My Depth coordinate is between 0 and maximum unsigned int for this project so I want to know how to get this to draw, the only way I could do it in the past was to change it to floats between -1 and 1, but this is inconvenient and time consuming as I want to be able to draw millions of points.Dast 99
@Dast99: It would greatly help if you'd added source code that shows how you're currently drawing your stuff. Just showing the code for setting the transformation matrix uniforms hardly helps.datenwolf
Sorry I can't do it the moment I will as soon as I can, basically I'm mapping the vbo to client side(cuda actually) setting the values then using glDrawArrays(GL_POINTS,...) to draw them to the screen.Dast 99

1 Answers

0
votes

The issue appeared to be with the cols variable, it needed to be inverted to work otherwise it was off the screen.