鍵盤"1"&"2"控制水平方向旋轉角度
#include <GL/glut.h>
float angleX =0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=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);
}
void display()
{
glEnable(GL_DEPTH_TEST);
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("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
增加球,讓球跟著茶壺轉~
#include <GL/glut.h>
float angleX =0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=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);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glTranslatef(0.5, 0, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glTranslatef(-0.5, 0, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glTranslatef(0, 0.5, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glTranslatef(0, -0.5, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glTranslatef(0.5, -0.5, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
根目錄子目錄概念,階層式管理。
#include <GL/glut.h>
float angleX =0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=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);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();//最外層再用一組Push& PopMatrix包起來做保護
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glTranslatef(0.5, 0, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glTranslatef(-0.5, 0, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glTranslatef(0, 0.5, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glTranslatef(0, -0.5, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glTranslatef(0.5, -0.5, 0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPopMatrix();//階層式管理
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
================================================================
#include <GL/glut.h>
float angleX =0, angle2=0, angle3=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3') angle2+=5;
if(key=='4') angle2-=5;
if(key=='5') angle3+=5;
if(key=='6') 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);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();//最外層再用一組Push& PopMatrix包起來做保護
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPushMatrix();//右邊肩膀
glTranslatef(0.5, 0, 0);
glutSolidSphere(0.2, 30, 30);
glPushMatrix();//右手臂
glRotatef(angle2, 0,0,1);
glTranslatef(0.2, 0, 0);
glScalef(0.4, 0.2, 0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左邊肩膀
glTranslatef(-0.5, 0, 0);
glutSolidSphere(0.2, 30, 30);
glPushMatrix();//右手臂
glRotatef(angle2, 0,0,1);
glTranslatef(-0.2, 0, 0);
glScalef(0.4, 0.2, 0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0, 0.4, 0);
glutSolidSphere(0.25, 30, 30);
glPopMatrix();
glPushMatrix();//下面
glTranslatef(0, -0.25, 0);
glutSolidSphere(0.25, 30, 30);
glPushMatrix();//左大腿
glRotatef(angle3, 1,0,0);
glTranslatef(-0.1, -0.2, 0);
glScalef(0.15, 0.3, 0.2);
glutSolidCube(1);
glPushMatrix();//左膝蓋
glRotatef(angle3, 1,0,0);
glTranslatef(-0.1, -0.38, 0);
glutSolidSphere(0.09,30,30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右大腿
glRotatef(angle3, 1,0,0);
glTranslatef(0.1, -0.2, 0);
glScalef(0.15, 0.3, 0.2);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//右膝蓋
glRotatef(angle2, 0,0,1);
glTranslatef(0.1, -0.38, 0);
glutSolidSphere(0.09,30,30);
glPopMatrix();
glPopMatrix();
glPopMatrix();//階層式管理
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
沒有留言:
張貼留言