1.與上週一樣打造一個茶壺
(這是複習的部分)
PS程式碼(再複習一次程式碼吧~):
#include <GL/glut.h>///使用GLUT外掛
void display()
{
glutSolidTeapot(0.3);///實心的茶壺
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv); ///main的參數於進來
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///啟動3D的顯示功能,兩倍顯示buffer,有3D深度值
glutCreateWindow("Week02 Yes!!"); ///建3D窗子
glutDisplayFunc(display); ///等一下怎麼畫
glutMainLoop(); ///主迴圈,卡在這,不會結束
}
2.輸入Mouse函式
(滑鼠點擊任意位置會顯示坐標哦~)
PS程式碼:
#include <GL/glut.h>///使用GLUT外掛
#include <stdio.h>///為了printf
void display()
{
glutSolidTeapot(0.3);///實心的茶壺
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{///mouse函式:左中右鍵,DOWN/UP,x坐標,y坐標
printf("%d %d %d %d\n", button,state,x,y);
}
int main(int argc, char**argv)
{
glutInit(&argc,argv); ///main的參數於進來
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///啟動3D的顯示功能,兩倍顯示buffer,有3D深度值
glutCreateWindow("Week03"); ///建3D窗子
glutDisplayFunc(display);
glutMouseFunc(mouse); ///mouse的函式
glutMainLoop(); ///主迴圈,卡在這,不會結束
}
3.輸入Motion函式
(茶壺會根據鼠標位置移動~是不是很厲害!)
PS程式碼:
#include <GL/glut.h>///使用GLUT外掛
#include <stdio.h>///為了printf
float teapotX=0,teapotY=0;///TODO2:這裡是-1.0~+1.0的坐標
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///TODO2:清理背景
glPushMatrix();///TODO2:備份矩陣
glTranslatef(teapotX,teapotY,0);///TODO2:移動
glutSolidTeapot(0.3);///實心的茶壺
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{///mouse函式:左中右鍵,DOWN/UP,x坐標,y坐標
printf("%f %f\n",(x-150)/150.0,(y-150)/150.0);
teapotX= (x-150)/150.0;
teapotY=-(y-150)/150.0;///-1變+1
}
int main(int argc, char**argv)
{
glutInit(&argc,argv); ///main的參數於進來
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///啟動3D的顯示功能,兩倍顯示buffer,有3D深度值
glutCreateWindow("Week03"); ///建3D窗子
glutDisplayFunc(display);
glutMouseFunc(mouse); ///mouse的函式
glutMainLoop(); ///主迴圈,卡在這,不會結束
}
4.用滑鼠寫程式碼
PS程式碼:
#include <GL/glut.h>///使用GLUT外掛
#include <stdio.h>///為了printf
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///TODO2:清理背景
glBegin(GL_POLYGON);
glEnd();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{///mouse函式:左中右鍵,DOWN/UP,x坐標,y坐標
if(state==GLUT_DOWN){
printf(" glVertex2f((%d-150)/150.0,-(%d-150.0)/150.0);\n",x,y);
}
}
int main(int argc, char**argv)
{
glutInit(&argc,argv); ///main的參數於進來
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///啟動3D的顯示功能,兩倍顯示buffer,有3D深度值
glutCreateWindow("Week03"); ///建3D窗子
glutDisplayFunc(display);
glutMouseFunc(mouse); ///mouse的函式
glutMainLoop(); ///主迴圈,卡在這,不會結束
}
***鈴鐺程式碼:
#include <GL/glut.h>///使用GLUT外掛
#include <stdio.h>///為了printf
#include <math.h>
void display()
{
glClearColor(255/255.0,255/255.0,255/255.0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///TODO2:清理背景
glBegin(GL_POLYGON);
glColor3f(170/255.0,135/255.0,87/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.01){
glVertex2f(0.75*cos(angle),0.75*sin(angle));
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(242/255.0,207/255.0,54/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.01){
glVertex2f(0.7*cos(angle),0.7*sin(angle));
}
glEnd();
glTranslatef(0, -0.2, 0);
glBegin(GL_POLYGON);
glColor3f(170/255.0,135/255.0,87/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.01){
glVertex2f(0.17*cos(angle),0.17*sin(angle));
}
glEnd();
glTranslatef(0, 0.3, 0);
glBegin(GL_POLYGON);
glColor3f(170/255.0,135/255.0,87/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.01){
glVertex2f(0.8*cos(angle),0.1*sin(angle));
}
glEnd();
glTranslatef(0, 0.05, 0);
glBegin(GL_POLYGON);
glColor3f(242/255.0,207/255.0,54/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.01){
glVertex2f(0.7*cos(angle),0.1*sin(angle));
}
glEnd();
glTranslatef(0, 0.59, 0);
glBegin(GL_POLYGON);
glColor3f(170/255.0,135/255.0,87/255.0);
for(float angle=0;angle<=3.1415;angle+=0.01){
glVertex2f(0.12*cos(angle),0.12*sin(angle));}
glEnd();
glBegin(GL_POLYGON);
glTranslatef(0, 0.5, 0);
glColor3f(242/255.0,207/255.0,54/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.01){
glVertex2f(0.08*cos(angle),0.08*sin(angle));}
glEnd();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{///mouse函式:左中右鍵,DOWN/UP,x坐標,y坐標
if(state==GLUT_DOWN){
printf(" glVertex2f((%d-150)/150.0,-(%d-150.0)/150.0);\n",x,y);
}
}
int main(int argc, char**argv)
{
glutInit(&argc,argv); ///main的參數於進來
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///啟動3D的顯示功能,兩倍顯示buffer,有3D深度值
glutCreateWindow("Week03"); ///建3D窗子
glutDisplayFunc(display);
glutMouseFunc(mouse); ///mouse的函式
glutMainLoop(); ///主迴圈,卡在這,不會結束
}




沒有留言:
張貼留言