我是week03喔
到這個網址去http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/下載data win32 glut32.dll
一開始先練習上一周的茶壺到這個網址去http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/下載data win32 glut32.dll
程式碼:
#include <GL/glut.h>
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.接下來將程式碼改成這樣
#include <stdio.h>
void display()
{
glutSolidTeapot( 0.3 ); //寫出實心茶壺
glutSwapBuffers(); //將茶壺變兩倍大
}
void mouse(int button, int state, int x, int 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); //如何去畫
glutMainLoop(); //主迴圈停在這,並且不會結束
}
再接著:
這樣可以改變茶壺位置
#include <GL/glut.h>
#include <stdio.h>
float teapotX=0, teapotY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX, teapotY, 0);
glutSolidTeapot( 0.3 ); //寫出實心茶壺
glPopMatrix();
glutSwapBuffers(); //將茶壺變兩倍大
}
void mouse(int button, int state, int x, int y)
{
printf("%f %f\n",(x-150)/150.0, (y-150)/150.0);
teapotX=(x-150)/150.0;
teapotY=(y-150)/150.0;
}
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); //如何去畫
glutMainLoop(); //主迴圈停在這,並且不會結束
}
用滑鼠描圖:
先用滑鼠在小的黑色視窗描圖,大的視窗會出現描過的程式碼(座標),這個時候就複製大視窗的程式碼貼在原本的程式碼上,就會出現描過的圖形了
#include <GL/glut.h>
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f((160-150)/150.0, - (36-150)/150.0);
glVertex2f((155-150)/150.0, - (48-150)/150.0);
glVertex2f((137-150)/150.0, - (61-150)/150.0);
glVertex2f((123-150)/150.0, - (89-150)/150.0);
glVertex2f((88-150)/150.0, - (115-150)/150.0);
glVertex2f((75-150)/150.0, - (142-150)/150.0);
glVertex2f((53-150)/150.0, - (179-150)/150.0);
glVertex2f((39-150)/150.0, - (204-150)/150.0);
glVertex2f((53-150)/150.0, - (206-150)/150.0);
glVertex2f((87-150)/150.0, - (205-150)/150.0);
glVertex2f((123-150)/150.0, - (205-150)/150.0);
glVertex2f((160-150)/150.0, - (209-150)/150.0);
glVertex2f((215-150)/150.0, - (211-150)/150.0);
glVertex2f((279-150)/150.0, - (214-150)/150.0);
glVertex2f((272-150)/150.0, - (198-150)/150.0);
glVertex2f((249-150)/150.0, - (175-150)/150.0);
glVertex2f((217-150)/150.0, - (127-150)/150.0);
glVertex2f((201-150)/150.0, - (85-150)/150.0);
glVertex2f((181-150)/150.0, - (51-150)/150.0);
glVertex2f((168-150)/150.0, - (33-150)/150.0);
glVertex2f((164-150)/150.0, - (35-150)/150.0);
glVertex2f((162-150)/150.0, - (40-150)/150.0);
glEnd();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN)
{
printf("glVertex2f((%d-150)/150.0, - (%d-150)/150.0);\n",x,y);
}
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f((160-150)/150.0, - (36-150)/150.0);
glVertex2f((155-150)/150.0, - (48-150)/150.0);
glVertex2f((137-150)/150.0, - (61-150)/150.0);
glVertex2f((123-150)/150.0, - (89-150)/150.0);
glVertex2f((88-150)/150.0, - (115-150)/150.0);
glVertex2f((75-150)/150.0, - (142-150)/150.0);
glVertex2f((53-150)/150.0, - (179-150)/150.0);
glVertex2f((39-150)/150.0, - (204-150)/150.0);
glVertex2f((53-150)/150.0, - (206-150)/150.0);
glVertex2f((87-150)/150.0, - (205-150)/150.0);
glVertex2f((123-150)/150.0, - (205-150)/150.0);
glVertex2f((160-150)/150.0, - (209-150)/150.0);
glVertex2f((215-150)/150.0, - (211-150)/150.0);
glVertex2f((279-150)/150.0, - (214-150)/150.0);
glVertex2f((272-150)/150.0, - (198-150)/150.0);
glVertex2f((249-150)/150.0, - (175-150)/150.0);
glVertex2f((217-150)/150.0, - (127-150)/150.0);
glVertex2f((201-150)/150.0, - (85-150)/150.0);
glVertex2f((181-150)/150.0, - (51-150)/150.0);
glVertex2f((168-150)/150.0, - (33-150)/150.0);
glVertex2f((164-150)/150.0, - (35-150)/150.0);
glVertex2f((162-150)/150.0, - (40-150)/150.0);
glEnd();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN)
{
printf("glVertex2f((%d-150)/150.0, - (%d-150)/150.0);\n",x,y);
}
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}



沒有留言:
張貼留言