close

參考書籍-OpenGL 3D繪圖互動程式設計

 

理論

openGL-(5個座標空間) @ 奇怪的(´・ω・`)增加了的部落格 :: 痞客邦 :: (pixnet.net)  

openGL-(正交投影&透視投影) @ 奇怪的(´・ω・`)增加了的部落格 :: 痞客邦 :: (pixnet.net) 


實作

openGL-視窗建立(詳解) @ 奇怪的(´・ω・`)增加了的部落格 :: 痞客邦 :: (pixnet.net)  下面沒有介紹的這裡有。

openGL-建立茶壺(詳解) @ 奇怪的(´・ω・`)增加了的部落格 :: 痞客邦 :: (pixnet.net)  下面沒有介紹的這裡有。

openGL-建立清單(詳解) @ 奇怪的(´・ω・`)增加了的部落格 :: 痞客邦 :: (pixnet.net) 下面沒有介紹的這裡有。

openGL-旋轉茶壺(詳解) @ 奇怪的(´・ω・`)增加了的部落格 :: 痞客邦 :: (pixnet.net) 下面沒有介紹的這裡有。

openGL-鍵盤改變旋轉茶壺角度(詳解) @ 奇怪的(´・ω・`)增加了的部落格 :: 痞客邦 :: (pixnet.net)  下面沒有介紹的這裡有。

openGL-特殊鍵更改背景顏色(詳解) @ 奇怪的(´・ω・`)增加了的部落格 :: 痞客邦 :: (pixnet.net)  下面沒有介紹的這裡有。

 


按下滑鼠拖移可以旋轉茶壺,但是兩個軸以上的旋轉會怪怪的,放開滑鼠後茶壺會回到原位。

image


#include "GL/freeglut.h"
#include "GL/glut.h"

float clickPt_x;
float clickPt_y;
float dotx;
float doty;
float background=0.0f;
float teapot_size=1.0f;
int MENU_SIZE_1=1;
int MENU_SIZE_2=2;
int MENU_EXIT=3;
int MENU_COLOR_3=4;
int MENU_COLOR_4=5;
int MENU_COLOR_5=6;
int MENU_COLOR_6=7;
int COLOR_R=200;
int COLOR_G=120;
int COLOR_B=0;
int rotateAngle=0;
int timer_interval = 50;
int positionX=0;
int positionY=0;
int positionZ=0;


void My_Display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glRotatef(rotateAngle , positionX, positionY, positionZ);
    glColor3ub(COLOR_R,COLOR_G ,COLOR_B);
    glutWireTeapot(teapot_size);
    glutSwapBuffers();    
    
}

void My_Reshape(int  width, int  height)
{
    int aspect = width * 1.0f / height;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0,0,width, height);
    gluPerspective(60.0f, aspect, 0.1f, 10.0f);
}

void My_Keyboard(unsigned char key, int x, int y)
{
    if(key == 'x')
    {
        positionX=1;
        positionY=0;
        positionZ=0;
    }
    else if(key == 'X')
    {
        positionX=-1;
        positionY=0;
        positionZ=0;
    }
    else if(key == 'y')
    {
        positionX=0;
        positionY=1;
        positionZ=0;
    }
    else if(key == 'Y')
    {
        positionX=0;
        positionY=-1;
        positionZ=0;
    }
    else if(key == 'z')
    {
        positionX=0;
        positionY=0;
        positionZ=1;
    }
    else if(key == 'Z')
    {
        positionX=0;
        positionY=0;
        positionZ=-1;
    }
}

void My_Specialkey(int key, int x, int y)
{
    if(key == GLUT_KEY_F1)
        glClearColor(0.0f, 0.0f,0.0f,1.0f);
    else if(key == GLUT_KEY_F2)
        glClearColor(1.0f, 1.0f,1.0f,1.0f);
    else if(key == GLUT_KEY_F3)
        glClearColor(1.0f, 0.0f,0.0f,1.0f);    
    else if(key == GLUT_KEY_F4)
        glClearColor(0.0f, 1.0f,0.0f,1.0f);
    else if(key == GLUT_KEY_F5)
        glClearColor(0.0f, 0.0f,1.0f,1.0f);
}

