c++ - OpenGL: Perspective matrix shows nothing -
i doing basic opengl qt in order try , simple geometry render. i've created cube between (-1,-1,-1)
, (1,1,1)
, rotated overall shape recognisable, , can see if use orthographic projection matrix:
qmatrix4x4 orthographicmatrix(float top, float bottom, float left, float right, float near, float far) { return qmatrix4x4((2.0f)/(right-left), 0, 0, -(right+left)/(right-left), 0, (2.0f)/(top-bottom), 0, -(top+bottom)/(top-bottom), 0, 0, -(2.0f)/(far-near), -(far+near)/(far-near), 0, 0, 0, 1); } ... qmatrix4x4 projection = orthographicmatrix(2, -2, 2, -2, 0.01f, 2);
the formula generating orthographic/projection matrices taken https://solarianprogrammer.com/2013/05/22/opengl-101-matrices-projection-view-model/
this matrix allows me see cube if translate camera 1 on z:
however, if use perspective matrix (again using formula linked page):
qmatrix4x4 perspectivematrix(float fov, float aspectratio, float near, float far) { float top = near * qtan((m_pi/180.0f) * (fov/2.0f)); float bottom = -top; float right = top * aspectratio; float left = -right; return qmatrix4x4((2.0f-near)/(right-left), 0, (right+left)/(right-left), 0, 0, (2.0f-near)/(top-bottom), (top+bottom)/(top-bottom), 0, 0, 0, -(far+near)/(far-near), -(2.0f-far-near)/(far-near), 0, 0, -1, 0); } ... qmatrix4x4 projection = perspectivematrix(60.0f, (float)width()/(float)height(), 0.01f, 2);
i no geometry displaying @ same camera position:
i've checked code many times , can't work out why nothing shows up. gather must perspective matrix code seems follow specified on solarian programmer page. wrong here?
for reference, shaders are:
// vertex #version 330 core in vec3 vertexposition_modelspace; uniform mat4 mvp; void main() { vec4 v = vec4(vertexposition_modelspace,1); gl_position = mvp * v; } // fragment #version 330 core out vec3 color; float remap(float inp) { while (inp>200.0) inp-=200.0; return inp/200.0; } void main() { color = vec3(remap(gl_fragcoord.x), remap(gl_fragcoord.y), 0); }
it seems made mistakes implementing perspective matrix reference.
for example, first entry (2.0f-near)/(right-left)
in code while (2.0f * near)/(right-left)
on website. similar errors in other fields of matrix.
Comments
Post a Comment