TextureSample.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "TextureSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Graphics", "Textures", TextureSample, 6);
  5. #endif
  6. Node* addQuadModelAndNode(Scene* scene, Mesh* mesh)
  7. {
  8. Model* model = Model::create(mesh);
  9. Node* node = scene->addNode();
  10. node->setModel(model);
  11. SAFE_RELEASE(model);
  12. return node;
  13. }
  14. Node* addQuadModelAndNode(Scene* scene, float x, float y, float width, float height,
  15. float s1 = 0.0f, float t1 = 0.0f, float s2 = 1.0f, float t2 = 1.0f)
  16. {
  17. Mesh* mesh = Mesh::createQuad(x, y, width, height, s1, t1, s2, t2);
  18. Node* node = addQuadModelAndNode(scene, mesh);
  19. SAFE_RELEASE(mesh);
  20. return node;
  21. }
  22. Material* setTextureUnlitMaterial(Model* model, const char* texturePath, bool mipmap = true)
  23. {
  24. Material* material = model->setMaterial("res/shaders/textured.vert", "res/shaders/textured.frag");
  25. material->setParameterAutoBinding("u_worldViewProjectionMatrix", "WORLD_VIEW_PROJECTION_MATRIX");
  26. // Load the texture from file.
  27. Texture::Sampler* sampler = material->getParameter("u_diffuseTexture")->setValue(texturePath, mipmap);
  28. if (mipmap)
  29. sampler->setFilterMode(Texture::LINEAR_MIPMAP_LINEAR, Texture::LINEAR);
  30. else
  31. sampler->setFilterMode(Texture::LINEAR, Texture::LINEAR);
  32. sampler->setWrapMode(Texture::CLAMP, Texture::CLAMP);
  33. material->getStateBlock()->setCullFace(true);
  34. material->getStateBlock()->setDepthTest(true);
  35. material->getStateBlock()->setDepthWrite(true);
  36. return material;
  37. }
  38. TextureSample::TextureSample()
  39. : _font(NULL), _scene(NULL), _zOffset(0.0f)
  40. {
  41. }
  42. void TextureSample::initialize()
  43. {
  44. // Create the font for drawing the framerate.
  45. _font = Font::create("res/ui/arial.gpb");
  46. // Create an empty scene.
  47. _scene = Scene::create();
  48. // Create a camera and add it to the scene as the active camera.
  49. Camera* camera = Camera::createPerspective(45.0f, getAspectRatio(), 1, 1000);
  50. Node* cameraNode = _scene->addNode("camera");
  51. cameraNode->setCamera(camera);
  52. _scene->setActiveCamera(camera);
  53. cameraNode->translate(0, 0, 50);
  54. SAFE_RELEASE(camera);
  55. const float fontSize = 18;
  56. const float cubeSize = 10.0f;
  57. float x, y, textWidth;
  58. // Find the width of the cube in screen space
  59. _scene->getActiveCamera()->project(getViewport(), Vector3(cubeSize, 0, 0), &x, &y);
  60. textWidth = x - (getWidth() >> 1);
  61. // Textured quad mesh
  62. {
  63. Node* node = addQuadModelAndNode(_scene, 0, 0, cubeSize, cubeSize);
  64. setTextureUnlitMaterial(node->getModel(), "res/png/color-wheel.png");
  65. node->setTranslation(-25, cubeSize, 0);
  66. // Find the position of the node in screen space
  67. _scene->getActiveCamera()->project(getViewport(), node->getTranslationWorld(), &x, &y);
  68. _text.push_back(_font->createText("Quad: Textured", Rectangle(x, y, textWidth, fontSize), Vector4::one(), fontSize, Font::ALIGN_TOP_HCENTER, false));
  69. }
  70. // Textured quad points
  71. {
  72. Mesh* mesh = Mesh::createQuad(Vector3(0, cubeSize, 0), Vector3(0, 0, 0), Vector3(cubeSize, cubeSize, 0), Vector3(cubeSize, 0, 0));
  73. Node* node = addQuadModelAndNode(_scene, mesh);
  74. SAFE_RELEASE(mesh);
  75. setTextureUnlitMaterial(node->getModel(), "res/png/color-wheel.png");
  76. node->setTranslation(-14, cubeSize, 0);
  77. _scene->getActiveCamera()->project(getViewport(), node->getTranslationWorld(), &x, &y);
  78. _text.push_back(_font->createText("Quad: Points", Rectangle(x, y, textWidth, fontSize), Vector4::one(), fontSize, Font::ALIGN_TOP_HCENTER, false));
  79. }
  80. // Texture clamp
  81. {
  82. Node* node = addQuadModelAndNode(_scene, 0, 0, cubeSize, cubeSize, -1, -1, 2, 2);
  83. setTextureUnlitMaterial(node->getModel(), "res/png/color-wheel.png");
  84. node->setId("clamp");
  85. node->setTranslation(-3, cubeSize, 0);
  86. _scene->getActiveCamera()->project(getViewport(), node->getTranslationWorld(), &x, &y);
  87. _text.push_back(_font->createText("Wrap: Clamp", Rectangle(x, y, textWidth, fontSize), Vector4::one(), fontSize, Font::ALIGN_TOP_HCENTER, false));
  88. }
  89. // Texture wrapped+repeat
  90. {
  91. Node* node = addQuadModelAndNode(_scene, 0, 0, cubeSize, cubeSize, -1, -1, 2, 2);
  92. setTextureUnlitMaterial(node->getModel(), "res/png/color-wheel.png");
  93. node->setId("repeat");
  94. Texture::Sampler* sampler = node->getModel()->getMaterial()->getParameter("u_diffuseTexture")->getSampler();
  95. if (sampler)
  96. {
  97. sampler->setWrapMode(Texture::REPEAT, Texture::REPEAT);
  98. }
  99. node->setTranslation(8, cubeSize, 0);
  100. _scene->getActiveCamera()->project(getViewport(), node->getTranslationWorld(), &x, &y);
  101. _text.push_back(_font->createText("Wrap: Repeat", Rectangle(x, y, textWidth, fontSize), Vector4::one(), fontSize, Font::ALIGN_HCENTER, false));
  102. }
  103. // Mipmapping Off
  104. {
  105. Node* node = addQuadModelAndNode(_scene, 0, 0, cubeSize, cubeSize);
  106. setTextureUnlitMaterial(node->getModel(), "res/png/logo.png", false);
  107. node->setId("mipmap off");
  108. node->setTranslation(-25.5f, -2.5f, 0);
  109. _scene->getActiveCamera()->project(getViewport(), node->getTranslationWorld(), &x, &y);
  110. _text.push_back(_font->createText("MipMap: Off", Rectangle(x, y, textWidth, fontSize), Vector4::one(), fontSize, Font::ALIGN_HCENTER, false));
  111. }
  112. // Mipmapping On
  113. {
  114. Node* node = addQuadModelAndNode(_scene, 0, 0, cubeSize, cubeSize);
  115. setTextureUnlitMaterial(node->getModel(), "res/png/logo.png");
  116. node->setId("mipmap on");
  117. node->setTranslation(-5.5f, -2.5f, 0);
  118. _scene->getActiveCamera()->project(getViewport(), node->getTranslationWorld(), &x, &y);
  119. _text.push_back(_font->createText("MipMap: On", Rectangle(x, y, textWidth, fontSize), Vector4::one(), fontSize, Font::ALIGN_HCENTER, false));
  120. }
  121. }
  122. void TextureSample::finalize()
  123. {
  124. // Model and font are reference counted and should be released before closing this sample.
  125. SAFE_RELEASE(_scene);
  126. SAFE_RELEASE(_font);
  127. for (std::list<Font::Text*>::iterator it = _text.begin(); it != _text.end(); ++it)
  128. {
  129. SAFE_DELETE(*it);
  130. }
  131. _text.clear();
  132. }
  133. void TextureSample::update(float elapsedTime)
  134. {
  135. Node* n1 = _scene->findNode("mipmap on");
  136. Node* n2 = _scene->findNode("mipmap off");
  137. // move these nodes between 0 and -900 using a sine wave
  138. float z = -(sin((getAbsoluteTime() / 1500.0L) * MATH_PI) + 1) * 900.0f / 2.0f;
  139. n1->setTranslationZ(z);
  140. n2->setTranslationZ(z);
  141. }
  142. void TextureSample::render(float elapsedTime)
  143. {
  144. // Clear the color and depth buffers
  145. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  146. // Visit all the nodes in the scene, drawing the models/mesh.
  147. _scene->visit(this, &TextureSample::drawScene);
  148. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  149. _font->start();
  150. for (std::list<Font::Text*>::const_iterator it = _text.begin(); it != _text.end(); ++it)
  151. {
  152. _font->drawText(*it);
  153. }
  154. _font->finish();
  155. }
  156. void TextureSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  157. {
  158. switch (evt)
  159. {
  160. case Touch::TOUCH_PRESS:
  161. if (x < 75 && y < 50)
  162. {
  163. // Toggle Vsync if the user touches the top left corner
  164. setVsync(!isVsync());
  165. }
  166. break;
  167. case Touch::TOUCH_RELEASE:
  168. break;
  169. case Touch::TOUCH_MOVE:
  170. break;
  171. };
  172. }
  173. bool TextureSample::drawScene(Node* node)
  174. {
  175. Model* model = node->getModel();
  176. if (model)
  177. model->draw();
  178. return true;
  179. }