So I have the following...
float s = 0.5f;
void renderFrameLine() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat vVertices[] = { s, s, 0.0f, s, -s, 0.0f, -s, s,
0.0f};
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
This works great so now I want to add depth so I get ready by changing my vertex shader as following...
GLbyte vShaderStr[] =
"attribute vec4 vPosition; \n"
"uniform mat4 Projection; \n"
"void main() \n"
"{ \n"
" gl_Position = Projection * vPosition; \n"
"} \n";
And based on this code in GLM...
template <typename valType>
GLM_FUNC_QUALIFIER detail::tmat4x4<valType> perspectiveFov
(
valType const & fov,
valType const & width,
valType const & height,
valType const & zNear,
valType const & zFar
)
{
#ifdef GLM_FORCE_RADIANS
valType rad = fov;
#else
valType rad = glm::radians(fov);
#endif
valType h = glm::cos(valType(0.5) * rad) / glm::sin(valType(0.5) * rad);
valType w = h * height / width; ///todo max(width , Height) / min(width , Height)?
detail::tmat4x4<valType> Result(valType(0));
Result[0][0] = w;
Result[1][1] = h;
Result[2][2] = - (zFar + zNear) / (zFar - zNear);
Result[2][3] = - valType(1);
Result[3][2] = - (valType(2) * zFar * zNear) / (zFar - zNear);
return Result;
}
I create the following function to create a perspective matrix...
void glFrustumf(float near, float far){
float aspectRatio = .5;
float DEG2RAD = 3.14159f / 180.0f;
float fov = 90*DEG2RAD;
float h = cosf(0.5f*fov)/sinf(0.5f*fov);
float w = h * aspectRatio;
float a = - (near+far)/(near - far);
float b = - ((2*far*near)/(far-near));
float proj[16] = {
w, 0, 0, 0,
0, h, 0, 0,
0, 0, a, 1,
0, 0, b, 0
};
GLint projectionUniform = glGetUniformLocation(programObject, "Projection");
glUniformMatrix4fv(projectionUniform, 1, 0, &proj[0]);
}
This doesn't seem to work and instead returns a blank screen. Can anyone tell me what I am doing wrong?
Update
I also tried
void glFrustumf(float near, float far, float left, float right, float bottom, float top){
float deltaX = right - left;
float deltaY = top - bottom;
float deltaZ = far - near;
float a = 2.0f * near / deltaX;
float b = 2.0f * near / deltaY;
float c = (right + left) / deltaX;
float d = (top + bottom) / deltaY;
float e = -(near + far) / deltaZ;
float f = -2.0f * near * far / deltaZ;
float proj[16] = {
a, 0, 0, 0,
0, b, 0, 0,
c, d, e, -1.0f,
0, 0, f, 0
};
GLint projectionUniform = glGetUniformLocation(programObject, "Projection");
glUniformMatrix4fv(projectionUniform, 1, GL_FALSE, &proj[0]);
}
but no dice...
Per previous comment
Based on this line...
2 nearVal right - left 0 A 0 0 2 nearVal top - bottom B 0 0 0 C D 0 0 -1 0
A = right + left right - left
B = top + bottom top - bottom
C = - farVal + nearVal farVal - nearVal
D = - 2 farVal nearVal farVal - nearVal
I changed to this...
float proj[16] = {
a, 0, c, 0,
0, b, d, 0,
0, 0, e, f,
0, 0, -1, 0
};
But still just black...
Also tried...
void glFrustumf(float near, float far){
float aspectRatio = .5;
float DEG2RAD = 3.14159f / 180.0f;
float fov = 90*DEG2RAD;
float w = fov/aspectRatio;
float a = - (near+far)/(near - far);
float b = - ((2*far*near)/(near - far));
float proj[16] = {
w, 0, 0, 0,
0, fov, 0, 0,
0, 0, a, b,
0, 0, -1, 0
};
GLint projectionUniform = glGetUniformLocation(programObject, "Projection");
glUniformMatrix4fv(projectionUniform, 1, 0, &proj[0]);
}
Also tried ...
void glFrustumf(float near, float far){
float aspectRatio = .5;
float DEG2RAD = 3.14159f / 180.0f;
float fov = 90*DEG2RAD;
float cot = (1 / (tanf(fov)/2));
float cota = cot /aspectRatio;
float a = - (far+near)/(near - far);
float b = - ((2*far*near)/(near-far));
float proj[16] = {
cota, 0, 0, 0,
0, cot, 0, 0,
0, 0, a, b,
0, 0, -1, 0
};
GLint projectionUniform = glGetUniformLocation(programObject, "Projection");
glUniformMatrix4fv(projectionUniform, 1, 0, &proj[0]);
}