2014-04-10 6 views
-1

코드가 있지만 매트릭스 방향이 내 목적에 어필하지 않습니다. 누군가 방향을 변환하는 방법을 가르쳐 줄 수 있습니다. 현재 XZY로 설정되어 있지만 반영하고 싶습니다. XYZ, 할 수있는 일을 강조 해주십시오.xzy, xyz 방향 변환

내가 vertex3f 할 때 (100, 100, 10); 예를 들어, 10 값은 내 그리드의 Z 값을 반영해야합니다.

#include <stdlib.h> 
#include <math.h> 
#include <stdio.h> 
#include <Windows.h> 
#include <glut.h> 
#include <iostream> 

using namespace std; 

const float sensitivity = 0.005; 
const float walk_speed = 0.5; 

float cam_pos[3] = { 100.5, 10.0f, 50 }; 
float cam_view[3] = { -1.0f, 0.0f, 1.0f }; 

static int old_x, old_y, half_width, half_height; 

int width = 1024, height = 768; 

void updateKeys() 
{ 
    if (GetAsyncKeyState('W')){ 
     cam_pos[0] += cam_view[0] * walk_speed; 
     cam_pos[1] += cam_view[1] * walk_speed; 
     cam_pos[2] += cam_view[2] * walk_speed; 
    } 
    if (GetAsyncKeyState('S')){ 
     cam_pos[0] -= cam_view[0] * walk_speed; 
     cam_pos[1] -= cam_view[1] * walk_speed; 
     cam_pos[2] -= cam_view[2] * walk_speed; 
    } 
    if (GetAsyncKeyState('A')){ 
     cam_pos[0] += cam_view[2] * walk_speed; 

     cam_pos[2] -= cam_view[0] * walk_speed; 
    } 
    if (GetAsyncKeyState('D')){ 
     cam_pos[0] -= cam_view[2] * walk_speed; 

     cam_pos[2] += cam_view[0] * walk_speed; 
    } 

    if (GetAsyncKeyState(VK_SPACE)){ 
     cam_pos[1] += walk_speed; 
    } 
} 


void renderScene(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    //3d camera 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    gluLookAt(
     cam_pos[0], cam_pos[1], cam_pos[2], 
     cam_pos[0] + cam_view[0], cam_pos[1] + cam_view[1], cam_pos[2] + cam_view[2], 
     0.0f, 1.0f, 0.0f); 

    //render grid 
    glBegin(GL_LINES); 
    for (int i = 0; i <= 100; i++) { 
     if (i == 0) { glColor3f(.6, .3, .3); } 
     else { glColor3f(.25, .25, .25); }; 
     glVertex3f(i, 0, 0); 
     glVertex3f(i, 0, 100); 
     if (i == 0) { glColor3f(.3, .3, .6); } 
     else { glColor3f(.25, .25, .25); }; 
     glVertex3f(0, 0, i); 
     glVertex3f(100, 0, i); 
    }; 
    glEnd(); 

    glEnable(GL_POINT_SMOOTH); 
    glPointSize(50.0f); 
    glColor3f(1, 0, 0); 
    glBegin(GL_POINTS); 
    glVertex3f(0, 0, 0); 


    //X, Z, Y 
    glVertex3f(10, -10, 10); 
    glEnd(); 

    updateKeys(); 
    glutSwapBuffers(); 
} 

void normalize(float *v) 
{ 
    float magnitude = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); 
    v[0] /= magnitude; 
    v[1] /= magnitude; 
    v[2] /= magnitude; 
} 

