当前位置:网站首页>8.使用二次几何体技术,在屏幕上绘制一个上小下大的柱体。
8.使用二次几何体技术,在屏幕上绘制一个上小下大的柱体。
2022-07-24 05:16:00 【陌小呆^O^】
#define GLUT_DISABLE_ATEXIT_HACK
#include "windows.h"
#include <stdio.h>
#include <gl/glut.h>
#include "math.h"
GLUquadricObj* quadratic; /**< 二次几何体 */
GLfloat rot = 0.0; /**< 用于旋转物体 */
GLboolean light = false; /**< 用于开启/关闭光源 */
GLboolean lp = false; /**< 判断L键是否释放 */
GLboolean sp = false; /**< 判断空格键是否释放 */
struct TextureInfo
{
unsigned int ID;
BYTE* pBytes;
};
TextureInfo TexInfo[4];
/** 绘制模式 */
GLint renderMode[] = { GLU_FILL,GLU_LINE,GLU_SILHOUETTE };
GLuint mode = 0; /**< 绘制模式索引 */
/** 定义光源的属性值 */
GLfloat LightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; /**< 环境光参数 */
GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; /**< 漫射光参数 */
GLfloat LightSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; /**< 镜面光参数 */
GLfloat LightPosition[] = { 0.0f, 0.0f, 5.0f, 1.0f }; /**< 光源位置 */
//初始化OpenGL
void init(void)
{
/** 用户自定义的初始化过程 */
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D); /**< 启用纹理映射 */
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
/** 设置光源的属性值 */
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); /**< 设置环境光 */
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); /**< 设置漫射光 */
glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpecular); /**< 设置漫射光 */
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition); /**< 设置光源位置 */
/** 启用光源 */
glEnable(GL_LIGHT1);
/**< 创建二次几何体 */
quadratic = gluNewQuadric();
gluQuadricNormals(quadratic, GLU_SMOOTH); /**< 使用平滑法线 */
gluQuadricTexture(quadratic, GL_TRUE); /**< 使用纹理 */
}
//主要的绘制过程
void display(void)
{
/** 用户自定义的绘制过程 */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /**< 清除缓存 */
glLoadIdentity();
/** 绘制过程 */
glTranslatef(0.0f, 0.0f, -8.0f);
///绘制圆柱体
glPushMatrix();
glTranslatef(-2.0f, 1.1f, 0.0f);
glRotatef(90, 1.0f, 0.0f, 0.0f);
glRotatef(rot, 1.0f, 0.0f, 1.0f);
gluCylinder(quadratic, 0.8f, 2.0f, 1.8f, 32, 32);
//gluCylinder(sphere,baseRadius,topRadius, height, slices, stacks);
glPopMatrix();
glFlush(); /**< 强制执行所有的OpenGL命令 */
}
//在窗口改变大小时调用
void reshape(int w, int h) {
glViewport(0, 0, w, h);//设置视口
glMatrixMode(GL_PROJECTION);//设置当前为投影变换模式
glLoadIdentity();//用单位矩阵替换当前变换矩阵
gluPerspective(45, (float)w / h, 4, 100.0);//设置正交投影视图体
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void TimerFunction(int value)
{
rot += 10;
if (rot == 360)
rot = 0;
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(500, TimerFunction, 1);
}
//处理键盘
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27://esc键退出
exit(0);
break;
case 'a':
case 'A':
lp = TRUE; /**< lp 设为 TRUE */
light = !light; /**< 切换光源的 TRUE/FALSE */
if (!light) /**< 如果没有光源 */
{
glDisable(GL_LIGHTING); /**< 禁用光源 */
}
else
{
glEnable(GL_LIGHTING); /**< 启用光源 */
}
break;
case 'd':
case 'D':
sp = TRUE; /**< lp 设为 TRUE */
mode += 1;
if (mode > 2)
mode = 0;
gluQuadricDrawStyle(quadratic, renderMode[mode]);
break;
default:
break;
}
}
int main(int argc, char* argv[]) //主函数: 参数数量&参数值
{
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutCreateWindow("Basic");//设置窗口标题
init();//初始化OpenGL
glutDisplayFunc(display);//设置显示回调函数
glutReshapeFunc(reshape);//设置reshape回调函数
glutKeyboardFunc(keyboard);//设置键盘回调函数
glutTimerFunc(500, TimerFunction, 1);
glutMainLoop();//进入主循环
}
边栏推荐
- MySQL connection
- 【STL】Map &unordered_map
- 4. 在屏幕上绘制一个红色三角形,一个黄色正方形。三角形在后,小;正方形在前,大。使用融合技术,使得可以透过正方形看到三角形,源和目标融合因子分别为GL_SRC_ALPHA和GL_ONE_MINUS
- T 1-5
- I'm interested in reading efficient reading - the most cost-effective self investment
- NFS shared services
- reflex
- 输入若干数据,找出最大值输出。(键盘和文件读取)
- This is the first article
- Pointer learning diary (II)
猜你喜欢
随机推荐
用双向链表实现栈(C)
谈谈对未来的想法
安装Pytorch+anaconda+cuda+cudnn
Handwritten ORM framework
Reading excerpts from Liu run's "bottom logic"
ssm的整合
1. Pedestrian recognition based on incremental occlusion generation and confrontation suppression
Relationship between sample and population in Statistics: sample success ratio + central limit theorem (sample mean)
关于numpy基础用法的一个整理
【dp】数字三角形
5.模板缓存,绘制一个正方形只能在三角形内移动
Learn AI linear regression from Li Mu. The code implementation from scratch is super detailed
JMeter upload and download files
Pointer learning diary (II)
模拟加法 & 结构体基本用法
Machine vision learning summary
Tips for using the built-in variable vars in BeanShell
反射的介绍
SSM整合
Read the summary of "machine learning - Zhou Zhihua"







![Embedded system transplantation [3] - uboot burning and use](/img/36/69daec5f1fe41bd3d0433d60816bba.png)

