2014 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2014年4月11日 星期五
01160554
#include <GL/glut.h>
#include "glm.h"
float angle = 0;
GLMmodel* pmodel = NULL;
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();
}
void eye()
{
glPushMatrix();
glColor3f(1, 1, 1);
glutSolidSphere(0.1, 20, 20);
glColor3f(0, 0, 0);
glTranslatef(0, 0, 0.1);
glutSolidSphere(0.02, 10, 10);
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 1, 0);
glRotatef(-90, 1, 0, 0);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glutSwapBuffers();
}
void idle()
{
angle++;
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key == ' ')
angle+=20;
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 };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
int main()
{
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("week08");
glutDisplayFunc(display);
glutIdleFunc(idle);//閒置時執行
glutKeyboardFunc(keyboard);
//開啟燈光
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);
//讀取obj檔
pmodel = glmReadOBJ("bear.obj");
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
glutMainLoop();
}
//////////////////////////////
timer作法
void timer(int t)
{
glutTimerFunc(10, timer, t+1);
angle += 5;
glutPostRedisplay();
}
int main()
{
///....
glutTimerFunc(1500, timer, 0);
///....
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言