使用SceneKit和SCNGeometryPrimitiveTypePoint进行自定义几何时,如何调用glPointSize()(或等效的SceneKit) [英] How to call glPointSize() (or the SceneKit equivalent) when making custom geometries using SceneKit and SCNGeometryPrimitiveTypePoint

查看:432
本文介绍了使用SceneKit和SCNGeometryPrimitiveTypePoint进行自定义几何时,如何调用glPointSize()(或等效的SceneKit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个iOS应用,该应用使用自定义几何在SceneKit中渲染点云. 这篇文章对于使我到达那里非常有帮助(尽管我将其翻译为Objective-C),DavidRönnqvist的书《使用SceneKit进行3D图形》(参见有关自定义几何的章节)也是如此.该代码可以正常工作,但是我想使这些点以更大的点大小呈现-目前这些点非常小.

I'm writing an iOS app that renders a pointcloud in SceneKit using a custom geometry. This post was super helpful in getting me there (though I translated this to Objective-C), as was David Rönnqvist's book 3D Graphics with SceneKit (see chapter on custom geometries). The code works fine, but I'd like to make the points render at a larger point size - at the moment the points are super tiny.

根据OpenGL文档,您可以通过调用glPointSize()来实现.据我了解,SceneKit建立在OpenGL之上,因此我希望有一种方法可以使用SceneKit来访问此功能或进行等效操作.任何建议将不胜感激!

According to the OpenGL docs, you can do this by calling glPointSize(). From what I understand, SceneKit is built on top of OpenGL so I'm hoping there is a way to access this function or do the equivalent using SceneKit. Any suggestions would be much appreciated!

我的代码如下.我还在此处上发布了一个可访问bitbucket的小示例应用程序.

My code is below. I've also posted a small example app on bitbucket accessible here.

// set the number of points
NSUInteger numPoints = 10000;

// set the max distance points
int randomPosUL = 2;
int scaleFactor = 10000; // because I want decimal points
                         // but am getting random values using arc4random_uniform

PointcloudVertex pointcloudVertices[numPoints];

for (NSUInteger i = 0; i < numPoints; i++) {

    PointcloudVertex vertex;

    float x = (float)(arc4random_uniform(randomPosUL * 2 * scaleFactor));
    float y = (float)(arc4random_uniform(randomPosUL * 2 * scaleFactor));
    float z = (float)(arc4random_uniform(randomPosUL * 2 * scaleFactor));

    vertex.x = (x - randomPosUL * scaleFactor) / scaleFactor;
    vertex.y = (y - randomPosUL * scaleFactor) / scaleFactor;
    vertex.z = (z - randomPosUL * scaleFactor) / scaleFactor;

    vertex.r = arc4random_uniform(255) / 255.0;
    vertex.g = arc4random_uniform(255) / 255.0;
    vertex.b = arc4random_uniform(255) / 255.0;

    pointcloudVertices[i] = vertex;

    //        NSLog(@"adding vertex #%lu with position - x: %.3f y: %.3f z: %.3f | color - r:%.3f g: %.3f b: %.3f",
    //              (long unsigned)i,
    //              vertex.x,
    //              vertex.y,
    //              vertex.z,
    //              vertex.r,
    //              vertex.g,
    //              vertex.b);
}

// convert array to point cloud data (position and color)
NSData *pointcloudData = [NSData dataWithBytes:&pointcloudVertices length:sizeof(pointcloudVertices)];

// create vertex source
SCNGeometrySource *vertexSource = [SCNGeometrySource geometrySourceWithData:pointcloudData
                                                                   semantic:SCNGeometrySourceSemanticVertex
                                                                vectorCount:numPoints
                                                            floatComponents:YES
                                                        componentsPerVector:3
                                                          bytesPerComponent:sizeof(float)
                                                                 dataOffset:0
                                                                 dataStride:sizeof(PointcloudVertex)];

// create color source
SCNGeometrySource *colorSource = [SCNGeometrySource geometrySourceWithData:pointcloudData
                                                                  semantic:SCNGeometrySourceSemanticColor
                                                               vectorCount:numPoints
                                                           floatComponents:YES
                                                       componentsPerVector:3
                                                         bytesPerComponent:sizeof(float)
                                                                dataOffset:sizeof(float) * 3
                                                                dataStride:sizeof(PointcloudVertex)];

// create element
SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:nil
                                                            primitiveType:SCNGeometryPrimitiveTypePoint
                                                           primitiveCount:numPoints
                                                            bytesPerIndex:sizeof(int)];

// create geometry
SCNGeometry *pointcloudGeometry = [SCNGeometry geometryWithSources:@[ vertexSource, colorSource ] elements:@[ element]];

// add pointcloud to scene
SCNNode *pointcloudNode = [SCNNode nodeWithGeometry:pointcloudGeometry];
[self.myView.scene.rootNode addChildNode:pointcloudNode];

推荐答案

我当时正在研究自己在ios中渲染点云的问题,并通过"vade"在Twitter上找到了解决方案,并想将它发布在这里供其他人使用:

I was looking into rendering point clouds in ios myself and found a solution on twitter, by a "vade", and figured I post it here for others:

ProTip:SceneKit着色器修改器非常有用:

ProTip: SceneKit shader modifiers are useful:

mat.shaderModifiers = @{SCNShaderModifierEntryPointGeometry : @"gl_PointSize = 16.0;"};

这篇关于使用SceneKit和SCNGeometryPrimitiveTypePoint进行自定义几何时,如何调用glPointSize()(或等效的SceneKit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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