2009年8月1日土曜日

COLLADA DOMのViewer用のあれこれをCocoaで利用できないか?

あっちこっちのサンプルを引っ付け合わせてやってみた.
参考: 前回エントリー

1.とりあえずこんな感じのライブラリ周り.
Frameworksのツリー

Other Frameworks下のライブラリはcollada-domに入ってたり作ったりしたヤツ.
Cg.frameworkは前回エントリーでインストールしたヤツで/Library/Frameworksの下.
libz, libxml2は/usr/lib下.

LinkedとOtherの使い分け知らない.Linkedが静的でOhterが動的?

2.とりあえずこんな感じのコード.
MyView.h
#import <Cocoa/Cocoa.h>
#import <OpenGL/CGLRenderers.h>
#import <OpenGL/gl.h>
#import <OpenGL/glu.h>
#import "Crt/CrtRender.h"

@interface MyView : NSOpenGLView
{
NSTimer *timer;
}
@end


MyView.mm (拡張子はmm!)
#import "MyView.h"
@implementation MyView
CrtRender _CrtRender;
- (void) heartbeat
{
if(![[NSApplication sharedApplication] isHidden]){
[self setNeedsDisplay:YES];
}
}
- (id) initWithFrame: (NSRect) frame
{
NSOpenGLPixelFormatAttribute attribs [] = {
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFAStencilSize, 8,
0
};
NSOpenGLPixelFormat *fmt;

{
fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
NSOpenGLContext *preflight = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
[preflight makeCurrentContext];
[fmt release];

const GLubyte* extensions = glGetString(GL_EXTENSIONS);
if ((GL_FALSE == gluCheckExtension((GLubyte *)"GL_ARB_shader_objects", extensions)) ||
(GL_FALSE == gluCheckExtension((GLubyte *)"GL_ARB_shading_language_100", extensions)) ||
(GL_FALSE == gluCheckExtension((GLubyte *)"GL_ARB_vertex_shader", extensions)) ||
(GL_FALSE == gluCheckExtension((GLubyte *)"GL_ARB_fragment_shader", extensions)))
{
attribs [3] = NSOpenGLPFARendererID;
attribs [4] = kCGLRendererGenericFloatID;
}
[preflight release];
}
fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
[super initWithFrame: frame pixelFormat: fmt];
[[super openGLContext] makeCurrentContext];
[fmt release];

return self;
}
- (void) dealloc
{
if (timer) {
[timer invalidate];
[timer release];
}
[super dealloc];
}
- (void) awakeFromNib
{
NSRect r= [self bounds];
GLsizei width= ((int) r.size.width);
GLsizei height= ((int) r.size.height);
_CrtRender.SetScreenWidth(width);
_CrtRender.SetScreenHeight(height);

_CrtRender.Init();
//_CrtRender.SetUsingVBOs( CrtTrue );
_CrtRender.SetUsingNormalMaps( CrtTrue );
_CrtRender.Load( "/Users/work/Documents/dominos.dae" );
_CrtRender.SetAnimationOn( CrtTrue );

timer = [NSTimer scheduledTimerWithTimeInterval: (1.0f/60.0f)
target: self
selector: @selector(heartbeat)
userInfo: nil
repeats: YES];
[timer retain];

[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSEventTrackingRunLoopMode];

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable( GL_CULL_FACE );
glCullFace( GL_BACK );
}
- (void) drawRect: (NSRect) rect
{
GLfloat width= rect.size.width;
GLfloat height= rect.size.height;
if (height == 0)
{
height= 1;
}
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
_CrtRender.SetScreenWidth(width);
_CrtRender.SetScreenHeight(height);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

if(_CrtRender.GetScene()) {
_CrtRender.Render();
}

[[self openGLContext] flushBuffer];
}
@end



3.とりあえずこんな感じの'ヘッダー検索'の設定.

(collada-domが/Users/work/Documents/で展開されたという前提)


4.とりあえずこんな感じのIB.
CustomViewを張ってMyViewに指定.
Autosizingも全部チェックしといて拡大縮小に対応しとくとよいかもw.


5.とりあえずこんな感じの結果.

videoオリジナルのViewrでの動画はこちら

0 件のコメント: