2018年1月11日 星期四
2017年12月14日 星期四
翁驊成的學習筆記 week 14
week 14 Rasterization光柵化
step 1.P語言
size(600,600,P3D);
background(255);
//glBegin();
beginShape(TRIANGLE);
//glColor3f(r,g,b);
//glVertex3f(x,y,z);
stroke(255,0,0); vertex(300,100);
stroke(255,255,0); vertex(500,500);
stroke(0,0,255); vertex(100,500);
//glEnd();
endShape();

void setup()
{
size(600,600,P3D);
}
void draw(){
background(255);
beginShape(TRIANGLE);
fill(255,0,0);vertex(300,100,0);
fill(255,255,0);vertex(500,500,100); //Z值表示該點在圖層的位置 ,Z較大在圖層上方
fill(0,0,255);vertex(100,500,0);
endShape();
beginShape(TRIANGLE);
fill(255,200,0);vertex(500,100,0);
fill(100,255,0);vertex(500,500,20);
fill(0,0,20);vertex(100,500,0);
endShape();
}
2017年12月7日 星期四
翁驊成的學習筆記 week 13
week 13 V.R體驗 & color
step 1.體驗V.R
step 2.color
size(255,255);
colorMode(RGB,255); //RGB or HSB or...
for(int x=0;x<255;x++)
{
for(int y=0;y<255;y++)
{
stroke(x,y,255);
point(x,y);
}
}
step 3.多張貼圖(TextureID)
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
float mat[]={1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
GLUquadric * quad;
GLuint idBall, idCanvas, idTeapot;
float ballSize=0.2;
float posX=0.5, posY=0.5, posWin=0.1;
int oldX=0, oldY=0;
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
double dist(float x, float y){
return sqrt(x*x+y*y);
}
void motion(int x, int y){
int dx=x-oldX, dy=oldY-y;
oldX=x; oldY=y;
glPushMatrix();
glRotatef( dist(dx,dy)/2, dy, -dx, 0);
glMultMatrixf(mat);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glPopMatrix();
glutPostRedisplay();
posX+=dx/500.0*posWin;
posY-=dy/500.0*posWin;
static FILE * fout=NULL;
if(fout==NULL)fout=fopen("trace.txt", "w+");
fprintf(fout, "%d %d\n", dx, dy);
static IplImage * img = NULL;
if(img==NULL){
img=cvLoadImage("imgCanvas.png"); ///OpenCV讀圖
}
int px=posX*img->width, py=posY*img->height, pdx=-(dx/200.0*posWin)*img->width, pdy=(dy/500.0*posWin)*img->height;
cvLine(img, cvPoint(px,py), cvPoint(px+pdx, py+pdy), cvScalar(255,0,0) );
glBindTexture(GL_TEXTURE_2D, idCanvas); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}
void display()
{
glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0, 0.5, 0);
glBindTexture(GL_TEXTURE_2D, idTeapot);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0, ballSize);
glNormal3f(0,0,-1);
glColor3f(1,1,1);
glBindTexture(GL_TEXTURE_2D, idCanvas);
glBegin(GL_POLYGON);
glTexCoord2f(posX-posWin,posY-posWin);glVertex2f(-1,+1);
glTexCoord2f(posX-posWin,posY+posWin);glVertex2f(-1,-1);
glTexCoord2f(posX+posWin,posY+posWin);glVertex2f(+1,-1);
glTexCoord2f(posX+posWin,posY-posWin);glVertex2f(+1,+1);
glEnd();
glPopMatrix();
glPushMatrix();
glMultMatrixf(mat);
glBindTexture(GL_TEXTURE_2D, idBall);
gluQuadricTexture(quad, 1);
gluQuadricOrientation(quad, GLU_OUTSIDE);
gluSphere(quad, ballSize, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y){
...
}
int myTexture(char *filename)
{
IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
return id;
}
void myLighting(){
...
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("roach");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
printf("Use mouse to roll the ball and to draw trace\n");
quad = gluNewQuadric();
idBall = myTexture("imgBall.png");///重點:一定不可在display()讀圖檔
idCanvas=myTexture("imgCanvas.png");///重點: 你要在main()只讀一次
idTeapot=myTexture("idTeapot.jpg");
myLighting();///時機點要在 glutMainLoop()之前, glutCreateWindow()之後
glutMainLoop();
}
2017年11月30日 星期四
翁驊成的學習筆記 week 12
week 12 Scene 場景
step 1.跑範例程式
myGL_Frustrum221_MultiView_2012_1221-20171130T060949Z-001.zip
int main(int argc,char **argv) //要加這個
{
glutInit(&argc,argv);
printf("Usage\n"
"(1) Mouse Drag to rotate\n"
"(2) Key UP/DOWN/LEFT/RIGHT to rotate\n"
"(3) Key '+'/'-' to resize\n"
"(4) Key 'o' for Orthogonal Projection\n"
"(5) Key 'p' for Perspective Projection\n"
"(6) Key '1'(red ball) and '2'(green ball) to switch to Kinect Frustrum\n"
"(7) Key ASDW for (humanX, humanY) movememt\n"
"It will read myGL_Frustrum.txt for Kinect Positions and Targets");
readKinectCFG();
initGL();
glutMainLoop();
return 0;
}
step 2.P語言
下載Collada loader for sketch up and blender && Picking
Viewing 2D
false 改 true
simple
test BVH(骨架)
step 3.圖片變 3D
2017年11月23日 星期四
翁驊成的學習筆記 week 11
week 11 聲音--音效、音樂
step 1.
可以播wav的方法:
glut
/* Program entry point */#include <mmsystem.h> //多媒體、聲音
int main(int argc, char *argv[])
{
PlaySoundA("file.wav", NULL, SND_ASYNC); //SND_SYNC 等同步
//SND_ASYNC 不等,下一行繼續
...
}
step 2.
///PlaySoundA("file.wav", NULL, SND_SYNC);不能跑
使用 CMP3_MCI.h 即可
#include <stdio.h>
#include "CMP3_MCI.h" ///使用外掛
CMP3_MCI mp3; ///宣告變數
int main()
{
mp3.Load("file.mp3"); ///讀入 mp3檔
mp3.Play(); ///Play播放mp3檔
printf("現在在等待輸入a\n");
int a;
scanf("%d", &a); ///等待輸入時,程式還沒結束,為了卡住讓音樂一直播
#include "CMP3_MCI.h" ///使用外掛
CMP3_MCI mp3; ///宣告變數
int main()
{
mp3.Load("file.mp3"); ///讀入 mp3檔
mp3.Play(); ///Play播放mp3檔
printf("現在在等待輸入a\n");
int a;
scanf("%d", &a); ///等待輸入時,程式還沒結束,為了卡住讓音樂一直播
}
step 3.
P語言
import ddf.minim.*; //use ddf's minim music library
Minim minim;
AudioPlayer player; //corresponding to one mp3 file
AudioPlayer player2;
void setup()
{
minim=new Minim(this); //constructor of Minim Object
player=minim.loadFile("file.mp3"); //minim load to player
player.play();
}
void draw()
{
}
2017年11月16日 星期四
翁驊成的學習筆記 week 10
week 10 P語言-App & Siggraph
step 1.實作
//(右上角)Java 改成 Add Mode 加 Android
void setup()
{
fullScreen();
}
void draw()
{
if(mousePressed) background(255,0,0);
else background(0,255,0);
}
2017年11月9日 星期四
翁驊成的學習筆記 week 09
week 09 P語言
step 1.Bump Mapping & 實作
網址體驗:
https://www.openprocessing.org/sketch/249457

執行processing.exe:

使用processing讀圖:
PImage img=loadImage("kuso.jpg");
size(300,300);
image(img,0,0,300,300);

PImage img;
void setup()
{
size(1280,960);
img=loadImage("naruto.jpg");
}
void draw()
{
//rect(mouseX,mouseY,300,300);
image(img,mouseX,mouseY);
}

int [][]map={{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,2,2,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
PImage imgDragon,imgBrick,imgBrick2;
void setup()
{
size(800,760);
imgDragon=loadImage("dragon.png");
imgBrick=loadImage("brick.png");
imgBrick2=loadImage("brick2.png");
}
float dragonX=200, dragonY=100, dragonVX=0, dragonVY=0;
boolean dragonOnFloor=false;
void draw()
{
background(255);
//for(int x=0;x<20;x++) image(imgBrick,x*50,500,50,50);
for(int x=0;x<14;x++){
for(int y=0;y<11;y++){
if(map[y][x]==1) image(imgBrick,x*50,y*50,50,50);
if(map[y][x]==2) image(imgBrick2,x*50,y*50,50,50);
}
}
image(imgDragon,dragonX,dragonY,100,100);
dragonY += dragonVY;
dragonX += dragonVX;
dragonVY += 0.98;
if(dragonY>=500-100)
{
dragonY=500-100;
dragonVY=0;
dragonOnFloor=true;
}
}
void keyPressed()
{
if(keyCode==UP && dragonOnFloor) { dragonVY=-15; dragonOnFloor=false;}
if(keyCode==RIGHT) dragonVX=5;
if(keyCode==LEFT) dragonVX=-5;
}
void keyReleased()
{
if(keyCode==RIGHT || keyCode==LEFT) dragonVX=0;
}

2017年11月2日 星期四
翁驊成的學習筆記 week 08
week 08 Texture貼圖
step 1.地球貼圖
//使用opencv2.1
//path要2 or 3打勾
//week08右鍵build options->search directories->compiler add c:\OpenCV2.1\include
->linker add c:\OpenCV2.1\lib
->linker setting add cv210,cxcore210,highgui210
#include <opencv/highgui.h>
int main()
{
IplImage * img=cvLoadImage("earth.jpg");
cvNamedWindow("hello opencv2.1");
cvShowImage("hello", img);
cvWaitKey(0);
return 0;
}
//glut project也能做
//earth.jpg放在freeglut\bin裡面
step 2.地"球"轉動(老師範例)
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
GLUquadric * quad;
GLuint id;
float angle=0;
void display()
{
glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ///自動轉
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1); ///angle
gluQuadricTexture(quad, 1);
gluSphere(quad, 1, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();
glFlush();
}
void timer(int t)
{
glutTimerFunc(20, timer, 0); /// 1000 msec 50fps:20msec
angle+=1; /// 自動轉
glutPostRedisplay();
}
int myTexture(char *filename)
{
IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); ///產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
/// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
/// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
/// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
/// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
return id;
void myInit()
{
quad = gluNewQuadric();
id = myTexture("earth.jpg");
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("opencv3D");
glutDisplayFunc(display);
glutTimerFunc(0, timer, 0);
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
2017年10月26日 星期四
翁驊成的學習筆記 week 07
week 07 用mouse調光源
.....
static void idle(void)
{
glutPostRedisplay();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f }; //不用const
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
void motion(int x, int y)
{
light_position[0]= (x-150)/150.0*2;
light_position[1]=-(y-150)/150.0*2;
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glutPostRedisplay();
}
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutMotionFunc(motion);
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
2017年10月19日 星期四
翁驊成的學習筆記 week 06
week 06 實作
step 1.實作範例(打光)
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
void drawmodel(void)
{
if (!pmodel) {
pmodel = glmReadOBJ("data/al.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //3D深度測試,清depth buffer才能正確繪圖
drawmodel();
glutSwapBuffers();
}
GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 }; //打光(3)
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); //3D深度測試,要準備好depth buffer記憶體
glutCreateWindow("week06");
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST); //3D深度測試,打開Enable
glEnable(GL_LIGHTING); //打光(2),打開Enable
glEnable(GL_LIGHT0); //打光(1),打開Enable
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glutMainLoop();
}
step 2.實作範例(轉動+移動)
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
int rotateX=0,rotateY=0,rotateZ=0;
int oldx=0,oldy=0;
void keyboard(unsigned char key, int x, int y)
{
printf("Now : %c (%d %d)\n",key,x,y);
if(key=='1')rotateX++;
if(key=='2')rotateY++;
if(key=='3')rotateZ++;
glutPostRedisplay();
}
void mouse(int button, int state,int x, int y)
{
printf("button:%d state:%d (%d %d)\n",button,state,x,y);
if(state==GLUT_DOWN)
{
oldx=x;
oldy=y;
}
}
void motion(int x,int y)
{
rotateY += -(x-oldx);
rotateX += -(y-oldy);
oldx=x;
oldy=y;
oldx=x;
oldy=y;
glutPostRedisplay();
}
void drawmodel(void)
{
if (!pmodel)
{
pmodel = glmReadOBJ("data/al.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//drawmodel();
glPushMatrix();
glRotatef(rotateX,1,0,0);
glRotatef(rotateY,0,1,0);
glRotatef(rotateZ,0,0,1);
drawmodel();
glPopMatrix();
glutSwapBuffers();
}
GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 };
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glutMainLoop();
}
2017年10月12日 星期四
翁驊成的學習筆記 week 05
week 05 Viewing
step 1.範例
1.到http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/下載data.zip,windows.zip,glut32.dll並解壓縮
2.操作transformation.exe
3.操作projection.exe
glu(常用功能)
gluPerspective(fovy,aspect,zNear,zFar);
fovy //視野大小
aspect //xy比例
zNear,zFar //遠近透視
gluOrtho(left,right,bottom,up,near,far);
gluFrustum(left,right,bottom,up,near,far);
gluLookAt(eyeX,eyeY,eyeZ, //自己位置
centerX,centerY,centerZ, //目標位置
upX,upY,upZ);
step 2.實作範例
1.先到moodle下載week05
2.setting->compiler改成gcc //或是都不打勾 //或是.cpp改.c
3.執行
2017年10月5日 星期四
翁驊成的學習筆記 week 04
week 04 Translate,Rotate,Scale,矩陣Matrix--Push,Pop
step 1.簡單範例
static void display(void)
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; //時間函數
const double a = t*90.0; //a隨時間函數變化
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glColor3d(1,0,0);
glPushMatrix(); //備份矩陣
glTranslated(-2.4,1.2,-6); //移動 //d是浮點數
//右手座標系統
glRotated(60,1,0,0); //沿著某軸轉動幾度 //沿著x軸轉動60度
glRotated(a,0,0,1); //a隨時間函數變化
glScaled(2, 2, 2); //縮放 //放大兩倍 //參考2
glColor3f(0,1,1); //改顏色
glutSolidTeapot(1); //茶壺
glutSolidSphere(1,slices,stacks);
glPopMatrix(); //還原矩陣
glPushMatrix();
glTranslated(0,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glColor3f(1,1,0);
glutSolidTeapot(1);
glutSolidCone(1,1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glColor3f(1,0,1);
glutSolidTeapot(1);
glutSolidTorus(0.2,0.8,slices,stacks);
glPopMatrix();
...
1.

2.

step 2.實作
#include <GL/glut.h>
#include <stdio.h>
float dx=0,dy=0,dz=0;
int oldX=0,oldY=0,oldZ=0;
void display()
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();
glTranslated(dx,dy,dz);
//glRotated(60,1,0,0);
//glRotated(a,1,0,0);
//glScaled(3,3,3);
//glutWireSphere(1,slices,stacks);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
dx +=(x-oldX)/150.0; //At the previous point, add the displacement.
dy += -(y-oldY)/150.0;
oldX = x;
oldY = y;
glutPostRedisplay(); // Redraw
}
void mouse(int button, int state, int x, int y)
{
if(state == GLUT_DOWN)
{
oldX = x;
oldY = y;
}
if(state == GLUT_UP)
{
printf("彈起來了\n");
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
#include <stdio.h>
float dx=0,dy=0,dz=0;
int oldX=0,oldY=0,oldZ=0;
void display()
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();
glTranslated(dx,dy,dz);
//glRotated(60,1,0,0);
//glRotated(a,1,0,0);
//glScaled(3,3,3);
//glutWireSphere(1,slices,stacks);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
dx +=(x-oldX)/150.0; //At the previous point, add the displacement.
dy += -(y-oldY)/150.0;
oldX = x;
oldY = y;
glutPostRedisplay(); // Redraw
}
void mouse(int button, int state, int x, int y)
{
if(state == GLUT_DOWN)
{
oldX = x;
oldY = y;
}
if(state == GLUT_UP)
{
printf("彈起來了\n");
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
2017年9月28日 星期四
翁驊成的學習筆記 week 03
week 03 補充程式範例&Vertex Buffer/Array
step 1.學姊的小小兵程式範例
#include <GL/glut.h>
#include <stdio.h>
//Drawing funciton
void draw(void)
{
FILE *f;
float w, h, c;
float r, g, b;
float i, j, x, y;
//Background color
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Read pixel file //讀檔
//注意:minion.txt須放到freeglut\bin裡
f = fopen("minion.txt", "r");
//Read width, height, channel
fscanf(f, "%f %f %f", &w, &h, &c);
//Set point size
glPointSize(10.0f); //放大10倍
//Begin drawing
glBegin(GL_POINTS);
for(i=0; i<w; i++){
for(j=0; j<h; j++){
//Read r, g, b
fscanf(f, "%f %f %f", &r, &g, &b); //用這個程式會慢
//Geometric transformation
x = -(i-(w/2.0))/(w/2.0);
y = (j-(h/2.0))/(h/2.0);
//Draw(in python, read as bgr)
glColor3f(b/255.0, g/255.0, r/255.0); //是b,g,r喔!
glVertex3f(y, x, 0);
}
}
//End drawing
glEnd();
fclose(f);
//Draw order
glutSwapBuffers();
}
//Main program
int main(int argc, char **argv)
{
glutInit(&argc, argv);
//Simple buffer
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
glutInitWindowPosition(50,25);
glutInitWindowSize(300, 300);
glutCreateWindow("minion");
//Call to the drawing function
glutDisplayFunc(draw);
glutMainLoop();
return 0;
}
step 2.足球的程式範例
1.下載3D exploration
2.到http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/網站下載圖並拉進3D exploration
3.選SampleAPP
4.建立OpenGL專案並把原本的main從.c檔改成.cpp檔
5.貼上3D足球的程式碼(把會當的地方改掉)
6.完成並執行
2017年9月21日 星期四
翁驊成的學習筆記 week 02
week 02 第一個圖學程式
Step 1.茶壺
#include <GL/glut.h>void display()
{
glClearColor(1,1,1,1); //清背景顏色 (R,G,B,A) ex:紅1000
//參考1
glClear(GL_COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
glColor3f(1,0,1); //物件色彩(紫)
//3是3個參數,f是0.0-1.0之間的浮點數
//參考2
glutDolidTeapot(0.3); //茶壺 glut外掛
glutSwapBuffers(); //交換繪圖Buffers
}
int main(int argc, char *argv[]) //主程式函式,吃argc參數數量,argv是參數的全部,用陣列存
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); //設定顯示模式
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display); //註冊display
glutMainLoop(); //主要迴圈,一直跑
}
1.

2.

Step 2.三角形
#include <GL/glut.h>
void display()
{
glClearColor(1,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,1); //物件色彩(紫)
glBegin(GL_POLYGON); //開始畫(多邊形)
glVertex3f(1,1,0); //三個頂點 //3是x,y,z 3個參數,f是0.0-1.0之間的浮點數
glVertex3f(-1,1,0);
glVertex3f(0,-1,0);
glEnd(); //結束畫
//參考3
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
}
3.

Step 3.彩色三角形
#include <GL/glut.h>void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON); //開始畫(多邊形)
//彩色三角形在頂點前用函式glColor3f(R,G,B);
glColor3f(1,0,0); glVertex3f(1,1,0); //三個頂點
glColor3f(0,1,0); glVertex3f(-1,1,0);
glColor3f(0,0,1); glVertex3f(0,-1,0);
//參考4
glEnd(); //結束畫
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
}
4.
2017年9月14日 星期四
翁驊成的學習筆記 week 01
訂閱:
文章 (Atom)

































