2015 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2015年6月17日 星期三
2015年6月10日 星期三
Week16 課堂作業, 許志遙
複習-基本TRT
#include <GL/glut.h>
float angle1=0;
void display(){
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();///Part 0,
glColor3f(0, 0, 1);
glutSolidTeapot(0.3);///body base 先做基本的身體, 再藏起來
glPushMatrix();
glTranslatef(0.5, 0.15, 0);
glRotatef(angle1, 0,0,1);
glTranslatef(0.3, 0, 0);
glScalef(1, 0.3, 0.3);
glColor3f(1, 0, 0);
glutSolidSphere(0.3, 30, 30);
glPopMatrix();
glPopMatrix();
glFlush();
}
void motion(int x, int y){
angle1=x;
glutPostRedisplay();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
2015年6月3日 星期三
Week15 課堂作業, 許志遙
(單一)鍵盤控制模擬機器人試做
#include <GL/glut.h>
#include <stdio.h>
float x1=-0.4, y1=0, x2=0, y2=0, angle=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(x1, y1, 0);//要掛在哪裡?
glRotatef(angle, 0,0,1);
glTranslatef(-0.25, -0.11, 0);
glColor3f(1,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPushMatrix();
glTranslatef(x2, y2, 0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glFlush();///先不用 glutSwapBuffers(); 配合 glutInitDisplayMode(GLUT_DOUBLE);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='6') x1+=0.1;
if(key=='4') x1-=0.1;
if(key=='8') y1+=0.1;
if(key=='2') y1-=0.1;
if(key=='7') angle+=10;
if(key=='9') angle-=10;
printf("%f %f\n", x1, y1);
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("Iron Man!!");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
同上(加入複數轉動)
#include <GL/glut.h>
#include <stdio.h>
float x1=-0.4, y1=0, x2=0, y2=0, angle=0,angle2=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(x2, y2, 0);//要掛在哪裡?
glRotatef(angle, 0,0,1);
glColor3f(0,0,1);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(x1, y1, 0);
glRotatef(angle2, 0,0,1);
glTranslatef(-0.25, -0.11, 0);
glColor3f(0,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glFlush();///先不用 glutSwapBuffers(); 配合 glutInitDisplayMode(GLUT_DOUBLE);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='6') x1+=0.1;
if(key=='4') x1-=0.1;
if(key=='8') y1+=0.1;
if(key=='2') y1-=0.1;
if(key=='7') angle+=10; ///全體轉動
if(key=='9') angle-=10; ///全體轉動
if(key=='1') angle2+=10;
if(key=='3') angle2-=10;
printf("%f %f\n", x1, y1);
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("Iron Man!!");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
#include <GL/glut.h>
#include <stdio.h>
float x1=-0.4, y1=0, x2=0, y2=0, angle=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(x1, y1, 0);//要掛在哪裡?
glRotatef(angle, 0,0,1);
glTranslatef(-0.25, -0.11, 0);
glColor3f(1,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPushMatrix();
glTranslatef(x2, y2, 0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glFlush();///先不用 glutSwapBuffers(); 配合 glutInitDisplayMode(GLUT_DOUBLE);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='6') x1+=0.1;
if(key=='4') x1-=0.1;
if(key=='8') y1+=0.1;
if(key=='2') y1-=0.1;
if(key=='7') angle+=10;
if(key=='9') angle-=10;
printf("%f %f\n", x1, y1);
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("Iron Man!!");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
同上(加入複數轉動)
#include <stdio.h>
float x1=-0.4, y1=0, x2=0, y2=0, angle=0,angle2=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(x2, y2, 0);//要掛在哪裡?
glRotatef(angle, 0,0,1);
glColor3f(0,0,1);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(x1, y1, 0);
glRotatef(angle2, 0,0,1);
glTranslatef(-0.25, -0.11, 0);
glColor3f(0,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glFlush();///先不用 glutSwapBuffers(); 配合 glutInitDisplayMode(GLUT_DOUBLE);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='6') x1+=0.1;
if(key=='4') x1-=0.1;
if(key=='8') y1+=0.1;
if(key=='2') y1-=0.1;
if(key=='7') angle+=10; ///全體轉動
if(key=='9') angle-=10; ///全體轉動
if(key=='1') angle2+=10;
if(key=='3') angle2-=10;
printf("%f %f\n", x1, y1);
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("Iron Man!!");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
關節控制
#include <GL/glut.h>
float angle=0, angle2=0, angle3=0, angle4=0,oldX=0, oldY=0;
void disBody(){
glPushMatrix();
glScalef(1,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}
void disArm(){
glPushMatrix();
glScalef(0.6,0.3,0.3);
glutSolidCube(1);
glPopMatrix();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(angle,0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0,0,1);
disArm(); ///1.做出arm
glPushMatrix();
glTranslatef(0.25,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(angle2,0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0,1,0);
disArm(); ///1.做出arm
glPopMatrix();
glPopMatrix();
///左手
glPushMatrix();
glTranslatef(-0.5,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(-angle3,0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(-0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0.5,0.5,1);
disArm(); ///1.做出arm
glPushMatrix();
glTranslatef(-0.25,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(-angle4,0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(-0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0.5,1,0.5);
disArm(); ///1.做出arm
glPopMatrix();
glPopMatrix();
disBody();
glFlush();
}
void motion(int x, int y){
angle-=y-oldY;
oldY=y; oldX=x;
}
void keyboard(unsigned char key, int x, int y){
if(key=='1') angle++;
if(key=='2') angle2++;
if(key=='3') angle3++;
if(key=='4') angle4++;
if(key=='5') angle--;
if(key=='6') angle2--;
if(key=='7') angle3--;
if(key=='8') angle4--;
glutPostRedisplay();
}
void timer(int t){
glutTimerFunc(20,timer,t+1);
//angle++;
glutPostRedisplay();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
同上(改良)
#include <GL/glut.h>
float angle[10]={0,0,0,0,0,0,0,0,0,0}, oldX=0, oldY=0;
int now=0;
void disBody(){
glPushMatrix();
glScalef(1,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}
void disArm(){
glPushMatrix();
glScalef(0.6,0.3,0.3);
glutSolidCube(1);
glPopMatrix();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(angle[1],0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0,0,1);
disArm(); ///1.做出arm
glPushMatrix();
glTranslatef(0.25,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(angle[2],0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0,1,0);
disArm(); ///1.做出arm
glPopMatrix();
glPopMatrix();
///左手
glPushMatrix();
glTranslatef(-0.5,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(-angle[3],0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(-0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0.5,0.5,1);
disArm(); ///1.做出arm
glPushMatrix();
glTranslatef(-0.25,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(-angle[4],0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(-0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0.5,1,0.5);
disArm(); ///1.做出arm
glPopMatrix();
glPopMatrix();
disBody();
glFlush();
}
void motion(int x, int y){
angle[now]-=y-oldY;
oldY=y; oldX=x;
}
void keyboard(unsigned char key, int x, int y){
if(key=='1') now=1;
if(key=='2') now=2;
if(key=='3') now=3;
if(key=='4') now=4;
glutPostRedisplay();
}
同上(加入紀錄資料,輸出txt)
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
float angle[8]={0,0,0,0,0,0,0,0}, oldX=0, oldY=0;
int now=0;
void disBody(){
glPushMatrix();
glScalef(1,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}
void disArm(){
glPushMatrix();
glScalef(0.6,0.3,0.3);
glutSolidCube(1);
glPopMatrix();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(angle[1],0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0,0,1);
disArm(); ///1.做出arm
glPushMatrix();
glTranslatef(0.25,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(angle[2],0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0,1,0);
disArm(); ///1.做出arm
glPopMatrix();
glPopMatrix();
///左手
glPushMatrix();
glTranslatef(-0.5,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(-angle[3],0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(-0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0.5,0.5,1);
disArm(); ///1.做出arm
glPushMatrix();
glTranslatef(-0.25,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(-angle[4],0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(-0.25,0,0); ///2.arm右移,柄在中間
glColor3f(0.5,1,0.5);
disArm(); ///1.做出arm
glPopMatrix();
glPopMatrix();
disBody();
glFlush();
}
void motion(int x, int y){
angle[now]-=y-oldY;
oldY=y; oldX=x;
if(fout==NULL) fout=fopen("a.txt","w+"); ///存檔資料用
for(int i=0;i<8;i++){
///存檔資料用
printf("%.1f\t", angle[i]);
fprintf(fout, "%.1f\t", angle[i]);
}
printf("\n");
fprintf(fout, "\n");
}
void keyboard(unsigned char key, int x, int y){
if(key=='1') now=1;
if(key=='2') now=2;
if(key=='3') now=3;
if(key=='4') now=4;
glutPostRedisplay();
}
void timer(int t){
glutTimerFunc(20,timer,t+1);
//angle++;
glutPostRedisplay();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
2015年5月27日 星期三
Week14 課堂作業, 許志遙
機器人試作
#include <GL/glut.h>
float angle=0;
void display(){
GLfloat pos[]={0,0,-1,0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.6,0.2,0); ///4.下面全部,都會對壺柄做旋轉
glRotatef(angle,0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(0.3,0,0); ///2.茶壺右移,柄在中間
glutSolidTeapot(0.3); ///1.做出茶壺
glPopMatrix();
glutSolidTeapot(0.3);
glFlush();
}
void timer(int t){
glutTimerFunc(20,timer,t+1);
angle++;
glutPostRedisplay();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMainLoop();
}
帶入函式
#include <GL/glut.h>
float angle=0;
void disBody(){
glPushMatrix();
glScalef(1,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}
void disArm(){
glPushMatrix();
glScalef(0.6,0.3,0.3);
glutSolidCube(1);
glPopMatrix();
}
void display(){
GLfloat pos[]={0,0,-1,0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,0.25,0); ///4.下面全部,都會對壺柄做旋轉
glRotatef(angle,0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(0.3,0,0); ///2.茶壺右移,柄在中間
disArm(); ///1.做出茶壺
glPopMatrix();
disBody();
glFlush();
}
void timer(int t){
glutTimerFunc(20,timer,t+1);
angle++;
glutPostRedisplay();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMainLoop();
}
帶入手腕
#include <GL/glut.h>
float angle=0, oldX=0, oldY=0;
void disBody(){
glPushMatrix();
glScalef(1,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}
void disArm(){
glPushMatrix();
glScalef(0.6,0.3,0.3);
glutSolidCube(1);
glPopMatrix();
}
void display(){
GLfloat pos[]={0,0,-1,0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///右手
glPushMatrix();
glTranslatef(0.5,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(angle,0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(0.25,0,0); ///2.arm右移,柄在中間
disArm(); ///1.做出arm
glPushMatrix();
glTranslatef(0.25,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(angle,0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(0.25,0,0); ///2.arm右移,柄在中間
disArm(); ///1.做出arm
glPopMatrix();
glPopMatrix();
///左手
glPushMatrix();
glTranslatef(-0.5,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(-angle,0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(-0.25,0,0); ///2.arm右移,柄在中間
disArm(); ///1.做出arm
glPushMatrix();
glTranslatef(-0.25,0,0); ///4.下面全部,都會對arm做旋轉
glRotatef(-angle,0,0,1); ///3.下面全部,都對中間做選轉
glTranslatef(-0.25,0,0); ///2.arm右移,柄在中間
disArm(); ///1.做出arm
glPopMatrix();
glPopMatrix();
disBody();
glFlush();
}
void motion(int x, int y){
angle-=y-oldY;
oldY=y; oldX=x;
}
void mouse(int button, int state, int x, int y){
if(state==GLUT_DOWN) {
oldX=x; oldY=y;
}
}
void timer(int t){
glutTimerFunc(20,timer,t+1);
//angle++;
glutPostRedisplay();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
2015年5月20日 星期三
Week13 課堂作業, 許志遙
以前在畫茶壺時,大小為0~1之間,並且調整畫面比例會變掉,於是有了以下的方法......
#include <GL/glut.h>
void display(){
glPushMatrix();
glutSolidTeapot(100);
glPopMatrix();
glFlush();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitWindowSize(800,600);
glutCreateWindow("Camera");
glutDisplayFunc(display);
glViewport(0,0,800,600);
glMatrixMode(GL_PROJECTION);
glOrtho(-400,400,-300,300,-1000,1000);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
}
但是上訴的方法在手動調整視窗的時候茶壺又變形了,於是又有了下面的方法
#include <GL/glut.h>
void display(){
glPushMatrix();
glutSolidTeapot(100);
glPopMatrix();
glFlush();
}
void reshape(int w, int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-w/2,w/2,-h/2,h/2,-1000,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitWindowSize(800,600);
glutCreateWindow("Camera");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}
加入計時器,自動轉
#include <GL/glut.h>
float eyeX=-100, eyeY=80, eyeZ=-200;
void display(){
GLfloat pos[] = {0,0,200,0};
glLightfv(GL_LIGHT0, GL_POSITION, pos); ///打燈光
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyeX,eyeY,eyeZ, 0,0,0, 0,1,0);
glPushMatrix();
glutSolidTeapot(100);
glPopMatrix();
glPopMatrix();
glFlush();
}
void motion(int x, int y){ ///eye-移動
eyeX=-400; eyeY=300-y;
glutPostRedisplay();
}
void reshape(int w, int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,w/h,0.001,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void timer(int t){ ///計時
glutTimerFunc(20,timer,t+1);
eyeX+=2;
if(eyeX>300) eyeY+=4;
glutPostRedisplay();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitWindowSize(800,600);
glutCreateWindow("Camera");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMotionFunc(motion);
glutTimerFunc(0,timer,0);
glutMainLoop();
}
2015年5月13日 星期三
Week12 課堂作業, 許志遙
讀入.obj檔
#include "glm.h" ///引入glm.h和glm.c(要copy到目錄)
GLMmodel * pmodel = NULL;
void display(){
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glFlush();
}
int main(int argc, char**argv){
pmodel=glmReadOBJ("porsche.obj");
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
glutInit(&argc, argv);
glutCreateWindow("carrrrrrrrr!!");
glutDisplayFunc(display);
glutMainLoop();
}
GLMmodel * pmodel = NULL;
void display(){
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glFlush();
}
int main(int argc, char**argv){
pmodel=glmReadOBJ("porsche.obj");
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
glutInit(&argc, argv);
glutCreateWindow("carrrrrrrrr!!");
glutDisplayFunc(display);
glutMainLoop();
}
打燈
#include "glm.h"
GLMmodel * pmodel = NULL;
void display(){
GLfloat pos[] = {0.0, 0.0, -1.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glFlush();
}
int main(int argc, char**argv){
pmodel=glmReadOBJ("porsche.obj");
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
glutInit(&argc, argv);
glutCreateWindow("carrrrrrrrr!!");
glutDisplayFunc(display);
glutMainLoop();
}
2015年5月6日 星期三
Week11 課堂作業, 許志遙
地球等速旋轉(加入計時器)
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0; ///自動轉很帥
GLUquadric * quad;
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);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
angle+=1;///自動轉很帥
}
void myInit()
{
quad = gluNewQuadric();
IplImage * img = cvLoadImage("earth.jpg"); ///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);
} ///最後一行最難/最重要, 所貼圖影像的資料都設定好
void timer(int t)
{/// 1000 msec 50fps:20msec
glutTimerFunc(20, timer, 0);
//printf("timer now\n");
glutPostRedisplay();
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("earth");
glutDisplayFunc(display); ///顯示
//glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
glutTimerFunc(0, timer, 0);
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
茶壺上下繞圈旋轉
#include <GL/glut.h>
#include <math.h>
float angle=0, potX=0, potY=0;
void timer(int t){
glutTimerFunc(10, timer, t+1);
angle+=1; //每次timer轉1度
potX=cos(angle/180.0*3.14);
potY=0.5*sin(angle/180.0*3.14);
glutPostRedisplay();
}
void display(){
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX, potY, 0);
glRotatef(angle, 0,1,0); //angle轉動
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMainLoop();
}
茶壺上下旋轉 ※註解掉紅色的
#include <GL/glut.h>
#include <math.h>
float angle=0, potX=0, potY=0;
void timer(int t){
glutTimerFunc(10, timer, t+1);
angle+=1; //每次timer轉1度
//potX=cos(angle/180.0*3.14);
potY=0.5*sin(angle/180.0*3.14);
glutPostRedisplay();
}
void display(){
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX, potY, 0);
glRotatef(angle, 0,1,0); //angle轉動
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMainLoop();
}
#include <GL/glut.h>
#include <math.h>
float angle=0, potX=0, potY=0, potVX=0, potVY=0;
int potState=0;
void timer(int t){
glutTimerFunc(10, timer, t+1);
angle+=1; //每次timer轉1度
if(potState==0) potY=0.5*sin(angle/180.0*3.14);
if(potState==2){potX+=potVX; potY+=potVY;}
glutPostRedisplay();
}
void display(){
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.01);
glPushMatrix();
glTranslatef(potX, potY, 0);
glRotatef(angle, 0,1,0); //angle轉動
glutSolidTeapot(0.01);
glPopMatrix();
glFlush();
}
void motion(int x, int y){
if(potState==1){potX=(x-350)/350.0; potY=-(y-350)/350.0;}
}
void mouse(int button, int state, int x, int y){
if(state==GLUT_DOWN) potState=1;
else if(state==GLUT_UP){
potState=2;
potVX=-(x-350)/3500.0;potVY=(y-350)/3500.0;
}
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("angry Teapot");
glutReshapeWindow(700,700);
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
2015年4月29日 星期三
Week10 課堂作業, 許志遙
在Code Blocks運用opevcv&glut
#include <opencv/highgui.h>
#include <GL/glut.h>
float angle=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glFlush();
angle+=0.1;
}
int main(int argc, char**argv){
IplImage*img=cvLoadImage("earth.jpg");
cvNamedWindow("2D");
cvShowImage("2D",img);
cvWaitKey(1);
glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
將貼圖貼到3D物件上
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
float angle=0; ///自動轉很帥
void display(){
glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glRotatef(angle, 0,1,0);///自動轉很帥
glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
angle+=0.1;///自動轉很帥
}
void myInit(){
IplImage * img = cvLoadImage("earth.jpg"); ///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
/// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
/// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
/// 貼圖參數, 放大時的內插, 用最近點
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);
} ///最後一行最難/最重要, 所有貼圖影像的資料都設定好
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
glutIdleFunc(display); ///自動轉很帥 (閒閒沒事幹, 就重畫)
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
2015年4月15日 星期三
Week08 課堂作業, 許志遙
全螢幕+射擊聲
#include <stdio.h>
#include <GL/glut.h>
#include <mmsystem.h>
void display(){
glClearColor(0.5,0.5,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void keyboard(unsigned char key, int x, int y){
exit(0);
}
void mouse(int button, int state, int x, int y){ //,mouseFUC
if(state==GLUT_DOWN){
PlaySound("Data/Shot.wav",NULL,SND_ASYNC); //音訊位置
printf("Shot!!!\n");
}
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("hellow");
glutFullScreen(); //全螢幕
glutDisplayFunc(display); //引入
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
#include <stdio.h>
#include <GL/glut.h>
#include <mmsystem.h>
float potX=-1, potY=0;
void display(){
glClearColor(0.5,0.5,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX, potY, 0); //瓶子
glutSolidTeapot(0.05);
glPopMatrix();
glFlush();
potX+=0.001;
if(potX>1.1) potX=-1.1;
}
void keyboard(unsigned char key, int x, int y){
exit(0);
}
void mouse(int button, int state, int x, int y){ //,mouseFUC
if(state==GLUT_DOWN){
PlaySound("Data/Shot.wav",NULL,SND_ASYNC); //音訊位置
printf("Shot!!!\n");
float mouseX=2*x/1280.0-1, mouseY=-(2*y/1024.0 -1);
if(abs(mouseX-potX)<0.001 &&abs(mouseY-potY)<0.001){ //範圍
printf("YA! Got it\n");
PlaySound("Data/potbrk.wav",NULL,SND_ASYNC);
}
}
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("hellow");
glutFullScreen(); //全螢幕
glutDisplayFunc(display); //引入
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
2015年4月8日 星期三
Week07 課堂作業, 許志遙
鍵盤事件
#include <GL/glut.h>
#include <stdio.h>
void display(){
glutSolidTeapot(0.3);
glFlush();
}
void keyboard(unsigned char key, int x, int y){
if(key=='a') printf("AAA\n");
else if(key=='b') printf("BBB\n");
}
int main(int argc, char **argv){
glutInit(&argc,argv);
glutCreateWindow("02160163");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard); //鍵盤
glutMainLoop();
}
加入滑鼠/聲音播放事件
#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
void display(){
glutSolidTeapot(0.3);
glFlush();
}
void keyboard(unsigned char key, int x, int y){
if(key=='a') printf("AAA\n");
else if(key=='b') printf("BBB\n");
}
void mouse(int button, int state, int x,int y){
printf("now playing sound\n");
//PlaySound("UZI.wav", NULL, SND_ASYNC); //SND_SYNC循環播放
PlaySound("madmoo.wav", NULL, SND_ASYNC); //SND_ASYNC按一下播放一次
}
int main(int argc, char **argv){
glutInit(&argc,argv);
glutCreateWindow("02160163");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard); //鍵盤
glutMouseFunc(mouse);
glutMainLoop();
}
鋼琴事件
#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
void display(){
glutSolidTeapot(0.3);
glFlush();
}
void keyboard(unsigned char key, int x, int y){
if(key=='1') {printf("Do\n"); PlaySound("do.wav", NULL, SND_ASYNC);}
else if(key=='2') {printf("Re\n"); PlaySound("re.wav", NULL, SND_ASYNC);}
else if(key=='3') {printf("Mi\n"); PlaySound("mi.wav", NULL, SND_ASYNC);}
else if(key=='4') {printf("Fa\n"); PlaySound("fa.wav", NULL, SND_ASYNC);}
else if(key=='5') {printf("So\n"); PlaySound("sol.wav", NULL, SND_ASYNC);}
else if(key=='6') {printf("La\n"); PlaySound("la.wav", NULL, SND_ASYNC);}
else if(key=='7') {printf("Si\n"); PlaySound("si.wav", NULL, SND_ASYNC);}
}
void mouse(int button, int state, int x,int y){
printf("now playing sound\n");
//PlaySound("UZI.wav", NULL, SND_ASYNC); //SND_SYNC循環播放
PlaySound("madmoo.wav", NULL, SND_ASYNC); //SND_ASYNC按一下播放一次
}
int main(int argc, char **argv){
glutInit(&argc,argv);
glutCreateWindow("02160163");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard); //鍵盤
glutMouseFunc(mouse);
glutMainLoop();
}
※以上圖片以及音樂都要丟到bin/Debug裡
訂閱:
文章 (Atom)











