以下代码显示了OpenGL C语言中的直筒/管道.
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#define PI 3.1415927
void draw_cylinder(GLfloat radius, GLfloat height, GLubyte R, GLubyte G, GLubyte B)
{
GLfloat x = 0.0;
GLfloat y = 0.0;
GLfloat angle = 0.0;
GLfloat angle_stepsize = 0.1;
// Draw the tube
glColor3ub(R-40,G-40,B-40);
glBegin(GL_QUAD_STRIP);
angle = 0.0;
while( angle < 2*PI ) {
x = radius * cos(angle);
y = radius * sin(angle);
glVertex3f(x, y , height);
glVertex3f(x, y , 0.0);
angle = angle + angle_stepsize;
}
glVertex3f(radius, 0.0, height);
glVertex3f(radius, 0.0, 0.0);
glEnd();
// Draw the circle on top of cylinder
glColor3ub(R,G,B);
glBegin(GL_POLYGON);
angle = 0.0;
while( angle < 2*PI ) {
x = radius * cos(angle);
y = radius * sin(angle);
glVertex3f(x, y , height);
angle = angle + angle_stepsize;
}
glVertex3f(radius, 0.0, height);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-0.5,0.0,-2.5);
glRotatef(100.0, 0.725, 1.0, 1.0);
draw_cylinder(0.15, 1.0, 255, 160, 100);
glFlush();
}
void reshape(int width, int height)
{
if (width == 0 || height == 0) return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(35.0, (GLdouble)width/(GLdouble)height,0.5, 20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, width, height);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,580);
glutCreateWindow("Create Cylinder");
glClearColor(0.0,0.0,0.0,0.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此刻它绘制一个直筒/管.我想把它曲线看起来像这样.
目前,您正在一次绘制圆柱体的整个高度......要创建一个曲面,您必须使用现有代码并让它创建一系列微小的圆柱体,每个圆柱体的高度都很小,然后将它们堆叠起来以消耗原始的圆柱体高度。
一种方法是引入一个新函数,该函数将成为您的父函数
void draw_cylinder(GLfloat radius, GLfloat height, GLubyte R, GLubyte G, GLubyte B)
Run Code Online (Sandbox Code Playgroud)
也许可以称之为
draw_curved_cylinder
Run Code Online (Sandbox Code Playgroud)
在这个新函数中,您有一个循环,您可以在其中调用并为其draw_cylinder提供每个小圆柱体的参数...当前您的绘制函数盲目地将高度从 0 拉伸到给定的height...将其替换为给定小圆柱体的设置圆柱体...还要使最终圆柱体弯曲,每个小圆柱体必须使其 X 和 Y 坐标遵循弯曲轨迹,因此在新函数中draw_curved_cylinder增量这些坐标,以便它们在合成每个新小圆柱体时发生变化
PS - 请注意,您没有使用现代 OpenGL - glBegin 已过时,应避免使用