2018年3月24日 星期六

Week03 複習、滑鼠、移動

(1)復習上週所教的迴圈


1.先到http://jsyeh.org/3dcg10下載data.zipwindows.zipglut32.dll 這三個檔案




2.將data.zipwindows.zip 解壓縮



3.壓縮好後,將glut32.dll複製到windows裡





4.執行 Shapes.exe,就可以完成





!!Shapes 介面與功能

glColor3f( ,  , );   為顏色設定
glVertex2f( , );    為點座標    

    
!!各函數程式的顯示出來結果
(PS:小葉老師有PO在電腦圖學的社團裡,還有將線加粗的程式......)

P.S:GL_QUAD_STRIP和GL_TRIANGLE_STRIP的點位置
(老師說宛如縫衣服般)




(2)複習上週的GLUT 茶杯

程式碼:                                                                             
#include <stdio.h>                                                            
#include <GL/glut.h>                                                        
void display()                                                                    
{                                                                                          
    glClear(GL_COLOR_BUFFER_BIT);                  
   // 為清理畫面                                                           
glutSolidTeapot(0.3);                                          
     //本身就有直接畫出茶壺的程式,()為設定大小    
    glutSwapBuffers();                                                    
//交換double buffers 來顯示畫出的東西              
}                                                                                        
int main(int argc,char **argv)                                        
{                                                                                        
    glutInit(&argc, argv);                                               
//初始Init(Initialize)GLUT參數設定                  
    glutInitDisplayMode(GLUT_DOUBLE);              
//顯示模式為double buffers                                 
                      //(其他像是 GLUT_RGB與GLUT_DEPTH 老師以後會教)    
    glutCreateWindow("QWQ 你好我是視窗名稱!!");
//建立視窗,("...")裡的輸入為視窗顯示名稱    
    glutDisplayFunc(display);                                      
//顯示函式display()用來畫圖的                           
    glutMainLoop();                                                        
//主要的迴圈,用來控制程式                             
}                                                                    
忘記了的話!一樣有給上週連結!!

(3)MOUSE 滑鼠 操作

1.打開CodeBlock,建立專案GLUT,先寫出茶杯程式

2.開始改寫程式

【滑鼠點茶杯後,另一個小黑框就會顯示滑鼠所點的座標】



(紅色部分為改寫程式!!)
程式碼:                                                                          
#include <stdio.h>                                                            
#include <GL/glut.h>                                                        
void display()                                                                    
{                                                                                          
    glClear(GL_COLOR_BUFFER_BIT);                  
   // 為清理畫面                                                           
glutSolidTeapot(0.3);                                          
     //本身就有直接畫出茶壺的程式,()為設定大小    
    glutSwapBuffers();                                                    
//交換double buffers 來顯示畫出的東西              
}                                                                                        



void mouse(int button,int state,int x, int y)                        
{                                                                                        
                       ///button: 左、中、右鍵, state: 按下去/彈起來, x,y:mouse位置
          printf("%d %d %d %d\n", button,state,x,y);                 
}                                                                                       

int main(int argc,char **argv)                                        
{                                                                                        
    glutInit(&argc, argv);                                               
//初始Init(Initialize)GLUT參數設定                  
    glutInitDisplayMode(GLUT_DOUBLE);              
//顯示模式為double buffers                                 
    glutCreateWindow("QWQ 你好我是視窗名稱!!");
//建立視窗,("...")裡的輸入為視窗顯示名稱    



glutMouseFunc(mouse);                                 
    ///glutMouseFunc(motion);                                  

glutDisplayFunc(display);                                      
//顯示函式display()用來畫圖的                           
    glutMainLoop();                                                        
//主要的迴圈,用來控制程式                             
}                                                                                

3.再來所印出的座標變成浮點數!




程式碼:                                                                           

#include <stdio.h>                                                            

#include <GL/glut.h>                                                        

void display()                                                                    

{                                                                                          

    glClear(GL_COLOR_BUFFER_BIT);                  

   // 為清理畫面                                                           

glutSolidTeapot(0.3);                                          

     //本身就有直接畫出茶壺的程式,()為設定大小    

    glutSwapBuffers();                                                    

//交換double buffers 來顯示畫出的東西              

}                                                                                        



