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)


預設會縮放,案x、y、z,X、Y、Z改變旋轉方向,按下右鍵可以改變茶壺顏色、大小,其他功能的介紹可以看上面的連結。

image


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

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

float backgroundGray=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_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(backgroundGray, backgroundGray,backgroundGray,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);
    glutMainLoop();
    return 0;    
}

int positionX=0;
int positionY=0;
int positionZ=0;

旋轉方向,可以用右手定則,大拇指指向正x方向(右邊),其他四隻手指就是旋轉方向。


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;
    }
}

六個按鍵,六個旋轉方向。


glutKeyboardFunc(My_Keyboard);

鍵盤事件,按下鍵盤會觸發的函式。

arrow
arrow
    創作者介紹
    創作者 讀書小天地 的頭像
    讀書小天地

    書籍分享天地

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