Question

I want to integrate cocos3d in one of my view. So, I have a normal view controller(MapEditorViewController) and a view, in my view controller, (I created a IBOutlet CCGLView *openGLView) in which I want cocos3d to be in. In my view controller, I have a method setupCocos3D :

MapEditorViewController

- (void)setupCocos3D {

    [[CCDirector sharedDirector] setOpenGLView:openGLView];

    // Create the customized CC3Layer that supports 3D rendering.
    CC3Layer* cc3Layer = [HelloWorldLayer node];

    // Create the customized 3D scene and attach it to the layer.
    // Could also just create this inside the customer layer.
    cc3Layer.cc3Scene = [testScene scene];

    // Assign to a generic variable so we can uncomment options below to play with the capabilities
    CC3ControllableLayer* mainLayer = cc3Layer;

    // The 3D layer can run either directly in the scene, or it can run as a smaller "sub-window"
    // within any standard CCLayer. So you can have a mostly 2D window, with a smaller 3D window
    // embedded in it. To experiment with this smaller embedded 3D window, uncomment the following lines:
    //  CGSize winSize = CCDirector.sharedDirector.winSize;
    //  cc3Layer.position = ccp(30.0, 30.0);
    //  cc3Layer.contentSize = CGSizeMake(winSize.width - 100.0, winSize.width - 40.0);
    //  cc3Layer.alignContentSizeWithDeviceOrientation = YES;
    //  mainLayer = [CC3ControllableLayer node];
    //  [mainLayer addChild: cc3Layer];

    // A smaller 3D layer can even be moved around on the screen dyanmically. To see this in action,
    // uncomment the lines above as described, and also uncomment the following two lines.
    //  cc3Layer.position = ccp(0.0, 0.0);
    //  [cc3Layer runAction: [CCMoveTo actionWithDuration: 15.0 position: ccp(500.0, 250.0)]];

    // Attach the layer to the controller and run a scene with it.


    [CCDirector sharedDirector].animationInterval = (1.0f / kAnimationFrameRate);
    [CCDirector sharedDirector].displayStats = YES;
[[CCDirector sharedDirector] enableRetinaDisplay: YES];

    [[CCDirector sharedDirector] runWithScene:mainLayer];
}

setupCocos3D is called in viewDidLoad of the class MapEditorViewController.

I have the two files testScene and testLayer (called HelloWorldLayer here) that are copied from the hello world default cocos3d project.

testScene.m

/**
 * Constructs the 3D scene.
 *
 * Adds 3D objects to the scene, loading a 3D 'hello, world' message
 * from a POD file, and creating the camera and light programatically.
 *
 * When adapting this template to your application, remove all of the content
 * of this method, and add your own to construct your 3D model scene.
 *
 * NOTES:
 *
 * 1) To help you find your scene content once it is loaded, the onOpen method below contains
 *    code to automatically move the camera so that it frames the scene. You can remove that
 *    code once you know where you want to place your camera.
 *
 * 2) The POD file used for the 'hello, world' message model is fairly large, because converting a
 *    font to a mesh results in a LOT of triangles. When adapting this template project for your own
 *    application, REMOVE the POD file 'hello-world.pod' from the Resources folder of your project.
 */