void mouse(int button,int state,int x, int y)                        

{                                                                                        

                       ///button: 左、中、右鍵, state: 按下去/彈起來, x,y:mouse位置

             ///printf("%d %d %d %d\n", button,state,x,y);                  

///以上為剛剛的程式                                

                                             if(state==GLUT_DOWN)         ///如果mouse往下,才印程式                   

{                                                                 

                                           printf("   glVertex2f(%f, %f);\n", (x-150)/150.0,(150-y)/150.0);

                     ///將圖分成象限,座標有正負也變成浮點數      

}                                                                
}                                                                                       

int main(int argc,char **argv)                                        

{                                                                                        

    glutInit(&argc, argv);                                               

//初始Init(Initialize)GLUT參數設定                  

    glutInitDisplayMode(GLUT_DOUBLE);              

//顯示模式為double buffers                                 

    glutCreateWindow("QWQ 你好我是視窗名稱!!");

//建立視窗,("...")裡的輸入為視窗顯示名稱    



glutMouseFunc(mouse);                                 

    ///glutMouseFunc(motion);                                  

glutDisplayFunc(display);                                      

//顯示函式display()用來畫圖的                           

    glutMainLoop();                                                        

//主要的迴圈,用來控制程式                             

}                                            



(4)可以用滑鼠移動小黑框裡的茶杯,但有BUG!!(下週會修BUG)


(紅色部分為改寫程式!!)

程式碼:                                                                                 
#include <stdio.h>                                                                           
#include <GL/glut.h>                                                                        
float teapotX=0, teapotY=0;   ///Now: 茶壺的座標                            
void display()                                                                                    
{                                                                                                       
           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///(8)清理畫面                                                                      
    glPushMatrix();           ///Now: 備份矩陣                                    
                           glTranslatef(teapotX, teapotY, 0);          ///Now: 依照茶壺的座標移動
                     glutSolidTeapot(0.3);                                   ///(9)大小為0.3的茶壺
    glPopMatrix();                                                 ///Now: 還原矩陣
                                  glutSwapBuffers();                               ///(10)交換double buffers來顯示畫出的畫面
}                                                                                                     
void mouse(int button,int state,int x, int y)                                  
{                                                                                                     
     ///button: 左、中、右鍵, state: 按下去/彈起來, x,y:mouse位置
                                          ///printf("%d %d %d %d\n", button,state,x,y);                                                                

///以上為剛剛的程式                                                     

                   ///if(state==GLUT_DOWN)         ///如果mouse往下,才印程式                   

{                                                                                           

                    ///printf("   glVertex2f(%f, %f);\n", (x-150)/150.0,(150-y)/150.0);

///將圖分成象限,座標有正負也變成浮點數           


}                                                                                         

}                                                                                                 
void motion(int x, int y)                                                                 
{                                                                                                    
    teapotX=(x-150)/150.0;teapotY=(150-y)/150.0;                 
///Now: 依照motion時的 x,y 來改teapot的座標              
    glutPostRedisplay();                                                          
                  ///Now: 請3M Post便利貼,貼出來說,有礦石,要記得Redisplay() }                                                                                                   
int main(int argc, char **argv)                   ///(2)主要的函式 main
{                                                                                                  
              glutInit(&argc, argv);             ///(3)初始Initialize你的glut參數設定
    glutInitDisplayMode(GLUT_DOUBLE);                              
///(4)顯示模式:double buffers                                         
                    glutCreateWindow("QWQ 你好我是視窗名稱!!");          ///(5)建立視窗
    glutMouseFunc(mouse);                                                     
    glutMotionFunc(motion);        ///Now: 滑鼠motion事件使用

                             glutDisplayFunc(display);                          ///(6)顯示函式display()用來畫圖的
       glutMainLoop();                    ///(7)主要的迴圈,用來控制程式
}                                                                                                  
             【顆顆~會移動的茶杯式不是很夢幻呢~~~~】

沒有留言:

張貼留言