CreateSceneSample.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/arial18.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(1.0f, 1.0f, 1.0f);
  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", 0);
  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. // Bind the light's direction to the material.
  99. material->getParameter("u_lightDirection")->bindValue(lightNode, &Node::getForwardVectorView);
  100. // Bind the light's color to the material.
  101. material->getParameter("u_lightColor")->setValue(lightNode->getLight()->getColor());
  102. // Set the ambient color of the material.
  103. material->getParameter("u_ambientColor")->setValue(Vector3(1, 1, 1));
  104. // Load the texture from file.
  105. Texture::Sampler* sampler = material->getParameter("u_diffuseTexture")->setValue("res/png/box-diffuse.png", true);
  106. sampler->setFilterMode(Texture::LINEAR_MIPMAP_LINEAR, Texture::LINEAR);
  107. material->getStateBlock()->setCullFace(true);
  108. material->getStateBlock()->setDepthTest(true);
  109. material->getStateBlock()->setDepthWrite(true);
  110. _cubeNode = _scene->addNode("cube");
  111. _cubeNode->setModel(cubeModel);
  112. _cubeNode->rotateY(MATH_PIOVER4);
  113. SAFE_RELEASE(cubeModel);
  114. }
  115. void CreateSceneSample::finalize()
  116. {
  117. SAFE_RELEASE(_font);
  118. SAFE_RELEASE(_scene);
  119. }
  120. void CreateSceneSample::update(float elapsedTime)
  121. {
  122. // Rotate the directional light.
  123. _cubeNode->rotateY(elapsedTime * 0.002 * MATH_PI);
  124. }
  125. void CreateSceneSample::render(float elapsedTime)
  126. {
  127. // Clear the color and depth buffers
  128. clear(CLEAR_COLOR_DEPTH, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0);
  129. // Visit all the nodes in the scene, drawing the models.
  130. _scene->visit(this, &CreateSceneSample::drawScene);
  131. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  132. }
  133. void CreateSceneSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  134. {
  135. switch (evt)
  136. {
  137. case Touch::TOUCH_PRESS:
  138. if (x < 75 && y < 50)
  139. {
  140. // Toggle Vsync if the user touches the top left corner
  141. setVsync(!isVsync());
  142. }
  143. break;
  144. case Touch::TOUCH_RELEASE:
  145. break;
  146. case Touch::TOUCH_MOVE:
  147. break;
  148. };
  149. }
  150. bool CreateSceneSample::drawScene(Node* node)
  151. {
  152. Model* model = node->getModel();
  153. if (model)
  154. model->draw();
  155. return true;
  156. }