-(void) initializeScene {

    // Create the camera, place it back a bit, and add it to the scene
    CC3Camera* cam = [CC3Camera nodeWithName: @"Camera"];
    cam.location = cc3v( 0.0, 0.0, 6.0 );
    [self addChild: cam];

    // Create a light, place it back and to the left at a specific
    // position (not just directional lighting), and add it to the scene
    CC3Light* lamp = [CC3Light nodeWithName: @"Lamp"];
    lamp.location = cc3v( -2.0, 0.0, 0.0 );
    lamp.isDirectionalOnly = NO;
    [cam addChild: lamp];

    // This is the simplest way to load a POD resource file and add the
    // nodes to the CC3Scene, if no customized resource subclass is needed.
    [self addContentFromPODFile: @"hello-world.pod"];


    // Create OpenGL ES buffers for the vertex arrays to keep things fast and efficient,
    // and to save memory, release the vertex content in main memory because it is now redundant.
    [self createGLBuffers];
    [self releaseRedundantContent];

    // That's it! The scene is now constructed and is good to go.

    // To help you find your scene content once it is loaded, the onOpen method below contains
    // code to automatically move the camera so that it frames the scene. You can remove that
    // code once you know where you want to place your camera.

    // If you encounter problems displaying your models, you can uncomment one or more of the
    // following lines to help you troubleshoot. You can also use these features on a single node,
    // or a structure of nodes. See the CC3Node notes for more explanation of these properties.
    // Also, the onOpen method below contains additional troubleshooting code you can comment
    // out to move the camera so that it will display the entire scene automatically.

    // Displays short descriptive text for each node (including class, node name & tag).
    // The text is displayed centered on the pivot point (origin) of the node.
//  self.shouldDrawAllDescriptors = YES;

    // Displays bounding boxes around those nodes with local content (eg- meshes).
//  self.shouldDrawAllLocalContentWireframeBoxes = YES;

    // Displays bounding boxes around all nodes. The bounding box for each node
    // will encompass its child nodes.
//  self.shouldDrawAllWireframeBoxes = YES;

    // If you encounter issues creating and adding nodes, or loading models from
    // files, the following line is used to log the full structure of the scene.
    LogInfo(@"The structure of this scene is: %@", [self structureDescription]);

    // ------------------------------------------

    // And to add some dynamism, we'll animate the 'hello, world' message
    // using a couple of actions...

    // Fetch the 'hello, world' object that was loaded from the POD file and start it rotating
    CC3MeshNode* helloTxt = (CC3MeshNode*)[self getNodeNamed: @"Hello"];
    CCActionInterval* partialRot = [CC3RotateBy actionWithDuration: 1.0
                                                          rotateBy: cc3v(0.0, 30.0, 0.0)];
    [helloTxt runAction: [CCRepeatForever actionWithAction: partialRot]];

    // To make things a bit more appealing, set up a repeating up/down cycle to
    // change the color of the text from the original red to blue, and back again.
    GLfloat tintTime = 8.0f;
    ccColor3B startColor = helloTxt.color;
    ccColor3B endColor = { 50, 0, 200 };
    CCActionInterval* tintDown = [CCTintTo actionWithDuration: tintTime
                                                          red: endColor.r
                                                        green: endColor.g
                                                         blue: endColor.b];
    CCActionInterval* tintUp = [CCTintTo actionWithDuration: tintTime
                                                        red: startColor.r
                                                      green: startColor.g
                                                       blue: startColor.b];
     CCActionInterval* tintCycle = [CCSequence actionOne: tintDown two: tintUp];
    [helloTxt runAction: [CCRepeatForever actionWithAction: tintCycle]];
}


#pragma mark Updating custom activity

/**
 * This template method is invoked periodically whenever the 3D nodes are to be updated.
 *
 * This method provides your app with an opportunity to perform update activities before
 * any changes are applied to the transformMatrix of the 3D nodes in the scene.
 *
 * For more info, read the notes of this method on CC3Node.
 */
-(void) updateBeforeTransform: (CC3NodeUpdatingVisitor*) visitor {}

/**
 * This template method is invoked periodically whenever the 3D nodes are to be updated.
 *
 * This method provides your app with an opportunity to perform update activities after
 * the transformMatrix of the 3D nodes in the scen have been recalculated.
 *
 * For more info, read the notes of this method on CC3Node.
 */
-(void) updateAfterTransform: (CC3NodeUpdatingVisitor*) visitor {
    // If you have uncommented the moveWithDuration: invocation in the onOpen: method, you
    // can uncomment the following to track how the camera moves, where it ends up, and what
    // the camera's clipping distances are, in order to determine how to position and configure
    // the camera to view the entire scene.
//  LogDebug(@"Camera: %@", activeCamera.fullDescription);
}


#pragma mark Scene opening and closing

/**
 * Callback template method that is invoked automatically when the CC3Layer that
 * holds this scene is first displayed.
 *
 * This method is a good place to invoke one of CC3Camera moveToShowAllOf:... family
 * of methods, used to cause the camera to automatically focus on and frame a particular
 * node, or the entire scene.
 *
 * For more info, read the notes of this method on CC3Scene.
 */
