绘制球体 - 顶点的顺序 [英] Draw Sphere - order of vertices

查看:258
本文介绍了绘制球体 - 顶点的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在没有任何引擎的情况下在纯OpenGL ES 2.0中绘制球体。我写下一个代码:

  int GenerateSphere(int Slices,float radius,GLfloat ** vertices,GLfloat ** colors){
srand(time(NULL));
int i = 0,j = 0;
int Parallels =切片;
float tempColor = 0.0f;
int VerticesCount =(Parallels + 1)*(Slices + 1);
float angleStep =(2.0f * M_PI)/((float)Slices);

//为缓冲区分配内存
if(vertices!= NULL){
* vertices = malloc(sizeof(GLfloat)* 3 * VerticesCount);
}
if(colors!= NULL){
* colors = malloc(sizeof(GLfloat)* 4 * VerticesCount); (i = 0; i }

{
(j = 0; j< Slices + 1; j ++){

int vertex =(i *(Slices + 1)+ j)* 3;

(* vertices)[vertex + 0] = radius * sinf(angleStep *(float)i)*
sinf(angleStep *(float)j);
(* vertices)[vertex + 1] = radius * cosf(angleStep *(float)i);
(* vertices)[vertex + 2] = radius * sinf(angleStep *(float)i)*
cosf(angleStep *(float)j);
if(colors){
int colorIndex =(i *(Slices + 1)+ j)* 4;
tempColor =(float)(rand()%100)/100.0f;

(* colors)[colorIndex + 0] = 0.0f;
(* colors)[colorIndex + 1] = 0.0f;
(* colors)[colorIndex + 2] = 0.0f;
(* colors)[colorIndex +(rand()%4)] = tempColor;
(*颜色)[colorIndex + 3] = 1.0f;
}
}
}
返回VerticesCount;
}

我使用下一个代码绘制它:

  glDrawArrays(GL_TRIANGLE_STRIP,0,userData-> numVertices); 

其中userData-> numVertices - 来自函数GenerateSphere的VerticesCount。
但在屏幕上绘制系列三角形,这些不是球体近似!
我想,我需要计算顶点并使用OpenGL ES 2.0函数glDrawElements()(包含数组,包含数字顶点)。但是在屏幕上绘制的一系列三角形不是球体近似。
如何绘制球体近似值?如何指定顺序顶点(OpenGL ES 2.0术语中的索引)?

解决方案

在你开始使用OpenGL ES中的任何东西之前,有一些建议:


避免膨胀CPU / GPU性能

通过渲染形状离线来消除激烈的计算周期使用其他程序肯定会有所帮助。这些程序将提供有关形状/网格的额外细节,除了导出包含形状等点[x,y,z]的合成集合外。



这种痛苦的方式回来了,因为我一直试图搜索算法来渲染球体等,然后试图优化它们。我只是想在未来节省你的时间。只需使用Blender,然后使用您最喜爱的编程语言来解析从Blender导出的obj文件,我使用Perl。下面是渲染球体的步骤:(使用glDrawElements,因为obj文件包含索引数组)

  1)下载并安装搅拌机。 

  3)选择整个形状并对其进行三角化。 

  4)导出一个obj文件并对其进行解析。 



您应该能够掌握从该文件渲染球体的逻辑: http://pastebin.com/4esQdVPP 。它适用于Android,但概念相同。
希望这有助于。


I would like draw sphere in pure OpenGL ES 2.0 without any engines. I write next code:

 int GenerateSphere (int Slices, float radius, GLfloat **vertices, GLfloat **colors) {
 srand(time(NULL));
 int i=0, j = 0;
 int Parallels = Slices ;
 float tempColor = 0.0f;    
 int VerticesCount = ( Parallels + 1 ) * ( Slices + 1 );
 float angleStep = (2.0f * M_PI) / ((float) Slices);

 // Allocate memory for buffers
 if ( vertices != NULL ) {
    *vertices = malloc ( sizeof(GLfloat) * 3 * VerticesCount );
 }
 if ( colors != NULL) {
    *colors = malloc( sizeof(GLfloat) * 4 * VerticesCount);
 }

 for ( i = 0; i < Parallels+1; i++ ) {
     for ( j = 0; j < Slices+1 ; j++ ) {

         int vertex = ( i * (Slices + 1) + j ) * 3;

         (*vertices)[vertex + 0] = radius * sinf ( angleStep * (float)i ) *
                    sinf ( angleStep * (float)j );
         (*vertices)[vertex + 1] = radius * cosf ( angleStep * (float)i );
         (*vertices)[vertex + 2] = radius * sinf ( angleStep * (float)i ) *
                        cosf ( angleStep * (float)j );
         if ( colors ) {
                int colorIndex = ( i * (Slices + 1) + j ) * 4;
                tempColor = (float)(rand()%100)/100.0f;

                (*colors)[colorIndex + 0] =  0.0f;
                (*colors)[colorIndex + 1] =  0.0f;
                (*colors)[colorIndex + 2] =  0.0f;
                (*colors)[colorIndex + (rand()%4)] = tempColor;
                (*colors)[colorIndex + 3] =  1.0f;
            }
        }
    }
    return VerticesCount;
}

I'm drawing it with using next code:

glDrawArrays(GL_TRIANGLE_STRIP, 0, userData->numVertices);

Where userData->numVertices - VerticesCount from function GenerateSphere. But on screen draws series triangles, these aren't sphere approximation! I think, I need to numerate vertices and use OpenGL ES 2.0 function glDrawElements() (with array, contained number vertices). But series of triangles drawn on the screen is not a sphere approximation. How can I draw sphere approximation? How specify order vertices (indices in OpenGL ES 2.0 terms)?

解决方案

Before you start with anything in OpenGL ES, here is some advice:

Avoid bloating CPU/GPU performance

Removing intense cycles of calculations by rendering the shapes offline using another program will surely help. These programs will provide additional details about the shapes/meshes apart from exporting the resultant collection of points [x,y,z] comprising the shapes etc.

I went through all this pain way back, because I kept trying to search for algorithms to render spheres etc and then trying to optimize them. I just wanted to save your time in the future. Just use Blender and then your favorite programming language to parse the obj files that are exported from Blender, I use Perl. Here are the steps to render sphere: (use glDrawElements because the obj file contains the array of indices)

1) Download and install Blender.

2) From the menu, add sphere and then reduce the number of rings and segments.

3) Select the entire shape and triangulate it.

4) Export an obj file and parse it for the meshes.

You should be able to grasp the logic to render sphere from this file: http://pastebin.com/4esQdVPP. It is for Android, but the concepts are same. Hope this helps.

这篇关于绘制球体 - 顶点的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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