Example #1
0
static void initialize()
{
	srand(time(nullptr));

	Audio::initialize();
	Audio::load("sounds/checkpoint.wav", "checkpoint");
	Audio::load("sounds/ship.wav", "ship");
	Audio::load("sounds/asteroid.wav", "asteroid");
	Audio::load("sounds/explosion.wav", "explosion");
	Audio::load("sounds/timeout.wav", "timeout");
	Audio::load("sounds/ding.wav", "ding");

	level.generate();

	ShaderProgram *asteroidShader = new ShaderProgram("shaders/asteroid.vert", "shaders/default.frag");
	asteroidShader->bindAttribLocation("in_Position", 0);
	asteroidShader->bindAttribLocation("in_Normal", 1);
	asteroidShader->bindAttribLocation("in_TextureCoords", 2);
	asteroidShader->bindAttribLocation("in_Tangent", 3);
	asteroidShader->bindAttribLocation("in_InstancePosition", 4);
	asteroidShader->bindAttribLocation("in_InstanceScale", 5);
	asteroidShader->bindAttribLocation("in_InstanceRotation", 6);
	asteroidShader->bindFragDataLocation("out_Color", 0);
	asteroidShader->link();
	Registry::shaders["asteroid"] = asteroidShader;

	asteroidShader->bind();
	(*asteroidShader)["u_DiffuseTexture"].set1i(1);
	(*asteroidShader)["u_NormalTexture"].set1i(2);
	(*asteroidShader)["u_EmitTexture"].set1i(3);
	(*asteroidShader)["u_SpecularTexture"].set1i(4);
	asteroidShader->unbind();

	Registry::textures["asteroid-diffuse"] = loadPngTexture("textures/asteroid-diffuse.png", true);
	Registry::textures["asteroid-emit"] = loadPngTexture("textures/asteroid-emit.png", true);
	Registry::textures["asteroid-normal"] = loadPngTexture("textures/asteroid-normal.png", true);
	Registry::textures["asteroid-specular"] = loadPngTexture("textures/asteroid-specular.png", true);
	Registry::models["asteroid"] = loadCobjModel("models/asteroid.cobj");

	// ----- Cubemap -----

	Cubemap *cubemap = loadPngCubemap({
		"textures/stars.png",
		"textures/stars.png",
		"textures/stars.png",
		"textures/stars.png",
		"textures/sun.png",
		"textures/stars.png"
	}, true);
	Registry::cubemap = cubemap;

	Registry::models["environment_cube"] = loadCobjModel("models/environment_cube.cobj");

	ShaderProgram *shaderCubemap = new ShaderProgram("shaders/cubemap.vert", "shaders/cubemap.frag");
	shaderCubemap->bindAttribLocation("in_Position", 0);
	shaderCubemap->bindFragDataLocation("out_Color", 0);
	shaderCubemap->link();

	shaderCubemap->bind();
	(*shaderCubemap)["u_Cubemap"].set1i(1);
	shaderCubemap->unbind();

	Registry::shaders["cubemap"] = shaderCubemap;

	// ----- Planet -----

	Registry::textures["planet"] = loadPngTexture("textures/planet.png", true);
	Registry::models["planet"] = loadCobjModel("models/planet.cobj");

	ShaderProgram *planetShader = new ShaderProgram("shaders/planet.vert", "shaders/planet.frag");
	planetShader->bindAttribLocation("in_Position", 0);
	planetShader->bindAttribLocation("in_Normal", 1);
	planetShader->bindAttribLocation("in_TextureCoords", 2);
	planetShader->bindFragDataLocation("out_Color", 0);
	planetShader->link();
	planetShader->bind();
	(*planetShader)["u_Texture"].set1i(1);
	planetShader->unbind();

	Registry::shaders["planet"] = planetShader;

	planetShader = new ShaderProgram("shaders/planet.vert", "shaders/atmosphere.frag");
	planetShader->bindAttribLocation("in_Position", 0);
	planetShader->bindAttribLocation("in_Normal", 1);
	planetShader->bindAttribLocation("in_TextureCoords", 2);
	planetShader->bindFragDataLocation("out_Color", 0);
	planetShader->link();
	planetShader->bind();
	(*planetShader)["u_Texture"].set1i(1);
	planetShader->unbind();

	Registry::shaders["planet-atmosphere"] = planetShader;

	// ----- Screen quad -----

	float quadCoords[] = {0, 0, 1, 0, 1, 1, 0, 1};
	unsigned int quadIndices[] = {0, 1, 3, 3, 1, 2};

	VertexArray *quadVao = new VertexArray(VertexArray::Triangles, 6);

	Buffer *quadCoordsBuffer = new Buffer(Buffer::Array, Buffer::StaticDraw);
	quadCoordsBuffer->data(8 * sizeof(float), reinterpret_cast<const void*>(quadCoords));

	Buffer *quadIndicesBuffer = new Buffer(Buffer::ElementArray, Buffer::StaticDraw);
	quadIndicesBuffer->data(6 * sizeof(unsigned int), reinterpret_cast<const void*>(quadIndices));

	quadVao->addAttrib(0, VertexAttrib(quadCoordsBuffer, 2, VertexAttrib::Float));
	quadVao->setElementIndexArray(ElementIndexArray(quadIndicesBuffer));

	Registry::screenQuad = quadVao;

	// ----- Spaceship -----

	Registry::textures["ship-diffuse"] = loadPngTexture("textures/ship-diffuse.png", true);
	Registry::textures["ship-emit"] = loadPngTexture("textures/ship-emit.png", true);
	Registry::textures["ship-normal"] = loadPngTexture("textures/ship-normal.png", true);
	Registry::textures["ship-specular"] = loadPngTexture("textures/ship-specular.png", true);
	Registry::models["ship"] = loadCobjModel("models/ship.cobj");

	ShaderProgram *shipShader = new ShaderProgram("shaders/ship.vert", "shaders/default.frag");
	shipShader->bindAttribLocation("in_Position", 0);
	shipShader->bindAttribLocation("in_Normal", 1);
	shipShader->bindAttribLocation("in_TextureCoords", 2);
	shipShader->bindAttribLocation("in_Tangent", 3);
	shipShader->bindFragDataLocation("out_Color", 0);
	shipShader->link();
	Registry::shaders["ship"] = shipShader;

	shipShader->bind();
	(*shipShader)["u_DiffuseTexture"].set1i(1);
	(*shipShader)["u_NormalTexture"].set1i(2);
	(*shipShader)["u_EmitTexture"].set1i(3);
	(*shipShader)["u_SpecularTexture"].set1i(4);
	shipShader->unbind();

	// ----- Overlay -----

	ShaderProgram *overlayShader = new ShaderProgram("shaders/overlay.vert", "shaders/overlay.frag");
	overlayShader->bindAttribLocation("in_Position", 0);
	overlayShader->bindFragDataLocation("out_Color", 0);
	overlayShader->link();
	Registry::shaders["overlay"] = overlayShader;

	// ----- Checkpoint -----

	float pointCoords[] = {0, 0, 0};
	VertexArray *pointVao = new VertexArray(VertexArray::Points, 1);
	Buffer *pointCoordsBuffer = new Buffer(Buffer::Array, Buffer::StaticDraw);
	pointCoordsBuffer->data(3 * sizeof(float), reinterpret_cast<const void*>(pointCoords));
	pointVao->addAttrib(0, VertexAttrib(pointCoordsBuffer, 3, VertexAttrib::Float));
	Registry::point = pointVao;

	ShaderProgram *checkpointShader = new ShaderProgram("shaders/checkpoint.vert", "shaders/checkpoint.frag");
	checkpointShader->bindAttribLocation("in_Position", 0);
	checkpointShader->bindFragDataLocation("out_Color", 0);
	checkpointShader->link();
	Registry::shaders["checkpoint"] = checkpointShader;

	Registry::models["checkpoint"] = loadCobjModel("models/checkpoint.cobj");

	// ----- Arrow -----

	Registry::models["arrow"] = loadCobjModel("models/arrow.cobj");

	ShaderProgram *arrowShader = new ShaderProgram("shaders/arrow.vert", "shaders/arrow.frag");
	arrowShader->bindAttribLocation("in_Position", 0);
	arrowShader->bindAttribLocation("in_TextureCoords", 2);
	arrowShader->bindFragDataLocation("out_Color", 0);
	arrowShader->link();

	arrowShader->bind();
	(*arrowShader)["u_DiffuseTexture"].set1i(1);
	arrowShader->unbind();

	Registry::shaders["arrow"] = arrowShader;
	Registry::textures["arrow-diffuse"] = loadPngTexture("textures/arrow-diffuse.png", true);

	// ----- Screens -----

	Registry::textures["screen-title"] = loadPngTexture("textures/screens/title.png", false);
	Registry::textures["screen-won"] = loadPngTexture("textures/screens/won.png", false);
	Registry::textures["screen-crashed"] = loadPngTexture("textures/screens/crashed.png", false);
	Registry::textures["screen-timeout"] = loadPngTexture("textures/screens/time-out.png", false);
	Registry::textures["screen-controls"] = loadPngTexture("textures/screens/controls.png", false);
	Registry::textures["screen-level"] = loadPngTexture("textures/screens/level.png", false);

	ShaderProgram *screenShader = new ShaderProgram("shaders/postprocess.vert", "shaders/screen.frag");
	screenShader->bindAttribLocation("in_Position", 0);
	screenShader->bindFragDataLocation("out_Color", 0);
	screenShader->link();
	screenShader->bind();
	(*screenShader)["u_Texture"].set1i(1);
	screenShader->unbind();

	Registry::shaders["screen"] = screenShader;
}