c++ - 生成带三角条的平面

标签 c++ opengl geometry plane gl-triangle-strip

生成顶点列表以使用三角形带绘制平面的最佳算法是什么?

我正在寻找一个函数,它接收平面的宽度和高度并返回一个包含正确索引顶点的 float 组。

宽度表示每行的顶点数。

height 表示每列的顶点数。

float* getVertices( int width, int height ) {
    ...
}

void render() {
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, getVertices(width,heigth));
    glDrawArrays(GL_TRIANGLE_STRIP, 0, width*height);
    glDisableClientState(GL_VERTEX_ARRAY);
}

最佳答案

谢谢大家。我已经对此进行了编码。这是对的吗?还是生成的 strip 有些错误?

int width;
int height;
float* vertices = 0;
int* indices = 0;

int getVerticesCount( int width, int height ) {
    return width * height * 3;
}

int getIndicesCount( int width, int height ) {
    return (width*height) + (width-1)*(height-2);
}

float* getVertices( int width, int height ) {
    if ( vertices ) return vertices;

    vertices = new float[ getVerticesCount( width, height ) ];
    int i = 0;

    for ( int row=0; row<height; row++ ) {
        for ( int col=0; col<width; col++ ) {
            vertices[i++] = (float) col;
            vertices[i++] = 0.0f;
            vertices[i++] = (float) row;
        }
    }

    return vertices;
}

int* getIndices( int width, int height ) {
    if ( indices ) return indices;

    indices = new int[ iSize ];
    int i = 0;

    for ( int row=0; row<height-1; row++ ) {
        if ( (row&1)==0 ) { // even rows
            for ( int col=0; col<width; col++ ) {
                indices[i++] = col + row * width;
                indices[i++] = col + (row+1) * width;
            }
        } else { // odd rows
            for ( int col=width-1; col>0; col-- ) {
                indices[i++] = col + (row+1) * width;
                indices[i++] = col - 1 + + row * width;
            }
        }
    }
    if ( (mHeight&1) && mHeight>2 ) {
        mpIndices[i++] = (mHeight-1) * mWidth;
    }

    return indices;
}

void render() {
    glEnableClientState( GL_VERTEX_ARRAY );
    glVertexPointer( 3, GL_FLOAT, 0, getVertices(width,height) );
    glDrawElements( GL_TRIANGLE_STRIP, getIndicesCount(width,height), GL_UNSIGNED_INT, getIndices(width,height) );
    glDisableClientState( GL_VERTEX_ARRAY );
}

当 width=4 和 height=4 这就是我得到的: enter image description here

在这里我正在修改一些顶点高度: enter image description here

关于c++ - 生成带三角条的平面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5915753/

相关文章:

c++ - 使用 SDL2 和 OpenGL 旋转相机和三角形绘制不会显示任何内容?

c++ - 快速提问 : explain this typedef

c++ - Emacs 24 前奏。 .emacs 冲突?

opengl - 如何使用 optirun 检查 linux 系统支持哪个版本的 OpenGL?

python - 使用 PyOpenGL 和 PyQt5 制作球体时出现问题

c++ - 是否有计算在单色背景上绘制的 Sprite 边界矩形的算法?

opencv - 使用 opencv 和 python 检测瓶子上的盖子

c++ - 为什么我的纹理会旋转 90 度?

c++ - 在 Linux 中将用户囚禁到 GUI 程序

image - 扭曲图像出现在圆柱投影中