WEEK03
week03-1 translate
先到https://jsyeh.org/3dcg10下載windows.zip及data.zip
-windows.zip 解壓縮 下載\windows\Transformation.exe
-data.zip 解壓縮 下載\windows\data\很多3D模型檔
把解壓縮完的data放入解壓縮完的windows
執行\windows\Transformation.exe
-glTranslatef(x,y,z);綠色的數字,上下拉動
如果閃退,就是data.zip解錯目錄
week03-2
從codeblock開啟freeglut
刪除全部程式碼,貼上上週10行程式碼
#include <GL/glut.h>
void display()
{
glutSolidTeapot( 0.3 );
glutSwapBuffers( );
}
int main(int argc, char *argv[ ])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03");
glutDisplayFunc(display);
glutMainLoop( );
}
新增三行程式,讓茶壺變到右上
void display()
{
glPushMatrix(); ///備份矩陣
glTranslatef( 0.5, 0.5, 0); ///改變位置
glutSolidTeapot( 0.3 );
glPopMatrix(); ///還原矩陣
glutSwapBuffers( );
}
week03-3 translate mouse
開啟新的GLUT專案
複製week03-2 translae的程式
使用global變數,來讓座標改變
#include <GL/glut.h>
float X=0, Y=0, Z=0;
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glTranslatef(X,Y,Z);
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
void mouse( int button, int state, int x, int y )
{
X = (x-150)/150.0;
Y = -(y-150)/150.0;
}
int main(int argc, char *argv[ ])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03");
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutMainLoop();
}
week03-4 mouse homework
#include <GL/glut.h>
#include <stdio.h>
float X=0, Y=0, Z=0;
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glBegin(GL_LINE_LOOP);
glVertex2f(-0.01, 0.15);
glVertex2f(0.00, 0.29);
glVertex2f(0.00, 0.35);
glVertex2f(0.01, 0.37);
glVertex2f(0.07, 0.38);
glVertex2f(0.11, 0.38);
glVertex2f(0.15, 0.36);
glVertex2f(0.17, 0.29);
glVertex2f(0.18, 0.23);
glVertex2f(0.18, 0.21);
glVertex2f(0.14, 0.13);
glVertex2f(0.14, 0.13);
glEnd();
glBegin(GL_LINE_LOOP);///橘色是空心
glVertex2f(-0.28, 0.13);
glVertex2f(-0.31, 0.17);
glVertex2f(-0.35, 0.21);
glVertex2f(-0.35, 0.27);
glVertex2f(-0.35, 0.33);
glVertex2f(-0.31, 0.38);
glVertex2f(-0.27, 0.39);
glVertex2f(-0.23, 0.37);
glVertex2f(-0.22, 0.31);
glVertex2f(-0.19, 0.25);
glVertex2f(-0.18, 0.21);
glVertex2f(-0.17, 0.19);
glVertex2f(-0.16, 0.17);
glEnd();
glBegin(GL_POLYGON);///紫色是實心
glVertex2f(-0.21, -0.13);
glVertex2f(-0.27, -0.05);
glVertex2f(-0.29, 0.03);
glVertex2f(-0.29, 0.08);
glVertex2f(-0.21, 0.11);
glVertex2f(-0.14, 0.11);
glVertex2f(-0.05, 0.11);
glVertex2f(0.11, 0.11);
glVertex2f(0.18, 0.07);
glVertex2f(0.18, 0.01);
glVertex2f(0.18, -0.03);
glVertex2f(0.14, -0.07);
glVertex2f(0.06, -0.11);
glVertex2f(0.03, -0.13);
glVertex2f(-0.08, -0.13);
glVertex2f(-0.21, -0.12);
glEnd();
///藍色可以更動
glutSwapBuffers();
}
void mouse( int button, int state, int x, int y )
{
X = (x-150)/150.0;
Y = -(y-150)/150.0;
if(state==GLUT_DOWN) printf(" glVertex2f(%.2f, %.2f);\n",X,Y);
}
int main(int argc, char *argv[ ])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03");
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutMainLoop();
}




沒有留言:
張貼留言