課堂練習二: 用 matrix 保護後, 做旋轉、移動、放大縮小
課堂練習三:使徒可以旋轉
#include<GL/glut.h>
void pencil()
{
glBegin(GL_POLYGON);
glColor3f(1,1,0);
glVertex2f(0.2,0.8);
glVertex2f(-0.2,0.8);
glVertex2f(-0.2,-0.2);
glVertex2f(0.2,-0.2);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1,0,0);
glVertex2f(-0.2,1);
glVertex2f(0.2,1);
glVertex2f(0.2,0.8);
glVertex2f(-0.2,0.8);
glEnd();
glColor3f(1,0.5,0.5);
glPushMatrix();
glTranslatef(0,-0.2,0);
glRotatef(90,1,0,0);
glutSolidCone(0.2,0.6,10,10);
glPopMatrix();
glColor3f(0,0,0);
glPushMatrix();
glTranslatef(0,-0.5,0);
glRotatef(90,1,0,0);
glutSolidCone(0.1,0.3,10,10);
glPopMatrix();
}
void display()
{
glClearColor(0.5,1,0.5,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutIdleFunc(display);
glRotatef(1,1,1,0);
pencil();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("00161045_Shape");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
#include<GL/glut.h>
float rot=0;
void wheel(float x, float y, float z)
{
glColor3f(1,0.7,2);
glPushMatrix();
glTranslatef(x,y,z);
glutSolidSphere(0.1,10,10);
glPopMatrix();
}
void lamp(float x, float y, float z)
{
glColor3f(0.2,0.6,2);
glPushMatrix();
glTranslatef(x,y,z);
glutSolidSphere(0.1,10,10);
glPopMatrix();
}
void car()
{
glPushMatrix();
glScalef(1,0.3,0.5);
glColor3f(0,1,0);
glutWireCube(1);
glColor3f(1,0.2,0.1);
glutSolidCube(1);
glPopMatrix();
wheel(0.2,-0.15,0.2);
wheel(-0.3,-0.15,0.2);
wheel(-0.3,-0.15,-0.2);
wheel(0.2,-0.15,-0.2);
lamp(0.5,0.004,0.15);
lamp(0.5,0.004,-0.15);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClearColor(0.8,1,0.8,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(-10,1,0,0);
glRotatef(rot,0,1,0);
car();
glPopMatrix();
glutSwapBuffers();
}
void idle()
{
rot++;
display();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("00161045_Shape");
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
沒有留言:
張貼留言