如何在“相机”始终瞄准对象的 OpenGL 中通过鼠标拖动移动“相机”?

问题描述

我是 OpenGL 的新手,GL、glugluT 似乎已经弃用了很长时间,但有一个家庭作业,我正在弄清楚如何将相机移动到不同的角度,但相机将始终指向对象。

我已经通过互联网和 stackoverflow 进行了搜索,但似乎所有人都在使用 glutMainLoop(); 但就我而言,我在 while() 中使用了 WinMain 循环,其中循环继续呼叫 display()。所以我正在尝试一些我从互联网上获得的随机代码,它在 glutMouseFunc(mouse) 之前调用 glutMainLoop();,我认为它与我的 while() 循环类似,但它不起作用.

有人能解释一下原因吗?

#include <Windows.h>
#include <gl/GL.h>
#include <gl/glu.h>
#include <gl/gluT.h>

#define WINDOW_TITLE "OpenGL Window"


LRESULT WINAPI WindowProcedure(HWND hWnd,UINT msg,WParaM wParam,LParaM lParam)
{
    switch (msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    case WM_KEYDOWN:
        if (wParam == VK_ESCAPE) PostQuitMessage(0);
        break;

    default:
        break;
    }

    return DefWindowProc(hWnd,msg,wParam,lParam);
}
//--------------------------------------------------------------------

bool initPixelFormat(HDC hdc)
{
    PIXELFORMATDESCRIPTOR pfd;
    ZeroMemory(&pfd,sizeof(PIXELFORMATDESCRIPTOR));

    pfd.cAlphaBits = 8;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 24;
    pfd.cStencilBits = 0;

    pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;

    pfd.iLayerType = PFD_MAIN_PLANE;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;

    // choose pixel format returns the number most similar pixel format available
    int n = ChoosePixelFormat(hdc,&pfd);

    // set pixel format returns whether it sucessfully set the pixel format
    if (SetPixelFormat(hdc,n,&pfd))
    {
        return true;
    }
    else
    {
        return false;
    }
}
//--------------------------------------------------------------------
void spindisplay(void)
{
    spin = spin + 2.0;
    if (spin > 360.0)
        spin = spin - 360.0;
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    switch (button) {
    case gluT_LEFT_BUTTON:
        if (state == gluT_DOWN)
            glutIdleFunc(spindisplay);
        break;
    case gluT_MIDDLE_BUTTON:
        if (state == gluT_DOWN)
            glutIdleFunc(NULL);
        break;
    default:
        break;
    }
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    glColor3f(1,0);
    glutSolidCube(0.2);
    
}
//--------------------------------------------------------------------

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int nCmdshow)
{
    WNDCLASSEX wc;
    ZeroMemory(&wc,sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.hInstance = GetModuleHandle(NULL);
    wc.lpfnWndProc = WindowProcedure;
    wc.lpszClassName = WINDOW_TITLE;
    wc.style = CS_HREDRAW | CS_VREDRAW;

    if (!RegisterClassEx(&wc)) return false;

    HWND hWnd = CreateWindow(WINDOW_TITLE,WINDOW_TITLE,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,800,600,NULL,wc.hInstance,NULL);

    //--------------------------------
    //  Initialize window for OpenGL
    //--------------------------------

    HDC hdc = GetDC(hWnd);

    //  initialize pixel format for the window
    initPixelFormat(hdc);

    //  get an openGL context
    HglrC hglrc = wglCreateContext(hdc);

    //  make context current
    if (!wglMakeCurrent(hdc,hglrc)) return false;

    //--------------------------------
    //  End initialization
    //--------------------------------

    ShowWindow(hWnd,nCmdshow);

    MSG msg;
    ZeroMemory(&msg,sizeof(msg));

    while (true)
    {
        if (PeekMessage(&msg,PM_REMOVE))
        {
            if (msg.message == WM_QUIT) break;

            TranslateMessage(&msg);
            dispatchMessage(&msg);
        }
        glutMouseFunc(mouse);
        display();

        SwapBuffers(hdc);
    }

    UnregisterClass(WINDOW_TITLE,wc.hInstance);

    return true;
}
//--------------------------------------------------------------------

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...