Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

ios - Confused about Orthographic Projection of camera in SceneKit

I want to use orthographic projection to display 3D scene in my app. In my code I put a box in scene and set orthographic projection of Point of view like blow. Camera at (0,0,500) look at -z direction, and box at origin of world. So camera should be able to capture the box.

let cameraNode = SCNNode()
let pov = SCNCamera()
pov.usesOrthographicProjection = true
let width = UISreen.main.bounds.size.width
let glMat = GLKMatrix4MakeOrtho(-width/2, width/2, -width/2, width/2, 1, 1000)
pov.projectionTransform = SCNMatrix4FromGLKMatrix4(glMat)
cameraNode.camera = pov
cameraNode.position = SCNVector3.init(0, 0, 500)
scene.rootNode.addChildNode(cameraNode)

let boxGeo = SCNBox.init(width: 100, height: 100, length: 1, chamferRadius: 0)
let box = SCNNode.init(geometry: boxGeo)
scene.rootNode.addChildNode(box)

But I can see nothing. I find out If set orthographicScale to width/2.0 will works correctly.

pov.orthographicScale = Double(width/2);

Question 1 : I don't know why this works. I read docs of apple but still confused. https://developer.apple.com/documentation/scenekit/scncamera/1436612-orthographicscale?language=objc

orthographicScale

Specifies the camera’s magnification factor when using an orthographic projection.

Why I need magnify orthographic projection? I already set the dimension of it through GLKMatrix4MakeOrtho.

I'm not sure whether it is relate to viewport transform? Because viewport transform in OpenGL computed as follows: https://docs.microsoft.com/en-us/windows/desktop/opengl/glviewport

enter image description here

Question 2 : I also find If I create projection matrix like blow it works same as before.

let glMat = GLKMatrix4MakeOrtho(0, width, 0, width, 1, 1000)

In OpenGL it means different viewing box and will display different partition of scene.

Any help is appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suppose, you can't see your 3D objects in the scene using GLKMatrix4MakeOrtho, due to the fact that so many classes of GLKit for iOS and macOS are deprecated now.

In order to see your 3D object, you need to manually set Far Z Clipping parameter in Attributes Inspector. These two fields are near and far clipping planes of the frustum of your Orthographic Camera. A default value of Far=100 doesn't allow you see the scene further than 100 units away.

enter image description here

Then use a Scale property to set a scale factor for your objects in an ortho view.

In order to use it programmatically via GLKMatrix4MakeOrtho, you need to import GLKit and OpenGL modules (as well as all the necessary delegate protocols) into your project at first.

import GLKit
import OpenGL

// Obj-C GLKMatrix4

GLK_INLINE GLKMatrix4 GLKMatrix4MakeOrtho(float left, 
                                          float right,
                                          float bottom, 
                                          float top,
                                          float nearZ, 
                                          float farZ) {
    float ral = right + left;
    float rsl = right - left;
    float tab = top + bottom;
    float tsb = top - bottom;
    float fan = farZ + nearZ;
    float fsn = farZ - nearZ;

    GLKMatrix4 m = { 2.0f / rsl, 0.0f, 0.0f, 0.0f,
                     0.0f, 2.0f / tsb, 0.0f, 0.0f,
                     0.0f, 0.0f, -2.0f / fsn, 0.0f,
                     -ral / rsl, -tab / tsb, -fan / fsn, 1.0f };

    return m;
}

Also, on iOS, GLKit requires an OpenGL ES 2.0 context. In macOS, GLKit requires an OpenGL context that supports the OpenGL 3.2 Core Profile.

But remember! Many GL Features are deprecated, so you'd better use Metal framework. Look at deprecated classes of GLKit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...