////////////////////////////////////////////////////////////////////////////////////////////
//Cylinder.cpp
#include "stdafx.h"
#pragma warning(disable : 4136) //X86
#define Y 2.0 //圆柱高度
int recursion_depth=4;//递归深度
static GLdouble vrtx[8][3]={
//bottom
{ 1.0, 0.0, 0.0},//0
{ 0.0, 0.0,-1.0},//1
{-1.0, 0.0, 0.0},//2
{ 0.0, 0.0, 1.0},//3
//top
{ 1.0, Y, 0.0}, //4
{ 0.0, Y,-1.0}, //5
{-1.0, Y, 0.0}, //6
{ 0.0, Y, 1.0}, //7
};
static GLuint quads[4][4]={
{0,1,5,4},{1,2,6,5},{2,3,7,6},{3,0,4,7}
};
void normalize(double v[3],BOOL b3D){//向量归一化
if(b3D){//在三维空间内对v进行归一化
GLfloat d=(GLfloat)sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);//求出向量V的模
if(d==0.0){
MessageBox(NULL,"向量的模为零!!程序将退出!!","圆柱演示",MB_OK|MB_ICONERROR);
exit(0);
}
v[0]/=d;v[1]/=d;v[2]/=d;
}
else{//在XOZ平面内把向量归一化
GLfloat d=(GLfloat)sqrt(v[0]*v[0]+v[2]*v[2]);
if(d==0.0){
MessageBox(NULL,"向量的模为零!!程序将退出!!","圆柱演示",MB_OK|MB_ICONERROR);
exit(0);
}
v[0]/=d;v[2]/=d;
}
}
void drawSurface(double *V1,double *V2,double *V3,double *V4){
glBegin(GL_POLYGON);
glNormal3dv(V1);
glVertex3dv(V1);
glNormal3dv(V2);
glVertex3dv(V2);
glNormal3dv(V2);
glVertex3dv(V3);
glNormal3dv(V1);
glVertex3dv(V4);
glEnd();
glBegin(GL_TRIANGLES);//上表面
glNormal3d(0.0,1.0,0.0);//上法线方向
glVertex3dv(V3);
glNormal3d(0.0,1.0,0.0);//上法线方向
glVertex3d(0.0,Y,0.0); //上底圆心
glNormal3d(0.0,1.0,0.0);//上法线方向
glVertex3dv(V4);
glEnd();
//因为下底面不接受光照,所以完全可以不绘制下底面以加快速度
/* glBegin(GL_TRIANGLES);//下底面
glNormal3d(0.0,-1.0,0.0);//下法线方向
glVertex3dv(V2);
glNormal3d(0.0,-1.0,0.0);//下法线方向
glVertex3d(0.0,0.0,0.0); //下底圆心
glNormal3d(0.0,-1.0,0.0);//下法线方向
glVertex3dv(V1);
glEnd();
*/
}
void subDivide(double *bottomV1,double *bottomV2,double *topV2,double *topV1,int depth){
GLdouble topV12[3],bottomV12[3];
if(depth==0){
drawSurface(bottomV1,bottomV2,topV2,topV1);
return;
}
for(int i=0;i<3;i++){
bottomV12[i]=(bottomV1[i]+bottomV2[i])/2.0;
topV12[i]=(topV1[i]+topV2[i])/2.0;
}
normalize(topV12,false);//在XOZ平面内对topV12向量进行归一化
normalize(bottomV12,false);
subDivide(bottomV1,bottomV12,topV12,topV1,depth-1);
subDivide(bottomV12,bottomV2,topV2,topV12,depth-1);
}
//初始化光照模型及视图模型转换设置后开始实际的绘图
void _stdcall display (void){
// glClearColor(1.0,0.0,1.0,1.0); //设置背景颜色
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //设置背景颜色
glColor3f(1.0,1.0,0.0);//设置绘制图形所用颜色
//设置图形转换信息
glLoadIdentity(); //清空矩阵
glTranslatef(0.0,-1.0,-5.0); //把圆柱体向Y负方向平移1单位,向Z负方向平移5单位
glRotatef(15.0,1.0,0.0,0.0); //把圆柱体绕X轴旋转15°
//开始具体绘制图形
for(int i=0;i<4;i++){
subDivide(
&vrtx[quads[i][0]] [0],
&vrtx[quads[i][1]] [0],
&vrtx[quads[i][2]] [0],
&vrtx[quads[i][3]] [0],
recursion_depth
);
}
glFlush();//结束图形绘制的顶点给定,并最终输出至显示器完成图形的显示
}
void myInit (void){
GLfloat mtrxAmbient[] = {0.8,0.8,0.8,1.0};
GLfloat mtrxSpecular[] = {1.0,1.0,1.0,1.0};
GLfloat mtrxShininess[] = {70.0};
GLfloat light_position[] = {1.0,1.0,1.0,0.0};
GLfloat white_light[] = {1.0,1.0,1.0,0.0};
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_SMOOTH); //设置明暗模型为平直
//定义材质
glMaterialfv(GL_FRONT,GL_SPECULAR,mtrxSpecular);
glMaterialfv(GL_FRONT, GL_SHININESS, mtrxShininess);
//打开光照
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,mtrxAmbient);//定义全局环境光
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
//初始化光照模型及视图模型转换设置
void _stdcall myReshape(int w,int h){
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();/* define the projection */
if(w<=h)//保证视口区不成比例缩放时,图形保持原比例不变
glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//主函数
int main(int argc,char* argv[]){
auxInitDisplayMode(AUX_SINGLE|AUX_RGBA|AUX_DEPTH16);
auxInitPosition(0,0,500,500);
auxInitWindow("递归法离散圆柱体演示程序");
myInit();
auxReshapeFunc(myReshape);
auxMainLoop(display);
return 0;
}
OpenGl.rar_opengl 圆柱_opengl圆柱_opengl绘制圆柱_圆柱_绘制圆柱
版权申诉
14 浏览量
2022-09-23
23:08:16
上传
评论
收藏 9KB RAR 举报
朱moyimi
- 粉丝: 61
- 资源: 1万+
最新资源
- 南京邮电大学数学实验:熟练掌握 Matlab 软件的基本命令和操作
- 2017校招真题校园招聘真题算法题(37道)Python源码.zip
- 基于单片机protues仿真的多功能自动饮水机系统设计(仿真图、源代码、演示视频)
- 二叉树7-1-1.cpp
- android 9.0 原生模拟器 签名文件
- 技术面试最后反问面试官的话 校招面试非技术问题有哪些 非技术问题如何回答.png
- NB-IOT-BC26全网通模块Altium+ CADENCE +PADS三种格式(原理图SCH+PCB封装库)文件.zip
- 基于微信小程序开发的校园失物招领系统源码毕业设计(优质项目源码).zip
- 词向量是一种将自然语言中的单词转换为数值向量的技术,它能够捕捉词义和上下文信息
- nmap与masscan的简单使用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