/ecere/src/gfx/3D/meshes/SkyBox.ec

https://github.com/redj/ecere-sdk · C · 215 lines · 175 code · 20 blank · 20 comment · 10 complexity · 8d48ad4165bd154c8c12a13d89ba43ca MD5 · raw file

  1. namespace gfx3D::meshes;
  2. /****************************************************************************
  3. ECERE Runtime Library
  4. Copyright (c) 2001 Jerome Jacovella-St-Louis
  5. All Rights Reserved.
  6. skybox.ec - SkyBox 3D Model
  7. ****************************************************************************/
  8. import "Display"
  9. import "Object" // TOFIX: If this is not here, Cube/Object gets registered as a 'class' rather than class 'struct'
  10. static const char * faceNames[] = { "up", "fr", "dn", "bk", "rt", "lf" };
  11. public class SkyBox : Object
  12. {
  13. public:
  14. bool Create(DisplaySystem displaySystem)
  15. {
  16. bool result = false;
  17. if(this)
  18. {
  19. InitializeMesh(displaySystem);
  20. if(mesh.Allocate({vertices = true, texCoords1 = !cubeMap }, 24, displaySystem))
  21. {
  22. PrimitiveGroup group;
  23. Vector3Df vertices[24] =
  24. {
  25. { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
  26. { (float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
  27. { (float)size.x/2, (float)size.y/2,-(float)size.z/2 },
  28. { -(float)size.x/2, (float)size.y/2,-(float)size.z/2 },
  29. { -(float)size.x/2,-(float)size.y/2, (float)size.z/2 },
  30. { (float)size.x/2,-(float)size.y/2, (float)size.z/2 },
  31. { (float)size.x/2, (float)size.y/2, (float)size.z/2 },
  32. { -(float)size.x/2, (float)size.y/2, (float)size.z/2 },
  33. { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
  34. { (float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
  35. { (float)size.x/2, (float)size.y/2,-(float)size.z/2 },
  36. { -(float)size.x/2, (float)size.y/2,-(float)size.z/2 },
  37. { -(float)size.x/2,-(float)size.y/2, (float)size.z/2 },
  38. { (float)size.x/2,-(float)size.y/2, (float)size.z/2 },
  39. { (float)size.x/2, (float)size.y/2, (float)size.z/2 },
  40. { -(float)size.x/2, (float)size.y/2, (float)size.z/2 },
  41. { -(float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
  42. { (float)size.x/2,-(float)size.y/2,-(float)size.z/2 },
  43. { (float)size.x/2, (float)size.y/2,-(float)size.z/2 },
  44. { -(float)size.x/2, (float)size.y/2,-(float)size.z/2 },
  45. { -(float)size.x/2,-(float)size.y/2, (float)size.z/2 },
  46. { (float)size.x/2,-(float)size.y/2, (float)size.z/2 },
  47. { (float)size.x/2, (float)size.y/2, (float)size.z/2 },
  48. { -(float)size.x/2, (float)size.y/2, (float)size.z/2 }
  49. };
  50. uint16 indices[6][4] =
  51. {
  52. // up, front, down, back, right, left
  53. /*
  54. { 20,21,16,17 },
  55. { 7,6,4,5 },
  56. { 19,18,23,22 },
  57. { 2,3,1,0 },
  58. { 14,10,13,9 },
  59. { 11,15,8,12 }
  60. */
  61. { 20,21,17,16 },
  62. { 7,6,5,4 },
  63. { 19,18,22,23 },
  64. { 2,3,0,1 },
  65. { 14,10,9,13 },
  66. { 11,15,12,8 }
  67. };
  68. int c;
  69. CopyBytes(mesh.vertices, vertices, sizeof(vertices));
  70. if(cubeMap)
  71. {
  72. Material material;
  73. char materialName[280];
  74. char name[256];
  75. sprintf(name, "%s/.%s", folder ? folder : ":skycube", extension ? extension : "pcx");
  76. sprintf(materialName, "SKYBOX %s", name);
  77. material = displaySystem.AddNamedMaterial(materialName);
  78. if(material)
  79. {
  80. material.flags = { noFog = true, cubeMap = true, noLighting = true, setupTextures = true, update = true };
  81. material.opacity = 1;
  82. material.emissive.r = material.emissive.g = material.emissive.b = 1;
  83. material.baseMap = cubeMap;
  84. }
  85. for(c = 0; c<6; c++)
  86. {
  87. group = mesh.AddPrimitiveGroup(triFan, 4);
  88. if(group)
  89. {
  90. group.material = material;
  91. CopyBytes(group.indices, indices[c], sizeof(indices[c]));
  92. mesh.UnlockPrimitiveGroup(group);
  93. }
  94. }
  95. }
  96. else
  97. {
  98. Pointf oldTexCoords[24] =
  99. {
  100. { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 },
  101. { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
  102. { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
  103. { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 },
  104. { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
  105. { 0, 1 }, { 1, 1 }, { 1, 0 }, { 0, 0 }
  106. };
  107. Pointf newTexCoords[24] =
  108. {
  109. { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
  110. { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 },
  111. { 1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 },
  112. { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
  113. { 0, 1 }, { 1, 1 }, { 1, 0 }, { 0, 0 },
  114. { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 }
  115. };
  116. Pointf * texCoords = newStyle ? newTexCoords : oldTexCoords;
  117. CopyBytes(mesh.texCoords, texCoords, sizeof(newTexCoords));
  118. for(c = 0; c<6; c++)
  119. {
  120. Material material;
  121. char materialName[280];
  122. char name[256];
  123. sprintf(name, "%s/%s.%s", folder ? folder : ":skycube", faceNames[c], extension ? extension : "pcx");
  124. sprintf(materialName, "SKYBOX %s - %s", faceNames[c], name);
  125. material = displaySystem.AddNamedMaterial(materialName);
  126. if(material)
  127. {
  128. material.flags = { noFog = true, noLighting = true, setupTextures = true, update = true };
  129. material.opacity = 1;
  130. material.emissive.r = material.emissive.g = material.emissive.b = 1;
  131. material.baseMap = Bitmap { };
  132. material.baseMap.LoadMipMaps(name, null, displaySystem);
  133. displaySystem.AddTexture(name, material.baseMap);
  134. }
  135. group = mesh.AddPrimitiveGroup(triFan, 4);
  136. //group = mesh.AddPrimitiveGroup(quads, sizeof(indices[c]) / sizeof(uint16));
  137. if(group)
  138. {
  139. group.material = material;
  140. CopyBytes(group.indices, indices[c], sizeof(indices[c]));
  141. mesh.UnlockPrimitiveGroup(group);
  142. }
  143. }
  144. }
  145. result = true;
  146. mesh.Unlock(0);
  147. }
  148. SetMinMaxRadius(true);
  149. }
  150. return result;
  151. }
  152. void Render(Camera camera, Display display)
  153. {
  154. if(this)
  155. {
  156. display.depthTest = false;
  157. display.depthWrite = false;
  158. flags.viewSpace = true;
  159. flags.transform = true;
  160. flags.root = true;
  161. // TODO: Not working...
  162. // transform.orientation.Inverse(camera.cOrientation);
  163. {
  164. Quaternion iquat;
  165. iquat.Inverse(camera.cOrientation);
  166. transform.orientation = iquat;
  167. }
  168. UpdateTransform();
  169. display.DrawObject(this);
  170. display.depthWrite = true;
  171. display.depthTest = true;
  172. }
  173. }
  174. property Vector3Df size { set { size = value; } };
  175. property const char * folder { set { folder = value; } };
  176. property const char * extension { set { extension = value; } };
  177. property bool newStyle { set { newStyle = value; } };
  178. property CubeMap cubeMap
  179. {
  180. set { cubeMap = value; }
  181. };
  182. private:
  183. SkyBox()
  184. {
  185. size = { 10000,10000,10000 };
  186. folder = ":skycube";
  187. extension = "pcx";
  188. }
  189. Vector3Df size;
  190. const char * folder, * extension;
  191. bool newStyle;
  192. CubeMap cubeMap;
  193. }