Hi,
I’m trying to make a camera movement that move across x,y,z coordinates.
The problem I have right now is, I can’t make the z-coordinate working as expected, it doesn’t get smaller or bigger even though I’ve set the perspective (using esPerspective). Instead, my object will disappear slowly or it looks like some of the parts being eaten by darkness :shock: (since my background is black).
I’m using esUtil library for the Init, Draw, and Keyboard event callback.
Here’s my keyboard event codes:
// keyboard event
void keyboardEvent ( ESContext *esContext, unsigned char key, int x, int y)
{
UserData *userData = (UserData*) esContext->userData;
switch(key)
{
// =================== TRANSLATION ==============
case 'a': // x-coord Left
userData->x -= 0.01;
matrixTranslation(userData->wvMatrix, userData->x, userData->y, userData->z);
break;
case 'd': // x-coord Right
userData->x += 0.01;
matrixTranslation(userData->wvMatrix, userData->x, userData->y, userData->z);
break;
case 'w': // y-coord Up
userData->y += 0.01;
matrixTranslation(userData->wvMatrix, userData->x, userData->y, userData->z);
break;
case 's': // y-coord Down
//camY -= 1;
userData->y -= 0.01;
matrixTranslation(userData->wvMatrix, userData->x, userData->y, userData->z);
break;
case '[': // z-coord Further Away
userData->z -= 0.01;
matrixTranslation(userData->wvMatrix, userData->x, userData->y, userData->z);
break;
case ']': // z-coord Going into
myZ += 0.01;
matrixTranslation(userData->wvMatrix, userData->x, userData->y, userData->z);
break;
default: break;
};
matrixMultiply(userData->worldviewproj, userData->wvMatrix, userData->projection);
}
And this is my matrixTranslation code:
// Translation
void matrixTranslation(GLfloat (&result)[16], GLfloat tx, GLfloat ty, GLfloat tz)
{
result[0] = result[5] = result[10] = result[15] = 1.0f;
result[1] = result[2] = result[3] = result[4] = 0.0f;
result[6] = result[7] = result[8] = result[9] = 0.0f;
result[11] = 0.0f;
result[12] = tx;
result[13] = ty;
result[14] = tz;
}
And this is my Draw function:
void Draw ( ESContext *esContext )
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
UserData *userData = (UserData*) esContext->userData;
glUseProgram(userData->programObject);
// Load the vertices position & enable the attribute
glVertexAttribPointer(userData->a_posLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), userData->m_vertices);
glEnableVertexAttribArray(userData->a_posLoc);
// Load the texture coordinate & enable the attribute
glVertexAttribPointer(userData->texCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &userData->m_vertices[3]);
glEnableVertexAttribArray(userData->texCoordLoc);
// Bind texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, userData->textureId);
glUniform1i(userData->samplerLoc, 0);
glUniformMatrix4fv(userData->u_mvpLoc, 1, GL_FALSE, (const float*)&userData->worldviewproj);
glDrawElements(GL_TRIANGLES, userData->m_indicesSize , GL_UNSIGNED_INT, userData->m_indices);
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
}
I called the esPerspective in the Initialization, like this:
userData->x = 0.0;
userData->y = 0.0;
userData->z = 0.01f;
// Set to identity values
matrixIdentity(userData->worldviewproj);
matrixIdentity(userData->wvMatrix);
matrixIdentity(userData->projection);
esPerspective((ESMatrix*)&userData->projection, 45.0f, aspect, userData->z, 100.0f);
The x and y movement can work properly with this value initialization, but the z will produce a weird result that I don’t know how to solve. I’ve been changing the position of esPerspective several times, changing the values but unsuccessful.
And I’ve another question:
Is the value of nearZ and farZ must be positive? Because when I set my values to negative, the x and y will still work properly, though the z still problematic.
But, if I set my userData->z to any value > 0.01, when I move my X or Y coordinates, the object will be gone and it won’t return.
I assume it went somewhere behind the camera itself, but I don’t understand why, since I didn’t press any key that change the z-value.
Is there anything else that I need to enable beside this:
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glCullFace(GL_FRONT);
Can someone help? I’ve been trying to find the mistake for the past 5 days and it’s driving me crazy. :x