CreateSceneSample.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "CreateSceneSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Scene", "Create Scene", CreateSceneSample, 1);
  5. #endif
  6. static Mesh* createCubeMesh(float size = 1.0f)
  7. {
  8. float a = size * 0.5f;
  9. float vertices[] =
  10. {
  11. -a, -a, a, 0.0, 0.0, 1.0, 0.0, 0.0,
  12. a, -a, a, 0.0, 0.0, 1.0, 1.0, 0.0,
  13. -a, a, a, 0.0, 0.0, 1.0, 0.0, 1.0,
  14. a, a, a, 0.0, 0.0, 1.0, 1.0, 1.0,
  15. -a, a, a, 0.0, 1.0, 0.0, 0.0, 0.0,
  16. a, a, a, 0.0, 1.0, 0.0, 1.0, 0.0,
  17. -a, a, -a, 0.0, 1.0, 0.0, 0.0, 1.0,
  18. a, a, -a, 0.0, 1.0, 0.0, 1.0, 1.0,
  19. -a, a, -a, 0.0, 0.0, -1.0, 0.0, 0.0,
  20. a, a, -a, 0.0, 0.0, -1.0, 1.0, 0.0,
  21. -a, -a, -a, 0.0, 0.0, -1.0, 0.0, 1.0,
  22. a, -a, -a, 0.0, 0.0, -1.0, 1.0, 1.0,
  23. -a, -a, -a, 0.0, -1.0, 0.0, 0.0, 0.0,
  24. a, -a, -a, 0.0, -1.0, 0.0, 1.0, 0.0,
  25. -a, -a, a, 0.0, -1.0, 0.0, 0.0, 1.0,
  26. a, -a, a, 0.0, -1.0, 0.0, 1.0, 1.0,
  27. a, -a, a, 1.0, 0.0, 0.0, 0.0, 0.0,
  28. a, -a, -a, 1.0, 0.0, 0.0, 1.0, 0.0,
  29. a, a, a, 1.0, 0.0, 0.0, 0.0, 1.0,
  30. a, a, -a, 1.0, 0.0, 0.0, 1.0, 1.0,
  31. -a, -a, -a, -1.0, 0.0, 0.0, 0.0, 0.0,
  32. -a, -a, a, -1.0, 0.0, 0.0, 1.0, 0.0,
  33. -a, a, -a, -1.0, 0.0, 0.0, 0.0, 1.0,
  34. -a, a, a, -1.0, 0.0, 0.0, 1.0, 1.0
  35. };
  36. short indices[] =
  37. {
  38. 0, 1, 2, 2, 1, 3, 4, 5, 6, 6, 5, 7, 8, 9, 10, 10, 9, 11, 12, 13, 14, 14, 13, 15, 16, 17, 18, 18, 17, 19, 20, 21, 22, 22, 21, 23
  39. };
  40. unsigned int vertexCount = 24;
  41. unsigned int indexCount = 36;
  42. VertexFormat::Element elements[] =
  43. {
  44. VertexFormat::Element(VertexFormat::POSITION, 3),
  45. VertexFormat::Element(VertexFormat::NORMAL, 3),
  46. VertexFormat::Element(VertexFormat::TEXCOORD0, 2)
  47. };
  48. Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 3), vertexCount, false);
  49. if (mesh == NULL)
  50. {
  51. GP_ERROR("Failed to create mesh.");
  52. return NULL;
  53. }
  54. mesh->setVertexData(vertices, 0, vertexCount);
  55. MeshPart* meshPart = mesh->addPart(Mesh::TRIANGLES, Mesh::INDEX16, indexCount, false);
  56. meshPart->setIndexData(indices, 0, indexCount);
  57. return mesh;
  58. }
  59. CreateSceneSample::CreateSceneSample()
  60. : _font(NULL), _scene(NULL), _cubeNode(NULL)
  61. {
  62. }
  63. void CreateSceneSample::initialize()
  64. {
  65. // Create the font for drawing the framerate.
  66. _font = Font::create("res/common/arial.gpb");
  67. // Create a new empty scene.
  68. _scene = Scene::create();
  69. // Create the camera.
  70. Camera* camera = Camera::createPerspective(45.0f, getAspectRatio(), 1.0f, 10.0f);
  71. Node* cameraNode = _scene->addNode("camera");
  72. // Attach the camera to a node. This determines the position of the camera.
  73. cameraNode->setCamera(camera);
  74. // Make this the active camera of the scene.
  75. _scene->setActiveCamera(camera);
  76. SAFE_RELEASE(camera);
  77. // Move the camera to look at the origin.
  78. cameraNode->translate(0, 1, 5);
  79. cameraNode->rotateX(MATH_DEG_TO_RAD(-11.25f));
  80. // Create a white light.
  81. Light* light = Light::createDirectional(0.75f, 0.75f, 0.75f);
  82. Node* lightNode = _scene->addNode("light");
  83. lightNode->setLight(light);
  84. // Release the light because the node now holds a reference to it.
  85. SAFE_RELEASE(light);
  86. lightNode->rotateX(MATH_DEG_TO_RAD(-45.0f));
  87. // Create the cube mesh and model.
  88. Mesh* cubeMesh = createCubeMesh();
  89. Model* cubeModel = Model::create(cubeMesh);
  90. // Release the mesh because the model now holds a reference to it.
  91. SAFE_RELEASE(cubeMesh);
  92. // Create the material for the cube model and assign it to the first mesh part.
  93. Material* material = cubeModel->setMaterial("res/shaders/textured.vert", "res/shaders/textured.frag", "DIRECTIONAL_LIGHT_COUNT 1");
  94. // These parameters are normally set in a .material file but this example sets them programmatically.
  95. // Bind the uniform "u_worldViewProjectionMatrix" to use the WORLD_VIEW_PROJECTION_MATRIX from the scene's active camera and the node that the model belongs to.
  96. material->setParameterAutoBinding("u_worldViewProjectionMatrix", "WORLD_VIEW_PROJECTION_MATRIX");
  97. material->setParameterAutoBinding("u_inverseTransposeWorldViewMatrix", "INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX");
  98. // Set the ambient color of the material.
  99. material->getParameter("u_ambientColor")->setValue(Vector3(0.2f, 0.2f, 0.2f));
  100. // Bind the light's color and direction to the material.
  101. material->getParameter("u_directionalLightColor[0]")->setValue(lightNode->getLight()->getColor());
  102. material->getParameter("u_directionalLightDirection[0]")->bindValue(lightNode, &Node::getForwardVectorWorld);
  103. // Load the texture from file.
  104. Texture::Sampler* sampler = material->getParameter("u_diffuseTexture")->setValue("res/png/crate.png", true);
  105. sampler->setFilterMode(Texture::LINEAR_MIPMAP_LINEAR, Texture::LINEAR);
  106. material->getStateBlock()->setCullFace(true);
  107. material->getStateBlock()->setDepthTest(true);
  108. material->getStateBlock()->setDepthWrite(true);
  109. _cubeNode = _scene->addNode("cube");
  110. _cubeNode->setModel(cubeModel);
  111. _cubeNode->rotateY(MATH_PIOVER4);
  112. SAFE_RELEASE(cubeModel);
  113. }
  114. void CreateSceneSample::finalize()
  115. {
  116. SAFE_RELEASE(_font);
  117. SAFE_RELEASE(_scene);
  118. }
  119. void CreateSceneSample::update(float elapsedTime)
  120. {
  121. // Rotate the directional light.
  122. _cubeNode->rotateY(elapsedTime * 0.001 * MATH_PI);
  123. }
  124. void CreateSceneSample::render(float elapsedTime)
  125. {
  126. // Clear the color and depth buffers
  127. clear(CLEAR_COLOR_DEPTH, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0);
  128. // Visit all the nodes in the scene, drawing the models.
  129. _scene->visit(this, &CreateSceneSample::drawScene);
  130. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  131. }
  132. void CreateSceneSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  133. {
  134. switch (evt)
  135. {
  136. case Touch::TOUCH_PRESS:
  137. if (x < 75 && y < 50)
  138. {
  139. // Toggle Vsync if the user touches the top left corner
  140. setVsync(!isVsync());
  141. }
  142. break;
  143. case Touch::TOUCH_RELEASE:
  144. break;
  145. case Touch::TOUCH_MOVE:
  146. break;
  147. };
  148. }
  149. bool CreateSceneSample::drawScene(Node* node)
  150. {
  151. Model* model = node->getModel();
  152. if (model)
  153. model->draw();
  154. return true;
  155. }