如何在现代OpenGL中绘制圆柱 [英] How to draw cylinder in modern opengl

查看:761
本文介绍了如何在现代OpenGL中绘制圆柱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,如何在现代OpenGL中绘制圆柱体?我正在将GLFW与OpenGL 3.x一起使用.我最初的想法是创建一个函数,以圆为单位计算底部和顶部的顶点位置,然后在这些顶点之间绘制线.但是我不知道如何实现这一目标.有人能找到一个好的解决方案吗?

My question is simple, how do I draw a cylinder in modern OpenGL? I'm using GLFW together with OpenGL 3.x. My thought at first was to create a function that computes the vertex positions at the bottom and at the top as circles and then draw lines between these vertices. But I have no idea how to implement this.. Does anyone have a good solution?

推荐答案

我已经使用了一段时间了,希望对以后的人们有所帮助.

I have been using this for a while now and I hope it will help people in the future.

struct {
   GLfloat x,z, y_start, y_end;
}each_pole; // struct
std::vector<each_pole> each_pole_vector; // vector of structs

//Cylinder with y axis up
GLfloat cylinder_height = 1.0f,
        cylinder_radius = 0.5f,
        nr_of_points_cylinder = 360.f;

for (int i = 0; i < nr_of_points_cylinder; ++i)
{
    GLfloat u = i / (GLfloat)nr_of_points_cylinder;

    //Where the cylinder is in the x and z positions (3D space) 
    each_pole.x = center.x 
    + cylinder_radius*cos(2*M_PI*u); 
    each_pole.z = center.z 
    + cylinder_radius*sin(2*M_PI*u); 

    each_pole.y_start = 0.0f;
    each_pole.y_end = cylinder_height;

    each_pole_vector.push_back(each_pole);

}

return each_pole_vector;

这篇关于如何在现代OpenGL中绘制圆柱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