//
// Sphere.cpp
// Sphere
//
// Created by John Ryland on 2/10/17.
// Copyright © 2017 John Ryland. All rights reserved.
//
#include "../Framework/Framework.h"
DECLARE_PROGRAM_UNIFORMS(VertexColorProgram)
DECLARE_UNIFORM(mat4, modelViewProjectionMatrix)
DECLARE_PROGRAM_UNIFORMS_END
DECLARE_VERTEX(VertexColorVertex)
DECLARE_ATTRIB(vec3, pos, GL_FALSE)
DECLARE_ATTRIB(col4i, col, GL_TRUE)
DECLARE_VERTEX_END
#include "Sphere.h"
const char* s_vertexColorsVertexShader =
R"(
varying lowp vec4 colorVarying;
void main()
{
colorVarying = col;
gl_Position = modelViewProjectionMatrix * vec4(pos, 1.0);
}
)";
const char* s_vertexColorsFragmentShader =
R"(
varying lowp vec4 colorVarying;
void main()
{
gl_FragColor = colorVarying;
}
)";
Sphere::~Sphere()
{
m_demoModelContext.shutdown();
}
void Sphere::prepare()
{
OptionsType options;
m_demoModelContext.setup(s_vertexColorsVertexShader, s_vertexColorsFragmentShader, Medium, options);
m_demoModelContext.m_vertexArray.m_vertexData.clear();
std::vector<vec3f> sphereData;
CreateIcoSphere(5, sphereData);
for (int i = 0; i < sphereData.size(); i++)
{
m_demoModelContext.m_vertexArray.m_vertexData.emplace_back(VertexColorVertex{ sphereData[i].x, sphereData[i].y, sphereData[i].z, static_cast<uint8_t>(sphereData[i].x*255), static_cast<uint8_t>(sphereData[i].y*255), static_cast<uint8_t>(sphereData[i].z*255) } );
}
m_demoModelContext.update();
m_demoModelContext.setFlag(clearColor | clearDepth | enableCullFace | enableDepthTest);
m_demoModelContext.setBackgroundColor(0.0f, 0.0f, 0.0f);
}
void Sphere::onResize(const vec2f& a_shape)
{
Math::makePerspectiveMatrix4x4(m_projectionMatrix, Math::degreesToRadians(45.0f), a_shape.x / a_shape.y, 0.1f, 100.0f);
}
void Sphere::update(float a_seconds)
{
float trans[3] = { 0.0f, 0.0f, -20.0f };
float rotate[3] = { fmod(a_seconds*23.f, 360.0f), fmod(a_seconds*37.f, 360.0f), fmod(a_seconds*46.0f, 360.0f)};
float baseModelViewMatrix[16];
Math::translationRotationScaleToMatrix4x4(baseModelViewMatrix, trans, rotate, 5.0f);
//Math::translateMatrix4x4(baseModelViewMatrix, -0.5f, -0.5f, -0.5f);
Math::multiplyMatrix4x4(m_demoModelContext.m_uniforms.m_modelViewProjectionMatrix.m[0], m_projectionMatrix, baseModelViewMatrix);
}
void Sphere::draw()
{
m_demoModelContext.draw();
}
REGISTER_DEMO_CONTEXT("Sphere", Sphere)