OpenGL绘制的桌子---比较初级

来源:互联网 发布:西安全球程序员节直播 编辑:程序博客网 时间:2024/04/27 17:51
#include <GL/glut.h>#include <stdlib.h>float fTranslate;float fRotate;float fScale     = 1.0f;void Draw_Table() {glPushMatrix();glScalef (5.0, 4.0, 1.0);glutWireCube(1.0);glPopMatrix();glPushMatrix();glTranslatef(0.0,0.0,-2.0);glPushMatrix();glTranslatef(1.5,1.0,0.0);glScalef (1.0, 1.0, 3.0);glutWireCube(1.0);glPopMatrix();glPushMatrix();glTranslatef(1.5,-1.0,0.0);glScalef (1.0, 1.0, 3.0);glutWireCube(1.0);glPopMatrix();glPushMatrix();glTranslatef(-1.5,-1.0,0.0);glScalef (1.0, 1.0, 3.0);glutWireCube(1.0);glPopMatrix();glPushMatrix();glTranslatef(-1.5,1.0,0.0);glScalef (1.0, 1.0, 3.0);glutWireCube(1.0);glPopMatrix();glPopMatrix();}void reshape(int width, int height){if (height==0){height=1;}glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(120.0f,(GLfloat)width/(GLfloat)height,0.1f,20.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void idle(){glutPostRedisplay();}void redraw(){glClear(GL_COLOR_BUFFER_BIT);glLoadIdentity();glPushMatrix();glTranslatef(-11.0f, 0.0f,-6.0f);glTranslatef(0.0f, fTranslate, 0.0f);Draw_Table();glPopMatrix();   glPushMatrix();glTranslatef(0.0f, 0.0f,-6.0f);glRotatef(fRotate, 0, 1.0f, 0);Draw_Table();glPopMatrix();glPushMatrix();glTranslatef(11.0f,0.0f,-6.0f);glScalef(fScale, fScale, fScale);Draw_Table();glPopMatrix();fTranslate += 0.005f;fRotate    += 0.5f;fScale     -= 0.005f;if(fTranslate > 0.5f) fTranslate = 0.0f;if(fScale < 0.5f)     fScale     = 1.0f;glutSwapBuffers();}int main (int argc,  char *argv[]){  glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);glutInitWindowSize(840,480);                                                           glutCreateWindow("Simple GLUT App");glutDisplayFunc(redraw);glutReshapeFunc(reshape);glutIdleFunc(idle);  glutMainLoop();  return 0;}

#include <GL/glut.h>#include <stdlib.h>float fTranslate;float fRotate;float fScale     = 1.0f;void Draw_Table() {glPushMatrix();glScalef (1.0, 1.0, 0.8);glutSolidCube(1.0);glPopMatrix();glPushMatrix();glTranslatef(0.0,0.0,-0.8);glPushMatrix();glScalef (2.0, 2.0, 0.8);glutSolidCube(1.0);glPopMatrix();glTranslatef(0.0,0.0,-0.8);glPushMatrix();glScalef (3.0, 3.0, 0.8);glutSolidCube(1.0);glPopMatrix();glTranslatef(0.0,0.0,-0.8);glPushMatrix();glScalef (4.0, 4.0, 0.8);glutSolidCube(1.0);glPopMatrix();glTranslatef(0.0,0.0,-0.8);glPushMatrix();glScalef (5.0, 5.0, 0.8);glutSolidCube(1.0);glPopMatrix();glTranslatef(0.0,0.0,-0.8);glPushMatrix();glScalef (6.0, 6.0, 0.8);glutSolidCube(1.0);glPopMatrix();glTranslatef(0.0,0.0,-3.0);glPushMatrix();glTranslatef(2.0,2.0,0.0);GLUquadricObj *cylinder_obj_1;cylinder_obj_1=gluNewQuadric();glPushMatrix();gluCylinder(cylinder_obj_1,0.5,0.5,3.0,20,20);glPopMatrix();glPopMatrix();glPushMatrix();glTranslatef(-2.0,2.0,0.0);GLUquadricObj *cylinder_obj_2;cylinder_obj_2=gluNewQuadric();glPushMatrix();gluCylinder(cylinder_obj_2,0.5,0.5,3.0,20,20);glPopMatrix();glPopMatrix();glPushMatrix();glTranslatef(-2.0,-2.0,0.0);GLUquadricObj *cylinder_obj_3;cylinder_obj_3=gluNewQuadric();glPushMatrix();gluCylinder(cylinder_obj_3,0.5,0.5,3.0,20,20);glPopMatrix();glPopMatrix();glPushMatrix();glTranslatef(2.0,-2.0,0.0);GLUquadricObj *cylinder_obj_4;cylinder_obj_4=gluNewQuadric();glPushMatrix();gluCylinder(cylinder_obj_4,0.5,0.5,3.0,20,20);glPopMatrix();glPopMatrix();glPopMatrix();}void reshape(int width, int height){if (height==0){height=1;}glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(120.0f,(GLfloat)width/(GLfloat)height,0.1f,20.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void idle(){glutPostRedisplay();}void redraw(){glClear(GL_COLOR_BUFFER_BIT);glLoadIdentity();glPushMatrix();glTranslatef(-11.0f, 0.0f,-6.0f);glTranslatef(0.0f, fTranslate, 0.0f);Draw_Table();glPopMatrix();   glPushMatrix();glTranslatef(0.0f, 0.0f,-6.0f);glRotatef(fRotate, 0, 1.0f, 0);Draw_Table();glPopMatrix();glPushMatrix();glTranslatef(11.0f,0.0f,-6.0f);glScalef(fScale, fScale, fScale);Draw_Table();glPopMatrix();fTranslate += 0.005f;fRotate    += 0.5f;fScale     -= 0.005f;if(fTranslate > 0.5f) fTranslate = 0.0f;if(fScale < 0.5f)     fScale     = 1.0f;glutSwapBuffers();}int main (int argc,  char *argv[]){  glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);glutInitWindowSize(840,480);                                                           glutCreateWindow("Simple GLUT App");glutDisplayFunc(redraw);glutReshapeFunc(reshape);glutIdleFunc(idle);  glutMainLoop();  return 0;}


0 0
原创粉丝点击