资源简介
利用OPENGL开发的茶壶示例,包括灯光投影、马赛克渲染、茶壶旋转以及键盘、鼠标的响应,点击键盘1/2/3或者鼠标的左、中、右均可以改变茶壶的旋转方向。
程序含有源代码和编译好的可执行程序,且源代码中关键代码段均有详细的中文注释,方便大家掌握基本开发方法。

代码片段和文件信息
#define GLUT_DISABLE_ATEXIT_HACK
#include
#include
GLfloat planes[]= {-1.0 0.0 1.0 0.0};
GLfloat planet[]= {0.0 -1.0 0.0 1.0};
GLfloat vertices[][3] = {{-1.0-1.0-1.0}{1.0-1.0-1.0}
{1.01.0-1.0} {-1.01.0-1.0} {-1.0-1.01.0}
{1.0-1.01.0} {1.01.01.0} {-1.01.01.0}};
GLfloat colors[][4] = {{0.00.00.00.5}{1.00.00.00.5}
{1.01.00.00.5} {0.01.00.00.5} {0.00.01.00.5}
{1.00.01.00.5} {1.01.01.00.5} {0.01.01.00.5}};
typedef struct lightStruct
{
GLfloat position[4];
GLfloat ambient[4];
GLfloat diffuse[4];
GLfloat specular[4];
} lightStruct;//定义光源数据结构
lightStruct light=
{
{10.01.00.01.0} //灯光位置
{0.00.00.01.0} //环境光
{3.02.00.01.0} //漫反射光
{0.00.00.01.0} //镜面光
};
void setLight()
{
glLightfv(GL_LIGHT0 GL_POSITION light.position);
glLightfv(GL_LIGHT0 GL_AMBIENT light.ambient);
glLightfv(GL_LIGHT0 GL_DIFFUSE light.diffuse);
glLightfv(GL_LIGHT0 GL_SPECULAR light.specular);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
}
void polygon(int a int b int c int d)
{
glBegin(GL_POLYGON);
/* glColor4fv(colors[a]);*/
/* glTexCoord2f(0.00.0); */
glVertex3fv(vertices[a]);
/* glColor4fv(colors[b]); */
/* glTexCoord2f(0.01.0); */
glVertex3fv(vertices[b]);
/* glColor4fv(colors[c]); */
/* glTexCoord2f(1.01.0); */
glVertex3fv(vertices[c]);
/* glColor4fv(colors[d]); */
/* glTexCoord2f(1.00.0); */
glVertex3fv(vertices[d]);
glEnd();
}
void colorcube(void)
{
/* map vertices to faces */
polygon(0321);
polygon(2376);
polygon(0473);
polygon(1265);
polygon(4567);
polygon(0154);
}
static GLfloat theta[] = {0.00.00.0};
static GLint axis = 2;
void display(void)
{
//清除显存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
setLight();
//以下代码用来控制旋转的方向
glRotatef(theta[0] 1.0 0.0 0.0);
glRotatef(theta[1] 0.0 1.0 0.0);
glRotatef(theta[2] 0.0 0.0 1.0);
/* colorcube();*/
//以下代码用来控制茶壶大小
glutSolidTeapot(1.0);
//交换缓冲区
glutSwapBuffers();
}
void spinCube()
{
//控制旋转的角度和速度
theta[axis] += 0.1;
if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
/* display(); */
glutPostRedisplay();
}
//用来捕获鼠标点击的情况并给相应的角度控制变量赋值
void mouse(int btn int state int x int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}
void myReshape(int w int h)
{
//控制显示图像在窗口中的大小
glViewport(0 0 w h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0 2.0 -2.0 * (GLfloat) h / (GLfloat) w
2.0 * (GLfloat) h / (GLfloat) w -10.0 10.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h
2.0 * (GLfloat) w / (GLfloat) h -2.0 2.0 -10.0 10.0);
glMatrixMode(GL_MO
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2011-01-03 21:09 TeaPot\
目录 0 2011-01-03 21:09 TeaPot\Debug\
文件 192634 2011-01-03 21:09 TeaPot\Debug\TeaPot.exe
文件 211456 2011-01-03 21:09 TeaPot\Debug\TeaPot.ilk
文件 12891 2011-01-03 21:09 TeaPot\Debug\TeaPot.obj
文件 294584 2011-01-03 21:09 TeaPot\Debug\TeaPot.pch
文件 361472 2011-01-03 21:09 TeaPot\Debug\TeaPot.pdb
文件 33792 2011-01-03 21:09 TeaPot\Debug\vc60.idb
文件 45056 2011-01-03 21:09 TeaPot\Debug\vc60.pdb
文件 5829 2011-01-02 21:13 TeaPot\TeaPot.c
文件 3399 2011-01-03 21:09 TeaPot\TeaPot.dsp
文件 520 2011-01-03 21:09 TeaPot\TeaPot.dsw
文件 33792 2011-01-03 21:09 TeaPot\TeaPot.ncb
文件 48640 2011-01-03 21:09 TeaPot\TeaPot.opt
文件 2107 2011-01-03 21:09 TeaPot\TeaPot.plg
- 上一篇:基于uml的公交查询用例图
- 下一篇:平面坐标转换-四参数仿射变换源码
相关资源
- OpenGL参考手册
- flash as3 多点触控-缩放-旋转-滑动.zi
- 旋转矩阵求欧拉角的简单算法
- 三维重建(旋转)由已知对应图像点
- Qt Creator opengl实现四元数鼠标控制轨迹
- OpenGL文档,api大全,可直接查询函数
- opengl轮廓字体源代码
- MFC读三维模型obj文件
- 利用OpenGL写毛笔字算法
- MFC中OpenGL面和体的绘制以及动画效果
- 三维地形的仿真显示实现了对地图的
- vc利用MFC底层开发的二维GIS管理软件,
- 基于OPENGL的光线跟踪源代码368758
- VC 实现三维旋转(源码)
- 自编用openGL实现3D分形树,分形山
- OpenGL球形贴图自旋程序
- OpenGL导入贴图的Texture类
- 计算机图形学(openGL)代码
- 用OpenGL开发的机械臂运动仿真程序(
- OpenGL-3D坦克模拟
- 易语言矩阵的旋转源码易语言GDI矩阵
- 在s = 200 GeV的极化质子-质子碰撞中,
- OPENGL实现世界上最小的3D游戏
- 高能分解中的旋转螺旋方法:彩色玻
- 违反洛伦兹背景的量子校正旋转声学
- 在de-Sitter时空中旋转标准玻色子星
- Chern-Simons动态引力中的非扰动旋转黑
- VS2012OpenGL配置所需要的全部libdllh文件
- 旋转活塞式流量计的测量原理
- delphi基于gdi+的图片旋转任意角度
评论
共有 条评论