zyneragetro
zyneragetro

Reputation: 149

OpenGL - incorrect drawing of 2D overlay on top of 3D Scene

I am trying to draw a 2D overlay on top of a 3D model where it must appear on the left corner of the screen but what I get is a the rectangle which I am trying to draw is placed in the middle of the 3D model. You can see below for the screenshot of my output. enter image description here

My setup for the orthogonal matrix is like this

2 / (right - left), 0, 0, 0,
0, 2 / (top - bottom), 0, 0,
0, 0, 2 / (far - near), 0,
-(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near), 1

For the steps on how do I draw a 2D overlay on top of 3D, I followed this answer. Also it looks like the rectangle is not an overlay at all based on the output. Hope you can help me :)

EDIT :

This is how I draw the model. Notice that this block of code will be used twice to draw 2 models

GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, cc * sizeof(GLfloat), v, GL_STATIC_DRAW);

GLuint tcbuffer;
glGenBuffers(1, &tcbuffer);
glBindBuffer(GL_ARRAY_BUFFER, tcbuffer);
glBufferData(GL_ARRAY_BUFFER, tcc * sizeof(GLfloat), vt, GL_STATIC_DRAW);

GLuint ncbuffer;
glGenBuffers(1, &ncbuffer);
glBindBuffer(GL_ARRAY_BUFFER, ncbuffer);
glBufferData(GL_ARRAY_BUFFER, ncc * sizeof(GLfloat), vn, GL_STATIC_DRAW);

glEnableVertexAttribArray(m_vert);
glBindBuffer(GL_ARRAY_BUFFER, vbufferid);
glVertexAttribPointer(m_vert, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(m_texcoord);
glBindBuffer(GL_ARRAY_BUFFER, tcbufferid);
glVertexAttribPointer(m_texcoord, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(m_color);
glBindBuffer(GL_ARRAY_BUFFER, cbufferid);
glVertexAttribPointer(m_color, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(m_vertexnormal);
glBindBuffer(GL_ARRAY_BUFFER, ncbufferid);
glVertexAttribPointer(m_vertexnormal, 3, GL_FLOAT, GL_FALSE, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, vc);

And for the setup of the projections.

Matrix44 fov_matrix(double fov, double aspect, double near, double far) {
    var yScale = 1.0 / Math.tan(fov / 2);
    var xScale = yScale / aspect;
    return(Matrix44.for_values(
        xScale, 0, 0, 0,
        0, yScale, 0, 0,
        0, 0, (far + near) / (near - far), -1,
        0, 0, 2 * far * near / (near - far), 0
    ));
}

Matrix44 orthographic_matrix(double left, double right, double bottom, double top, double near, double far) {
    return(Matrix44.for_values(
        2 / (right - left), 0, 0, 0,
        0, 2 / (top - bottom), 0, 0,
        0, 0, 2 / (far - near), 0,
        -(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near), 1
    ));
}

var projm = fov_matrix(MathConstant.M_PI/4, get_aspect_ratio(), 1.0, 100.0);
var orthoprojm = orthographic_matrix(0.0, 800, 600, 0.0, -100.0, 1.0);

Upvotes: 1

Views: 793

Answers (1)

ivaigult
ivaigult

Reputation: 6687

Your projection matricies looks correct to me. Looks like matrices that were used for Spanch Bob drawing were used for your overlay too. Check:

  1. Both shaders for overlay are compiled. glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
  2. Overlay program is properly linked. glGetProgramiv(program, GL_LINK_STATUS, &status);
  3. Check that you are using correct program. glGetIntegeriv(GL_CURRENT_PROGRAM, &program)
  4. glGetUniformLocation(program, "yourMVPmatrix") returns correct id.

Note, that if any program was not properly linked OpenGL won't race error on any of glDraw*.

Upvotes: 2

Related Questions