#include<windows.h>
#include<gl/glut.h>
#include<gl/gl.h>
#include<gl/glu.h> //参数指定正方形的位置和大小
GLfloat x1=100.0f;
GLfloat y1=150.0f;
GLsizei rsize=; //正方形运动变化的步长
GLfloat xstep=1.0f;
GLfloat ystep=1.0f; //窗口的大小
GLfloat windowWidth;
GLfloat windowHeight; //画的物体
void DrawDUA();
void DrawTR(); //属性开关
void SunShine(void);//光照 //三维
GLfloat rtri;
GLfloat rquad; //开关
BOOL Draw3D=true;//三维动画演示开关
BOOL Draw2D=false;//二维动画演示开关
BOOL DrawAtoms=false;//原子动画演示开关 //三维属性开关
BOOL AS=true;//透视投影开关
BOOL OR=false;//正交平行投影
BOOL LIGHT=true;//光照开关 void RenderScene(void)
{
if(Draw3D)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(LIGHT)
SunShine(); glTranslatef(-1.5f,0.0f,-6.0f);
glRotatef(rtri,0.0f,1.0f,0.0f); DrawTR(); glLoadIdentity();
glTranslatef(1.5f,0.0f,-6.0f);
glRotatef(rquad,1.0f,0.0f,0.0f);
glColor3f(0.5f,0.5f,1.0f); DrawDUA(); rtri+=1.0f;
rquad-=0.5f;
} if(Draw2D)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(x1,y1,x1+rsize,y1+rsize);
} if(DrawAtoms)
{
//绕核旋转角度
static float fElect1=0.0f; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //重置模型视图矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(LIGHT)
SunShine(); //将图形沿Z轴负向移动
glTranslatef(0.0f,0.0f,-250.0f); //绘制红色原子核
glColor3f(1.0f,0.0f,0.0f);
glutWireSphere(10.0f,,); //绘制颜色变成绿色
glColor3f(0.0f,1.0f,0.0f); //绘制第一个电子
//保存当前的模型视图矩阵
glPushMatrix();
glRotatef(fElect1,0.0f,1.0f,0.0f);//绕y轴旋转一定角度
glTranslatef(90.0f,0.0f,0.0f);//平移一段距离
glutSolidSphere(6.0f,,);//画出电子 //恢复矩阵
glPopMatrix();
glColor3f(0.0f,0.0f,1.0f);//绘制颜色变成蓝色
//第二个电子
glPushMatrix();
glRotatef(45.0f,0.0f,0.0f,1.0f);
glRotatef(fElect1,0.0f,1.0f,0.0f);
glTranslatef(-70.0f,0.0f,0.0f);
glutSolidSphere(6.0f,,);
glPopMatrix(); glColor3f(1.0f,1.0f,0.0f);//绘制颜色变成黄色
//第三个电子
glPushMatrix();
glRotatef(-45.0f,0.0f,0.0f,1.0f);
glRotatef(fElect1,0.0f,1.0f,0.0f);
glTranslatef(0.0f,0.0f,60.0f);
glutSolidSphere(6.0f,,);
glPopMatrix();
fElect1+=10.0f;
if(fElect1>360.0f)
fElect1=10.0f;
}
glutSwapBuffers();
} void ChangeSize(GLsizei w,GLsizei h)
{
if(h==)
h==; //设置视区尺寸
glViewport(,,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); if(Draw3D||DrawAtoms)
{
//修剪空间(透视投影)
if(AS)
{
GLfloat fAspect;
fAspect =(float)w/(float)h;
gluPerspective(45.0,fAspect,1.0,500.0);
} //(正交平行投影)
if(OR)
{
if(w<=h)
glOrtho(-2.25,2.25,-2.25*h/w,2.25*h/w,-10.0,10.0);
else
glOrtho(-2.25*h/w,2.25*h/w,-2.25,2.25,-10.0,10.0);
} } if(Draw2D)
{
if(w<=h)
{
windowHeight=250.0f*h/w;
windowWidth=250.0f;
}
else
{
windowWidth=250.0f*w/h;
windowHeight=250.0f;
} glOrtho(0.0f,windowWidth,0.0f,windowHeight,1.0f,-1.0f);
} glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
} void TimerFunction(int value)
{
if(x1>windowWidth-rsize||x1<)
xstep=-xstep;
if(y1>windowHeight-rsize||y1<)
ystep=-ystep; x1+=xstep;
y1+=ystep; glutPostRedisplay();
glutTimerFunc(,TimerFunction,);
} void SetupRC(void)
{
if(Draw3D||DrawAtoms)
{
glEnable(GL_DEPTH_TEST);//启用深度测试
glFrontFace(GL_CCW);
}
glClearColor(0.0f,0.0f,1.0f,1.0f);
}
void TimerFunc(int value)
{
glutPostRedisplay();
if(Draw3D)
{
glutTimerFunc(,TimerFunc,);
}
if(DrawAtoms)
{
glutTimerFunc(,TimerFunc,);
} }
int main(int argc,char* argv[])
{
if(Draw2D)
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(,TimerFunction,);
}
if(Draw3D||DrawAtoms)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("原子示例");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
if(DrawAtoms)
glutTimerFunc(,TimerFunc,);
else
glutTimerFunc(,TimerFunc,);
} SetupRC();
glutMainLoop();
}
void DrawTR()
{
glBegin(GL_TRIANGLES);
//
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,1.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f); //
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,-1.0f); //
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,-1.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f); //
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,1.0f); glEnd();
} void DrawDUA()
{
glBegin(GL_QUADS); glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f); glColor3f(1.0f,0.5f,0.0f);
glVertex3f(1.0f,-1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,-1.0f); glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f); glColor3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f); glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glEnd();
} void SunShine(void)
{ GLfloat sun_light_position[]={0.0f,0.0f,0.0f,1.0f};
GLfloat sun_light_ambient[]={0.0f,0.0f,0.0f,1.0f};
GLfloat sun_light_diffuse[]={1.0f,1.0f,1.0f,1.0f};
GLfloat sun_light_specular[]={1.0f,1.0f,1.0f,1.0f}; glLightfv(GL_LIGHT0,GL_POSITION,sun_light_position);
glLightfv(GL_LIGHT0,GL_AMBIENT,sun_light_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,sun_light_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,sun_light_specular); glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
}

