Best way to create an MVC paradigm using OpenGL? - cocoa

I am beginning to learn about OpenGL development, specifically using Mac OS X and the Cocoa and/or CGL API's, so I will use those classes as examples, but my question is more about design rather than a particular implementation. Here's the situation:
I have a 'scene' object, that can contain or reference the data to render itself, and responds to a 'render' message to draw itself, without any transformations.
I have an NSView or NSOpenGLView object that creates the openGLContext and pixelFormats, resizes the view, and updates the ModelView and Projection based on any transformations that are passed to it. The view object also contains a camera struct that is the basis for the openGL transformations.
I have a controller object that is an NSResponder object, and responds to user inputs.
I don't know if this is the best arrangement; 'model' in this case should be the scene, and I suppose classically the controller should mediate action between the model and view, but it seems odd for the view to send [[controller scene] render] every time it wants to draw the view.
And I am not sure if the best place for the 'camera' is in the view. Should I have the scene object also include the camera looking at it, and have it respond to UI input from the controller, which is currently passed to the view?
Or is this the wrong approach altogether? I am trying to shoehorn something into MVC that really isn't meant for it. I am curious as to what sort of design patterns people out there use with OpenGL.

I see it the way you described. View is designed to control the view point and projection, so keeping a camera reference in it is logical. Scene, on the other side, works as a pure data container for me, not touching the rendering methods, contrary to your model.

Related

Add an event listener to a particular part of a 3D Object in Three.js

I am having a human body object and what I want is that my code should open a popup whenever I click on any of the specified body parts such as eyes, nose, arms, etc. The object is a single compiled .obj file and I'm unable to figure out as to how should I attach an event listener to the multiple body parts. Any kind of help is much appreciated!
PS: - I am also using orbit controls to handle the zoom and rotation of object.
Thanks in advance..
To solve this issue, it's necessary to design your model in a way such that individual components like arms, eyes etc. are independent 3D objects grouped together to build a more complex asset. This is something you have to ensure during the design phase with a tool like Blender.
In the next step export to glTF instead of OBJ, load the model with THREE.GLTFLoader into your app and then perform a recursive raycasting with the entire asset via:
raycaster.intersectObject( gltf.scene, true, intersects );
By executing this code in a pointermove or pointerdown event listener you can find out whether the user interacts with the model or not.
Notice that three.js has no system to directly assign event listeners to 3D objects. You have to use a different solution like raycasting for this.

SceneKit transform/rotate object

I want to create an app using SceneKit with a solid 3D object, whose shape I can change using a given user input. Is it better to do this with predefined animations or to make lots of smaller objects (as nodes) and change their arrangements to mimic a single object changing shape? Many thanks!
Depending on what you mean by 'change shape' you have following options:
If your object is simple (ex: SCNBox, SCNCone, SCNCylinder and alike) and you can change its shape the way you want using its animatable properties (ex: chamfer radius of SCNBox), then animations is simpliest way to go.
If your object is SCNNode with contents loaded from external source and by change its shape you mean scale/rotate/translate, you can also use animations.
If you can create complex object using graph of simple objects with acceptable performance and you can achieve desired shape change with animatable properties then you can also use animations.
If your object is complex (for example, based on some mathematical model) and/or performance is main prioprity for you, then the only way to go with SceneKit is constructing custom SCNGeometries (by filling SCNGeometrySource/SCGeometryElement) from your model.
Since question have tag blender and title mentions rotate, then perhaps (2) is best for you.

Unity Mecanim Scripting Dynamic Animation

