#include <GLUT.H>
#include <stdio.h>
#include <tchar.h>
GLuint sphereList, cubeList;
static float solidZ = 8.0;
static float transparentZ = -8.0;
#define ZINC 0.4
void init()
{
GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 0.15f};
GLfloat mat_shininess[] = {100.0};
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
GLfloat position[] = {0.5f, 0.5f, 1.0f, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
sphereList = glGenLists(1);
glNewList(sphereList, GL_COMPILE);
glutSolidSphere(0.4, 16, 16);
glEndList();
cubeList = glGenLists(1);
glNewList(cubeList, GL_COMPILE);
glutSolidCube(0.6);
glEndList();
//glEnable(GL_POLYGON_SMOOTH);
}
void display()
{
GLfloat mat_solid[] = {0.75, 0.75, 0.0, 1.0};
GLfloat mat_zero[] = {0.0, 0.0, 0.0, 1.0};
GLfloat mat_transparent[] = {0.0, 0.8, 0.8, 0.6};
GLfloat mat_emission[] = {0.0, 0.3, 0.3, 0.6};
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-0.15, -0.15, solidZ);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);
glCallList(sphereList);
glPopMatrix();
glPushMatrix();
glTranslatef(0.15, 0.15, transparentZ);
glRotatef(15.0, 1.0, 1.0, 0.0);
glRotatef(30.0, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
glEnable(GL_BLEND);
glDepthMask(GL_FALSE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glCallList(cubeList);
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
glPopMatrix();
glutSwapBuffers();
glFlush();
}
void idleFunc()
{
if(solidZ<=-8.0 || transparentZ>=8.0)
{
glutIdleFunc(NULL);
}
else
{
solidZ -= ZINC;
transparentZ += ZINC;
glutPostRedisplay();
}
}
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
float ratio = (float)width/(float)height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum(-ratio, ratio, -1, 1, 1, 100);
if(width<= height)
glOrtho(-1.5, 1.5, -1.5*(GLfloat)height/(GLfloat)width,
1.5*(GLfloat)height/(GLfloat)width, -10.0, 10.0);
else
glOrtho(-1.5*(GLfloat)width/(GLfloat)height, 1.5*(GLfloat)width/(GLfloat)height, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'r':
solidZ = 8.0;
transparentZ = -8.0;
glutPostRedisplay();
break;
case 'a':
solidZ = 8.0;
transparentZ = -8.0;
glutIdleFunc(idleFunc);
break;
case 27: exit(0);
default: break;
}
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 600);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
//glutIdleFunc(idleFunc);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
分享到:
相关推荐
演示模板缓冲区技术的使用,运用模板缓冲区技术显示场景的深度复杂度。 2、ShadowVolume 演示使用模板缓冲区技术实现实时阴影效果。 第15章 粒子系统 1、SprayParticles 演示粒子系统的具体实现,粒子系统类的封装...
8.1.1 深度缓冲区与深度测试 8.1.2 使用深度测试 8.2 半透明物体的绘制 8.2.1 alpha混合原理 8.2.2 利用alpha混合实现半透明效果 8.2.3 alpha混合系数 8.3 alpha测试 8.3.1 alpha测试原理 8.3.2 使用alpha测试 8.4 ...
课程从建立建立颜色缓冲区,绘制点,绘制线,绘制面,绘制图片,建立纹理,作图形的旋转,图像的混合,建立三维坐标系,实现模型矩阵,投影矩阵,观察矩阵,深度缓冲区等三维核心功能,实现基本的OpenGL核心绘制...
在帧缓冲区进行绘图 390 绘制阴影 392 如何实现阴影 392 示例程序(Shadow.js) 393 提高精度 399 示例程序(Shadow_highp.js) 400 加载三维模型 401 OBJ 文件格式 404 MTL 文件格式 405 示例程序...
在帧缓冲区进行绘图 390 绘制阴影 392 如何实现阴影 392 示例程序(Shadow.js) 393 提高精度 399 示例程序(Shadow_highp.js) 400 加载三维模型 401 OBJ 文件格式 404 MTL 文件格式 405 示例程序...
在帧缓冲区进行绘图 390 绘制阴影 392 如何实现阴影 392 示例程序(Shadow.js) 393 提高精度 399 示例程序(Shadow_highp.js) 400 加载三维模型 401 OBJ 文件格式 404 MTL 文件格式 405 示例程序...
在帧缓冲区进行绘图 390 绘制阴影 392 如何实现阴影 392 示例程序(Shadow.js) 393 提高精度 399 示例程序(Shadow_highp.js) 400 加载三维模型 401 OBJ 文件格式 404 MTL 文件格式 405 示例程序...
2.4.5 用伪代码表示算法 30 2.4.6 用计算机语言表示算法 31 2.5 结构化程序设计方法 31 3 数据类型、运算符与表达式 3.1 C语言的数据类型 32 3.2 常量与变量 33 23.2.1 常量和符号常量 33 3.2.2 变量 33 3.3 整型...
2.4.5 用伪代码表示算法 30 2.4.6 用计算机语言表示算法 31 2.5 结构化程序设计方法 31 3 数据类型、运算符与表达式 3.1 C语言的数据类型 32 3.2 常量与变量 33 23.2.1 常量和符号常量 33 3.2.2 变量 33 3.3 整型...
五、 三维变换.....................................................................................................................................25 5.1、模型变换和视图变换...............................