2014 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2014年6月12日 星期四
2014年5月30日 星期五
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 };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
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);
glutMainLoop();
void eye()
{
glPushMatrix();
glColor3f(1,1,1);
glutSolidSphere(0.1,20,20);
glTranslatef(0,0.03,0.08);
glColor3f(0,0,0);
glutSolidSphere(0.04,10,10);
glPopMatrix();
}
void head()
{
glPushMatrix();
glTranslatef(0,0.3,0);
glColor3f(0,0,1);
glutSolidSphere(0.3,30,30);
glColor3f(1,1,1);
glTranslatef(0,-0.01,0.1);
glutSolidSphere(0.25,30,30);
glPopMatrix();
glPushMatrix();
glColor3f(1,1,1);
//glutSolidSphere(0.2,30,30);
glPopMatrix();
}
void myTexture()
{
GLuint id;
IplImage * img = cvLoadImage("body.png");
cvCvtColor(img, img, CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}
{
GLuint id;
IplImage * img = cvLoadImage("body.png");
cvCvtColor(img, img, CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}
213123
#include <GL/glut.h>
float a = 0; //內插法比重
// 利用陣列, 存許多角度
float angle[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20] = {0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleNew[20] = {90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0], 0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0.5, 0,0);
glRotatef(angle[1], 0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void readNext()
{
a += 0.1; //比重一直增加,角度才會變化
for( int i=0; i<=20; i++){ //用for可以一次打20個
angle[i] = angleOld[i] * (1-a) + angleNew[i] * a;
}
}
void timer(int t)
{
glutTimerFunc(100, timer, 0);
readNext();
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if (key == 'r'){
readNext();
}
else if (key== 'p'){
glutTimerFunc(0, timer, 0);
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
2014年5月23日 星期五
#include<stdio.h>
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <stdio.h>
//IplImage *img1, *img2, *img3, *img4;
//GLuint id1, id2, id3, id4;
IplImage * img[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
GLuint id[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
//float angle1=0, angle2=0, angle3=0, angle4=0;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int angleID=1;
int oldX,oldY;
void mouse(int botton, int state, int x, int y)
{
oldX=x;
oldY=y;
}
void motion(int x, int y)
{
angle[angleID]+= x-oldX; oldX=x;
glutPostRedisplay();
}
void myPrepareTexture(int i)//用參數來配, 找對應正確的變數
{//技巧3: 可以把重覆的東西, 用函式來簡化
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img[i]->width, img[i]->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img[i]->imageData);
}
void myTexture()
{
img[0]=cvLoadImage("imgbody.png");
img[1]=cvLoadImage("imgHand1.png");
img[2]=cvLoadImage("imgLeg1.png");
for(int i=0;i<3;i++){//技巧2: 可以把重覆的東西, 用迴圈來簡化
myPrepareTexture(i);
}
}
void myPolygon(int num){//技巧3: 可以把重覆的東西, 用函式來簡化
glPushMatrix();
glScalef(0.3, 0.8, 0.8);
glBindTexture(GL_TEXTURE_2D, id[num]);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex3f( -0.5, 0.5, 0);
glTexCoord2f(1, 0); glVertex3f( 0.5, 0.5, 0);
glTexCoord2f(1, 1); glVertex3f( 0.5, -0.5, 0);
glTexCoord2f(0, 1); glVertex3f( -0.5, -0.5, 0);
glEnd();
glPopMatrix();
}
void myBody(){
glPushMatrix();
glScalef(0.7, 0.7, 0.7);
myPolygon(0);
glPopMatrix();
}
void myLeg(){
glPushMatrix();
glScalef(0.5, 0.5, 0.5);
myPolygon(1);
glPopMatrix();
}
void myHand(){
glPushMatrix();
glScalef(0.5, 0.5, 0.5);
myPolygon(2);
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
myBody();
glPushMatrix();
glTranslatef(0.1,0.1,0);//右手
glRotatef(angle[1], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();
glTranslatef(0,-0.2,0);//右手1
glRotatef(angle[2], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.1,0.1,0);//左手
glRotatef(-angle[3], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();
glTranslatef(0,-0.2,0);//左手1
glRotatef(-angle[4], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.1,-0.2,0);//左腿
glRotatef(-angle[5], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myLeg();
glPushMatrix();
glTranslatef(0,-0.2,0);//左腿1
glRotatef(-angle[6], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.1,-0.2,0);//右腿
glRotatef(-angle[7], 0,0,-1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myLeg();
glPushMatrix();
glTranslatef(0,-0.2,0);//右腿1
glRotatef(-angle[8], 0,0,-1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=NULL, *fin=NULL;
void timer(int t)
{
glutTimerFunc(1000,timer,0);
if(fin==NULL) fin = fopen("file.txt","r");
fscanf(fin,"%f %f %f %f %f %f %f\n",&angle[0], &angle[1],&angle[2],&angle[3],&angle[4],&angle[5],&angle[6],&angle[7]);
printf("%f %f %f %f %f %f %f\n",angle[0], angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='4') angleID=4;
if(key=='5') angleID=5;
if(key=='6') angleID=6;
if(key=='7') angleID=7;
if(key=='8') angleID=8;
if(key=='s'){
if(fout==NULL) fout = fopen("file.txt","w+");
printf("%f %f %f %f %f %f %f\n",angle[0], angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
fprintf(fout,"%f %f %f %f %f %f %f\n",angle[0], angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
}
if(key=='r'){
if(fin==NULL) fin = fopen("file.txt","r");
fscanf(fin,"%f %f %f %f %f %f %f\n",&angle[0], &angle[1],&angle[2],&angle[3],&angle[4],&angle[5],&angle[6],&angle[7]);
printf("%f %f %f %f %f %f %f\n",angle[0], angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
}
if(key=='p'){
glutTimerFunc(1000,timer,0);
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week12");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();//不要忘了加哦!
glutMainLoop();
}
2014年5月16日 星期五
8個關節可動
參數加入-lcv -lcxcore -lhighgui
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <stdio.h>
//IplImage *img1, *img2, *img3, *img4;
//GLuint id1, id2, id3, id4;
IplImage * img[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
GLuint id[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
//float angle1=0, angle2=0, angle3=0, angle4=0;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int angleID=1;
int oldX,oldY;
void mouse(int botton, int state, int x, int y)
{
oldX=x;
oldY=y;
}
void motion(int x, int y)
{
angle[angleID]+= x-oldX; oldX=x;
glutPostRedisplay();
}
void myPrepareTexture(int i)//用參數來配, 找對應正確的變數
{//技巧3: 可以把重覆的東西, 用函式來簡化
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img[i]->width, img[i]->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img[i]->imageData);
}
void myTexture()
{
img[0]=cvLoadImage("imgbody.png");
img[1]=cvLoadImage("imgHand1.png");
img[2]=cvLoadImage("imgLeg1.png");
for(int i=0;i<3;i++){//技巧2: 可以把重覆的東西, 用迴圈來簡化
myPrepareTexture(i);
}
}
void myPolygon(int num){//技巧3: 可以把重覆的東西, 用函式來簡化
glPushMatrix();
glScalef(0.3, 0.8, 0.8);
glBindTexture(GL_TEXTURE_2D, id[num]);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex3f( -0.5, 0.5, 0);
glTexCoord2f(1, 0); glVertex3f( 0.5, 0.5, 0);
glTexCoord2f(1, 1); glVertex3f( 0.5, -0.5, 0);
glTexCoord2f(0, 1); glVertex3f( -0.5, -0.5, 0);
glEnd();
glPopMatrix();
}
void myBody(){
glPushMatrix();
glScalef(0.7, 0.7, 0.7);
myPolygon(0);
glPopMatrix();
}
void myLeg(){
glPushMatrix();
glScalef(0.5, 0.5, 0.5);
myPolygon(1);
glPopMatrix();
}
void myHand(){
glPushMatrix();
glScalef(0.5, 0.5, 0.5);
myPolygon(2);
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
myBody();
glPushMatrix();
glTranslatef(0.1,0.1,0);//右手
glRotatef(angle[1], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();
glTranslatef(0,-0.2,0);//右手1
glRotatef(angle[2], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.1,0.1,0);//左手
glRotatef(-angle[3], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();
glTranslatef(0,-0.2,0);//左手1
glRotatef(-angle[4], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.1,-0.2,0);//左腿
glRotatef(-angle[5], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myLeg();
glPushMatrix();
glTranslatef(0,-0.2,0);//左腿1
glRotatef(-angle[6], 0,0,1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.1,-0.2,0);//右腿
glRotatef(-angle[7], 0,0,-1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myLeg();
glPushMatrix();
glTranslatef(0,-0.2,0);//右腿1
glRotatef(-angle[8], 0,0,-1);
glutWireSphere(0.1,20,20);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='4') angleID=4;
if(key=='5') angleID=5;
if(key=='6') angleID=6;
if(key=='7') angleID=7;
if(key=='8') angleID=8;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week12");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();//不要忘了加哦!
glutMainLoop();
}
2014年5月2日 星期五
week11
第一種方法:
#include <GL/glut.h>
float angleX=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX ,0,1,0);
glutWireTeapot(0.4);
glTranslatef(0.5,0,0);
glutSolidSphere(0.2,30,30);
glTranslatef(-0.9,0,0);//依照上一個位置來繼續推
glutSolidSphere(0.2,30,30);
glTranslatef(0.4,0.4,0);
glutSolidSphere(0.2,30,30);
glTranslatef(0.3,-0.8,0);
glutSolidSphere(0.2,30,30);
glTranslatef(-0.6,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glutSwapBuffers();
}
void keyboard (unsigned char key , int x,int y)
{
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] ={2.0f,5.0f,-5.0f,0.0f};
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
手腳身體轉動
#include <GL/glut.h>
float angleX=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX ,0,1,0);
glutWireTeapot(0.4);
glTranslatef(0.5,0,0);
glutSolidSphere(0.2,30,30);
glTranslatef(-0.9,0,0);//依照上一個位置來繼續推
glutSolidSphere(0.2,30,30);
glTranslatef(0.4,0.4,0);
glutSolidSphere(0.2,30,30);
glTranslatef(0.3,-0.8,0);
glutSolidSphere(0.2,30,30);
glTranslatef(-0.6,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glutSwapBuffers();
}
void keyboard (unsigned char key , int x,int y)
{
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] ={2.0f,5.0f,-5.0f,0.0f};
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
手腳身體轉動
#include <GL/glut.h>
float angleX=0,angle1=0,angle3=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX ,0,1,0);
glutWireTeapot(0.3);
glPushMatrix(); //右肩
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();//右手
glRotatef(angle1 ,0,0,1);
glTranslatef(0.3,0,0);
glScalef(2,0.5,0.5);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix(); //左肩
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();//左手
glRotatef(angle1 ,0,0,1);
glTranslatef(-0.3,0,0);
glScalef(2,0.5,0.5);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右腳
glTranslatef(0.2,-0.3,0);
glRotatef(angle3,1,0,0);
glTranslatef(0,-0.25,0);
glPushMatrix();
glScalef(0.2,0.5,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左腳
glTranslatef(-0.2,-0.3,0);
glRotatef(angle3,1,0,0);
glTranslatef(0,-0.25,0);
glPushMatrix();
glScalef(0.2,0.5,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard (unsigned char key , int x,int y)
{
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
if(key=='1') angle1+=3;
if(key=='2') angle1-=3;
if(key=='3') angle3+=5;
if(key=='4') angle3-=5;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] ={2.0f,5.0f,-5.0f,0.0f};
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
2014年4月25日 星期五
一般
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void keyboard(unsigned char key , int x , int y)
{
printf("%c %d %d\n",key,x,y);
if(key=='a') angle+=3;
if(key=='b') angle-=3;
glutPostRedisplay();
//glutSwapBuffers();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void keyboard(unsigned char key , int x , int y)
{
printf("%c %d %d\n",key,x,y);
if(key=='a') angle+=3;
if(key=='b') angle-=3;
glutPostRedisplay();
//glutSwapBuffers();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
打光
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void keyboard(unsigned char key , int x , int y)
{
printf("%c %d %d\n",key,x,y);
if(key=='a') angle+=3;
if(key=='b') angle-=3;
glutPostRedisplay();
//glutSwapBuffers();
}
void myLight()
{
const GLfloat light_position[] = {2.0f, 5.0f,0.0f,0.0f};
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
用滑鼠轉動(x軸)
#include <GL/glut.h>
#include <stdio.h>
float angleX=0, startX=0;
void mouse(int button,int state , int x , int y)
{
printf("%c %d %d\n",button,state,x,y);
startX=x;
glutPostRedisplay();
//glutSwapBuffers();
}
void motion(int x,int y)
{
printf("%d %d\n,x,y");
angleX=x - startX;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] = {2.0f, 5.0f,0.0f,0.0f};
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef(angleX,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}
#include <GL/glut.h>
#include <stdio.h>
float angleX=0, startX=0;
void mouse(int button,int state , int x , int y)
{
printf("%c %d %d\n",button,state,x,y);
startX=x;
glutPostRedisplay();
//glutSwapBuffers();
}
void motion(int x,int y)
{
printf("%d %d\n,x,y");
angleX=x - startX;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] = {2.0f, 5.0f,0.0f,0.0f};
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef(angleX,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}
2014年3月28日 星期五
week06
#include <opencv/highgui.h>
#include <GL/glut.h>
#include <opencv/cv.h>
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main()
{
/*IplImage*img=cvLoadImage("earth.jpg");
cvNamedWindow("week06");
cvShowImage("week06",img);
cvWaitKey(0);*/
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("01160465");
glutDisplayFunc(display);
glEnable(GL_TEXTURE_2D);
IplImage*img=cvLoadImage("earth.jpg");
cvCvtColor(img,img,CV_BGR2RGB);
GLuint id;
glGenTextures(1,&id);
glBindTexture(GL_TEXTURE_2D,id);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,img->width,img->height,0,GL_RGB,GL_UNSIGNED_BYTE,img->imageData);
glutMainLoop();
}
2014年3月21日 星期五
2014年3月14日 星期五
2014年3月7日 星期五
訂閱:
文章 (Atom)



