void rotate_view(float *view, float angle, float x, float y, float z) 
{ 
    float new_x; 
    float new_y; 
    float new_z; 

    float c = cos(angle); 
    float s = sin(angle); 

    new_x = (x*x*(1 - c) + c)  * view[0]; 
    new_x += (x*y*(1 - c) - z*s) * view[1]; 
    new_x += (x*z*(1 - c) + y*s) * view[2]; 

    new_y = (y*x*(1 - c) + z*s)  * view[0]; 
    new_y += (y*y*(1 - c) + c)  * view[1]; 
    new_y += (y*z*(1 - c) - x*s) * view[2]; 

    new_z = (x*z*(1 - c) - y*s)  * view[0]; 
    new_z += (y*z*(1 - c) + x*s) * view[1]; 
    new_z += (z*z*(1 - c) + c)  * view[2]; 

    view[0] = new_x; 
    view[1] = new_y; 
    view[2] = new_z; 

    normalize(view); 
} 

void motion(int x, int y) 
{ 
    float rot_x, rot_y; 
    float rot_axis[3]; 

    x -= half_width; 
    y -= half_height; 

    rot_x = -(float)(x - old_x) * sensitivity; 
    rot_y = -(float)(y - old_y) * sensitivity; 

    old_x = x; 
    old_y = y; 

    rotate_view(cam_view, rot_x, 0.0f, 1.0f, 0.0f); 

    rot_axis[0] = -cam_view[2]; 
    rot_axis[1] = 0.0f; 
    rot_axis[2] = cam_view[0]; 

    normalize(rot_axis); 

    rotate_view(cam_view, rot_y, rot_axis[0], rot_axis[1], rot_axis[2]); 
} 

void mouse(int button, int state, int x, int y) 
{ 
    old_x = x - half_width; 
    old_y = y - half_height; 

    glutPostRedisplay(); 
} 

void idle() 
{ 
    glutPostRedisplay(); 
} 

void keys(unsigned char c, int x, int y) 
{ 
    glutPostRedisplay(); 


    cout << "camera view: :" << cam_view[0] << "," << cam_view[1] << "," << cam_view[2] << endl; 
} 

void reshape(int w, int h) 
{ 
    width = w; 
    height = h; 

    half_height = w/2; 
    half_width = h/2; 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(45.0, (double)w/(double)h, 1.0, 10000.0); 

    glViewport(0, 0, w, h); 
} 


//---------------------------------------------------------------------- 
// Main program 
//---------------------------------------------------------------------- 
int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); 
    glutInitWindowPosition(100, 100); 
    glutInitWindowSize(width, height); 
    glutCreateWindow("OpenGL"); 

    glutDisplayFunc(renderScene); 
    glutKeyboardFunc(keys); 
    glutReshapeFunc(reshape); 
    glutMouseFunc(mouse); 
    glutMotionFunc(motion); 
    glutIdleFunc(idle); 


    // OpenGL init 
    glEnable(GL_DEPTH_TEST); 

    // enter GLUT event processing cycle 
    glutMainLoop(); 

    return 0; // this is just to keep the compiler happy 
} 
+0

"z"값을 지정해주십시오. 마찬가지로, 그것은 위 또는 아래 축입니까? – Vallentin

+0

아니 내 경우 Y는 위아래 축이며, 예를 들어 vertex3 (1,2,3)을 할 때 위아래 축을 반영하기 위해 3이 필요하지만 대신 y가 그것을 수행합니다. – Dean

+0

찾았습니다. 문제를 이해하는 데 200 줄 이상의 코드가 필요하다고 생각하기가 어렵습니다. [최소 예제] (http://sscce.org)로 줄이기 위해 노력해주십시오. –

답변

1

가 그 값을 "재 할당"변환 행렬을 사용

여기 내 코드입니다. 평소와 같이 modelview에서 해당 행렬을 푸시 할 수 있습니다.

단위 행렬은 다음과 같습니다

(1, 0, 0; 0, 1, 0; 0, 0, 1) 

귀하의 행렬은 다음과 같습니다

(1, 0, 0; 0, 0, 1; 0, 1, 0) 

난 당신이 차이를 발견 할 수 같아요. 그에 따라 동질적인 좌표에 맞게 4D 행렬까지 확장 할 수 있습니다.