现在的位置: 首页 > 综合 > 正文

OpenGL鼠标旋转物体

2018年02月15日 ⁄ 综合 ⁄ 共 2878字 ⁄ 字号 评论关闭

测试发现,还是NEHE的鼠标旋转物体效果好啊,但NEHE是在基于Windows 的程序框架下编写的,不少代码融入到了windows编程中,感觉比较混乱,所以就想提取出来,在基于控制台的框架下实现出来,在此框架下代码结构比较简单,便于以后再使用这些代码,也就是便于代码复用。主程序代码如下

#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>
#include "math.h"
#include "ArcBall.h"

// object
GLfloat vertices[] = {-1.0,-1.0,-1.0,1.0,-1.0,-1.0,
1.0,1.0,-1.0, -1.0,1.0,-1.0, -1.0,-1.0,1.0,
1.0,-1.0,1.0, 1.0,1.0,1.0, -1.0,1.0,1.0};

GLfloat colors[] = {0.0,0.0,0.0,1.0,0.0,0.0,
1.0,1.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0,
1.0,0.0,1.0, 1.0,1.0,1.0, 0.0,1.0,1.0};

GLubyte cubeIndices[]={0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};

// mouse control
Matrix4fT   Transform   = {  1.0f,  0.0f,  0.0f,  0.0f,                // NEW: Final Transform
0.0f,  1.0f,  0.0f,  0.0f,
0.0f,  0.0f,  1.0f,  0.0f,
0.0f,  0.0f,  0.0f,  1.0f };

Matrix3fT   LastRot     = {  1.0f,  0.0f,  0.0f,                    // NEW: Last Rotation
0.0f,  1.0f,  0.0f,
0.0f,  0.0f,  1.0f };

Matrix3fT   ThisRot     = {  1.0f,  0.0f,  0.0f,                    // NEW: This Rotation
0.0f,  1.0f,  0.0f,
0.0f,  0.0f,  1.0f };

ArcBallT    ArcBall(640.0f, 480.0f);                                // NEW: ArcBall
Instance
Point2fT    MousePt;                                               
// NEW: Current Mouse Point
bool        isClicked  = false;                                       
// NEW: Clicking The Mouse?
bool        isDragging = false;                                       
// NEW: Dragging The Mouse?

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

    glLoadIdentity();

    glPushMatrix();                                                   
// NEW: Prepare Dynamic Transform
    glMultMatrixf(Transform.M);                                       
// NEW: Apply Dynamic Transform
    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);
    glPopMatrix();   

    glutSwapBuffers();
}

void mouse(int btn, int state, int x, int y)
{
    if(btn==GLUT_LEFT_BUTTON)
    {
        switch(state)
        {
        case GLUT_DOWN:           
            isClicked = true;
            printf_s("startmotion: x = %d, y = %d\n",x,y);
            break;
        case GLUT_UP:
            isClicked = false;
            isDragging = false;
            printf_s("stopmotion: x = %d, y = %d\n",x,y);
            break;
        }
    }

    if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    {
        Matrix3fSetIdentity(&LastRot);                               
// Reset Rotation
        Matrix3fSetIdentity(&ThisRot);                               
// Reset Rotation
        Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot);        // Reset Rotation

        glutPostRedisplay();
    }
}

void mouseMotion(int x, int y)
{
    MousePt.s.X = x;
    MousePt.s.Y = y;   

    if (!isDragging)                                               
// Not Dragging
    {
        if (isClicked)                                               
// First Click
        {
            isDragging = true;                                       
// Prepare For Dragging
            LastRot = ThisRot;                                       
// Set Last Static Rotation To Last Dynamic One           
            ArcBall.click(&MousePt);             

抱歉!评论已关闭.