#include "MeshGame.h" // Declare our game instance MeshGame game; MeshGame::MeshGame() : _font(NULL), _scene(NULL), _modelNode(NULL), _touched(false), _touchX(0) { } MeshGame::~MeshGame() { } void MeshGame::initialize() { // Display the gameplay splash screen for at least 1 second. displayScreen(this, &MeshGame::drawSplash, NULL, 1000L); // Load the font _font = Font::create("res/arial.gpb"); // Load the scene from file _scene = Scene::load("res/mesh.scene"); // Get the duck node _modelNode = _scene->findNode("duck"); // Find the light node Node* lightNode = _scene->findNode("directionalLight1"); // Bind the light node's direction into duck's material. _modelNode->getModel()->getMaterial()->getParameter("u_directionalLightColor[0]")->setValue(lightNode->getLight()->getColor()); _modelNode->getModel()->getMaterial()->getParameter("u_directionalLightDirection[0]")->bindValue(lightNode, &Node::getForwardVectorView); // Update the aspect ratio for our scene's camera to match the current device resolution _scene->getActiveCamera()->setAspectRatio(getAspectRatio()); // Create the grid and add it to the scene. Model* model = createGridModel(); _scene->addNode("grid")->setModel(model); model->release(); } void MeshGame::finalize() { SAFE_RELEASE(_font); SAFE_RELEASE(_scene); } void MeshGame::update(float elapsedTime) { // Rotate model if (!_touched) _modelNode->rotateY(elapsedTime * MATH_DEG_TO_RAD(0.05f)); } void MeshGame::render(float elapsedTime) { // Clear the color and depth buffers. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0); // Visit all the nodes in the scene, drawing the models/mesh. _scene->visit(this, &MeshGame::drawScene); // Draw the fps drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate()); } void MeshGame::keyEvent(Keyboard::KeyEvent evt, int key) { if (evt == Keyboard::KEY_PRESS) { switch (key) { case Keyboard::KEY_ESCAPE: exit(); break; } } } void MeshGame::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex) { switch (evt) { case Touch::TOUCH_PRESS: { _touched = true; _touchX = x; } break; case Touch::TOUCH_RELEASE: { _touched = false; _touchX = 0; } break; case Touch::TOUCH_MOVE: { int deltaX = x - _touchX; _touchX = x; _modelNode->rotateY(MATH_DEG_TO_RAD(deltaX * 0.5f)); } break; default: break; }; } bool MeshGame::drawScene(Node* node) { Model* model = node->getModel(); if (model) model->draw(); return true; } void MeshGame::drawFrameRate(Font* font, const Vector4& color, unsigned int x, unsigned int y, unsigned int fps) { char buffer[10]; sprintf(buffer, "%u", fps); font->start(); font->drawText(buffer, x, y, color, font->getSize()); font->finish(); } void MeshGame::drawSplash(void* param) { clear(CLEAR_COLOR_DEPTH, Vector4(0, 0, 0, 1), 1.0f, 0); SpriteBatch* batch = SpriteBatch::create("res/logo_powered_white.png"); batch->start(); batch->draw(this->getWidth() * 0.5f, this->getHeight() * 0.5f, 0.0f, 512.0f, 512.0f, 0.0f, 1.0f, 1.0f, 0.0f, Vector4::one(), true); batch->finish(); SAFE_DELETE(batch); } Model* MeshGame::createGridModel(unsigned int lineCount) { // There needs to be an odd number of lines lineCount |= 1; const unsigned int pointCount = lineCount * 4; const unsigned int verticesSize = pointCount * (3 + 3); // (3 (position(xyz) + 3 color(rgb)) std::vector vertices; vertices.resize(verticesSize); const float gridLength = (float)(lineCount / 2); float value = -gridLength; for (unsigned int i = 0; i < verticesSize; ++i) { // Default line color is dark grey Vector4 color(0.3f, 0.3f, 0.3f, 1.0f); // Very 10th line is brighter grey if (((int)value) % 10 == 0) { color.set(0.45f, 0.45f, 0.45f, 1.0f); } // The Z axis is blue if (value == 0.0f) { color.set(0.15f, 0.15f, 0.7f, 1.0f); } // Build the lines vertices[i] = value; vertices[++i] = 0.0f; vertices[++i] = -gridLength; vertices[++i] = color.x; vertices[++i] = color.y; vertices[++i] = color.z; vertices[++i] = value; vertices[++i] = 0.0f; vertices[++i] = gridLength; vertices[++i] = color.x; vertices[++i] = color.y; vertices[++i] = color.z; // The X axis is red if (value == 0.0f) { color.set(0.7f, 0.15f, 0.15f, 1.0f); } vertices[++i] = -gridLength; vertices[++i] = 0.0f; vertices[++i] = value; vertices[++i] = color.x; vertices[++i] = color.y; vertices[++i] = color.z; vertices[++i] = gridLength; vertices[++i] = 0.0f; vertices[++i] = value; vertices[++i] = color.x; vertices[++i] = color.y; vertices[++i] = color.z; value += 1.0f; } VertexFormat::Element elements[] = { VertexFormat::Element(VertexFormat::POSITION, 3), VertexFormat::Element(VertexFormat::COLOR, 3) }; Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 2), pointCount, false); if (mesh == NULL) { return NULL; } mesh->setPrimitiveType(Mesh::LINES); mesh->setVertexData(&vertices[0], 0, pointCount); Model* model = Model::create(mesh); model->setMaterial("res/mesh.material#grid"); SAFE_RELEASE(mesh); return model; }