-(void) onOpen {

    // Move the camera to frame the scene. You can uncomment the LogDebug line in the
    // updateAfterTransform: method to track how the camera moves, where it ends up, and
    // what the camera's clipping distances are, in order to determine how to position
    // and configure the camera to view your entire scene. Then you can remove this code.
    //[self.activeCamera moveWithDuration: 3.0 toShowAllOf: self withPadding: 0.5f];

    // Uncomment this line to draw the bounding box of the scene.
//  self.shouldDrawWireframeBox = YES;
}

/**
 * Callback template method that is invoked automatically when the CC3Layer that
 * holds this scene has been removed from display.
 *
 * For more info, read the notes of this method on CC3Scene.
 */
-(void) onClose {}


#pragma mark Handling touch events 

/**
 * This method is invoked from the CC3Layer whenever a touch event occurs, if that layer
 * has indicated that it is interested in receiving touch events, and is handling them.
 *
 * Override this method to handle touch events, or remove this method to make use of
 * the superclass behaviour of selecting 3D nodes on each touch-down event.
 *
 * This method is not invoked when gestures are used for user interaction. Your custom
 * CC3Layer processes gestures and invokes higher-level application-defined behaviour
 * on this customized CC3Scene subclass.
 *
 * For more info, read the notes of this method on CC3Scene.
 */
-(void) touchEvent: (uint) touchType at: (CGPoint) touchPoint {}

/**
 * This callback template method is invoked automatically when a node has been picked
 * by the invocation of the pickNodeFromTapAt: or pickNodeFromTouchEvent:at: methods,
 * as a result of a touch event or tap gesture.
 *
 * Override this method to perform activities on 3D nodes that have been picked by the user.
 *
 * For more info, read the notes of this method on CC3Scene.
 */
-(void) nodeSelected: (CC3Node*) aNode byTouchEvent: (uint) touchType at: (CGPoint) touchPoint {

}

HelloWorldLayer.m

//
//  HelloWorldLayer.m
//  testCocos2d
//
//  Created by Etienne Noel on 2013-03-15.
//  Copyright __MyCompanyName__ 2013. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"

// Needed to obtain the Navigation Controller
#import "AppDelegate.h"




#pragma mark - HelloWorldLayer

// HelloWorldLayer implementation
@implementation HelloWorldLayer

/**
 * Override to set up your 2D controls and other initial state, and to initialize update processing.
 *
 * For more info, read the notes of this method on CC3Layer.
 */
-(void) initializeControls {
    [self scheduleUpdate];
}


#pragma mark Updating layer

/**
 * Override to perform set-up activity prior to the scene being opened
 * on the view, such as adding gesture recognizers.
 *
 * For more info, read the notes of this method on CC3Layer.
 */
-(void) onOpenCC3Layer {}

/**
 * Override to perform tear-down activity prior to the scene disappearing.
 *
 * For more info, read the notes of this method on CC3Layer.
 */
-(void) onCloseCC3Layer {}

/**
 * The ccTouchMoved:withEvent: method is optional for the <CCTouchDelegateProtocol>.
 * The event dispatcher will not dispatch events for which there is no method
 * implementation. Since the touch-move events are both voluminous and seldom used,
 * the implementation of ccTouchMoved:withEvent: has been left out of the default
 * CC3Layer implementation. To receive and handle touch-move events for object
 * picking, uncomment the following method implementation.
 */
/*
 -(void) ccTouchMoved: (UITouch *)touch withEvent: (UIEvent *)event {
 [self handleTouch: touch ofType: kCCTouchMoved];
 }
 */
@end

Everything is compiling and I only see a black screen. I have included the hello-world.pod file and in the console everything seems fine (the logging shows that the file is properly loaded).

Why am I not seeing the hello-world, even though I'm seeing the stats (fps,etc) ?

Was it helpful?

Solution

For me, adding this line in MapEditorViewController:

mainLayer.contentSize = CGSizeMake(2048, 1320);

just after the creation of mainLayer solved my problem...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top