Skip to content

Instantly share code, notes, and snippets.

@notanumber
Created August 14, 2012 22:47
Show Gist options
  • Save notanumber/3353668 to your computer and use it in GitHub Desktop.
Save notanumber/3353668 to your computer and use it in GitHub Desktop.
Simple Triangle
#ifndef TEMPLATEGAME_H_
#define TEMPLATEGAME_H_
#include "gameplay.h"
using namespace gameplay;
class TestApp : public Game
{
public:
TestApp();
void keyEvent(Keyboard::KeyEvent evt, int key);
protected:
void initialize();
void finalize();
void update(float elapsedTime);
void render(float elapsedTime);
private:
Scene* _scene;
bool drawScene(Node* node);
};
#endif
/*========================================================================*/
#include "TriangleApp.h"
// Declare our game instance
TriangleApp game;
TriangleApp::TriangleApp()
: _scene(NULL)
{
}
void TriangleApp::initialize()
{
Camera* camera = Camera::createPerspective(60.0f, (float)getWidth() / (float)getHeight(), 1.0f, 100.0f);
VertexFormat::Element elements[] = { VertexFormat::Element(VertexFormat::POSITION, 3) };
std::vector<float> vertices;
vertices.resize(3 * 3); // 3 pts with 3 floats (x, y, z) each for one triangle
int i = 0;
vertices[i++] = 0.5f;
vertices[i++] = 1.0f;
vertices[i++] = 0.0f;
vertices[i++] = 1.5f;
vertices[i++] = -0.5f;
vertices[i++] = 0.0f;
vertices[i++] = -0.5f;
vertices[i++] = -0.5f;
vertices[i++] = 0.0f;
Mesh *mesh = Mesh::createMesh(VertexFormat(elements, 1), sizeof(vertices), false);
mesh->setPrimitiveType(Mesh::TRIANGLES);
mesh->setVertexData(&vertices[0], 0, sizeof(vertices));
Model* model = Model::create(mesh);
model->setMaterial("res/shaders/colored-unlit.vert", "res/shaders/colored-unlit.frag");
model->getMaterial()->getParameter("u_diffuseColor")->setValue(Vector4(1.0f, 0.0f, 0.0f, 1.0f));
model->getMaterial()->setParameterAutoBinding("u_worldViewProjectionMatrix", Material::WORLD_VIEW_PROJECTION_MATRIX);
_scene = Scene::createScene();
_scene->addNode("camera")->setCamera(camera);
_scene->addNode("model")->setModel(model);
_scene->setActiveCamera(camera);
_scene->findNode("camera")->translateZ(4.0f);
SAFE_RELEASE(mesh);
SAFE_RELEASE(model);
}
void TriangleApp::finalize()
{
SAFE_RELEASE(_scene);
}
void TriangleApp::update(float elapsedTime)
{
}
void TriangleApp::render(float elapsedTime)
{
clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
_scene->visit(this, &TriangleApp::drawScene);
}
bool TriangleApp::drawScene(Node* node)
{
Model* model = node->getModel();
if (model)
model->draw(false);
return true;
}
void TriangleApp::keyEvent(Keyboard::KeyEvent evt, int key)
{
if (evt == Keyboard::KEY_PRESS)
{
switch (key)
{
case Keyboard::KEY_ESCAPE:
exit();
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment