So I am very new to OpenGL out I am playing around and trying to create a rectangle.
GLfloat vVertices[] = { -0.5f, -0.5f, 0.0f, -0.5f, 0.5f, 0.0f, 0.5f, -0.5f,
0.0f,0.5f,0.5f,0.0f};
GLfloat colors[] = {
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f};
glClear(GL_COLOR_BUFFER_BIT);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, colors);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
But when I try to make the top further back than the bottom by increasing the z of 2 and 4....
GLfloat vVertices[] = { -0.5f, -0.5f, 0.0f, -0.5f, 0.5f, 1.0f, 0.5f, -0.5f,
0.0f,0.5f,0.5f,1.0f};
I would expect the top to be larger than the bottom because it would be closer. This isn't what happens, instead it just looks like a regular old square. What am I missing here? From a little research online it looks like I might need to setup a frustum, however, I am not sure how to do that on GLES2. I tried the following...
glDepthRangef(-2.0f,2.0f);
But that didn't help either.
Update
Here is my function to set my projection ortho...
void glOrthof(float left, float right, float bottom, float top, float near, float far){
LOGD("In glOrtho");
float a = 2.0f / (right - left);
float b = 2.0f / (top - bottom);
float c = -2.0f / (far - near);
float tx = - (right + left)/(right - left);
float ty = - (top + bottom)/(top - bottom);
float tz = - (far + near)/(far - near);
float ortho[16] = {
a, 0, 0, 0,
0, b, 0, 0,
0, 0, c, 0,
tx, ty, tz, 1
};
GLint loc1= glGetUniformLocation(programObject, "project");
glUniform1i(loc1, 1.0f);
GLint projectionUniform = glGetUniformLocation(programObject, "Projection");
glUniformMatrix4fv(projectionUniform, 1, 0, &ortho[0]);
LOGD("In finished glOrtho");
}
Vertex Shader...
GLbyte vShaderStr[] =
"attribute vec4 vPosition; \n"
"uniform mat4 Projection; \n"
"uniform vec4 color; \n"
"attribute vec4 inColor; \n"
"varying vec4 fragColor; \n"
"uniform int project; \n"
"void main() \n"
"{ \n"
" if(project == 0){ \n"
" gl_Position = vPosition; \n"
" } \n"
" else{ \n"
" gl_Position = Projection * vPosition; \n"
" } \n"
" fragColor = inColor; \n"
"} \n";
And finally my call
glOrthof(0, widthScreen, heightScreen, 0, 2.0, -2.0);
UPDATE 2
per here I tried the following vertex shader...
GLbyte vShaderStr[] =
"attribute vec4 vPosition; \n"
"uniform mat4 Projection; \n"
"uniform mat4 Projection2; \n"
"uniform vec4 color; \n"
"attribute vec4 inColor; \n"
"varying vec4 fragColor; \n"
"uniform int project; \n"
"void main() \n"
"{ \n"
" if(project == 0){ \n"
" gl_Position = vPosition; \n"
" } \n"
" else{ \n"
" gl_Position = Projection2 * (Projection * vPosition); \n"
" } \n"
" fragColor = inColor; \n"
"} \n";
And the following additional function...
void glFrustumf(float near, float far){
LOGD("In glOrtho");
float angleOfView = 180.0;
float aspectRatio = 1;
// Some calculus before the formula.
float size = near * tanf(degree2Radian(angleOfView) / 2.0);
float left = -size, right = size, bottom = -size / aspectRatio, top = size / aspectRatio;
float a = 2.0f * near / (right - left);
float b = 2.0f *near / (top - bottom);
float c = (-2.0f*far*near) / (far - near);
float tx = - (right + left)/(right - left);
float ty = - (top + bottom)/(top - bottom);
float tz = - -(far + near)/(far - near);
float ortho[16] = {
a, 0, 0, 0,
0, b, 0, 0,
tx, ty, tz, -1,
0, 0, c, 0
};
GLint loc1= glGetUniformLocation(programObject, "project");
glUniform1i(loc1, 1.0f);
GLint projectionUniform = glGetUniformLocation(programObject, "Projection2");
glUniformMatrix4fv(projectionUniform, 1, 0, &ortho[0]);
LOGD("In finished glOrtho");
}
But nothing shows up when I add the new matrix.