在opengl中计算一个球体

编程入门 行业动态 更新时间:2024-10-15 14:19:40
本文介绍了在opengl中计算一个球体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想计算所有需要的顶点并用线连接它们,所以我最终想出了一个球体。有多少种方法可以做到这一点?而且顶点之间的线会是直的;我怎么能让他们弯曲我知道我可以使用glutWireSphere(),但我有兴趣实际计算顶点。我想到的一种方式是将所有顶点手动放入数组中,但我认为这不是它的完成方式。 解决方案 div>

复制并粘贴我最初写在使用Visual C ++在Opengl中创建3D球体

class SolidSphere {保护 std :: vector< GLfloat>顶点; std :: vector< GLfloat>法线; std :: vector< GLfloat> texcoords; std :: vector< GLushort>指数; $ b $ public: void SolidSphere(float radius,unsigned int rings,unsigned int sector) { float const R = 1./(float)(rings- 1); float const S = 1./(float)(sectors-1); int r,s; sphere_vertices.resize(rings * sectors * 3); sphere_normals.resize(rings * sectors * 3); sphere_texcoords.resize(rings * sectors * 2); std :: vector< GLfloat> :: iterator v = sphere_vertices.begin(); std :: vector< GLfloat> :: iterator n = sphere_normals.begin(); std :: vector< GLfloat> :: iterator t = sphere_texcoords.begin(); (s = 0; s <扇区; s ++){ float const y = sin(-M_PI_2 + M_PI * r * R)的情况下,对于(r = 0; r < ; float const x = cos(2 * M_PI * s * S)* sin(M_PI * r * R); float const z = sin(2 * M_PI * s * S)* sin(M_PI * r * R); * t ++ = s * S; * t ++ = r * R; * v ++ = x * radius; * v ++ = y * radius; * v ++ = z * radius; * n ++ = x; * n ++ = y; * n ++ = z; } sphere_indices.resize(rings * sectors * 4); std:vector< GLushort> :: iterator i = sphere_indices.begin();对于(s = 0; s <扇区; s ++){ * i ++ = r *扇区+ s;对于(r = 0; r

我该如何制作他们弯曲

你不能。所有OpenGL原语都是仿射的,即平面或直线。通过绘制具有足够分辨率的短直线部分来模拟曲率。

I want to calculate all the vertices needed and connect them with lines, so I eventually come up with a sphere. How many ways are there to do it? And also the lines between the vertices, will be straight; how can I make them "curved" I know that I can use glutWireSphere(), but I am interested in actually calculating the vertices. A way that I thought about it, was to put all the vertices manually in an array, but I guess that is not the way it's done.

解决方案

Copy and Pasting some code I originally wrote in Creating a 3D sphere in Opengl using Visual C++

class SolidSphere { protected std::vector<GLfloat> vertices; std::vector<GLfloat> normals; std::vector<GLfloat> texcoords; std::vector<GLushort> indices; public: void SolidSphere(float radius, unsigned int rings, unsigned int sectors) { float const R = 1./(float)(rings-1); float const S = 1./(float)(sectors-1); int r, s; sphere_vertices.resize(rings * sectors * 3); sphere_normals.resize(rings * sectors * 3); sphere_texcoords.resize(rings * sectors * 2); std::vector<GLfloat>::iterator v = sphere_vertices.begin(); std::vector<GLfloat>::iterator n = sphere_normals.begin(); std::vector<GLfloat>::iterator t = sphere_texcoords.begin(); for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) { float const y = sin( -M_PI_2 + M_PI * r * R ); float const x = cos(2*M_PI * s * S) * sin( M_PI * r * R ); float const z = sin(2*M_PI * s * S) * sin( M_PI * r * R ); *t++ = s*S; *t++ = r*R; *v++ = x * radius; *v++ = y * radius; *v++ = z * radius; *n++ = x; *n++ = y; *n++ = z; } sphere_indices.resize(rings * sectors * 4); std:vector<GLushort>::iterator i = sphere_indices.begin(); for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) { *i++ = r * sectors + s; *i++ = r * sectors + (s+1); *i++ = (r+1) * sectors + (s+1); *i++ = (r+1) * sectors + s; } } void draw(GLfloat x, GLfloat y, GLfloat z) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(x,y,z); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(3, GL_FLOAT, 0, &sphere_vertices[0]); glNormalPointer(GL_FLOAT, 0, &sphere_normals[0]); glTexCoordPointer(2, GL_FLOAT, 0, &sphere_texcoords[0]); glDrawElements(GL_QUADS, sphere_indices.size()/4, GL_UNSIGNED_SHORT, sphere_indices); glPopMatrix(); } }

how can I make them "curved"

You can't. All OpenGL primitives are "affine", i.e. planar or straight. Curvature is emulated by drawing short, straight sections with sufficient resolution.

更多推荐

在opengl中计算一个球体

本文发布于:2023-11-21 23:38:12,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:球体   opengl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!