(一)上週作業
(二)讓機器人
(三)Timer計時器
(四)鍵盤滑鼠控制
(五)機器人
- 作業一 : 計時器
步驟二 : 改程式碼,加入計時器。
程式 :
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle = 0 ;
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
angle++;
glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPopMatrix( );
glutSwapBuffers( );
}
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 };
#include <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
///glutIdleFunc(display);
glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
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();
}
步驟三 : 改程式碼,用計時器試著讓手臂旋轉。
程式 :
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle = 0 ;
void drawBody()
{
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm0()
{
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm1()
{
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
//angle++;
//glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix();
//drawBody();
//glutSolidTeapot(0.03);
glPushMatrix();///Left Part
glTranslatef(-0.22, +0.35, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1); ///(1) 先轉動
glTranslatef(0.22, -0.35, 0);///(2) move the rotate center to center
drawArm0();
glTranslatef(-0.48, +0.1, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1);///(1) 先轉動
glTranslatef(0.48, -0.1, 0);///(2) 接著將旋轉中心,放到真的中心, 記得把樓上先註解掉
drawArm1();
glPopMatrix();
glPopMatrix();
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPopMatrix( );
glutSwapBuffers( );
}
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 };
#include <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
///glutIdleFunc(display);
glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
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();
}
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle = 0 ;
void drawBody()
{
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm0()
{
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm1()
{
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
//angle++;
//glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix();
//drawBody();
//glutSolidTeapot(0.03);
glPushMatrix();///Left Part
glTranslatef(-0.22, +0.35, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1); ///(1) 先轉動
glTranslatef(0.22, -0.35, 0);///(2) move the rotate center to center
drawArm0();
glTranslatef(-0.48, +0.1, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1);///(1) 先轉動
glTranslatef(0.48, -0.1, 0);///(2) 接著將旋轉中心,放到真的中心, 記得把樓上先註解掉
drawArm1();
glPopMatrix();
glPopMatrix();
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPopMatrix( );
glutSwapBuffers( );
}
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 };
#include <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
///glutIdleFunc(display);
glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
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();
}
步驟四 : 讓兩隻手臂旋轉。
程式 :
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle = 0 ;
void drawBody()
{
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm0()
{
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm1()
{
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawRArm()
{
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRhand()
{
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawHead()
{
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRleg()
{
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
//angle++;
//glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix();
//drawBody();
glutSolidTeapot(0.03);
glPushMatrix();///Left Part
glTranslatef(-0.22, +0.35, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1); ///(1) 先轉動
glTranslatef(0.22, -0.35, 0);///(2) move the rotate center to center
drawArm0();
glTranslatef(-0.48, +0.1, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1);///(1) 先轉動
glTranslatef(0.48, -0.1, 0);///(2) 接著將旋轉中心,放到真的中心, 記得把樓上先註解掉
drawArm1();
glPopMatrix();
glPushMatrix();///Right Part
glTranslatef(+0.2, +0.36, 0);///3
glRotatef(angle, 0,0,1); ///(1)
glTranslatef(-0.2, -0.36, 0);///(2)
drawRArm();
glTranslatef(+0.48, +0.1, 0);///(3)
glRotatef(angle, 0,0,1);///(1)
glTranslatef(-0.48, -0.1, 0);///(2)
drawRhand();
glPopMatrix();
glPopMatrix();
drawHead();
drawRleg();
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPopMatrix( );
glutSwapBuffers( );
}
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 };
#include <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
///glutIdleFunc(display);
glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
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();
}
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle = 0 ;
void drawBody()
{
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm0()
{
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm1()
{
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawRArm()
{
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRhand()
{
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawHead()
{
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRleg()
{
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
//angle++;
//glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix();
//drawBody();
glutSolidTeapot(0.03);
glPushMatrix();///Left Part
glTranslatef(-0.22, +0.35, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1); ///(1) 先轉動
glTranslatef(0.22, -0.35, 0);///(2) move the rotate center to center
drawArm0();
glTranslatef(-0.48, +0.1, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1);///(1) 先轉動
glTranslatef(0.48, -0.1, 0);///(2) 接著將旋轉中心,放到真的中心, 記得把樓上先註解掉
drawArm1();
glPopMatrix();
glPushMatrix();///Right Part
glTranslatef(+0.2, +0.36, 0);///3
glRotatef(angle, 0,0,1); ///(1)
glTranslatef(-0.2, -0.36, 0);///(2)
drawRArm();
glTranslatef(+0.48, +0.1, 0);///(3)
glRotatef(angle, 0,0,1);///(1)
glTranslatef(-0.48, -0.1, 0);///(2)
drawRhand();
glPopMatrix();
glPopMatrix();
drawHead();
drawRleg();
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPopMatrix( );
glutSwapBuffers( );
}
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 };
#include <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
///glutIdleFunc(display);
glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
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();
}
作業二 :滑鼠操控
步驟一 : 從剛剛的程式修改,按住滑鼠移動,手臂跟著移動。
程式 :
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle = 0 ;
void drawBody()
{
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm0()
{
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm1()
{
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawRArm()
{
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRhand()
{
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawHead()
{
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRleg()
{
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawLleg()
{
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRfoot()
{
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawLfoot()
{
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
//angle++;
//glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix();
//drawBody();
glutSolidTeapot(0.03);
glPushMatrix();///Left Part
glTranslatef(-0.22, +0.35, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1); ///(1) 先轉動
glTranslatef(0.22, -0.35, 0);///(2) move the rotate center to center
drawArm0();
glTranslatef(-0.48, +0.1, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1);///(1) 先轉動
glTranslatef(0.48, -0.1, 0);///(2) 接著將旋轉中心,放到真的中心, 記得把樓上先註解掉
drawArm1();
glPopMatrix();
glPushMatrix();///Right Part
glTranslatef(+0.2, +0.36, 0);///3
glRotatef(angle, 0,0,1); ///(1)
glTranslatef(-0.2, -0.36, 0);///(2)
drawRArm();
glTranslatef(+0.48, +0.1, 0);///(3)
glRotatef(angle, 0,0,1);///(1)
glTranslatef(-0.48, -0.1, 0);///(2)
drawRhand();
glPopMatrix();
glPopMatrix();
drawHead();
drawRleg();
drawRfoot();
drawLleg();
drawLfoot();
glPopMatrix( );
glutSwapBuffers( );
}
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 };
#include <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int oldX=0, oldY=0;
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
glutPostRedisplay();
}
void motion(int x, int y)
{
angle += (x-oldX);
oldX=x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
///glutIdleFunc(display);
///glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
///glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
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();
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle = 0 ;
void drawBody()
{
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm0()
{
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm1()
{
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawRArm()
{
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRhand()
{
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawHead()
{
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRleg()
{
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawLleg()
{
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRfoot()
{
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawLfoot()
{
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
//angle++;
//glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix();
//drawBody();
glutSolidTeapot(0.03);
glPushMatrix();///Left Part
glTranslatef(-0.22, +0.35, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1); ///(1) 先轉動
glTranslatef(0.22, -0.35, 0);///(2) move the rotate center to center
drawArm0();
glTranslatef(-0.48, +0.1, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle, 0,0,1);///(1) 先轉動
glTranslatef(0.48, -0.1, 0);///(2) 接著將旋轉中心,放到真的中心, 記得把樓上先註解掉
drawArm1();
glPopMatrix();
glPushMatrix();///Right Part
glTranslatef(+0.2, +0.36, 0);///3
glRotatef(angle, 0,0,1); ///(1)
glTranslatef(-0.2, -0.36, 0);///(2)
drawRArm();
glTranslatef(+0.48, +0.1, 0);///(3)
glRotatef(angle, 0,0,1);///(1)
glTranslatef(-0.48, -0.1, 0);///(2)
drawRhand();
glPopMatrix();
glPopMatrix();
drawHead();
drawRleg();
drawRfoot();
drawLleg();
drawLfoot();
glPopMatrix( );
glutSwapBuffers( );
}
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 };
#include <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int oldX=0, oldY=0;
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
glutPostRedisplay();
}
void motion(int x, int y)
{
angle += (x-oldX);
oldX=x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
///glutIdleFunc(display);
///glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
///glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
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();
步驟二 : 改程式,讓一隻手隨滑鼠移動。
程式 :
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle[20] = {} ;///全家都是零
int now=0;
void drawBody()
{
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm0()
{
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm1()
{
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawRArm()
{
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRhand()
{
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawHead()
{
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRleg()
{
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawLleg()
{
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRfoot()
{
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawLfoot()
{
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
//angle++;
//glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix();
//drawBody();
glutSolidTeapot(0.03);
glPushMatrix();///Left Part
glTranslatef(-0.22, +0.35, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle[0], 0,0,1); ///(1) 先轉動
glTranslatef(0.22, -0.35, 0);///(2) move the rotate center to center
drawArm0();
glTranslatef(-0.48, +0.1, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle[1], 0,0,1);///(1) 先轉動
glTranslatef(0.48, -0.1, 0);///(2) 接著將旋轉中心,放到真的中心, 記得把樓上先註解掉
drawArm1();
glPopMatrix();
glPushMatrix();///Right Part
glTranslatef(+0.2, +0.36, 0);///3
glRotatef(angle[2], 0,0,1); ///(1)
glTranslatef(-0.2, -0.36, 0);///(2)
drawRArm();
glTranslatef(+0.48, +0.1, 0);///(3)
glRotatef(angle[3], 0,0,1);///(1)
glTranslatef(-0.48, -0.1, 0);///(2)
drawRhand();
glPopMatrix();
glPopMatrix();
drawHead();
drawRleg();
drawRfoot();
drawLleg();
drawLfoot();
glPopMatrix( );
glutSwapBuffers( );
}
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 };
#include <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
//angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int oldX=0, oldY=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='0')now=0;
if(key=='1')now=1;
if(key=='2')now=2;
if(key=='3')now=3;
if(key=='4')now=4;
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
glutPostRedisplay();
}
void motion(int x, int y)
{
angle[now] += (x-oldX);
oldX=x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
///glutIdleFunc(display);
///glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
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();
}
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle[20] = {} ;///全家都是零
int now=0;
void drawBody()
{
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm0()
{
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawArm1()
{
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
}
void drawRArm()
{
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRhand()
{
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawHead()
{
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRleg()
{
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawLleg()
{
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawRfoot()
{
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void drawLfoot()
{
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
}
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
//angle++;
//glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix();
//drawBody();
glutSolidTeapot(0.03);
glPushMatrix();///Left Part
glTranslatef(-0.22, +0.35, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle[0], 0,0,1); ///(1) 先轉動
glTranslatef(0.22, -0.35, 0);///(2) move the rotate center to center
drawArm0();
glTranslatef(-0.48, +0.1, 0);///(3) 最後,把它掛在對的位置
glRotatef(-angle[1], 0,0,1);///(1) 先轉動
glTranslatef(0.48, -0.1, 0);///(2) 接著將旋轉中心,放到真的中心, 記得把樓上先註解掉
drawArm1();
glPopMatrix();
glPushMatrix();///Right Part
glTranslatef(+0.2, +0.36, 0);///3
glRotatef(angle[2], 0,0,1); ///(1)
glTranslatef(-0.2, -0.36, 0);///(2)
drawRArm();
glTranslatef(+0.48, +0.1, 0);///(3)
glRotatef(angle[3], 0,0,1);///(1)
glTranslatef(-0.48, -0.1, 0);///(2)
drawRhand();
glPopMatrix();
glPopMatrix();
drawHead();
drawRleg();
drawRfoot();
drawLleg();
drawLfoot();
glPopMatrix( );
glutSwapBuffers( );
}
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 };
#include <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
//angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int oldX=0, oldY=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='0')now=0;
if(key=='1')now=1;
if(key=='2')now=2;
if(key=='3')now=3;
if(key=='4')now=4;
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
glutPostRedisplay();
}
void motion(int x, int y)
{
angle[now] += (x-oldX);
oldX=x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
///glutIdleFunc(display);
///glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
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();
}
沒有留言:
張貼留言