#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;///指標body
GLMmodel* ppmodel = NULL;///指標head
GLMmodel* pppmodel = NULL;///指標hand
GLMmodel* ppppmodel = NULL;///指標legleft
GLMmodel* pppppmodel = NULL;///指標legright
GLMmodel* ppppppmodel = NULL;///指標handright
float angle=0;float anglea=90;
int t=1;
///打光0 : 宣告陣列,裡面是光的屬性值
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 };
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 display()
{
glClearColor((255/255.0),(214/255.0),(160/255.0),0.1);///
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景
if (pmodel==NULL) {
pmodel = glmReadOBJ("body.obj");///讀入3D模型
glmUnitize(pmodel);///調整單位大小
glmFacetNormals(pmodel);///正確法向量
glmVertexNormals(pmodel, 90.0);///算出頂點(先有面才有點)
}
if (ppmodel==NULL) {///head
ppmodel = glmReadOBJ("head.obj");///讀入3D模型
glmUnitize(ppmodel);///調整單位大小
glmFacetNormals(ppmodel);///正確法向量
glmVertexNormals(ppmodel, 90.0);///算出頂點(先有面才有點)
}
if (pppmodel==NULL) {///hand
pppmodel = glmReadOBJ("hand.obj");///讀入3D模型
glmUnitize(pppmodel);///調整單位大小
glmFacetNormals(pppmodel);///正確法向量
glmVertexNormals(pppmodel, 90.0);///算出頂點(先有面才有點)
}
if (ppppppmodel==NULL) {///hand
ppppppmodel = glmReadOBJ("hand.obj");///讀入3D模型
glmUnitize(ppppppmodel);///調整單位大小
glmFacetNormals(ppppppmodel);///正確法向量
glmVertexNormals(ppppppmodel, 90.0);///算出頂點(先有面才有點
}
if (ppppmodel==NULL) {///leg
ppppmodel = glmReadOBJ("leg.obj");///讀入3D模型
glmUnitize(ppppmodel);///調整單位大小
glmFacetNormals(ppppmodel);///正確法向量
glmVertexNormals(ppppmodel, 90.0);///算出頂點(先有面才有點)
}
if (pppppmodel==NULL) {///leg
pppppmodel = glmReadOBJ("leg.obj");///讀入3D模型
glmUnitize(pppppmodel);///調整單位大小
glmFacetNormals(pppppmodel);///正確法向量
glmVertexNormals(pppppmodel, 90.0);///算出頂點(先有面才有點)
}
glPushMatrix();
{
glScalef(0.4,0.4,0.4);
glRotatef(180,0,1,0);///模型旋轉180度變成圖二
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);///畫
}
glPushMatrix();///head
{
glTranslatef(0,1.2,0);//由下面往上做所以最後把旋轉的以下所有東西移到(0.5,0.5)的地方
glRotatef(angle, 0,1,0);//以z軸旋轉
glTranslatef(0,0,0);//由下面往上做所以會先移動把軸心以x軸移動0.25
glScalef(0.8,0.4,0.4);
glRotatef(270,0,1,0);///模型旋轉180度變成圖二
glmDraw(ppmodel, GLM_SMOOTH | GLM_MATERIAL);///畫
}
glPopMatrix();
glPushMatrix();///leftthand
{
glTranslatef(0,1.2,0);//由下面往上做所以最後把旋轉的以下所有東西移到(0.5,0.5)的地方
glRotatef(angle, 0,0,1);//以z軸旋轉
glTranslatef(1,-0.6,0);//由下面往上做所以會先移動把軸心以x軸移動0.25
glScalef(0.8,0.8,0.8);
glRotatef(90,0,0,1);
glmDraw(pppmodel, GLM_SMOOTH | GLM_MATERIAL);///畫
}
glPopMatrix();
glPushMatrix();///righthand
{
glTranslatef(0,1.2,0);//由下面往上做所以最後把旋轉的以下所有東西移到(0.5,0.5)的地方
glRotatef(-angle, 0,0,1);//以z軸旋轉
glTranslatef(-1,-0.6,0);//由下面往上做所以會先移動把軸心以x軸移動0.25
glScalef(0.8,0.8,0.8);
glRotatef(270,0,0,1);
glmDraw(pppmodel, GLM_SMOOTH | GLM_MATERIAL);///畫
}
glPopMatrix();
glPushMatrix();///leg
{
glTranslatef(0,-1,0);//由下面往上做所以最後把旋轉的以下所有東西移到(0.5,0.5)的地方
glRotatef(-angle, 1,0,0);//以z軸旋轉
glTranslatef(-0.5,-0.5,0);//由下面往上做所以會先移動把軸心以x軸移動0.25
glScalef(0.8,0.8,0.8);
glRotatef(90,0,1,0);///模型旋轉180度變成圖二
glmDraw(ppppmodel, GLM_SMOOTH | GLM_MATERIAL);///畫
}
glPopMatrix();
glPushMatrix();///legright
{
glTranslatef(0,-1,0);//由下面往上做所以最後把旋轉的以下所有東西移到(0.5,0.5)的地方
glRotatef(-anglea, 1,0,0);//以z軸旋轉
glTranslatef(0.4,-0.5,0);//由下面往上做所以會先移動把軸心以x軸移動0.25
glScalef(0.8,0.8,0.8);
glRotatef(90,0,1,0);///模型旋轉180度變成圖二
glmDraw(ppppmodel, GLM_SMOOTH | GLM_MATERIAL);///畫
}
glPopMatrix();
glPopMatrix();
if(angle==90)
t=2;
else if(angle==0)
t=1;
if(t==1)
{
angle++;
anglea--;
}
else
{
angle--;
anglea++;
}
glutSwapBuffers();
}
int main(int argc, char**argv) ///高手等級的main()
{
glutInit(&argc,argv);///main的參數,於近來
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///啟動3D的顯示能力,兩倍顯示buffer,有3D深度值
glutCreateWindow("week010!!!"); ///建立3D窗子
glutDisplayFunc(display); ///等一下怎麼畫
///打光要3D的depth test
glutIdleFunc(display);
glEnable(GL_DEPTH_TEST);
///打光1 : 開啟光線
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
///打光2 : 設定light值 (拿前面的陣列來用)
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);
///打光 2-2 : 設定物體material
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(); ///主要迴圈卡在這裡,不會結束
}
讓codeblock可以辨識些副檔名
#include <GL/glut.h>
#include "glm.h"
GLMmodel *pmodelA=NULL;///身體, Al.obj
GLMmodel *pmodelB=NULL;///手臂, porsche.obj
GLMmodel *pmodelC=NULL;///手臂, porsche.obj
GLMmodel *pmodelD=NULL;
GLMmodel *pmodelE=NULL;
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 };
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 drawBody()
{
if(pmodelA==NULL){
pmodelA=glmReadOBJ("Al.obj");///執行目錄裡,有個Al.obj檔+Al.mtl檔
glmUnitize(pmodelA);///設成單位大小
glmFacetNormals(pmodelA);///重算(面)法向量
glmVertexNormals(pmodelA, 90);///重算(點)法向量
}///讀完OBJ,且設好了...
glPushMatrix();
glRotatef(180, 0,1,0);///對Y轉180度,向正面
glmDraw(pmodelA, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
int angle=30;
void drawArm()
{
if(pmodelB==NULL){
pmodelB=glmReadOBJ("porsche.obj");
glmUnitize(pmodelB);
glmFacetNormals(pmodelB);
glmVertexNormals(pmodelB, 90);
}
glPushMatrix();
glRotatef(90, 0,1,0);
glScalef(0.3, 0.3, 0.3);
glmDraw(pmodelB, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///glutSolidSphere(0.1, 30,30);///原點、圓心
drawBody();
glPushMatrix();///右手臂
glTranslatef(0.5, 0.4, 0);///第3個T
glRotatef(angle, 0,0,1);///第2個R
glTranslatef(0.3, 0, 0);///第1個T
drawArm();///右上手臂
glPushMatrix();
glTranslatef(0.25, 0, 0);///第3個T
glRotatef(angle, 0,0,1);///第2個R
glTranslatef(0.3, 0, 0);///第1個T
drawArm();///右下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左手臂
glTranslatef(-0.5, 0.4, 0);///第3個T
glRotatef(angle, 0,0,1);///第2個R
glTranslatef(-0.3, 0, 0);///第1個T
drawArm();///左上手臂
glPushMatrix();
glTranslatef(-0.25, 0, 0);///第3個T
glRotatef(angle, 0,0,1);///第2個R
glTranslatef(-0.3, 0, 0);///第1個T
drawArm();///左下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{///TODO3:
oldX=x;
}
void motion(int x, int y)///TODO3:
{///TODO3:
angle += (x-oldX);///TODO3:
oldX=x;///TODO3:
glutPostRedisplay();///TODO3:
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMotionFunc(motion);///TODO3:
glutMouseFunc(mouse);///TODO3:
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();
}
#include <GL/glut.h>
#include "glm.h"
GLMmodel *pmodelA=NULL;///身體, Al.obj
GLMmodel *pmodelB=NULL;///手臂, porsche.obj
GLMmodel *pmodelC=NULL;///手臂, porsche.obj
GLMmodel *pmodelD=NULL;
GLMmodel *pmodelE=NULL;
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 };
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 drawBody()
{
if(pmodelA==NULL){
pmodelA=glmReadOBJ("Al.obj");///執行目錄裡,有個Al.obj檔+Al.mtl檔
glmUnitize(pmodelA);///設成單位大小
glmFacetNormals(pmodelA);///重算(面)法向量
glmVertexNormals(pmodelA, 90);///重算(點)法向量
}///讀完OBJ,且設好了...
glPushMatrix();
glRotatef(180, 0,1,0);///對Y轉180度,向正面
glmDraw(pmodelA, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm()
{
if(pmodelB==NULL){
pmodelB=glmReadOBJ("porsche.obj");
glmUnitize(pmodelB);
glmFacetNormals(pmodelB);
glmVertexNormals(pmodelB, 90);
}
glPushMatrix();
glRotatef(90, 0,1,0);
glScalef(0.3, 0.3, 0.3);
glmDraw(pmodelB, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
int angle[4]={30,30,30,30};///每個關節角度,都希望可以自己動///TODO4
int angleID=0;///現在在動第???個關節 ///TODO4 決定用 keyboard 來幫我們切換關節的ID
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///glutSolidSphere(0.1, 30,30);///原點、圓心
drawBody();
glPushMatrix();///右手臂
glTranslatef(0.5, 0.4, 0);///第3個T
glRotatef(angle[0], 0,0,1);///第2個R///TODO4
glTranslatef(0.3, 0, 0);///第1個T
drawArm();///右上手臂
glPushMatrix();
glTranslatef(0.25, 0, 0);///第3個T
glRotatef(angle[1], 0,0,1);///第2個R///TODO4
glTranslatef(0.3, 0, 0);///第1個T
drawArm();///右下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左手臂
glTranslatef(-0.5, 0.4, 0);///第3個T
glRotatef(angle[2], 0,0,1);///第2個R///TODO4
glTranslatef(-0.3, 0, 0);///第1個T
drawArm();///左上手臂
glPushMatrix();
glTranslatef(-0.25, 0, 0);///第3個T
glRotatef(angle[3], 0,0,1);///第2個R ///TODO4
glTranslatef(-0.3, 0, 0);///第1個T
drawArm();///左下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{///TODO3:
oldX=x;
}
void motion(int x, int y)///TODO3:
{///TODO3:
angle[angleID] += (x-oldX);///TODO4
oldX=x;///TODO3:
glutPostRedisplay();///TODO3:
}
void keyboard(unsigned char key, int x, int y)///TODO4
{///TODO4
if(key=='0') angleID=0;///TODO4
if(key=='1') angleID=1;///TODO4
if(key=='2') angleID=2;///TODO4
if(key=='3') angleID=3;///TODO4
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMotionFunc(motion);///TODO3:
glutMouseFunc(mouse);///TODO3:
glutKeyboardFunc(keyboard);///TODO4:
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();
}



沒有留言:
張貼留言