| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #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 font
- _font = Font::create("res/arial.gpb");
- // Load mesh/scene from file
- _scene = Scene::load("res/duck.gpb");
- // Get the duck node
- _modelNode = _scene->findNode("duck");
- // Bind the material to the model
- _modelNode->getModel()->setMaterial("res/duck.material");
- // Find the light node
- Node* lightNode = _scene->findNode("directionalLight1");
- // Bind the light node's direction into duck's material.
- _modelNode->getModel()->getMaterial()->getParameter("u_lightDirection[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<float> 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/grid.material");
- SAFE_RELEASE(mesh);
- return model;
- }
|