PageRenderTime 74ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/cocos3d/cocos3d/Shaders/CC3ShaderSemantics.m

https://github.com/SandraGuzman/cocos3d
Objective C | 1739 lines | 1405 code | 180 blank | 154 comment | 74 complexity | cdf98d8faeea0cf87577a72ea094f72b MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * CC3ShaderSemantics.m
  3. *
  4. * cocos3d 2.0.0
  5. * Author: Bill Hollings
  6. * Copyright (c) 2011-2014 The Brenwill Workshop Ltd. All rights reserved.
  7. * http://www.brenwill.com
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. * http://en.wikipedia.org/wiki/MIT_License
  28. *
  29. * See header file CC3ShaderSemantics.h for full API documentation.
  30. */
  31. #import "CC3ShaderSemantics.h"
  32. #import "CC3GLSLVariable.h"
  33. #import "CC3NodeVisitor.h"
  34. #import "CC3MeshNode.h"
  35. #import "CC3Camera.h"
  36. #import "CC3Light.h"
  37. #import "CC3Scene.h"
  38. #import "CC3PointParticles.h"
  39. #import "CC3NodeAnimation.h"
  40. #import "CC3VertexSkinning.h"
  41. #import "CC3OpenGL.h"
  42. NSString* NSStringFromCC3Semantic(CC3Semantic semantic) {
  43. switch (semantic) {
  44. case kCC3SemanticNone: return @"kCC3SemanticNone";
  45. // VERTEX CONTENT --------------
  46. case kCC3SemanticVertexLocation: return @"kCC3SemanticVertexLocation";
  47. case kCC3SemanticVertexNormal: return @"kCC3SemanticVertexNormal";
  48. case kCC3SemanticVertexTangent: return @"kCC3SemanticVertexTangent";
  49. case kCC3SemanticVertexBitangent: return @"kCC3SemanticVertexBitangent";
  50. case kCC3SemanticVertexColor: return @"kCC3SemanticVertexColor";
  51. case kCC3SemanticVertexPointSize: return @"kCC3SemanticVertexPointSize";
  52. case kCC3SemanticVertexBoneWeights: return @"kCC3SemanticVertexBoneWeights";
  53. case kCC3SemanticVertexBoneIndices: return @"kCC3SemanticVertexBoneIndices";
  54. case kCC3SemanticVertexTexture: return @"kCC3SemanticVertexTexture";
  55. case kCC3SemanticHasVertexNormal: return @"kCC3SemanticHasVertexNormal";
  56. case kCC3SemanticShouldNormalizeVertexNormal: return @"kCC3SemanticShouldNormalizeVertexNormal";
  57. case kCC3SemanticShouldRescaleVertexNormal: return @"kCC3SemanticShouldRescaleVertexNormal";
  58. case kCC3SemanticHasVertexTangent: return @"kCC3SemanticHasVertexTangent";
  59. case kCC3SemanticHasVertexBitangent: return @"kCC3SemanticHasVertexBitangent";
  60. case kCC3SemanticHasVertexColor: return @"kCC3SemanticHasVertexColor";
  61. case kCC3SemanticHasVertexWeight: return @"kCC3SemanticHasVertexWeight";
  62. case kCC3SemanticHasVertexMatrixIndex: return @"kCC3SemanticHasVertexMatrixIndex";
  63. case kCC3SemanticHasVertexTextureCoordinate: return @"kCC3SemanticHasVertexTextureCoordinate";
  64. case kCC3SemanticHasVertexPointSize: return @"kCC3SemanticHasVertexPointSize";
  65. case kCC3SemanticIsDrawingPoints: return @"kCC3SemanticIsDrawingPoints";
  66. case kCC3SemanticShouldDrawFrontFaces: return @"kCC3SemanticShouldDrawFrontFaces";
  67. case kCC3SemanticShouldDrawBackFaces: return @"kCC3SemanticShouldDrawBackFaces";
  68. // ENVIRONMENT MATRICES --------------
  69. case kCC3SemanticModelLocalMatrix: return @"kCC3SemanticModelLocalMatrix";
  70. case kCC3SemanticModelLocalMatrixInv: return @"kCC3SemanticModelLocalMatrixInv";
  71. case kCC3SemanticModelLocalMatrixInvTran: return @"kCC3SemanticModelLocalMatrixInvTran";
  72. case kCC3SemanticModelMatrix: return @"kCC3SemanticModelMatrix";
  73. case kCC3SemanticModelMatrixInv: return @"kCC3SemanticModelMatrixInv";
  74. case kCC3SemanticModelMatrixInvTran: return @"kCC3SemanticModelMatrixInvTran";
  75. case kCC3SemanticViewMatrix: return @"kCC3SemanticViewMatrix";
  76. case kCC3SemanticViewMatrixInv: return @"kCC3SemanticViewMatrixInv";
  77. case kCC3SemanticViewMatrixInvTran: return @"kCC3SemanticViewMatrixInvTran";
  78. case kCC3SemanticModelViewMatrix: return @"kCC3SemanticModelViewMatrix";
  79. case kCC3SemanticModelViewMatrixInv: return @"kCC3SemanticModelViewMatrixInv";
  80. case kCC3SemanticModelViewMatrixInvTran: return @"kCC3SemanticModelViewMatrixInvTran";
  81. case kCC3SemanticProjMatrix: return @"kCC3SemanticProjMatrix";
  82. case kCC3SemanticProjMatrixInv: return @"kCC3SemanticProjMatrixInv";
  83. case kCC3SemanticProjMatrixInvTran: return @"kCC3SemanticProjMatrixInvTran";
  84. case kCC3SemanticViewProjMatrix: return @"kCC3SemanticViewProjMatrix";
  85. case kCC3SemanticViewProjMatrixInv: return @"kCC3SemanticViewProjMatrixInv";
  86. case kCC3SemanticViewProjMatrixInvTran: return @"kCC3SemanticViewProjMatrixInvTran";
  87. case kCC3SemanticModelViewProjMatrix: return @"kCC3SemanticModelViewProjMatrix";
  88. case kCC3SemanticModelViewProjMatrixInv: return @"kCC3SemanticModelViewProjMatrixInv";
  89. case kCC3SemanticModelViewProjMatrixInvTran: return @"kCC3SemanticModelViewProjMatrixInvTran";
  90. // CAMERA -----------------
  91. case kCC3SemanticCameraLocationModelSpace: return @"kCC3SemanticCameraLocationModelSpace";
  92. case kCC3SemanticCameraLocationGlobal: return @"kCC3SemanticCameraLocationGlobal";
  93. case kCC3SemanticCameraFrustum: return @"kCC3SemanticCameraFrustum";
  94. case kCC3SemanticCameraFrustumDepth: return @"kCC3SemanticCameraFrustumDepth";
  95. // MATERIALS --------------
  96. case kCC3SemanticColor: return @"kCC3SemanticColor";
  97. case kCC3SemanticMaterialColorAmbient: return @"kCC3SemanticMaterialColorAmbient";
  98. case kCC3SemanticMaterialColorDiffuse: return @"kCC3SemanticMaterialColorDiffuse";
  99. case kCC3SemanticMaterialColorSpecular: return @"kCC3SemanticMaterialColorSpecular";
  100. case kCC3SemanticMaterialColorEmission: return @"kCC3SemanticMaterialColorEmission";
  101. case kCC3SemanticMaterialOpacity: return @"kCC3SemanticMaterialOpacity";
  102. case kCC3SemanticMaterialShininess: return @"kCC3SemanticMaterialShininess";
  103. case kCC3SemanticMaterialReflectivity: return @"kCC3SemanticMaterialReflectivity";
  104. // LIGHTING --------------
  105. case kCC3SemanticIsUsingLighting: return @"kCC3SemanticIsUsingLighting";
  106. case kCC3SemanticSceneLightColorAmbient: return @"kCC3SemanticSceneLightColorAmbient";
  107. case kCC3SemanticLightIsEnabled: return @"kCC3SemanticLightIsEnabled";
  108. case kCC3SemanticLightPositionGlobal: return @"kCC3SemanticLightPositionGlobal";
  109. case kCC3SemanticLightPositionEyeSpace: return @"kCC3SemanticLightPositionEyeSpace";
  110. case kCC3SemanticLightPositionModelSpace: return @"kCC3SemanticLightPositionModelSpace";
  111. case kCC3SemanticLightInvertedPositionGlobal: return @"kCC3SemanticLightInvertedPositionGlobal";
  112. case kCC3SemanticLightInvertedPositionEyeSpace: return @"kCC3SemanticLightInvertedPositionEyeSpace";
  113. case kCC3SemanticLightInvertedPositionModelSpace: return @"kCC3SemanticLightInvertedPositionModelSpace";
  114. case kCC3SemanticLightColorAmbient: return @"kCC3SemanticLightColorAmbient";
  115. case kCC3SemanticLightColorDiffuse: return @"kCC3SemanticLightColorDiffuse";
  116. case kCC3SemanticLightColorSpecular: return @"kCC3SemanticLightColorSpecular";
  117. case kCC3SemanticLightAttenuation: return @"kCC3SemanticLightAttenuation";
  118. case kCC3SemanticLightSpotDirectionGlobal: return @"kCC3SemanticLightSpotDirectionGlobal";
  119. case kCC3SemanticLightSpotDirectionEyeSpace: return @"kCC3SemanticLightSpotDirectionEyeSpace";
  120. case kCC3SemanticLightSpotDirectionModelSpace: return @"kCC3SemanticLightSpotDirectionModelSpace";
  121. case kCC3SemanticLightSpotExponent: return @"kCC3SemanticLightSpotExponent";
  122. case kCC3SemanticLightSpotCutoffAngle: return @"kCC3SemanticLightSpotCutoffAngle";
  123. case kCC3SemanticLightSpotCutoffAngleCosine: return @"kCC3SemanticLightSpotCutoffAngleCosine";
  124. case kCC3SemanticFogIsEnabled: return @"kCC3SemanticFogIsEnabled";
  125. case kCC3SemanticFogColor: return @"kCC3SemanticFogColor";
  126. case kCC3SemanticFogAttenuationMode: return @"kCC3SemanticFogAttenuationMode";
  127. case kCC3SemanticFogDensity: return @"kCC3SemanticFogDensity";
  128. case kCC3SemanticFogStartDistance: return @"kCC3SemanticFogStartDistance";
  129. case kCC3SemanticFogEndDistance: return @"kCC3SemanticFogEndDistance";
  130. // TEXTURES --------------
  131. case kCC3SemanticTextureCount: return @"kCC3SemanticTextureCount";
  132. case kCC3SemanticTextureSampler: return @"kCC3SemanticTextureSampler";
  133. case kCC3SemanticTexture2DCount: return @"kCC3SemanticTexture2DCount";
  134. case kCC3SemanticTexture2DSampler: return @"kCC3SemanticTexture2DSampler";
  135. case kCC3SemanticTextureCubeCount: return @"kCC3SemanticTextureCubeCount";
  136. case kCC3SemanticTextureCubeSampler: return @"kCC3SemanticTextureCubeSampler";
  137. case kCC3SemanticTexUnitMode: return @"kCC3SemanticTexUnitMode";
  138. case kCC3SemanticTexUnitConstantColor: return @"kCC3SemanticTexUnitConstantColor";
  139. case kCC3SemanticTexUnitCombineRGBFunction: return @"kCC3SemanticTexUnitCombineRGBFunction";
  140. case kCC3SemanticTexUnitSource0RGB: return @"kCC3SemanticTexUnitSource0RGB";
  141. case kCC3SemanticTexUnitSource1RGB: return @"kCC3SemanticTexUnitSource1RGB";
  142. case kCC3SemanticTexUnitSource2RGB: return @"kCC3SemanticTexUnitSource2RGB";
  143. case kCC3SemanticTexUnitOperand0RGB: return @"kCC3SemanticTexUnitOperand0RGB";
  144. case kCC3SemanticTexUnitOperand1RGB: return @"kCC3SemanticTexUnitOperand1RGB";
  145. case kCC3SemanticTexUnitOperand2RGB: return @"kCC3SemanticTexUnitOperand2RGB";
  146. case kCC3SemanticTexUnitCombineAlphaFunction: return @"kCC3SemanticTexUnitCombineAlphaFunction";
  147. case kCC3SemanticTexUnitSource0Alpha: return @"kCC3SemanticTexUnitSource0Alpha";
  148. case kCC3SemanticTexUnitSource1Alpha: return @"kCC3SemanticTexUnitSource1Alpha";
  149. case kCC3SemanticTexUnitSource2Alpha: return @"kCC3SemanticTexUnitSource2Alpha";
  150. case kCC3SemanticTexUnitOperand0Alpha: return @"kCC3SemanticTexUnitOperand0Alpha";
  151. case kCC3SemanticTexUnitOperand1Alpha: return @"kCC3SemanticTexUnitOperand1Alpha";
  152. case kCC3SemanticTexUnitOperand2Alpha: return @"kCC3SemanticTexUnitOperand2Alpha";
  153. // MODEL ----------------
  154. case kCC3SemanticAnimationFraction: return @"kCC3SemanticAnimationFraction";
  155. case kCC3SemanticCenterOfGeometry: return @"kCC3SemanticCenterOfGeometry";
  156. case kCC3SemanticBoundingBoxMin: return @"kCC3SemanticBoundingBoxMin";
  157. case kCC3SemanticBoundingBoxMax: return @"kCC3SemanticBoundingBoxMax";
  158. case kCC3SemanticBoundingBoxSize: return @"kCC3SemanticBoundingBoxSize";
  159. case kCC3SemanticBoundingRadius: return @"kCC3SemanticBoundingRadius";
  160. // BONE SKINNING
  161. case kCC3SemanticVertexBoneCount: return @"kCC3SemanticVertexBoneCount";
  162. case kCC3SemanticBatchBoneCount: return @"kCC3SemanticBatchBoneCount";
  163. // BONE SKINNING MATRICES
  164. case kCC3SemanticBoneMatricesGlobal: return @"kCC3SemanticBoneMatricesGlobal";
  165. case kCC3SemanticBoneMatricesInvTranGlobal: return @"kCC3SemanticBoneMatricesInvTranGlobal";
  166. case kCC3SemanticBoneMatricesEyeSpace: return @"kCC3SemanticBoneMatricesEyeSpace";
  167. case kCC3SemanticBoneMatricesInvTranEyeSpace: return @"kCC3SemanticBoneMatricesInvTranEyeSpace";
  168. case kCC3SemanticBoneMatricesModelSpace: return @"kCC3SemanticBoneMatricesModelSpace";
  169. case kCC3SemanticBoneMatricesInvTranModelSpace: return @"kCC3SemanticBoneMatricesInvTranModelSpace";
  170. // BONE SKINNING DISCRETE TRANSFORMS
  171. case kCC3SemanticBoneQuaternionsGlobal: return @"kCC3SemanticBoneQuaternionsGlobal";
  172. case kCC3SemanticBoneTranslationsGlobal: return @"kCC3SemanticBoneTranslationsGlobal";
  173. case kCC3SemanticBoneScalesGlobal: return @"kCC3SemanticBoneScalesGlobal";
  174. case kCC3SemanticBoneQuaternionsEyeSpace: return @"kCC3SemanticBoneQuaternionsEyeSpace";
  175. case kCC3SemanticBoneTranslationsEyeSpace: return @"kCC3SemanticBoneTranslationsEyeSpace";
  176. case kCC3SemanticBoneScalesEyeSpace: return @"kCC3SemanticBoneScalesEyeSpace";
  177. case kCC3SemanticBoneQuaternionsModelSpace: return @"kCC3SemanticBoneQuaternionsModelSpace";
  178. case kCC3SemanticBoneTranslationsModelSpace: return @"kCC3SemanticBoneTranslationsModelSpace";
  179. case kCC3SemanticBoneScalesModelSpace: return @"kCC3SemanticBoneScalesModelSpace";
  180. // PARTICLES ------------
  181. case kCC3SemanticPointSize: return @"kCC3SemanticPointSize";
  182. case kCC3SemanticPointSizeAttenuation: return @"kCC3SemanticPointSizeAttenuation";
  183. case kCC3SemanticPointSizeMinimum: return @"kCC3SemanticPointSizeMinimum";
  184. case kCC3SemanticPointSizeMaximum: return @"kCC3SemanticPointSizeMaximum";
  185. case kCC3SemanticPointSpritesIsEnabled: return @"kCC3SemanticPointSpritesIsEnabled";
  186. // TIME ------------------
  187. case kCC3SemanticFrameTime: return @"kCC3SemanticFrameTime";
  188. case kCC3SemanticSceneTime: return @"kCC3SemanticSceneTime";
  189. case kCC3SemanticSceneTimeSine: return @"kCC3SemanticSceneTimeSine";
  190. case kCC3SemanticSceneTimeCosine: return @"kCC3SemanticSceneTimeCosine";
  191. case kCC3SemanticSceneTimeTangent: return @"kCC3SemanticSceneTimeTangent";
  192. // MISC ENVIRONMENT ---------
  193. case kCC3SemanticViewport: return @"kCC3SemanticViewport";
  194. case kCC3SemanticDrawCountCurrentFrame: return @"kCC3SemanticDrawCountCurrentFrame";
  195. case kCC3SemanticRandomNumber: return @"kCC3SemanticRandomNumber";
  196. case kCC3SemanticAppBase: return @"kCC3SemanticAppBase";
  197. case kCC3SemanticMax: return @"kCC3SemanticMax";
  198. default: return [NSString stringWithFormat: @"Unknown state semantic (%u)", semantic];
  199. }
  200. }
  201. #pragma mark -
  202. #pragma mark CC3GLSLVariableConfiguration
  203. @implementation CC3GLSLVariableConfiguration
  204. @synthesize name=_name, semantic=_semantic, semanticIndex=_semanticIndex;
  205. -(void) dealloc {
  206. [_name release];
  207. [super dealloc];
  208. }
  209. -(id) init {
  210. if ( (self = [super init]) ) {
  211. _name = nil;
  212. _semantic = kCC3SemanticNone;
  213. _semanticIndex = 0;
  214. }
  215. return self;
  216. }
  217. @end
  218. #pragma mark -
  219. #pragma mark CC3ShaderSemanticsBase
  220. @implementation CC3ShaderSemanticsBase
  221. +(id) semanticsDelegate { return [[[self alloc] init] autorelease]; }
  222. -(NSString*) nameOfSemantic: (GLenum) semantic { return NSStringFromCC3Semantic(semantic); }
  223. -(BOOL) configureVariable: (CC3GLSLVariable*) variable { return NO; }
  224. /**
  225. * Returns a variable scope derived from the specified semantic.
  226. *
  227. * Subclasses that permit application-specific semantics should override this method to
  228. * handle those additional semantics if they should not default to kCC3GLSLVariableScopeNode.
  229. */
  230. -(CC3GLSLVariableScope) variableScopeForSemantic: (GLenum) semantic {
  231. switch (semantic) {
  232. // Draw scope semantics
  233. case kCC3SemanticDrawCountCurrentFrame:
  234. case kCC3SemanticRandomNumber:
  235. case kCC3SemanticBatchBoneCount:
  236. case kCC3SemanticBoneMatricesGlobal:
  237. case kCC3SemanticBoneMatricesInvTranGlobal:
  238. case kCC3SemanticBoneMatricesEyeSpace:
  239. case kCC3SemanticBoneMatricesInvTranEyeSpace:
  240. case kCC3SemanticBoneMatricesModelSpace:
  241. case kCC3SemanticBoneMatricesInvTranModelSpace:
  242. case kCC3SemanticBoneQuaternionsGlobal:
  243. case kCC3SemanticBoneTranslationsGlobal:
  244. case kCC3SemanticBoneScalesGlobal:
  245. case kCC3SemanticBoneQuaternionsEyeSpace:
  246. case kCC3SemanticBoneTranslationsEyeSpace:
  247. case kCC3SemanticBoneScalesEyeSpace:
  248. case kCC3SemanticBoneQuaternionsModelSpace:
  249. case kCC3SemanticBoneTranslationsModelSpace:
  250. case kCC3SemanticBoneScalesModelSpace:
  251. return kCC3GLSLVariableScopeDraw;
  252. // Scene scope semantics
  253. case kCC3SemanticViewMatrix:
  254. case kCC3SemanticViewMatrixInv:
  255. case kCC3SemanticViewMatrixInvTran:
  256. case kCC3SemanticProjMatrix:
  257. case kCC3SemanticProjMatrixInv:
  258. case kCC3SemanticProjMatrixInvTran:
  259. case kCC3SemanticViewProjMatrix:
  260. case kCC3SemanticViewProjMatrixInv:
  261. case kCC3SemanticViewProjMatrixInvTran:
  262. case kCC3SemanticCameraLocationGlobal:
  263. case kCC3SemanticCameraFrustum:
  264. case kCC3SemanticCameraFrustumDepth:
  265. case kCC3SemanticViewport:
  266. case kCC3SemanticSceneLightColorAmbient:
  267. case kCC3SemanticLightIsEnabled:
  268. case kCC3SemanticLightPositionGlobal:
  269. case kCC3SemanticLightPositionEyeSpace:
  270. case kCC3SemanticLightInvertedPositionGlobal:
  271. case kCC3SemanticLightInvertedPositionEyeSpace:
  272. case kCC3SemanticLightColorAmbient:
  273. case kCC3SemanticLightColorDiffuse:
  274. case kCC3SemanticLightColorSpecular:
  275. case kCC3SemanticLightAttenuation:
  276. case kCC3SemanticLightSpotDirectionGlobal:
  277. case kCC3SemanticLightSpotDirectionEyeSpace:
  278. case kCC3SemanticLightSpotExponent:
  279. case kCC3SemanticLightSpotCutoffAngle:
  280. case kCC3SemanticLightSpotCutoffAngleCosine:
  281. case kCC3SemanticFogIsEnabled:
  282. case kCC3SemanticFogColor:
  283. case kCC3SemanticFogAttenuationMode:
  284. case kCC3SemanticFogDensity:
  285. case kCC3SemanticFogStartDistance:
  286. case kCC3SemanticFogEndDistance:
  287. case kCC3SemanticFrameTime:
  288. case kCC3SemanticSceneTime:
  289. case kCC3SemanticSceneTimeSine:
  290. case kCC3SemanticSceneTimeCosine:
  291. case kCC3SemanticSceneTimeTangent:
  292. return kCC3GLSLVariableScopeScene;
  293. // Node scope semantics
  294. default:
  295. return kCC3GLSLVariableScopeNode;
  296. }
  297. }
  298. /**
  299. * For semantics that may have more than one target, such as components of lights, or textures,
  300. * the iteration loops in this method are designed to deal with two situations:
  301. * - If the uniform is declared as an array of single types (eg- an array of floats, bools, or
  302. * vec3's), the uniform semantic index will be zero and the uniform size will be larger than one.
  303. * - If the uniform is declared as a scalar (eg- distinct uniforms for each light, etc), the
  304. * uniform size will be one, but the uniform semantic index can be larger than zero.
  305. */
  306. -(BOOL) populateUniform: (CC3GLSLUniform*) uniform withVisitor: (CC3NodeDrawingVisitor*) visitor {
  307. LogTrace(@"%@ drawing %@ retrieving semantic value for %@", self, visitor.currentNode, uniform.fullDescription);
  308. GLenum semantic = uniform.semantic;
  309. GLuint semanticIndex = uniform.semanticIndex;
  310. GLint uniformSize = uniform.size;
  311. CC3Material* mat;
  312. CC3SkinSection* skin;
  313. CC3PointParticleEmitter* emitter;
  314. CC3Matrix4x4 m4x4;
  315. CC3Matrix4x3 m4x3, mRslt4x3, tfmMtx, *pm4x3;
  316. CC3Matrix3x3 m3x3;
  317. CC3Viewport vp;
  318. CCTime sceneTime;
  319. GLuint boneCnt = 0, tuCnt = 0, texCnt = 0;
  320. BOOL isInverted = NO, isPtEmitter = NO;
  321. switch (semantic) {
  322. #pragma mark Setting attribute semantics
  323. // ATTRIBUTE QUALIFIERS --------------
  324. case kCC3SemanticHasVertexNormal:
  325. [uniform setBoolean: visitor.currentMesh.hasVertexNormals];
  326. return YES;
  327. case kCC3SemanticShouldNormalizeVertexNormal:
  328. [uniform setBoolean: (visitor.currentMeshNode.effectiveNormalScalingMethod == kCC3NormalScalingNormalize)];
  329. return YES;
  330. case kCC3SemanticShouldRescaleVertexNormal:
  331. [uniform setBoolean: (visitor.currentMeshNode.effectiveNormalScalingMethod == kCC3NormalScalingRescale)];
  332. return YES;
  333. case kCC3SemanticHasVertexTangent:
  334. [uniform setBoolean: visitor.currentMesh.hasVertexTangents];
  335. return YES;
  336. case kCC3SemanticHasVertexBitangent:
  337. [uniform setBoolean: visitor.currentMesh.hasVertexBitangents];
  338. return YES;
  339. case kCC3SemanticHasVertexColor:
  340. [uniform setBoolean: visitor.currentMesh.hasVertexColors];
  341. return YES;
  342. case kCC3SemanticHasVertexWeight:
  343. [uniform setBoolean: visitor.currentMesh.hasVertexBoneWeights];
  344. return YES;
  345. case kCC3SemanticHasVertexMatrixIndex:
  346. [uniform setBoolean: visitor.currentMesh.hasVertexBoneIndices];
  347. return YES;
  348. case kCC3SemanticHasVertexTextureCoordinate:
  349. [uniform setBoolean: visitor.currentMesh.hasVertexTextureCoordinates];
  350. return YES;
  351. case kCC3SemanticHasVertexPointSize:
  352. [uniform setBoolean: visitor.currentMesh.hasVertexPointSizes];
  353. return YES;
  354. case kCC3SemanticIsDrawingPoints:
  355. [uniform setBoolean: visitor.currentMeshNode.drawingMode == GL_POINTS];
  356. return YES;
  357. case kCC3SemanticShouldDrawFrontFaces:
  358. [uniform setBoolean: !visitor.currentMeshNode.shouldCullFrontFaces];
  359. return YES;
  360. case kCC3SemanticShouldDrawBackFaces:
  361. [uniform setBoolean: !visitor.currentMeshNode.shouldCullBackFaces];
  362. return YES;
  363. #pragma mark Setting environment matrix semantics
  364. // ENVIRONMENT MATRICES --------------
  365. case kCC3SemanticModelLocalMatrix:
  366. // Get local matrix as P(-1).T where T is node transform P(-1) is inv-xfm of parent
  367. [visitor.currentMeshNode.parent.globalTransformMatrixInverted populateCC3Matrix4x3: &m4x3];
  368. [visitor.currentMeshNode.globalTransformMatrix populateCC3Matrix4x3: &tfmMtx];
  369. CC3Matrix4x3Multiply(&mRslt4x3, &m4x3, &tfmMtx);
  370. [uniform setMatrix4x3: &mRslt4x3];
  371. return YES;
  372. case kCC3SemanticModelLocalMatrixInv:
  373. // Get local matrix as P(-1).T where T is node transform P(-1) is inv-xfm of parent
  374. [visitor.currentMeshNode.parent.globalTransformMatrixInverted populateCC3Matrix4x3: &m4x3];
  375. [visitor.currentMeshNode.globalTransformMatrix populateCC3Matrix4x3: &tfmMtx];
  376. CC3Matrix4x3Multiply(&mRslt4x3, &m4x3, &tfmMtx);
  377. // Now invert
  378. CC3Matrix4x3InvertAdjoint(&mRslt4x3);
  379. [uniform setMatrix4x3: &mRslt4x3];
  380. return YES;
  381. case kCC3SemanticModelLocalMatrixInvTran:
  382. // Get local matrix as P(-1).T where T is node transform P(-1) is inv-xfm of parent
  383. [visitor.currentMeshNode.parent.globalTransformMatrixInverted populateCC3Matrix4x3: &m4x3];
  384. [visitor.currentMeshNode.globalTransformMatrix populateCC3Matrix4x3: &tfmMtx];
  385. CC3Matrix4x3Multiply(&mRslt4x3, &m4x3, &tfmMtx);
  386. CC3Matrix3x3PopulateFrom4x3(&m3x3, &mRslt4x3);
  387. CC3Matrix3x3InvertAdjointTranspose(&m3x3);
  388. [uniform setMatrix3x3: &m3x3];
  389. return YES;
  390. case kCC3SemanticModelMatrix:
  391. [uniform setMatrix4x3: visitor.modelMatrix];
  392. return YES;
  393. case kCC3SemanticModelMatrixInv:
  394. CC3Matrix4x3PopulateFrom4x3(&m4x3, visitor.modelMatrix);
  395. CC3Matrix4x3InvertAdjoint(&m4x3);
  396. [uniform setMatrix4x3: &m4x3];
  397. return YES;
  398. case kCC3SemanticModelMatrixInvTran:
  399. CC3Matrix3x3PopulateFrom4x3(&m3x3, visitor.modelMatrix);
  400. CC3Matrix3x3InvertAdjointTranspose(&m3x3);
  401. [uniform setMatrix3x3: &m3x3];
  402. return YES;
  403. case kCC3SemanticViewMatrix:
  404. [uniform setMatrix4x3: visitor.viewMatrix];
  405. return YES;
  406. case kCC3SemanticViewMatrixInv:
  407. CC3Matrix4x3PopulateFrom4x3(&m4x3, visitor.viewMatrix);
  408. CC3Matrix4x3InvertAdjoint(&m4x3);
  409. [uniform setMatrix4x3: &m4x3];
  410. return YES;
  411. case kCC3SemanticViewMatrixInvTran:
  412. CC3Matrix3x3PopulateFrom4x3(&m3x3, visitor.viewMatrix);
  413. CC3Matrix3x3InvertAdjointTranspose(&m3x3);
  414. [uniform setMatrix3x3: &m3x3];
  415. return YES;
  416. case kCC3SemanticModelViewMatrix:
  417. [uniform setMatrix4x3: visitor.modelViewMatrix];
  418. return YES;
  419. case kCC3SemanticModelViewMatrixInv:
  420. CC3Matrix4x3PopulateFrom4x3(&m4x3, visitor.modelViewMatrix);
  421. CC3Matrix4x3InvertAdjoint(&m4x3);
  422. [uniform setMatrix4x3: &m4x3];
  423. return YES;
  424. case kCC3SemanticModelViewMatrixInvTran:
  425. CC3Matrix3x3PopulateFrom4x3(&m3x3, visitor.modelViewMatrix);
  426. CC3Matrix3x3InvertAdjointTranspose(&m3x3);
  427. [uniform setMatrix3x3: &m3x3];
  428. return YES;
  429. case kCC3SemanticProjMatrix:
  430. [uniform setMatrix4x4: visitor.projMatrix];
  431. return YES;
  432. case kCC3SemanticProjMatrixInv:
  433. CC3Matrix4x4PopulateFrom4x4(&m4x4, visitor.projMatrix);
  434. CC3Matrix4x4InvertAdjoint(&m4x4);
  435. [uniform setMatrix4x4: &m4x4];
  436. return YES;
  437. case kCC3SemanticProjMatrixInvTran:
  438. CC3Matrix3x3PopulateFrom4x4(&m3x3, visitor.projMatrix);
  439. CC3Matrix3x3InvertAdjointTranspose(&m3x3);
  440. [uniform setMatrix3x3: &m3x3];
  441. return YES;
  442. case kCC3SemanticViewProjMatrix:
  443. [uniform setMatrix4x4: visitor.viewProjMatrix];
  444. return YES;
  445. case kCC3SemanticViewProjMatrixInv:
  446. CC3Matrix4x4PopulateFrom4x4(&m4x4, visitor.viewProjMatrix);
  447. CC3Matrix4x4InvertAdjoint(&m4x4);
  448. [uniform setMatrix4x4: &m4x4];
  449. return YES;
  450. case kCC3SemanticViewProjMatrixInvTran:
  451. CC3Matrix3x3PopulateFrom4x4(&m3x3, visitor.viewProjMatrix);
  452. CC3Matrix3x3InvertAdjointTranspose(&m3x3);
  453. [uniform setMatrix3x3: &m3x3];
  454. return YES;
  455. case kCC3SemanticModelViewProjMatrix:
  456. [uniform setMatrix4x4: visitor.modelViewProjMatrix];
  457. return YES;
  458. case kCC3SemanticModelViewProjMatrixInv:
  459. CC3Matrix4x4PopulateFrom4x4(&m4x4, visitor.modelViewProjMatrix);
  460. CC3Matrix4x4InvertAdjoint(&m4x4);
  461. [uniform setMatrix4x4: &m4x4];
  462. return YES;
  463. case kCC3SemanticModelViewProjMatrixInvTran:
  464. CC3Matrix3x3PopulateFrom4x4(&m3x3, visitor.modelViewProjMatrix);
  465. CC3Matrix3x3InvertAdjointTranspose(&m3x3);
  466. [uniform setMatrix3x3: &m3x3];
  467. return YES;
  468. #pragma mark Setting skinning semantics
  469. // BONE SKINNING ----------------
  470. case kCC3SemanticVertexBoneCount:
  471. [uniform setInteger: visitor.currentMeshNode.vertexBoneCount];
  472. return YES;
  473. case kCC3SemanticBatchBoneCount:
  474. [uniform setInteger: visitor.currentSkinSection.boneCount];
  475. return YES;
  476. // BONE SKINNING MATRICES ----------------
  477. case kCC3SemanticBoneMatricesGlobal:
  478. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  479. boneCnt = visitor.currentSkinSection.boneCount;
  480. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++)
  481. [uniform setMatrix4x3: [visitor globalBoneMatrixAt: boneIdx] at: boneIdx];
  482. return YES;
  483. case kCC3SemanticBoneMatricesInvTranGlobal:
  484. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  485. boneCnt = visitor.currentSkinSection.boneCount;
  486. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++) {
  487. CC3Matrix3x3PopulateFrom4x3(&m3x3, [visitor globalBoneMatrixAt: boneIdx]);
  488. CC3Matrix3x3InvertAdjointTranspose(&m3x3);
  489. [uniform setMatrix3x3: &m3x3 at: boneIdx];
  490. }
  491. return YES;
  492. case kCC3SemanticBoneMatricesEyeSpace:
  493. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  494. boneCnt = visitor.currentSkinSection.boneCount;
  495. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++)
  496. [uniform setMatrix4x3: [visitor eyeSpaceBoneMatrixAt: boneIdx] at: boneIdx];
  497. return YES;
  498. case kCC3SemanticBoneMatricesInvTranEyeSpace:
  499. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  500. boneCnt = visitor.currentSkinSection.boneCount;
  501. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++) {
  502. CC3Matrix3x3PopulateFrom4x3(&m3x3, [visitor eyeSpaceBoneMatrixAt: boneIdx]);
  503. CC3Matrix3x3InvertAdjointTranspose(&m3x3);
  504. [uniform setMatrix3x3: &m3x3 at: boneIdx];
  505. }
  506. return YES;
  507. case kCC3SemanticBoneMatricesModelSpace:
  508. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  509. boneCnt = visitor.currentSkinSection.boneCount;
  510. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++)
  511. [uniform setMatrix4x3: [visitor modelSpaceBoneMatrixAt: boneIdx] at: boneIdx];
  512. return YES;
  513. case kCC3SemanticBoneMatricesInvTranModelSpace:
  514. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  515. boneCnt = visitor.currentSkinSection.boneCount;
  516. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++) {
  517. CC3Matrix3x3PopulateFrom4x3(&m3x3, [visitor modelSpaceBoneMatrixAt: boneIdx]);
  518. CC3Matrix3x3InvertAdjointTranspose(&m3x3);
  519. [uniform setMatrix3x3: &m3x3 at: boneIdx];
  520. }
  521. return YES;
  522. // BONE SKINNING DISCRETE TRANSFORMS
  523. case kCC3SemanticBoneQuaternionsGlobal:
  524. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  525. boneCnt = visitor.currentSkinSection.boneCount;
  526. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++) {
  527. pm4x3 = [visitor globalBoneMatrixAt: boneIdx];
  528. [uniform setVector4: CC3Matrix4x3ExtractQuaternion(pm4x3) at: boneIdx];
  529. }
  530. return YES;
  531. case kCC3SemanticBoneTranslationsGlobal:
  532. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  533. boneCnt = visitor.currentSkinSection.boneCount;
  534. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++) {
  535. pm4x3 = [visitor globalBoneMatrixAt: boneIdx];
  536. [uniform setVector: CC3Matrix4x3ExtractTranslation(pm4x3) at: boneIdx];
  537. }
  538. return YES;
  539. case kCC3SemanticBoneScalesGlobal:
  540. case kCC3SemanticBoneScalesEyeSpace: // Same as global because view matrix is not scaled
  541. skin = visitor.currentSkinSection;
  542. boneCnt = skin.boneCount;
  543. CC3AssertBoneUniformForSkinSection(uniform, skin);
  544. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++)
  545. [uniform setVector: CC3VectorScale(visitor.currentMeshNode.globalScale,
  546. [skin boneAt: boneIdx].skeletalScale)];
  547. return YES;
  548. case kCC3SemanticBoneQuaternionsEyeSpace:
  549. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  550. boneCnt = visitor.currentSkinSection.boneCount;
  551. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++) {
  552. pm4x3 = [visitor eyeSpaceBoneMatrixAt: boneIdx];
  553. [uniform setVector4: CC3Matrix4x3ExtractQuaternion(pm4x3) at: boneIdx];
  554. }
  555. return YES;
  556. case kCC3SemanticBoneTranslationsEyeSpace:
  557. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  558. boneCnt = visitor.currentSkinSection.boneCount;
  559. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++) {
  560. pm4x3 = [visitor eyeSpaceBoneMatrixAt: boneIdx];
  561. [uniform setVector: CC3Matrix4x3ExtractTranslation(pm4x3) at: boneIdx];
  562. }
  563. return YES;
  564. case kCC3SemanticBoneQuaternionsModelSpace:
  565. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  566. boneCnt = visitor.currentSkinSection.boneCount;
  567. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++) {
  568. pm4x3 = [visitor modelSpaceBoneMatrixAt: boneIdx];
  569. [uniform setVector4: CC3Matrix4x3ExtractQuaternion(pm4x3) at: boneIdx];
  570. }
  571. return YES;
  572. case kCC3SemanticBoneTranslationsModelSpace:
  573. CC3AssertBoneUniformForSkinSection(uniform, visitor.currentSkinSection);
  574. boneCnt = visitor.currentSkinSection.boneCount;
  575. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++) {
  576. pm4x3 = [visitor modelSpaceBoneMatrixAt: boneIdx];
  577. [uniform setVector: CC3Matrix4x3ExtractTranslation(pm4x3) at: boneIdx];
  578. }
  579. return YES;
  580. case kCC3SemanticBoneScalesModelSpace:
  581. skin = visitor.currentSkinSection;
  582. boneCnt = skin.boneCount;
  583. CC3AssertBoneUniformForSkinSection(uniform, skin);
  584. for (GLuint boneIdx = 0; boneIdx < boneCnt; boneIdx++)
  585. [uniform setVector: [skin boneAt: boneIdx].skeletalScale];
  586. return YES;
  587. #pragma mark Setting camera semantics
  588. // CAMERA -----------------
  589. case kCC3SemanticCameraLocationGlobal:
  590. [uniform setVector: visitor.camera.globalLocation];
  591. return YES;
  592. case kCC3SemanticCameraLocationModelSpace:
  593. // Transform the global camera location to the local model space
  594. [uniform setVector: [visitor.currentMeshNode.globalTransformMatrixInverted
  595. transformLocation: visitor.camera.globalLocation]];
  596. return YES;
  597. case kCC3SemanticCameraFrustum: {
  598. // Applies the field of view angle to the narrower aspect.
  599. vp = visitor.camera.viewport;
  600. GLfloat aspect = (GLfloat) vp.w / (GLfloat) vp.h;
  601. CC3Camera* cam = visitor.camera;
  602. GLfloat fovWidth, fovHeight;
  603. if (aspect >= 1.0f) { // Landscape
  604. fovHeight = CC3DegToRad(cam.effectiveFieldOfView);
  605. fovWidth = fovHeight * aspect;
  606. } else { // Portrait
  607. fovWidth = CC3DegToRad(cam.effectiveFieldOfView);
  608. fovHeight = fovWidth / aspect;
  609. }
  610. [uniform setVector4: CC3Vector4Make(fovWidth, fovHeight,
  611. cam.nearClippingDistance,
  612. cam.farClippingDistance)];
  613. return YES;
  614. }
  615. case kCC3SemanticCameraFrustumDepth: {
  616. CC3Camera* cam = visitor.camera;
  617. [cam.projectionMatrix populateCC3Matrix4x4: &m4x4];
  618. [uniform setVector4: CC3Vector4Make(cam.farClippingDistance,
  619. cam.nearClippingDistance,
  620. m4x4.c3r3, m4x4.c4r3)];
  621. return YES;
  622. }
  623. case kCC3SemanticViewport:
  624. vp = visitor.camera.viewport;
  625. [uniform setIntVector4: CC3IntVector4Make(vp.x, vp.y, vp.w, vp.h)];
  626. return YES;
  627. #pragma mark Setting material semantics
  628. // MATERIALS --------------
  629. case kCC3SemanticColor:
  630. [uniform setColor4F: visitor.currentColor];
  631. return YES;
  632. case kCC3SemanticMaterialColorAmbient:
  633. [uniform setColor4F: visitor.currentMaterial.effectiveAmbientColor];
  634. return YES;
  635. case kCC3SemanticMaterialColorDiffuse:
  636. [uniform setColor4F: visitor.currentMaterial.effectiveDiffuseColor];
  637. return YES;
  638. case kCC3SemanticMaterialColorSpecular:
  639. [uniform setColor4F: visitor.currentMaterial.effectiveSpecularColor];
  640. return YES;
  641. case kCC3SemanticMaterialColorEmission:
  642. [uniform setColor4F: visitor.currentMaterial.effectiveEmissionColor];
  643. return YES;
  644. case kCC3SemanticMaterialOpacity:
  645. [uniform setFloat: visitor.currentMaterial.effectiveDiffuseColor.a];
  646. return YES;
  647. case kCC3SemanticMaterialShininess:
  648. [uniform setFloat: visitor.currentMaterial.shininess];
  649. return YES;
  650. case kCC3SemanticMaterialReflectivity:
  651. [uniform setFloat: visitor.currentMaterial.reflectivity];
  652. return YES;
  653. case kCC3SemanticMinimumDrawnAlpha:
  654. mat = visitor.currentMaterial;
  655. [uniform setFloat: (mat.shouldDrawLowAlpha ? 0.0f : mat.alphaTestReference)];
  656. return YES;
  657. #pragma mark Setting lighting semantics
  658. // LIGHTING --------------
  659. case kCC3SemanticIsUsingLighting:
  660. [uniform setBoolean: visitor.currentNode.shouldUseLighting];
  661. return YES;
  662. case kCC3SemanticSceneLightColorAmbient:
  663. [uniform setColor4F: visitor.scene.ambientLight];
  664. return YES;
  665. case kCC3SemanticLightIsEnabled:
  666. for (GLuint i = 0; i < uniformSize; i++) {
  667. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  668. [uniform setBoolean: light.visible at: i];
  669. }
  670. return YES;
  671. case kCC3SemanticLightInvertedPositionGlobal:
  672. isInverted = YES;
  673. case kCC3SemanticLightPositionGlobal:
  674. for (GLuint i = 0; i < uniformSize; i++) {
  675. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  676. CC3Vector4 ltPos = light.globalHomogeneousPosition;
  677. if (isInverted) ltPos = CC3Vector4HomogeneousNegate(ltPos);
  678. [uniform setVector4: ltPos at: i];
  679. }
  680. return YES;
  681. case kCC3SemanticLightInvertedPositionEyeSpace:
  682. isInverted = YES;
  683. case kCC3SemanticLightPositionEyeSpace:
  684. for (GLuint i = 0; i < uniformSize; i++) {
  685. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  686. CC3Vector4 ltPos = light.globalHomogeneousPosition;
  687. if (isInverted) ltPos = CC3Vector4HomogeneousNegate(ltPos);
  688. // Transform global position/direction to eye space and normalize if direction
  689. ltPos = CC3Matrix4x3TransformCC3Vector4(visitor.viewMatrix, ltPos);
  690. if (light.isDirectionalOnly) ltPos = CC3Vector4Normalize(ltPos);
  691. [uniform setVector4: ltPos at: i];
  692. }
  693. return YES;
  694. case kCC3SemanticLightInvertedPositionModelSpace:
  695. isInverted = YES;
  696. case kCC3SemanticLightPositionModelSpace:
  697. [visitor.currentMeshNode.globalTransformMatrixInverted populateCC3Matrix4x3: &m4x3];
  698. for (GLuint i = 0; i < uniformSize; i++) {
  699. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  700. CC3Vector4 ltPos = light.globalHomogeneousPosition;
  701. if (isInverted) ltPos = CC3Vector4HomogeneousNegate(ltPos);
  702. // Transform global position/direction to model space and normalize if direction
  703. ltPos = CC3Matrix4x3TransformCC3Vector4(&m4x3, ltPos);
  704. if (light.isDirectionalOnly) ltPos = CC3Vector4Normalize(ltPos);
  705. [uniform setVector4: ltPos at: i];
  706. }
  707. return YES;
  708. case kCC3SemanticLightColorAmbient:
  709. for (GLuint i = 0; i < uniformSize; i++) {
  710. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  711. ccColor4F ltColor = light.visible ? light.ambientColor : kCCC4FBlackTransparent;
  712. [uniform setColor4F: ltColor at: i];
  713. }
  714. return YES;
  715. case kCC3SemanticLightColorDiffuse:
  716. for (GLuint i = 0; i < uniformSize; i++) {
  717. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  718. ccColor4F ltColor = light.visible ? light.diffuseColor : kCCC4FBlackTransparent;
  719. [uniform setColor4F: ltColor at: i];
  720. }
  721. return YES;
  722. case kCC3SemanticLightColorSpecular:
  723. for (GLuint i = 0; i < uniformSize; i++) {
  724. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  725. ccColor4F ltColor = light.visible ? light.specularColor : kCCC4FBlackTransparent;
  726. [uniform setColor4F: ltColor at: i];
  727. }
  728. return YES;
  729. case kCC3SemanticLightAttenuation:
  730. for (GLuint i = 0; i < uniformSize; i++) {
  731. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  732. CC3AttenuationCoefficients ac = CC3AttenuationCoefficientsLegalize(light.attenuation);
  733. [uniform setVector: *(CC3Vector*)&ac at: i];
  734. }
  735. return YES;
  736. case kCC3SemanticLightSpotDirectionGlobal:
  737. for (GLuint i = 0; i < uniformSize; i++) {
  738. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  739. [uniform setVector: light.globalForwardDirection at: i];
  740. }
  741. return YES;
  742. case kCC3SemanticLightSpotDirectionEyeSpace:
  743. for (GLuint i = 0; i < uniformSize; i++) {
  744. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  745. CC3Vector spotDir = light.globalForwardDirection;
  746. // Transform global direction to eye space and normalize
  747. spotDir = CC3Matrix4x3TransformDirection(visitor.viewMatrix, spotDir);
  748. [uniform setVector: CC3VectorNormalize(spotDir) at: i];
  749. }
  750. return YES;
  751. case kCC3SemanticLightSpotDirectionModelSpace:
  752. [visitor.currentMeshNode.globalTransformMatrixInverted populateCC3Matrix4x3: &m4x3];
  753. for (GLuint i = 0; i < uniformSize; i++) {
  754. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  755. CC3Vector spotDir = light.globalForwardDirection;
  756. // Transform global direction to model space and normalize
  757. spotDir = CC3Matrix4x3TransformDirection(&m4x3, spotDir);
  758. [uniform setVector: CC3VectorNormalize(spotDir) at: i];
  759. }
  760. return YES;
  761. case kCC3SemanticLightSpotExponent:
  762. for (GLuint i = 0; i < uniformSize; i++) {
  763. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  764. [uniform setFloat: light.spotExponent at: i];
  765. }
  766. return YES;
  767. case kCC3SemanticLightSpotCutoffAngle:
  768. for (GLuint i = 0; i < uniformSize; i++) {
  769. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  770. [uniform setFloat: light.spotCutoffAngle at: i];
  771. }
  772. return YES;
  773. case kCC3SemanticLightSpotCutoffAngleCosine:
  774. for (GLuint i = 0; i < uniformSize; i++) {
  775. CC3Light* light = [visitor lightAt: (semanticIndex + i)];
  776. [uniform setFloat: cosf(CC3DegToRad(light.spotCutoffAngle)) at: i];
  777. }
  778. return YES;
  779. case kCC3SemanticFogIsEnabled:
  780. [uniform setBoolean: visitor.scene.fog.visible];
  781. return YES;
  782. case kCC3SemanticFogColor:
  783. [uniform setColor4F: visitor.scene.fog.diffuseColor];
  784. return YES;
  785. case kCC3SemanticFogAttenuationMode:
  786. [uniform setInteger: visitor.scene.fog.attenuationMode];
  787. return YES;
  788. case kCC3SemanticFogDensity:
  789. [uniform setFloat: visitor.scene.fog.density];
  790. return YES;
  791. case kCC3SemanticFogStartDistance:
  792. [uniform setFloat: visitor.scene.fog.startDistance];
  793. return YES;
  794. case kCC3SemanticFogEndDistance:
  795. [uniform setFloat: visitor.scene.fog.endDistance];
  796. return YES;
  797. #pragma mark Setting texture semantics
  798. // TEXTURES --------------
  799. case kCC3SemanticTextureCount:
  800. // Count all textures of any type
  801. [uniform setInteger: visitor.textureCount];
  802. return YES;
  803. case kCC3SemanticTextureSampler:
  804. // Samplers that can be any type are simply consecutive texture unit indices
  805. // starting at the semanticIndex of the uniform. Typically, semanticIndex > 0
  806. // and uniformSize > 1 are mutually exclusive.
  807. for (GLuint i = 0; i < uniformSize; i++) [uniform setInteger: (semanticIndex + i) at: i];
  808. return YES;
  809. case kCC3SemanticTexture2DCount:
  810. mat = visitor.currentMaterial;
  811. tuCnt = visitor.textureCount;
  812. // Count just the textures whose sampler semantic is of the correct type
  813. for (GLuint tuIdx = 0; tuIdx < tuCnt; tuIdx++)
  814. if ( [mat textureForTextureUnit: tuIdx].samplerSemantic == kCC3SemanticTexture2DSampler ) texCnt++;
  815. [uniform setInteger: texCnt];
  816. return YES;
  817. case kCC3SemanticTextureCubeCount:
  818. mat = visitor.currentMaterial;
  819. tuCnt = visitor.textureCount;
  820. // Count just the textures whose sampler semantic is of the correct type
  821. for (GLuint tuIdx = 0; tuIdx < tuCnt; tuIdx++)
  822. if ( [mat textureForTextureUnit: tuIdx].samplerSemantic == kCC3SemanticTextureCubeSampler ) texCnt++;
  823. [uniform setInteger: texCnt];
  824. return YES;
  825. case kCC3SemanticTexture2DSampler:
  826. // 2D samplers always come first and are consecutive, so we can simply use consecutive
  827. // texture unit indices starting at the semanticIndex of the uniform. Typically,
  828. // semanticIndex > 0 and uniformSize > 1 are mutually exclusive.
  829. for (GLuint i = 0; i < uniformSize; i++) [uniform setInteger: (semanticIndex + i) at: i];
  830. return YES;
  831. case kCC3SemanticTextureCubeSampler:
  832. // Cube samplers always come after 2D samplers, and are consecutive, so we can simply
  833. // use consecutive texture unit indices starting at the semanticIndex of the uniform,
  834. // plus an offset to skip any 2D textures. Typically, semanticIndex > 0 and
  835. // uniformSize > 1 are mutually exclusive.
  836. semanticIndex += visitor.currentShaderProgram.texture2DCount;
  837. for (GLuint i = 0; i < uniformSize; i++) [uniform setInteger: (semanticIndex + i) at: i];
  838. return YES;
  839. // The semantics below mimic OpenGL ES 1.1 configuration functionality for combining texture units.
  840. // In most shaders, these will be left unused in favor of customized the texture combining in code.
  841. case kCC3SemanticTexUnitMode:
  842. for (GLuint i = 0; i < uniformSize; i++) {
  843. CC3TextureUnit* tu = [visitor currentTextureUnitAt: (semanticIndex + i)];
  844. [uniform setInteger: (tu ? tu.textureEnvironmentMode : GL_MODULATE) at: i];
  845. }
  846. return YES;
  847. case kCC3SemanticTexUnitConstantColor:
  848. for (GLuint i = 0; i < uniformSize; i++) {
  849. CC3TextureUnit* tu = [visitor currentTextureUnitAt: (semanticIndex + i)];
  850. [uniform setColor4F: (tu ? tu.constantColor : kCCC4FBlackTransparent) at: i];
  851. }
  852. return YES;
  853. case kCC3SemanticTexUnitCombineRGBFunction:
  854. for (GLuint i = 0; i < uniformSize; i++) {
  855. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  856. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  857. [uniform setInteger: (isCTU ? ctu.combineRGBFunction : GL_MODULATE) at: i];
  858. }
  859. return YES;
  860. case kCC3SemanticTexUnitSource0RGB:
  861. for (GLuint i = 0; i < uniformSize; i++) {
  862. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  863. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  864. [uniform setInteger: (isCTU ? ctu.rgbSource0 : GL_TEXTURE) at: i];
  865. }
  866. return YES;
  867. case kCC3SemanticTexUnitSource1RGB:
  868. for (GLuint i = 0; i < uniformSize; i++) {
  869. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  870. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  871. [uniform setInteger: (isCTU ? ctu.rgbSource1 : GL_PREVIOUS) at: i];
  872. }
  873. return YES;
  874. case kCC3SemanticTexUnitSource2RGB:
  875. for (GLuint i = 0; i < uniformSize; i++) {
  876. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  877. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  878. [uniform setInteger: (isCTU ? ctu.rgbSource2 : GL_CONSTANT) at: i];
  879. }
  880. return YES;
  881. case kCC3SemanticTexUnitOperand0RGB:
  882. for (GLuint i = 0; i < uniformSize; i++) {
  883. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  884. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  885. [uniform setInteger: (isCTU ? ctu.rgbOperand0 : GL_SRC_COLOR) at: i];
  886. }
  887. return YES;
  888. case kCC3SemanticTexUnitOperand1RGB:
  889. for (GLuint i = 0; i < uniformSize; i++) {
  890. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  891. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  892. [uniform setInteger: (isCTU ? ctu.rgbOperand1 : GL_SRC_COLOR) at: i];
  893. }
  894. return YES;
  895. case kCC3SemanticTexUnitOperand2RGB:
  896. for (GLuint i = 0; i < uniformSize; i++) {
  897. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  898. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  899. [uniform setInteger: (isCTU ? ctu.rgbOperand2 : GL_SRC_ALPHA) at: i];
  900. }
  901. return YES;
  902. case kCC3SemanticTexUnitCombineAlphaFunction:
  903. for (GLuint i = 0; i < uniformSize; i++) {
  904. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  905. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  906. [uniform setInteger: (isCTU ? ctu.combineAlphaFunction : GL_MODULATE) at: i];
  907. }
  908. return YES;
  909. case kCC3SemanticTexUnitSource0Alpha:
  910. for (GLuint i = 0; i < uniformSize; i++) {
  911. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  912. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  913. [uniform setInteger: (isCTU ? ctu.alphaSource0 : GL_TEXTURE) at: i];
  914. }
  915. return YES;
  916. case kCC3SemanticTexUnitSource1Alpha:
  917. for (GLuint i = 0; i < uniformSize; i++) {
  918. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  919. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  920. [uniform setInteger: (isCTU ? ctu.alphaSource1 : GL_PREVIOUS) at: i];
  921. }
  922. return YES;
  923. case kCC3SemanticTexUnitSource2Alpha:
  924. for (GLuint i = 0; i < uniformSize; i++) {
  925. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  926. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  927. [uniform setInteger: (isCTU ? ctu.alphaSource2 : GL_CONSTANT) at: i];
  928. }
  929. return YES;
  930. case kCC3SemanticTexUnitOperand0Alpha:
  931. for (GLuint i = 0; i < uniformSize; i++) {
  932. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  933. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  934. [uniform setInteger: (isCTU ? ctu.alphaOperand0 : GL_SRC_ALPHA) at: i];
  935. }
  936. return YES;
  937. case kCC3SemanticTexUnitOperand1Alpha:
  938. for (GLuint i = 0; i < uniformSize; i++) {
  939. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  940. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  941. [uniform setInteger: (isCTU ? ctu.alphaOperand1 : GL_SRC_ALPHA) at: i];
  942. }
  943. return YES;
  944. case kCC3SemanticTexUnitOperand2Alpha:
  945. for (GLuint i = 0; i < uniformSize; i++) {
  946. CC3ConfigurableTextureUnit* ctu = (CC3ConfigurableTextureUnit*)[visitor currentTextureUnitAt: (semanticIndex + i)];
  947. BOOL isCTU = [ctu isKindOfClass: [CC3ConfigurableTextureUnit class]];
  948. [uniform setInteger: (isCTU ? ctu.alphaOperand2 : GL_SRC_ALPHA) at: i];
  949. }
  950. return YES;
  951. #pragma mark Setting model semantics
  952. // MODEL ----------------
  953. case kCC3SemanticCenterOfGeometry:
  954. [uniform setVector: visitor.currentMeshNode.mesh.centerOfGeometry];
  955. return YES;
  956. case kCC3SemanticBoundingRadius:
  957. [uniform setFloat: visitor.currentMeshNode.mesh.radius];
  958. return YES;
  959. case kCC3SemanticBoundingBoxMin:
  960. [uniform setVector: visitor.currentMeshNode.mesh.boundingBox.minimum];
  961. return YES;
  962. case kCC3SemanticBoundingBoxMax:
  963. [uniform setVector: visitor.currentMeshNode.mesh.boundingBox.maximum];
  964. return YES;
  965. case kCC3SemanticBoundingBoxSize:
  966. [uniform setVector: CC3BoxSize(visitor.currentMeshNode.mesh.boundingBox)];
  967. return YES;
  968. case kCC3SemanticAnimationFraction:
  969. [uniform setFloat: [visitor.currentMeshNode animationTimeOnTrack: 0]];
  970. return YES;
  971. #pragma mark Setting particle semantics
  972. // PARTICLES ------------
  973. case kCC3SemanticPointSize:
  974. emitter = (CC3PointParticleEmitter*)visitor.currentNode;
  975. isPtEmitter = [emitter isKindOfClass: [CC3PointParticleEmitter class]];
  976. [uniform setFloat: (isPtEmitter ? emitter.normalizedParticleSize : 0.0f)];
  977. return YES;
  978. case kCC3SemanticPointSizeAttenuation: {
  979. emitter = (CC3PointParticleEmitter*)visitor.currentNode;
  980. isPtEmitter = [emitter isKindOfClass: [CC3PointParticleEmitter class]];
  981. CC3AttenuationCoefficients ac = (isPtEmitter)
  982. ? CC3AttenuationCoefficientsLegalize(emitter.particleSizeAttenuation)
  983. : kCC3AttenuationNone;
  984. [uniform setVector: *(CC3Vector*)&ac];
  985. return YES;
  986. }
  987. case kCC3SemanticPointSizeMinimum:
  988. emitter = (CC3PointParticleEmitter*)visitor.currentNode;
  989. isPtEmitter = [emitter isKindOfClass: [CC3PointParticleEmitter class]];
  990. [uniform setFloat: (isPtEmitter ? emitter.normalizedParticleSizeMinimum : 0.0f)];
  991. return YES;
  992. case kCC3SemanticPointSizeMaximum:
  993. emitter = (CC3PointParticleEmitter*)visitor.currentNode;
  994. isPtEmitter = [emitter isKindOfClass: [CC3PointParticleEmitter class]];
  995. [uniform setFloat: (isPtEmitter ? emitter.normalizedParticleSizeMaximum : 0.0f)];
  996. return YES;
  997. case kCC3SemanticPointSpritesIsEnabled:
  998. [uniform setBoolean: visitor.currentMeshNode.isDrawingPointSprites];
  999. return YES;
  1000. #pragma mark Setting time semantics
  1001. // TIME ------------------
  1002. case kCC3SemanticFrameTime:
  1003. [uniform setFloat: visitor.deltaTime];
  1004. return YES;
  1005. case kCC3SemanticSceneTime:
  1006. sceneTime = visitor.scene.elapsedTimeSinceOpened;
  1007. [uniform setPoint: ccp(sceneTime, fmodf(sceneTime, 1.0))];
  1008. return YES;
  1009. case kCC3SemanticSceneTimeSine:
  1010. sceneTime = visitor.scene.elapsedTimeSinceOpened;
  1011. [uniform setVector4: CC3Vector4Make(sinf(sceneTime),
  1012. sinf(sceneTime / 2.0f),
  1013. sinf(sceneTime / 4.0f),
  1014. sinf(sceneTime / 8.0f))];
  1015. return YES;
  1016. case kCC3SemanticSceneTimeCosine:
  1017. sceneTime = visitor.scene.elapsedTimeSinceOpened;
  1018. [uniform setVector4: CC3Vector4Make(cosf(sceneTime),
  1019. co

Large files files are truncated, but you can click here to view the full file