OpenGL三维与光照的更多相关文章

  1. OpenGL中的光照技术(翻译)

    Lighting:https://www.evl.uic.edu/julian/cs488/2005-11-03/index.html 光照 OpenGL中的光照(Linghting)是很重要的,为什 ...

  2. Qt OpenGL三维绘图

     简介 OpenGL是为三维绘图提供的标准应用编程接口. OpenGL处理的仅仅是三维绘图方面,而很少或是根本不提供图形用户界面编程方面的支持.OpenGL*应用程序的用户界面必须由其它工具包创建,比 ...

  3. Linux OpenGL 实践篇-6 光照

    经典光照模型 经典光照模型通过单独计算光源成分得到综合光照效果,然后添加到物体表面特定点,这些成分包括:环境光.漫反射光.镜面光. 环境光:是指不是来特定方向的光,在经典光照模型中基本是个常量. 漫反 ...

  4. 基于OpenGL三维软件开发

    实验原理: OpenGL在MFC下编程原理---- Windows操作系统对OpenGL的支持 在Windows下用GDI作图必须通过设备上下文(DeviceContext简写DC)调用相应的函数:用 ...

  5. OpenGL三维镂垫

    2015-12-12帮舍友尝试这个代码的时候发现舍友的会出现No GLSL support 后来发现舍友的版本2.0.2.1才能用 舍友的是glutInitContextVersion(3, 1);改 ...

  6. [图形学] Chp10 OpenGL三维观察程序示例

    10.10节书中给出了一个程序示例,有一个填充正方形,从侧面的角度观察并画到屏幕上. 图0 这里进一步画出一个立方体,将相机放入立方体中心,旋转相机,达到在立方体中旋转看到不同画面的效果. 步骤: 1 ...

  7. OpenGL ES2.0光照

    一.简单光照原理 平行光(正常光) 光照效果=   环境颜色 + 漫反射颜色 + 镜面反射颜色 点光源 光照效果=   环境颜色 + (漫反射颜色 + 镜面反射颜色)× 衰减因子 聚光灯 光照效果= ...

  8. OpenGL——三维多面体实现

    #include<iostream> #include <math.h> #include<Windows.h> #include <GL/glut.h> ...

  9. 三维投影总结:数学原理、投影几何、OpenGL教程、我的方法

    如果要得到pose视图,除非有精密的测量方法,否则进行大量的样本采集时很耗时耗力的.可以采取一些取巧的方法,正如A Survey on Partial of 3d shapes,描述的,可以利用已得到 ...

随机推荐

  1. 十张图了解Docker【转】

    这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(container)和镜像(image)之间的区别,并深入探讨容器和运行中的容器之间的区别. 当我对Docker技术还是一知半解的时候,我 ...

  2. 20155306 白皎 《网络攻防》 Exp2 后门原理与实践

    20155306 白皎 <网络攻防> Exp2 后门原理与实践 一.实践基础 后门程序又称特洛伊木马,其用途在于潜伏在电脑中,从事搜集信息或便于黑客进入的动作.后程序和电脑病毒最大的差别, ...

  3. mysql数据库的左连接,右连接,内链接。

    一般所说的左连接,外连接是指左外连接,右外连接.做个简单的测试你看吧.先说左外连接和右外连接:[TEST1@orcl#16-12月-11] SQL>select * from t1; ID NA ...

  4. Python 学习计划

    时间分为4周,全部自学,仅提供大纲.适用于Web方向: 1.Week1:读完<简明Python教程>,适应Python开发环境 2.Week2:写个爬虫,需要深入了解re.urllib2. ...

  5. SSISDB2:SSIS工程的操作实例

    SSISDB 系列随笔汇总: SSISDB1:使用SSISDB管理Package SSISDB2:SSIS工程的操作实例 SSISDB3:Package的执行实例 SSISDB4:当前正在运行的Pac ...

  6. Docker和CI/CD实战

    一.CICD和DevOps 前面已经了解了CI/CD,其实CI/CD已经存在多年了,只是最近软件工程方面又提出了敏捷开发.DevOps,又把CI/CD炒火了. 那么什么是DevOps?DevOps和C ...

  7. Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)

    在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截.但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因.它允许你通过它的方法名来拦截方法.另外, ...

  8. if _ else if _ else,case,程序逻辑判断- java基础

    //单个判端 if(){ } //双判端 if(){ }else{ } //多重判端 if(){ }else if(){ }else if(){ }else{ } package test1; // ...

  9. selenium 基本常用操作

    from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChains #鼠标操作 ...

  10. Netty源码分析第2章(NioEventLoop)---->第4节: NioEventLoop线程的启动

    Netty源码分析第二章: NioEventLoop   第四节: NioEventLoop线程的启动 之前的小节我们学习了NioEventLoop的创建以及线程分配器的初始化, 那么NioEvent ...