1
\$\begingroup\$

I am trying to change the rotation of my object with keyboards, when I move the object in the direction of x, y or z it is okay, it is responding correctly. But when I try to change rotation, it does nothing at all.

void keypress(unsigned char key, int x, int y) {
Matrix R;
HomVector a;
switch(key) {
case 'x':
    cam.position.x -= 0.2f;
    break;
case 'X':
    cam.position.x += 0.2f;
    break;
case 'y':
    cam.position.y -= 0.2f;
    break;
case 'Y':
    cam.position.y += 0.2f;
case 'z':
    cam.position.z -= 0.2f;
    break;
case 'Z':
    cam.position.z += 0.2f;
    break;
case 'i':
    cam.rotation.x -=90.0f;
    R = RotationMatrix(-90, X);
    V = MatMatMul(V, R);
    PV = MatMatMul(P, V);
    break;
case 'I':
    cam.rotation.x += 90.0f;
    R = RotationMatrix(90, X);
    V = MatMatMul(V, R);
    PV = MatMatMul(P, V);
    break;
case 'j':
    cam.rotation.y -= 90.0f;
    R = RotationMatrix(-90, Y);
    V = MatMatMul(V, R);
    PV = MatMatMul(P, V);
    break;
case 'J':
    cam.rotation.y += 90.0f;
    R = RotationMatrix(90, Y);
    V = MatMatMul(V, R);
    PV = MatMatMul(P, V);
    break;
case 'k':
    cam.rotation.z -= 90.0f;
    R = RotationMatrix(-90, Z);
    V = MatMatMul(V, R);
    PV = MatMatMul(P, V);
    break;
case 'K':
    cam.rotation.z += 90.0f;
    R = RotationMatrix(90, Z);
    V = MatMatMul(V, R);
    PV = MatMatMul(P, V);
    break;
//case 'Q':
case 'q':
    exit(0);
    break;
}
glutPostRedisplay();

}

This is the code, my matrix is changing after multiplication, but does not respond to glutPostRedisplay() function. If there is a problem, I cannot see it, if anyone can help, that would be great!

\$\endgroup\$
0
\$\begingroup\$

I can't post comments yet, but.. You said that you're moving the object, but from your code it seems that you're moving the camera (not the object). I see nothing wrong with the key presses though, the only thing that comes to mind is that if your camera happens to be centered on, say, a Cube, with no lights, rotating 90 degrees won't change the image so you could try a smaller value for the rotation.

You're not changing the matrixes when you press x,y and z (like you do for rotations) so you must be changing the matrixes elsewhere. Are you maybe overwriting the rotation matrix with the position somehow? What do you do with the R, V and PV matrixes in the display function?

\$\endgroup\$
0
\$\begingroup\$

my friend helped me to solve the problem, honestly, i still dont know what the problem was in the previous one, but i changed function as

void keypress(unsigned char key, int x, int y) {
Matrix R;
HomVector a;
switch(key) {
...
case 'i':
    cam.rotation.x -=1.0f;
    break;
case 'I':
    cam.rotation.x += 1.0f;
    break;
case 'j':
    cam.rotation.y -= 1.0f;
    break;
case 'J':
    cam.rotation.y += 1.0f;
    break;
...
case 'q':
    exit(0);
    break;
}
glutPostRedisplay();

}

After that i changed my display function as, i guess i should make changes in display function for updating display, otherwise i dont get any response.

void display(void) {
Mesh *mesh;

glClear(GL_COLOR_BUFFER_BIT);   

....

Matrix R = RotationMatrix(cam.rotation.x, X);
V = MatMatMul(V, R);
R = RotationMatrix(cam.rotation.y, Y);
V = MatMatMul(V,R);
R = RotationMatrix(cam.rotation.z, Z);
V = MatMatMul(V,R);

//P is projection transform matrix, V is view transform matrix
PV = MatMatMul(P, V); 

glUseProgram(shprg);

// render all meshes in the scene
mesh = meshList;

while (mesh != NULL) {
    renderMesh(mesh);
    mesh = mesh->next;
}

glFlush();

}

Thanks for comment, by the way!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.