原子电子运行模型

原子电子运行模型

  在原子电子运行模型当中运用了模型变换以及栈知识,具体代码如下:

// myOpengl.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


// simpleGL.cpp : Defines the entry point for the console application.
//
#include <stdlib.h>
#include <GLglut.h>

#include <iostream>
using namespace std;




void RenderScene()
{
    static GLfloat fElect = 0.0f;
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef(0.0f,0.0f,-100.0f);
    glColor3f(1.0f,0.0f,0.0f);
    glutSolidSphere(10.0f,25,25);
    glPushMatrix();

    glColor3f(1.0f,1.0f,0.0f);

    glRotatef(fElect,0.0f,1.0f,0.0f);
    glTranslatef(90.0f,0.0f,0.0f);
    glutSolidSphere(6.0f,25,25);

    glPopMatrix();
    glPushMatrix();

    glRotatef(45.0f,0.0f,0.0f,1.0f);    
    glRotatef(fElect,0.0f,1.0f,0.0f);
    glTranslatef(-70.0f,0.0f,0.0f);
    glutSolidSphere(6.0f,25,25);
    
    glPopMatrix();
    glPushMatrix();

    glRotatef(-45.0f,0.0f,0.0f,1.0f);
    glRotatef(fElect,0.0f,1.0f,0.0f);
    glTranslatef(0.0f,0.0f,60.0f);
    glutSolidSphere(6.0f,15,15);
    
    glPopMatrix();

    fElect += 10.0f;
    if(fElect>360.0)
        fElect = 0.0f;

    glutSwapBuffers();
}

void ChangeSize(GLsizei w,GLsizei h)
{
    if(h==0)
        h = 1;

    GLfloat aspectRatio = (GLfloat)w/(GLfloat)h;

    glViewport(0,0,w,h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if(w<h)
        glOrtho(-100.0,100.0,-100.0/aspectRatio,100.0/aspectRatio,200.0,-200.0);
    else
        glOrtho(-100.0*aspectRatio,100.0*aspectRatio,-100.0,100.0,200.0,-200.0);
     

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void SetupRC()
{
    glClearColor(0.0f,0.0f,0.0f,1.0f);
    glColor3f(1.0f,0.0f,0.0f);
}
void TimerFunction(int value)
{
    glutPostRedisplay();
    glutTimerFunc(33,TimerFunction,1);
}

int main(int argc, char *argv[])
{
   glutInit(&argc,argv);
   glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
   glutInitWindowSize(800,600);
   glutCreateWindow("Simple");

   glutDisplayFunc(RenderScene);
   glutReshapeFunc(ChangeSize);
   glutTimerFunc(33,TimerFunction,1);
  //glutSpecialFunc(SpecialKeys);

   SetupRC();
   glutMainLoop();
   return 0;
}

  首先,通过运行这个程序发现glRotatef(angle,0,0,1)函数总是以当前的XOY平面作为旋转的平面的,见代码:

glRotatef(45.0f,0.0f,0.0f,1.0f);    

glRotatef(fElect,0.0f,1.0f,0.0f);

glTranslatef(-70.0f,0.0f,0.0f);

glutSolidSphere(6.0f,25,25);

其中第一行决定了旋转的平面为经过45旋转后的XOZ平面,第二行类似。需要注意的是要使电子离开原子核绕原子旋转,必须先使用glRotatef()函数,然后使用glTranslatef()函数。
其次,使用glPushMatrix()函数使得当前缓冲区中的矩阵X被保存到栈中,并且当前缓冲区中的矩阵仍为X。当使用glPopMatrix()函数的时候可以将栈顶的矩阵恢复到缓冲区当中,同时弹出栈顶。
注意到代码当中成对出现的
glPopMatrix();
glPushMatrix();
这也是一种用法,通常将刚弹出的矩阵又保存入栈中,主要就是为了在缓冲区中反复使用的目的。
 
态度决定高度,细节决定成败,
原文地址:https://www.cnblogs.com/lxk2010012997/p/4227225.html