计算机图形学OpenGL(5)——太阳系实例,纯手工操作

来源:互联网 发布:ubuntu ant是什么 编辑:程序博客网 时间:2024/05/16 13:46

转动的太阳系,不像上节那样用到帧的技术,所以要改变参数只能手工来 运行时 按住 y

这个简陋的系统就会转动起来,如果你理解了上节的代码,那么你完全可以把,这节的代码,用上节的帧在来实现一遍,

////  main.cpp//  OpenGLClamp////  Created by ipud2 on 12/6/15.//  Copyright © 2015 I'm CEO,bitch. All rights reserved.//#include <iostream>#include <GLUT/GLUT.h>#define x(str) std::cout<<(str)<<std::endl;static int year =0, day = 0;void init(){    glClearColor(0,0,0,0);    glShadeModel(GL_FLAT);}void display(){        glClear(GL_COLOR_BUFFER_BIT);    glColor3f(1, 1,1);            glPushMatrix();    glRotatef((GLfloat)year, 0.0, 1.0, 0.0);    glutWireSphere(1.0, 20, 16);    glRotatef((GLfloat)year, 0.0, 1.0, 0.0);    glTranslatef(2.0, 0.0, 0.0);    glRotatef((GLfloat)day, 0.0, 1.0,0.0);    glutWireSphere(0.2, 10, 8);    glPopMatrix();            glutSwapBuffers();    glFlush();}void reshape (int w,int h){    glViewport(0, 0, (GLsizei)w, (GLsizei)h);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluPerspective(60.0, (float)w/(float)h, 1.0, 20.0);    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();    gluLookAt(0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);}void keyboard(unsigned char key,int x,int y){    switch (key)    {        case 'd':            day = (day + 10) % 360;            break;        case 'y':            year= (year + 10) %360;        default:            break;    }    //x(key);    glutPostRedisplay();}int main(int argc,char**argv){    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); //#define GLUT_RGBA GLUT_RGB    glutInitWindowSize(1000, 800);    glutInitWindowPosition(100, 100);    glutCreateWindow("OpenGL soloar system demostration");    init();    glutDisplayFunc(display);    glutReshapeFunc(reshape);        glutKeyboardFunc(keyboard);        glutMainLoop();    return 0;}


0 0
原创粉丝点击