opengl生成空心圆柱,测试可用生成空心圆柱,每一步都有注释,可用。
#include
#include
#include
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut.lib")
#include
void Cube()
{
glBegin(GL_QUAD_STRIP);//填充凸多边形
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(0.0f, 1.0f, -1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
glBegin(GL_QUAD_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(1.0f, 0.0f, -1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glEnd();
}
void Circle()
{
glBegin(GL_TRIANGLE_FAN);//扇形连续填充三角形串
glVertex3f(0.0f, 0.0f, 0.0f);//坐标顶点
int i = 0;
for (i = 0; i <= 100; i += 15)
{
float p = i * 3.14 / 180;
glVertex3f(sin(p), cos(p), 0.0f);//坐标顶点 每个点
}
glEnd();
}
void Cylinder(double j)//j 代表倍率 //外面的圆柱
{
glBegin(GL_QUAD_STRIP);//连续填充四边形串 GL_QUAD_STRIP 连续填充四边形串 mode:创建图元的类型
int i = 0;
for (i = 0; i <= 360; i += 15)
{
float p = i * 3.14 / 180;
glVertex3f(j*sin(p), j*cos(p), 1.0f);//指定每个点的坐标顶点 坐标 角度360应该就好
glVertex3f(j*sin(p), j*cos(p), 0.0f);
}
glEnd(); //结束 与glBegin对应
//Circle();//调用方法圆
//glTranslatef(0, 0, 1);//平移变换
//Circle();
}
void Cylinder_Bottom(double j)//j 代表倍率 //外面的圆柱
{
glBegin(GL_TRIANGLE_STRIP);
int i = 0;
for (i = 0; i <= 360; i += 15)
{
float p = i * 3.14 / 180;
glVertex3f(j*sin(p), j*cos(p), 0.0f);
glVertex3f(sin(p), cos(p), 0.0f);
}
glEnd();
}
void Cylinder_Hollow()//j 代表倍率 //外面的圆柱
{
Cylinder(1);
Cylinder(0.618);
Cylinder_Bottom(0.618);
glTranslatef(0, 0, 1);//平移变换
Cylinder_Bottom(0.618);
}
void Cone()
{
glBegin(GL_QUAD_STRIP);//连续填充四边形串
int i = 0;
for (i = 0; i <= 390; i += 15)
{
float p = i * 3.14 / 180;
glVertex3f(0, 0, 1.0f);
glVertex3f(sin(p), cos(p), 0.0f);
}
glEnd();
Circle();
}
void renderScene(void)//渲染
{
//static float i = 0;
//i += 0.01;
//if (i > 360)
// i = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除初始化
glLoadIdentity();//功能是重置当前指定的矩阵为单位矩阵
glPushMatrix();//当前矩阵堆栈推送,通过一个,复制当前矩阵
glColor3f(0, 1, 0);//颜色设置
//glTranslatef(-2, 2.0, -12); //沿X轴正方向平移x个单位(x是有符号数) Y Z 平移矩阵
glTranslatef(0, 0, -12); //沿X轴正方向平移x个单位(x是有符号数) Y Z 平移矩阵
glRotatef(45, 1, 1, 1);//旋转矩阵 以点(0,0,0)到点(x,y,z)为轴,旋转i角度;
//glRotatef(i, 1, 1, 1);//旋转矩阵 以点(0,0,0)到点(x,y,z)为轴,旋转i角度;
//Cylinder();//函数
Cylinder_Hollow();
glPopMatrix();//出 对应上方进 消除上一次的变换对本次变换的影响。使本次变换是以世界坐标系的原点为参考点进行
//glPushMatrix();
//glColor3f(1, 0, 0);
//glTranslatef(2, 2, -12);
//glRotatef(i, 1, 1, 1);
//Circle();
//glPopMatrix();
//glPushMatrix();
//glColor3f(0, 1, 1);
//glTranslatef(-2, -2, -12);
//glRotatef(i, 1, 1, 1);
//Cube();
//glPopMatrix();
//glPushMatrix();
//glColor3f(1, 1, 0);
//glTranslatef(2, -2, -12);
//glRotatef(i, 1, 1, 1);
//Cone();
//glPopMatrix();
glutSwapBuffers();//交互缓冲指针 双缓冲
}
void changeSize(int w, int h) {
// 防止除数即高度为0
// (你可以设置窗口宽度为0).
if (h == 0)
h = 1;
float ratio = 1.0* w / h;
// 单位化投影矩阵。
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// 设置视口大小为整个窗口大小
glViewport(0, 0, w, h);
// 设置正确的投影矩阵
gluPerspective(45, ratio, 1, 1000);
//下面是设置模型视图矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, -1.0, 0.0f, 1.0f, 0.0f);//设置观测点
}
int main(int argc, char * argv[])
{
glutInit(&argc, argv);//窗口初始化
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);//指定其显示模式的类型。GLUT_DEPTH//深度缓存区
glutInitWindowPosition(100, 100);//确定窗口位置
glutInitWindowSize(320, 320);//大小
glutCreateWindow("Hello OpenGL");//名字
glutDisplayFunc(renderScene);//注册一个绘图函数
glutIdleFunc(renderScene); //指定程序空闲时调用函数
glutReshapeFunc(changeSize); //指定窗口形状变化时的回调函数
glEnable(GL_DEPTH_TEST);//启用什么... 启用深度测试。根据坐标的远近自动隐藏被遮住的图形(材料)
glutMainLoop();//glutMainLoop进入GLUT事件处理循环,让所有的与“事件”有关的函数调用无限循环。
return 0;
}