void My_Mouse(int button, int state, int x, int y)
{
    if(button == GLUT_LEFT_BUTTON)
    {
        if(state == GLUT_DOWN)
        {
            clickPt_x = x;
            clickPt_y = y;
        }
    }
    positionY=0;
    positionX=0;
    rotateAngle=0;
    
}
void Mouse_Moving(int x, int y)
{
    glutPostRedisplay();
    dotx=x-clickPt_x;
    doty=clickPt_y-y;
    rotateAngle+=1;
    if(dotx >20)
        positionY=1;
    
    if(dotx<-20)
        positionY=-1;
    
    if(doty>20)
        positionX=-1;
    
    if(doty<-20)
        positionX=1;       
    
}

void My_Menu(int id)
{
    if(id == MENU_SIZE_1)
    {
        teapot_size+=1.0f;
    }else if(id == MENU_SIZE_2)
    {
        teapot_size-=1.0f;
    }else if(id == MENU_COLOR_3)
    {
        COLOR_R=255;
        COLOR_G=0;
        COLOR_B=0;
    }else if(id == MENU_COLOR_4)
    {
        COLOR_R=0;
        COLOR_G=0;
        COLOR_B=255;
    }else if(id == MENU_COLOR_5)
    {
        COLOR_R=0;
        COLOR_G=255;
        COLOR_B=0;
    }else if(id == MENU_COLOR_6)
    {
        COLOR_R=255;
        COLOR_G=255;
        COLOR_B=255;
    }else if(id == MENU_EXIT)
    {
        exit(0);
    }
    glutWireTeapot(teapot_size);
}
void My_Timer(int val)
{
    glutPostRedisplay();
    glutTimerFunc(timer_interval, My_Timer, val);
    rotateAngle +=2;
    rotateAngle%=360;
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
        
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(600,600);
    glutCreateWindow("glut");
    glClearColor(background, background,background,1.0f);
    
    
    int menu_main = glutCreateMenu(My_Menu);
    int menu_entry = glutCreateMenu(My_Menu);
    int menu_entry_2 = glutCreateMenu(My_Menu);
    glutSetMenu(menu_main);
    glutAddSubMenu("Teapot size", menu_entry);
    glutAddSubMenu("Teapot color", menu_entry_2);
    glutAddMenuEntry("Exit", MENU_EXIT);
    glutSetMenu(menu_entry);
    
    glutAddMenuEntry("+1.0", MENU_SIZE_1);
    glutAddMenuEntry("-1.0", MENU_SIZE_2);
    
    glutSetMenu(menu_entry_2);
    glutAddMenuEntry("Red", MENU_COLOR_3);
    glutAddMenuEntry("Blue", MENU_COLOR_4);
    glutAddMenuEntry("Green", MENU_COLOR_5);
    glutAddMenuEntry("White", MENU_COLOR_6);
    
    glutSetMenu(menu_main);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutReshapeFunc(My_Reshape);
    glutDisplayFunc(My_Display);
    //glutTimerFunc(timer_interval, My_Timer, 0);
    glutKeyboardFunc(My_Keyboard);
    glutSpecialFunc(My_Specialkey);
    glutMouseFunc(My_Mouse);
    glutMotionFunc(Mouse_Moving);
    
    glutMainLoop();
    return 0;    
}

完整程式碼:(紅色的部分為這次所新增的,其他程式碼可以到上面點選超連結,下面有介紹)

float clickPt_x; //按下滑鼠的位置
float clickPt_y; //按下滑鼠的位置
float dotx; //拖移-按下滑鼠的位置=旋轉方向
float doty;  //拖移-按下滑鼠的位置=旋轉方向

void My_Mouse(int button, int state, int x, int y)
{
    if(button == GLUT_LEFT_BUTTON) //滑鼠左鍵
    {
        if(state == GLUT_DOWN)   //按下
        {
            clickPt_x = x;  //紀錄x的位置
            clickPt_y = y;  //紀錄y的位置
        }
    }
    positionY=0; //茶壺歸位
    positionX=0; //茶壺歸位
    rotateAngle=0; //茶壺歸位
}

void Mouse_Moving(int x, int y)
{
    glutPostRedisplay(); //畫面持續更新
    dotx=x-clickPt_x; 
    doty=clickPt_y-y;
    rotateAngle+=1; //旋轉
    if(dotx >20)
        positionY=1;  //以哪個軸旋轉
    
    if(dotx<-20)
        positionY=-1; //以哪個軸旋轉
    
    if(doty>20)
        positionX=-1; //以哪個軸旋轉
    
    if(doty<-20)
        positionX=1; //以哪個軸旋轉
    
}
arrow
arrow
    創作者介紹
    創作者 讀書小天地 的頭像
    讀書小天地

    書籍分享天地

    讀書小天地 發表在 痞客邦 留言(0) 人氣()