OpenGL三维与光照
#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三维与光照的更多相关文章
- OpenGL中的光照技术(翻译)
Lighting:https://www.evl.uic.edu/julian/cs488/2005-11-03/index.html 光照 OpenGL中的光照(Linghting)是很重要的,为什 ...
- Qt OpenGL三维绘图
简介 OpenGL是为三维绘图提供的标准应用编程接口. OpenGL处理的仅仅是三维绘图方面,而很少或是根本不提供图形用户界面编程方面的支持.OpenGL*应用程序的用户界面必须由其它工具包创建,比 ...
- Linux OpenGL 实践篇-6 光照
经典光照模型 经典光照模型通过单独计算光源成分得到综合光照效果,然后添加到物体表面特定点,这些成分包括:环境光.漫反射光.镜面光. 环境光:是指不是来特定方向的光,在经典光照模型中基本是个常量. 漫反 ...
- 基于OpenGL三维软件开发
实验原理: OpenGL在MFC下编程原理---- Windows操作系统对OpenGL的支持 在Windows下用GDI作图必须通过设备上下文(DeviceContext简写DC)调用相应的函数:用 ...
- OpenGL三维镂垫
2015-12-12帮舍友尝试这个代码的时候发现舍友的会出现No GLSL support 后来发现舍友的版本2.0.2.1才能用 舍友的是glutInitContextVersion(3, 1);改 ...
- [图形学] Chp10 OpenGL三维观察程序示例
10.10节书中给出了一个程序示例,有一个填充正方形,从侧面的角度观察并画到屏幕上. 图0 这里进一步画出一个立方体,将相机放入立方体中心,旋转相机,达到在立方体中旋转看到不同画面的效果. 步骤: 1 ...
- OpenGL ES2.0光照
一.简单光照原理 平行光(正常光) 光照效果= 环境颜色 + 漫反射颜色 + 镜面反射颜色 点光源 光照效果= 环境颜色 + (漫反射颜色 + 镜面反射颜色)× 衰减因子 聚光灯 光照效果= ...
- OpenGL——三维多面体实现
#include<iostream> #include <math.h> #include<Windows.h> #include <GL/glut.h> ...
- 三维投影总结:数学原理、投影几何、OpenGL教程、我的方法
如果要得到pose视图,除非有精密的测量方法,否则进行大量的样本采集时很耗时耗力的.可以采取一些取巧的方法,正如A Survey on Partial of 3d shapes,描述的,可以利用已得到 ...
随机推荐
- 面试题-python基础
一.Python基础 1.什么是python?使用python有什么好处? python是一种编程语言,它有对象.模块.线程.异常处理和自动内存管理.它简洁,简单.方便.容易扩展.有许多自带的数据结果 ...
- Wannafly挑战赛5 D. 子序列
D. 子序列 链接 #include<cstdio> #include<algorithm> #include<cstring> #include<iostr ...
- luogu1117 [NOI2016]优秀的拆分
luogu1117 [NOI2016]优秀的拆分 https://www.luogu.org/problemnew/show/P1117 后缀数组我忘了. 此题哈希可解决95分(= =) 设\(l_i ...
- 【Java多线程】Executor框架的详解
在Java中,使用线程来异步执行任务.Java线程的创建与销毁需要一定的开销,如果我们为每一个任务创建一个新线程来执行,这些线程的创建与销毁将消耗大量的计算资源.同时,为每一个任务创建一个新线程来执行 ...
- Jmeter(二十一)_完整Demo
1:创建一个线程组 2:添加一个cookie管理器 3:设置你的信息头管理器:application/json;text/plain;charset=UTF-8 44 4:添加一个用户 ...
- 斐讯K2 PSG1218 刷机教程 基于Breed互刷 清除配置
Padavan官方论坛http://www.right.com.cn/forum/thread-161324-1-1.html Breed官方文档http://www.right.com.cn/for ...
- 查找linux镜像源中的软件版本并进行安装
输入以下代码进行软件查找 sudo apt-cache search YourSoftwareName 根据所得到的结果进行安装 sudo apt-get install YourSoftwareNa ...
- python数据分析的工具环境
python做数据分析的优势: 拥有大量的库为数据分析和处理提供了完整的工具链 随着库还在不断的增加的同时, 算法的实现也更加的创新.Numpy, matplotlib, scipy,scikit-l ...
- 沧桑巨变中焕发青春活力-记极1s HC5661A 打怪升级之路
最近发现一个新货umaxhosting年付10美元的便宜VPS.2杯喜茶的价格可以让你在国外拥有一个1024MB (1GB) DDR3 RAM.1024MB (1GB) vSwap.70GB RAID ...
- Python之pexpect详解
一.引子 Pexpect程序主要用于人机对话的模拟,就是那种系统提问,人来回答yes/no,或者账号登陆输入用户名和密码等等的情况.因为这种情况特别多而且繁琐,所以很多语言都有各种自己的实现.最初的第 ...