1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
|
//
// MyDocument.m
// DisplayLinkAsyncMoviePlayer
//
// Created by vade on 10/26/10.
// Copyright __MyCompanyName__ 2010 . All rights reserved.
//
#import "aiConfig.h"
#import "MyDocument.h"
#import <OpenGL/CGLMacro.h>
#pragma mark -
#pragma mark Helper Functions
#define aisgl_min(x,y) (x<y?x:y)
#define aisgl_max(x,y) (y>x?y:x)
static void color4_to_float4(const struct aiColor4D *c, float f[4])
{
f[0] = c->r;
f[1] = c->g;
f[2] = c->b;
f[3] = c->a;
}
static void set_float4(float f[4], float a, float b, float c, float d)
{
f[0] = a;
f[1] = b;
f[2] = c;
f[3] = d;
}
#pragma mark -
#pragma mark CVDisplayLink Callback
static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink,const CVTimeStamp *inNow,const CVTimeStamp *inOutputTime,CVOptionFlags flagsIn,CVOptionFlags *flagsOut,void *displayLinkContext)
{
CVReturn error = [(MyDocument*) displayLinkContext displayLinkRenderCallback:inOutputTime];
return error;
}
#pragma mark -
@implementation MyDocument
@synthesize _view;
- (id)init
{
self = [super init];
if (self != nil)
{
// initialization code
}
return self;
}
- (NSString *)windowNibName
{
return @"MyDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController
{
[super windowControllerDidLoadNib:windowController];
NSOpenGLPixelFormatAttribute attributes[] =
{
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFAMultisample,
NSOpenGLPFASampleBuffers, 2,
(NSOpenGLPixelFormatAttribute) 0
};
_glPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
if(!_glPixelFormat)
NSLog(@"Error creating PF");
_glContext = [[NSOpenGLContext alloc] initWithFormat:_glPixelFormat shareContext:nil];
const GLint one = 1;
[_glContext setValues:&one forParameter:NSOpenGLCPSwapInterval];
[_glContext setView:_view];
// Set up initial GL state.
CGLContextObj cgl_ctx = (CGLContextObj)[_glContext CGLContextObj];
glEnable(GL_MULTISAMPLE);
glClearColor(0.3, 0.3, 0.3, 0.3);
// enable color tracking
//glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
glEnable(GL_NORMALIZE);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
GLfloat global_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
GLfloat diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
GLfloat ambient[] = {0.2, 0.2f, 0.2f, 0.2f};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
GLfloat position[] = { 1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
// This is the only client state that always has to be set.
glEnableClientState(GL_VERTEX_ARRAY);
// end GL State setup.
// Display Link setup.
CVReturn error = kCVReturnSuccess;
error = CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);
if(error == kCVReturnError)
NSLog(@"Error Creating DisplayLink");
error = CVDisplayLinkSetOutputCallback(_displayLink,MyDisplayLinkCallback, self);
if(error == kCVReturnError)
NSLog(@"Error Setting DisplayLink Callback");
error = CVDisplayLinkStart(_displayLink);
if(error == kCVReturnError)
NSLog(@"Error Starting DisplayLink");
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
[openPanel beginSheetModalForWindow:[_view window] completionHandler:^(NSInteger result)
{
if (result == NSOKButton)
{
[openPanel orderOut:self]; // close panel before we might present an error
if([[NSFileManager defaultManager] fileExistsAtPath:[openPanel filename]])
{
// Load our new path.
// only ever give us triangles.
aiSetImportPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_LINE | aiPrimitiveType_POINT );
NSUInteger aiPostProccesFlags;
switch (2)
{
case 0:
aiPostProccesFlags = aiProcessPreset_TargetRealtime_Fast;
break;
case 1:
aiPostProccesFlags = aiProcessPreset_TargetRealtime_Quality;
break;
case 2:
aiPostProccesFlags = aiProcessPreset_TargetRealtime_MaxQuality;
break;
default:
aiPostProccesFlags = aiProcessPreset_TargetRealtime_MaxQuality;
break;
}
// aiProcess_FlipUVs is needed for VAO / VBOs, not sure why.
_scene = (aiScene*) aiImportFile([[openPanel filename] cStringUsingEncoding:[NSString defaultCStringEncoding]], aiPostProccesFlags | aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_PreTransformVertices | 0 );
if (_scene)
{
textureDictionary = [[NSMutableDictionary alloc] initWithCapacity:5];
// Why do I need to cast this !?
CGLContextObj cgl_ctx = (CGLContextObj)[_glContext CGLContextObj];
CGLLockContext(cgl_ctx);
[self loadTexturesInContext:cgl_ctx withModelPath:[[openPanel filename] stringByStandardizingPath]];
//NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSValue valueWithPointer:cgl_ctx], @"context", [self.inputModelPath stringByStandardizingPath], @"path", nil ];
//[self performSelectorInBackground:@selector(loadTexturesInBackground:) withObject:userInfo];
[self getBoundingBoxWithMinVector:&scene_min maxVectr:&scene_max];
scene_center.x = (scene_min.x + scene_max.x) / 2.0f;
scene_center.y = (scene_min.y + scene_max.y) / 2.0f;
scene_center.z = (scene_min.z + scene_max.z) / 2.0f;
// optional normalized scaling
normalizedScale = scene_max.x-scene_min.x;
normalizedScale = aisgl_max(scene_max.y - scene_min.y,normalizedScale);
normalizedScale = aisgl_max(scene_max.z - scene_min.z,normalizedScale);
normalizedScale = 1.f / normalizedScale;
if(_scene->HasAnimations())
NSLog(@"scene has animations");
[self createGLResourcesInContext:cgl_ctx];
CGLUnlockContext(cgl_ctx);
}
}
}
}]; // end block handler
}
- (void) close
{
CVDisplayLinkStop(_displayLink);
CVDisplayLinkRelease(_displayLink);
if(_scene)
{
aiReleaseImport(_scene);
_scene = NULL;
CGLContextObj cgl_ctx = (CGLContextObj)[_glContext CGLContextObj];
glDeleteTextures([textureDictionary count], textureIds);
[textureDictionary release];
textureDictionary = nil;
free(textureIds);
textureIds = NULL;
[self deleteGLResourcesInContext:cgl_ctx];
}
[_glContext release];
_glContext = nil;
[_glPixelFormat release];
_glPixelFormat = nil;
[super close];
}
- (CVReturn)displayLinkRenderCallback:(const CVTimeStamp *)timeStamp
{
CVReturn rv = kCVReturnError;
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
{
[self render];
rv = kCVReturnSuccess;
}
[pool release];
return rv;
}
- (void) render
{
CGLContextObj cgl_ctx = (CGLContextObj)[_glContext CGLContextObj];
CGLLockContext(cgl_ctx);
[_glContext update];
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, _view.frame.size.width, _view.frame.size.height);
GLfloat aspect = _view.frame.size.height/_view.frame.size.width;
glOrtho(-1, 1, - (aspect), aspect, -10, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslated(0.0, 0.0, 1.0);
// Draw our GL model.
if(_scene)
{
glScaled(normalizedScale , normalizedScale, normalizedScale);
// center the model
glTranslated( -scene_center.x, -scene_center.y, -scene_center.z);
glScaled(2.0, 2.0, 2.0);
static float i = 0;
i+=0.5;
glRotated(i, 0, 1, 0);
[self drawMeshesInContext:cgl_ctx];
}
CGLUnlockContext(cgl_ctx);
CGLFlushDrawable(cgl_ctx);
}
#pragma mark -
#pragma mark Loading
// Inspired by LoadAsset() & CreateAssetData() from AssimpView D3D project
- (void) createGLResourcesInContext:(CGLContextObj)cgl_ctx
{
// create new mesh helpers for each mesh, will populate their data later.
modelMeshes = [[NSMutableArray alloc] initWithCapacity:_scene->mNumMeshes];
// create OpenGL buffers and populate them based on each meshes pertinant info.
for (unsigned int i = 0; i < _scene->mNumMeshes; ++i)
{
NSLog(@"%u", i);
// current mesh we are introspecting
const aiMesh* mesh = _scene->mMeshes[i];
// the current meshHelper we will be populating data into.
MeshHelper* meshHelper = [[MeshHelper alloc] init];
// Handle material info
aiMaterial* mtl = _scene->mMaterials[mesh->mMaterialIndex];
// Textures
int texIndex = 0;
aiString texPath;
if(AI_SUCCESS == mtl->GetTexture(aiTextureType_DIFFUSE, texIndex, &texPath))
{
NSString* textureKey = [NSString stringWithCString:texPath.data encoding:[NSString defaultCStringEncoding]];
//bind texture
NSNumber* textureNumber = (NSNumber*)[textureDictionary valueForKey:textureKey];
//NSLog(@"applyMaterialInContext: have texture %i", [textureNumber unsignedIntValue]);
meshHelper.textureID = [textureNumber unsignedIntValue];
}
else
meshHelper.textureID = 0;
// Colors
aiColor4D dcolor = aiColor4D(0.8f, 0.8f, 0.8f, 1.0f);
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &dcolor))
[meshHelper setDiffuseColor:&dcolor];
aiColor4D scolor = aiColor4D(0.0f, 0.0f, 0.0f, 1.0f);
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &scolor))
[meshHelper setSpecularColor:&scolor];
aiColor4D acolor = aiColor4D(0.2f, 0.2f, 0.2f, 1.0f);
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &acolor))
[meshHelper setAmbientColor:&acolor];
aiColor4D ecolor = aiColor4D(0.0f, 0.0f, 0.0f, 1.0f);
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &ecolor))
[meshHelper setEmissiveColor:&ecolor];
// Culling
unsigned int max = 1;
int two_sided;
if((AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)) && two_sided)
[meshHelper setTwoSided:YES];
else
[meshHelper setTwoSided:NO];
// Create a VBO for our vertices
GLuint vhandle;
glGenBuffers(1, &vhandle);
glBindBuffer(GL_ARRAY_BUFFER, vhandle);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * mesh->mNumVertices, NULL, GL_STATIC_DRAW);
// populate vertices
Vertex* verts = (Vertex*)glMapBuffer(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);
for (unsigned int x = 0; x < mesh->mNumVertices; ++x)
{
verts->vPosition = mesh->mVertices[x];
if (NULL == mesh->mNormals)
verts->vNormal = aiVector3D(0.0f,0.0f,0.0f);
else
verts->vNormal = mesh->mNormals[x];
if (NULL == mesh->mTangents)
{
verts->vTangent = aiVector3D(0.0f,0.0f,0.0f);
verts->vBitangent = aiVector3D(0.0f,0.0f,0.0f);
}
else
{
verts->vTangent = mesh->mTangents[x];
verts->vBitangent = mesh->mBitangents[x];
}
if (mesh->HasVertexColors(0))
{
verts->dColorDiffuse = mesh->mColors[0][x];
}
else
verts->dColorDiffuse = aiColor4D(1.0, 1.0, 1.0, 1.0);
// This varies slightly form Assimp View, we support the 3rd texture component.
if (mesh->HasTextureCoords(0))
verts->vTextureUV = mesh->mTextureCoords[0][x];
else
verts->vTextureUV = aiVector3D(0.5f,0.5f, 0.0f);
if (mesh->HasTextureCoords(1))
verts->vTextureUV2 = mesh->mTextureCoords[1][x];
else
verts->vTextureUV2 = aiVector3D(0.5f,0.5f, 0.0f);
// TODO: handle Bone indices and weights
/* if( mesh->HasBones())
{
unsigned char boneIndices[4] = { 0, 0, 0, 0 };
unsigned char boneWeights[4] = { 0, 0, 0, 0 };
ai_assert( weightsPerVertex[x].size() <= 4);
for( unsigned int a = 0; a < weightsPerVertex[x].size(); a++)
{
boneIndices[a] = weightsPerVertex[x][a].mVertexId;
boneWeights[a] = (unsigned char) (weightsPerVertex[x][a].mWeight * 255.0f);
}
memcpy( verts->mBoneIndices, boneIndices, sizeof( boneIndices));
memcpy( verts->mBoneWeights, boneWeights, sizeof( boneWeights));
}
else
*/
{
memset( verts->mBoneIndices, 0, sizeof( verts->mBoneIndices));
memset( verts->mBoneWeights, 0, sizeof( verts->mBoneWeights));
}
++verts;
}
glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); //invalidates verts
glBindBuffer(GL_ARRAY_BUFFER, 0);
// set the mesh vertex buffer handle to our new vertex buffer.
meshHelper.vertexBuffer = vhandle;
// Create Index Buffer
// populate the index buffer.
NSUInteger nidx;
switch (mesh->mPrimitiveTypes)
{
case aiPrimitiveType_POINT:
nidx = 1;break;
case aiPrimitiveType_LINE:
nidx = 2;break;
case aiPrimitiveType_TRIANGLE:
nidx = 3;break;
default: assert(false);
}
// create the index buffer
GLuint ihandle;
glGenBuffers(1, &ihandle);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ihandle);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * mesh->mNumFaces * nidx, NULL, GL_STATIC_DRAW);
unsigned int* indices = (unsigned int*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY_ARB);
// now fill the index buffer
for (unsigned int x = 0; x < mesh->mNumFaces; ++x)
{
for (unsigned int a = 0; a < nidx; ++a)
{
// if(mesh->mFaces[x].mNumIndices != 3)
// NSLog(@"whoa dont have 3 indices...");
*indices++ = mesh->mFaces[x].mIndices[a];
}
}
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// set the mesh index buffer handle to our new index buffer.
meshHelper.indexBuffer = ihandle;
meshHelper.numIndices = mesh->mNumFaces * nidx;
// create the normal buffer. Assimp View creates a second normal buffer. Unsure why. Using only the interleaved normals for now.
// This is here for reference.
/* GLuint nhandle;
glGenBuffers(1, &nhandle);
glBindBuffer(GL_ARRAY_BUFFER, nhandle);
glBufferData(GL_ARRAY_BUFFER, sizeof(aiVector3D)* mesh->mNumVertices, NULL, GL_STATIC_DRAW);
// populate normals
aiVector3D* normals = (aiVector3D*)glMapBuffer(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);
for (unsigned int x = 0; x < mesh->mNumVertices; ++x)
{
aiVector3D vNormal = mesh->mNormals[x];
*normals = vNormal;
++normals;
}
glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); //invalidates verts
glBindBuffer(GL_ARRAY_BUFFER, 0);
meshHelper.normalBuffer = nhandle;
*/
// Create VAO and populate it
GLuint vaoHandle;
glGenVertexArraysAPPLE(1, &vaoHandle);
glBindVertexArrayAPPLE(vaoHandle);
glBindBuffer(GL_ARRAY_BUFFER, meshHelper.vertexBuffer);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12));
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(24));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(64));
//TODO: handle second texture
// VertexPointer ought to come last, apparently this is some optimization, since if its set once, first, it gets fiddled with every time something else is update.
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshHelper.indexBuffer);
glBindVertexArrayAPPLE(0);
// save the VAO handle into our mesh helper
meshHelper.vao = vaoHandle;
// Create the display list
GLuint list = glGenLists(1);
glNewList(list, GL_COMPILE);
float dc[4];
float sc[4];
float ac[4];
float emc[4];
// Material colors and properties
color4_to_float4([meshHelper diffuseColor], dc);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, dc);
color4_to_float4([meshHelper specularColor], sc);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, sc);
color4_to_float4([meshHelper ambientColor], ac);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ac);
color4_to_float4(meshHelper.emissiveColor, emc);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emc);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// Culling
if(meshHelper.twoSided)
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
// Texture Binding
glBindTexture(GL_TEXTURE_2D, meshHelper.textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// This binds the whole VAO, inheriting all the buffer and client state. Weeee
glBindVertexArrayAPPLE(meshHelper.vao);
glDrawElements(GL_TRIANGLES, meshHelper.numIndices, GL_UNSIGNED_INT, 0);
glEndList();
meshHelper.displayList = list;
// Whew, done. Save all of this shit.
[modelMeshes addObject:meshHelper];
[meshHelper release];
}
}
- (void) deleteGLResourcesInContext:(CGLContextObj)cgl_ctx
{
for(MeshHelper* helper in modelMeshes)
{
const GLuint indexBuffer = helper.indexBuffer;
const GLuint vertexBuffer = helper.vertexBuffer;
const GLuint normalBuffer = helper.normalBuffer;
const GLuint vaoHandle = helper.vao;
const GLuint dlist = helper.displayList;
glDeleteBuffers(1, &vertexBuffer);
glDeleteBuffers(1, &indexBuffer);
glDeleteBuffers(1, &normalBuffer);
glDeleteVertexArraysAPPLE(1, &vaoHandle);
glDeleteLists(1, dlist);
helper.indexBuffer = 0;
helper.vertexBuffer = 0;
helper.normalBuffer = 0;
helper.vao = 0;
helper.displayList = 0;
}
[modelMeshes release];
modelMeshes = nil;
}
- (void) drawMeshesInContext:(CGLContextObj)cgl_ctx
{
for(MeshHelper* helper in modelMeshes)
{
// Set up meterial state.
glCallList(helper.displayList);
}
}
- (void) loadTexturesInContext:(CGLContextObj)cgl_ctx withModelPath:(NSString*) modelPath
{
if (_scene->HasTextures())
{
NSLog(@"Support for meshes with embedded textures is not implemented");
return;
}
/* getTexture Filenames and Numb of Textures */
for (unsigned int m = 0; m < _scene->mNumMaterials; m++)
{
int texIndex = 0;
aiReturn texFound = AI_SUCCESS;
aiString path; // filename
// TODO: handle other aiTextureTypes
while (texFound == AI_SUCCESS)
{
texFound = _scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE, texIndex, &path);
NSString* texturePath = [NSString stringWithCString:path.data encoding:[NSString defaultCStringEncoding]];
// add our path to the texture and the index to our texture dictionary.
[textureDictionary setValue:[NSNumber numberWithUnsignedInt:texIndex] forKey:texturePath];
texIndex++;
}
}
textureIds = (GLuint*) malloc(sizeof(GLuint) * [textureDictionary count]); //new GLuint[ [textureDictionary count] ];
glGenTextures([textureDictionary count], textureIds);
NSLog(@"textureDictionary: %@", textureDictionary);
// create our textures, populate them, and alter our textureID value for the specific textureID we create.
// so we can modify while we enumerate...
NSDictionary *textureCopy = [textureDictionary copy];
// GCD attempt.
//dispatch_sync(_queue, ^{
int i = 0;
for(NSString* texturePath in textureCopy)
{
NSString* fullTexturePath = [[[modelPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[texturePath stringByStandardizingPath]] stringByStandardizingPath];
NSLog(@"texturePath: %@", fullTexturePath);
NSImage* textureImage = [[NSImage alloc] initWithContentsOfFile:fullTexturePath];
if(textureImage)
{
//NSLog(@"Have Texture Image");
NSBitmapImageRep* bitmap = [NSBitmapImageRep alloc];
[textureImage lockFocus];
[bitmap initWithFocusedViewRect:NSMakeRect(0, 0, textureImage.size.width, textureImage.size.height)];
[textureImage unlockFocus];
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureIds[i]);
//glPixelStorei(GL_UNPACK_ROW_LENGTH, [bitmap pixelsWide]);
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// generate mip maps
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// draw into our bitmap
int samplesPerPixel = [bitmap samplesPerPixel];
if(![bitmap isPlanar] && (samplesPerPixel == 3 || samplesPerPixel == 4))
{
glTexImage2D(GL_TEXTURE_2D,
0,
//samplesPerPixel == 4 ? GL_COMPRESSED_RGBA_S3TC_DXT3_EXT : GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
samplesPerPixel == 4 ? GL_RGBA8 : GL_RGB8,
[bitmap pixelsWide],
[bitmap pixelsHigh],
0,
samplesPerPixel == 4 ? GL_RGBA : GL_RGB,
GL_UNSIGNED_BYTE,
[bitmap bitmapData]);
}
// update our dictionary to contain the proper textureID value (from out array of generated IDs)
[textureDictionary setValue:[NSNumber numberWithUnsignedInt:textureIds[i]] forKey:texturePath];
[bitmap release];
}
else
{
[textureDictionary removeObjectForKey:texturePath];
NSLog(@"Could not Load Texture: %@, removing reference to it.", fullTexturePath);
}
[textureImage release];
i++;
}
//});
[textureCopy release];
}
- (void) getBoundingBoxWithMinVector:(struct aiVector3D*) min maxVectr:(struct aiVector3D*) max
{
struct aiMatrix4x4 trafo;
aiIdentityMatrix4(&trafo);
min->x = min->y = min->z = 1e10f;
max->x = max->y = max->z = -1e10f;
[self getBoundingBoxForNode:_scene->mRootNode minVector:min maxVector:max matrix:&trafo];
}
- (void) getBoundingBoxForNode:(const struct aiNode*)nd minVector:(struct aiVector3D*) min maxVector:(struct aiVector3D*) max matrix:(struct aiMatrix4x4*) trafo
{
struct aiMatrix4x4 prev;
unsigned int n = 0, t;
prev = *trafo;
aiMultiplyMatrix4(trafo,&nd->mTransformation);
for (; n < nd->mNumMeshes; ++n)
{
const struct aiMesh* mesh = _scene->mMeshes[nd->mMeshes[n]];
for (t = 0; t < mesh->mNumVertices; ++t)
{
struct aiVector3D tmp = mesh->mVertices[t];
aiTransformVecByMatrix4(&tmp,trafo);
min->x = aisgl_min(min->x,tmp.x);
min->y = aisgl_min(min->y,tmp.y);
min->z = aisgl_min(min->z,tmp.z);
max->x = aisgl_max(max->x,tmp.x);
max->y = aisgl_max(max->y,tmp.y);
max->z = aisgl_max(max->z,tmp.z);
}
}
for (n = 0; n < nd->mNumChildren; ++n)
{
[self getBoundingBoxForNode:nd->mChildren[n] minVector:min maxVector:max matrix:trafo];
}
*trafo = prev;
}
@end
|