想要渲染弯曲的圆柱体

想要渲染弯曲的圆柱体

本文介绍了OpenGL Glut C - 想要渲染弯曲的圆柱体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码显示了 OpenGL C 语言中的直圆柱体/管道.

 #include #include #include #include <math.h>#define PI 3.1415927无效的绘制圆柱体(GLfloat 半径,GLfloat 高度,GLubyte R,GLubyte G,GLubyte B){GLfloat x = 0.0;GLfloat y = 0.0;GLfloat 角度 = 0.0;GLfloatangle_stepsize = 0.1;//绘制管子glColor3ub(R-40,G-40,B-40);glBegin(GL_QUAD_STRIP);角度 = 0.0;而(角度<2*PI){x = 半径 * cos(角度);y = 半径 * sin(角度);glVertex3f(x, y, 高度);glVertex3f(x, y, 0.0);角度 = 角度 + 角度步长;}glVertex3f(半径,0.0,高度);glVertex3f(半径, 0.0, 0.0);glEnd();//在圆柱体上画圆glColor3ub(R,G,B);glBegin(GL_POLYGON);角度 = 0.0;而(角度<2*PI){x = 半径 * cos(角度);y = 半径 * sin(角度);glVertex3f(x, y, 高度);角度 = 角度 + 角度步长;}glVertex3f(半径,0.0,高度);glEnd();}无效显示(无效){glClear(GL_COLOR_BUFFER_BIT);glLoadIdentity();glTranslatef(-0.5,0.0,-2.5);glRotatef(100.0, 0.725, 1.0, 1.0);绘制圆柱体(0.15, 1.0, 255, 160, 100);glFlush();}无效重塑(整数宽度,整数高度){if (width == 0 || height == 0) 返回;glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(35.0, (GLdouble)width/(GLdouble)height,0.5, 20.0);glMatrixMode(GL_MODELVIEW);glViewport(0, 0, 宽度, 高度);}int main(int argc, char **argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(640,580);glutCreateWindow("创建圆柱体");glClearColor(0.0,0.0,0.0,0.0);glutDisplayFunc(显示);glutReshapeFunc(reshape);glutMainLoop();返回0;}

目前它绘制了一个直圆柱体/管道.我想让它看起来像

然后您必须定义弯曲半径以及弯曲开始和结束角度.下面的代码绘制一个弯曲的管道,从 bend_ang0bend_ang1,半径为 bend_radius.可以根据弯曲半径和管道长度计算弯曲角度:

GLfloat w0, w1, ang0, ang1, 角度, x, y, xb, yb, zb;内部 i, j;int 切片 = 8;GLfloat ben_radius = 1.0f;GLfloat 弯曲角度、弯曲角度 0、弯曲角度 1;弯曲角度 = 弯曲半径 * 高度;bend_ang0 = -bend_angle/2.0f;弯曲角度1 =弯曲角度/2.0f;for ( i = 0; i 

对于下面的图像,我激活了深度测试并更改了模型视图矩阵:

空显示(void){glEnable(GL_DEPTH_TEST);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glTranslatef(0.0f, -0.5f, -4.0f);glRotatef(-90.0f, 1.0f, 0.0, 0.0f);绘制圆柱体(0.15、2.0、255、160、100);glFlush();}

The following code shows a straight cylinder/pipe in OpenGL C language.

 #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;
}

At the moment it draws a straight cylinder/pipe. And I wanted to curve it to look like this.

解决方案

First I recommend to split the cylinder into slices. The following cone will draw exactly the same cylinder, but it splits the cylinder int slices. The slices have different colors to visualize the effect.

GLfloat h0, h1, angle, x, y;
int i, j;

int     slices      = 8;

for ( i = 0; i < slices; i++ )
{
    h0 = (float)i / (float)slices;
    h1 = (float)(i+1) / (float)slices;

    glColor3f( 1.0f-h0, 0.0, h1 );
    glBegin(GL_QUAD_STRIP);
        for ( j = 0; j <= 360; ++ j )
        {
            angle = PI * (float)j * PI / 180.0f;
            x = radius * cos(angle);
            y = radius * sin(angle);
            glVertex3f( x, y, h0 );
            glVertex3f( x, y, h1 );
        }
    glEnd();
}

Then you have to define a bend radius and a bend start and end angle. The following code draw a bended pipe form bend_ang0 to bend_ang1, with a radius bend_radius. The bend angles can be calculated in relation to the bend radius and the length of the pipe:

GLfloat w0, w1, ang0, ang1, angle, x, y, xb, yb, zb;
int i, j;

int     slices      = 8;
GLfloat bend_radius = 1.0f;

GLfloat bend_angle, bend_ang0, bend_ang1;

bend_angle = bend_radius * height;
bend_ang0  = -bend_angle/2.0f;
bend_ang1  = bend_angle/2.0f;

for ( i = 0; i < slices; i++ )
{
    w0 = (float)i / (float)slices;
    w1 = (float)(i+1) / (float)slices;

    ang0 = bend_ang0 + (bend_ang1-bend_ang0) * w0;
    ang1 = bend_ang0 + (bend_ang1-bend_ang0) * w1;

    glColor3f( 1.0f-w0, 0.0, w1 );
    glBegin(GL_QUAD_STRIP);

        for ( j = 0; j <= 360; ++ j )
        {
            angle = PI * (float)j * PI / 180.0f;
            x = radius * cos(angle) + bend_radius;
            y = radius * sin(angle);

            xb = sin( ang0 ) * x;
            yb = y;
            zb = cos( ang0 ) * x;
            glVertex3f( xb, yb, zb );

            xb = sin( ang1 ) * x;
            yb = y;
            zb = cos( ang1 ) * x;
            glVertex3f( xb, yb, zb );
        }
    glEnd();
}

For the following image I activated the depth test and changed the model view matrix:

void display(void)
{
    glEnable( GL_DEPTH_TEST );
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glTranslatef(0.0f, -0.5f, -4.0f);
    glRotatef(-90.0f, 1.0f, 0.0, 0.0f);

    draw_cylinder(0.15, 2.0, 255, 160, 100);

    glFlush();
}

这篇关于OpenGL Glut C - 想要渲染弯曲的圆柱体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:31