2019年3月8日 星期五

alanhc/張牧之 電腦圖學-week03

0.講解
1.體驗
    (1)小葉程式
2.開始
    (1)translate
    (2)碰到的問題
    (3)滑鼠移動
3.成品
4.檢討作業
    (1) 小畫家算點
5.小撇步






0.講解

移動、滑鼠
glTranslatef(par1,par2,par3)
translate:移動
f:float
par1:x, par1:y, par1:z
模型檔位置:\windows\data\

1.體驗

(1)先準備



(2)開始玩~
左鍵調數值

2.開始

(1)基本程式架構
#include <GL/glut.h> ///C:\Users\alant\Desktop\freeglut\include\GL/glut.h
#include <stdio.h>
///C:\Users\alant\Desktop\GLUT_week02\bin\Debug\GLUT_week.exe 2 3 4看結果
///如果要茶壺填色:貼圖
void display()
{
/*
畫圖程式
*/
glutSwapBuffers();///交換Buffer顯示
}
int main(int argc, char ** argv) ///幾個參數 幾個指標
{
//printf("Hello World!\n");
glutInit(&argc, argv);///初始化,啟動GLUT
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);///啟動GLUT的Display的模式 兩倍Buffer,有3D深度
glutCreateWindow("BASIC_GLUT_PROGRAM");///建立視窗
glutDisplayFunc(display); ///顯示函式是display
glutMainLoop();
}
view raw glut_basic.cpp hosted with ❤ by GitHub


(2)加入第4行

glTranslate要glutSolidTeapot前面!!!








(3) But!但一移動一下視窗會跑掉!!!
sol:加上 
    glPushMatrix
    glPopMatrix


(4) GG好可怕的畫面...


Sol: 加入19行 glclear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ),清掉

(5) 滑鼠:
glutMotionFunc(motion);
motion(int x, int y) 函式:滑鼠移動會呼叫此函式,並且pass兩個參數滑鼠x跟y
*記得在最後一行加上display()這樣才會更新物體的位置!





3.成品~~~~~~~



4.檢討作業:

(1)開小畫家

座標上下顛倒>調整座標負號


















(2)複製圖片到小畫家
紅線為x軸與y軸















5.小撇步

(1)winrar中文檔名解壓縮會有問題>下載 7zip
(2)使用小葉的圖學範例shape再開始寫程式
今天的程式碼
#include <GL/glut.h>
float x,y;
void display()
{
/* 會壞掉的程式碼
glPushMatrix();
glTranslatef(0.5,0.5,0);
glutSolidTeapot(0.3);
glPopMatrix();
glBegin(GL_POLYGON);
glColor3f(0.5,0.5,0);
glVertex2f(0.5,0.5);
glVertex2f(0.5,-0.5);
glVertex2f(-0.5,-0.5);
glVertex2f(-0.5,0.5);
glEnd();
*/
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); ///清掉
glPushMatrix();/// 解釋:https://stackoverflow.com/questions/23971752/understanding-glpushmatrix-and-glpopmatrix
glTranslatef(x,-y,0); ///移動(motion func.處理)
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int nowx, int nowy) ///get 滑鼠 x,y
{
x=(nowx-150)/150.0; y=(nowy-150)/150.0;
display();
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week03-06160485");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}

Homework 挑戰3D空間+滑鼠移動 [ Code ]

挑戰:畫3D圖形
目標:DNA
步驟:
(1)先畫旁邊螺旋(GL_LINE_STRIP)
(2)畫梯子透過兩個螺旋頂點連線,並使用(GL_LINES)
(3)加入還沒教的glRotate使其看起來較3D
(4)實驗motion->改變鬆緊程度及選轉速度
遇到問題:y軸上升座標>
解決:a/PI,從週期看,每次上升1/PI

沒有留言:

張貼留言