I have a scene with one fully-rigged model of human body
and I would like to users could make their own animations by transforming the model.
I am talking about an app with time-line and possibility of rotating certain muscles.
Could you please recommend me some way how to do that?
I can imagine storing some information about model state in certain times.. but I do not know how to save it as an animation.
You can't edit animations at runtime. You can create an animation from scratch and use AnimationClip.SetCurve to build up your animation, but you can't access the curves directly at runtime.
However in an editor script you can use the AnimationUtilitys to modify an Animationclip, but of course only in the editor since this is an editor class.
Answer picked from this thread.
I think best possible solution for you is to create many different animations for every body part by your own and let user to choose different combinations of animations (if you are using Animator or Animations). Or you can split model by body parts and let user change transform parameters with iTweens (move from A to B and change angle from C to D). Then u can easily save object's "from" transform and object's "to" transform and use it as animation.

Cocoa control to render a tile map

Is there any Cocoa control that is capable of drawing tile maps with multiple layers and multiple texture sources which can also intercept touches into single tiles? Having multiple layer support is not a real requirement but an optional feature (the views could still be stacked). Hardware acceleration is not needed at all.
So far I have toyed around with NSMatrix, IKImageBrowser and NSCollectionView but non of them felt like a good solution for the problem. Ideally I need an control similar to the one in Tiled.app. Is there anything, third party or built-in, or do I have to handcraft this control?
I fear that you will be hardly able to find a ready-to-use control for managing tile maps.
If I had to implement something like that on my own, I would consider using CATiledLayer, since this is the closest thing to a tile map control that I know of.
From CATiledLayer Reference:
CATiledLayer is a subclass of CALayer providing a way to asynchronously provide tiles of the layer's content, potentially cached at multiple levels of detail.
There is a nice sample by Bill Dudney (the author of "Core Animation for MacOS and the iPhone"). This sample could provide you a solid foundation for your own project, though it only displays one single PDF, allowing you to zoom in the area you clicked on (this requires rereading the tile at a different detail level).
Another interesting introduction can be found here. This is more step-by-step, so you might start with this.
Finally, on Cocoa is my Girlfriend there is a nice article, although it focuses on iOS, but you may find it anyway relevant.
Cocos2D supports building mac applications now
Article on cocos2d stating this: http://www.cocos2d-iphone.org/archives/1444
Aee here for how to do it: http://chris-fletcher.com/tag/cocos2d-os-x/
Aee here on how to use TMX tile maps with Cocos2D to build tile based maps: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:tiled_maps
This means you can use the power of Cocos2d and you will have to write much less code to get to where you want with a tile based map.
If you don't want to use Cocos2D:
It seems you would have to code it yourself, but it shouldn't be too hard to do.
First you can create your .TMX file using the tile editor "Tiled.app" then you would need to parse the XML using a standard xml library for Cocoa.
To lay out the tiles use a UIView for the overall container and then create a tile class that holds your tile display information and responds to clicks the tile class should extend UIView. For the tile class allow the assigning of a click delegate and assign your ViewController as the click delegate for all tiles so you can handle clicks easily with the clicked tile being passed to you.
Loop through your xml data and create and position the tiles in the overall UIView by using the tiles width/height and your tilemaps rows/columns.
I think in about a day or 2 you could have the tile map being rendered and clickable using the standard TMX format which will allow you to edit your map in "Tiled.app"
The TMX standard is covered here: https://github.com/bjorn/tiled/wiki/TMX-Map-Format
route-me might fit the bill.

Sliding from One View to Another

How would I change from one view to another with a slide transition?
Thanks.
Place them side by side, and use NSViewAnimation to perform the animation.
This may turn out tricky if the views' size can change. Doubly so if the two views are not the same size as each other.
Marcus Zarra gives a good example of how to use Core Animation to achieve this effect in his post on how to implement a wizard. It's very simple to do using subviews and a CATransition.
Implement all the actual drawing using CALayers. (Having not used Core Animation myself, I can't get more specific.) Put them in a single view, hosting that layer hierarchy, and use Core Animation to perform the slide transition.
This may not work so well if the views are not display-only—that is, if they host controls that the user can interact with. Layers are more or less display-only, since you can't put a view in a layer, and only views, not layers, are responders.

Resources