行动自由

时间:2013-12-01 18:05:47

标签: opengl game-physics glulookat

我在OpenGL中使用鼠标自由移动有点困惑,我想要的是拥有3D模型制作程序的控件。我正在使用Windows。

这是我的视口结构,非常标准:

struct viewport {
    float x;
    float y;
    float z;
    float angle[2];
};
struct viewport viewport;

我的gluLook致电:

glLoadIdentity();
gluLookAt(viewport.x, viewport.y, viewport.z, viewport.x + sin(viewport.angle[0] * M_PI / 180.0f), viewport.y + viewport.angle[1] * M_PI / 180.0f, viewport.z - cos(viewport.angle[0] * M_PI / 180.0f), 0.0f, 1.0f,  0.0f);

我的WM_MOVE,当用户按住鼠标右键时旋转场景:

case WM_MOUSEMOVE:
    if(GetAsyncKeyState(VK_RBUTTON)) {
        viewport.angle[0] -= (GET_X_LPARAM(lParam) - lastMouseX) / 16.0f;
        viewport.angle[1] += (GET_Y_LPARAM(lParam) - lastMouseY) / 16.0f;
    }
    lastMouseX = GET_X_LPARAM(lParam);
    lastMouseY = GET_Y_LPARAM(lParam);
    return 0;

我的WM_MOUSEWHEEL,放大和缩小场景:

case WM_MOUSEWHEEL:
    if(GET_WHEEL_DELTA_WPARAM(wParam) > 0) {
        viewport.x += sin(GET_WHEEL_DELTA_WPARAM(wParam) / 120.0f);
        viewport.y -= cos(GET_WHEEL_DELTA_WPARAM(wParam) / 120.0f);
    }
    else {
        viewport.x += sin(GET_WHEEL_DELTA_WPARAM(wParam) / 120.0f);
        viewport.y -= cos(GET_WHEEL_DELTA_WPARAM(wParam) / 120.0f);
    }
    return 0;

缩放代码确实不起作用,我认为我在Y轴上旋转的代码是错误的,看起来很奇怪。

我能够让缩放大部分工作,Y和X和Z没有链接,但它有点像。我这次发布了我的完整main.c,所以你可以看到更多的代码:

#include "main.h"
#include <windowsx.h>
#include <math.h>

#define argc    __argc
#define argv    __argv

#define GRID_SIZE   50

short quit = 0;
short dead = 0;

HWND hWnd;
HDC hDC;
HGLRC hRC;

struct viewport {
    float x;
    float y[2];
    float z;
    float angle;
};
struct viewport viewport = { GRID_SIZE / 2.0f, { 1.0f, 1.0f }, GRID_SIZE / 2.0f, 180.0f };

float lastMouseX, lastMouseY = 0;

void die(void) {
    if(!dead) {
        quit = 1;
        dead = 1;
        deinitText();
        deinitOpenGL();
        DestroyWindow(hWnd);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) {
    if(argc != 2) {
        return 0;
    }



    WNDCLASS wc;
    MSG msg;

    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "ObjectViewerC";
    if(RegisterClass(&wc) == 0) {
        die();
    }

    hWnd = CreateWindow("ObjectViewerC", argv[1], WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 0, 0, 512, 512, NULL, NULL, hInstance, NULL);

    if(hWnd == NULL) {
        die();
    }

    if(!initOpenGL()) {
        die();
    }
    if(!initExtensions()) {
        die();
    }

    if(!initTimer()) {
    }

    float frameStartTime = 0.0f;
    float frameEndTime = 0.0f;

    initText();

    lookNice();
    set3D();

    while(!quit) {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            if(msg.message == WM_QUIT) {
                quit = 1;
            }
            else {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
        else {
            frameStartTime = getTime();

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            glLoadIdentity();
            gluLookAt(viewport.x, viewport.y[0], viewport.z, viewport.x + sin(viewport.angle * M_PI / 180.0f), viewport.y[1], viewport.z - cos(viewport.angle * M_PI / 180.0f), 0.0f, 1.0f,  0.0f);

            glColor3f(1, 1, 1);
            glBegin(GL_LINES);
            int i = 0;
            for(i = 0; i <= GRID_SIZE; i++) {
                glVertex3f(i, 0, 0);
                glVertex3f(i, 0, GRID_SIZE);
                glVertex3f(0, 0, i);
                glVertex3f(GRID_SIZE, 0, i);
            };
            glEnd();

            SwapBuffers(hDC);

            do {
                frameEndTime = getTime();
            } while(frameEndTime < frameStartTime + framerate);
        }
    }

    die();
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch(message) {
        case WM_CREATE:
            return 0;

        case WM_CLOSE:
            PostQuitMessage(0);
            return 0;

        case WM_DESTROY:
            die();
            return 0;

        case WM_KEYDOWN:
            switch(wParam) {
                case VK_ESCAPE:
                    PostQuitMessage(0);
                    return 0;
            }
            return 0;

        case WM_LBUTTONDOWN:
            return 0;

        case WM_LBUTTONUP:
            return 0;

        case WM_MOUSEMOVE:
            if(GetAsyncKeyState(VK_RBUTTON)) {
                viewport.angle -= (GET_X_LPARAM(lParam) - lastMouseX) / 11.2f;
                viewport.y[1] += ((GET_Y_LPARAM(lParam) - lastMouseY) / 580.0f);
            }
            lastMouseX = GET_X_LPARAM(lParam);
            lastMouseY = GET_Y_LPARAM(lParam);
            return 0;

        case WM_MOUSEWHEEL:
            if(GET_WHEEL_DELTA_WPARAM(wParam) > 0) {
                viewport.x += sin(viewport.angle * M_PI / 180.0f) / 15.0f;
                viewport.z -= cos(viewport.angle * M_PI / 180.0f) / 15.0f;
                viewport.y[0] -= sin(viewport.y[0] - viewport.y[1]) / 15.0f;
                viewport.y[1] -= sin(viewport.y[0] - viewport.y[1]) / 15.0f;
            }
            else {
                viewport.x -= sin(viewport.angle * M_PI / 180.0f) / 15.0f;
                viewport.z += cos(viewport.angle * M_PI / 180.0f) / 15.0f;
                viewport.y[0] += sin(viewport.y[0] - viewport.y[1]) / 15.0f;
                viewport.y[1] += sin(viewport.y[0] - viewport.y[1]) / 15.0f;
            }
            return 0;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);        
    }
}

1 个答案:

答案 0 :(得分:1)

对于y旋转:我认为你应该为这些参数使用与viewport.xyz不同的向量。

如您所见,gluLookAt的参数是:

void gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ,
                  GLdouble centerX, GLdouble centerY, GLdouble centerZ,
                  GLdouble upX, GLdouble upY, GLdouble upZ);

所以参数4到6定义了你在处看的点。见图:

http://i.stack.imgur.com/fUBKD.gif

因此,如果你想环顾四周,你必须调整 at 向量,而不是 eye 向量。为此,请尝试以下方法:

  1. eye 之间的矢量(at-eye)
  2. 围绕y轴转动该向量(在网络上查找向量旋转)
  3. 将矢量添加到 eye =&gt;你有 at vector
  4. 对于滚动,我想你想要的是将眼睛向量移动到相机正在看的方向。所以像eye + = normalized(at-eye)* factor。