one planet is always the sun and rendering the sun dynamically now.

This commit is contained in:
Andreas Ortmann 2016-09-30 22:47:28 +02:00
parent e1102e3231
commit f2bdadd49f
6 changed files with 75 additions and 42 deletions

View file

@ -1,20 +1,26 @@
#version 120
varying vec3 vertex;
varying vec3 normal;
varying vec3 vertex;
varying vec3 normal;
varying vec3 lightDirection;
uniform vec3 materialColor;
uniform vec3 lightColor;
uniform vec3 lightPosition;
varying vec3 lightDirection;
uniform vec3 lightColor;
uniform vec3 lightPosition;
uniform float materialKind;
uniform vec3 materialColor;
void main()
{
vec3 Eye = normalize(-vertex);
vec3 Reflected = normalize(reflect( -lightPosition, normal));
vec3 IAmbient = vec3(0.2f);
vec3 IAmbient = vec3(0.05f);
if (materialKind == 6) {
// sun: shines by itself
gl_FragColor = vec4(materialColor, 1.0);
return;
}
// TODO: add noise texture
vec3 IDiffuse = vec3(materialColor) * lightColor * max(dot(normal, lightDirection), 0.0);

View file

@ -60,11 +60,7 @@ namespace endofthejedi {
// TODO: add ONE sun planet
// TODO: add lights for explosions
glm::vec3 c = glm::vec3(1.0, 1.0, 0.8);
glUniform3f(m_shader.location("lightColor"), c.x, c.y, c.z);
glm::vec3 p = glm::vec3(0.3f, 0.4f, 0.0f);
glUniform3f(m_shader.location("lightPosition"), p.x, p.y, p.z);
configureLightningInShader();
renderPlanets();
renderShips();
@ -167,13 +163,15 @@ namespace endofthejedi {
{
m_planetModel->bind();
// TODO: put material into attributes and render witd glDrawInstanced
// too (same for missiles)
for (const game::Planet *planet : m_state->planets) {
glm::mat4 model = computeModelMatrix(planet);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
glm::vec3 c = planet->getColor();
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
glUniform1f(m_shader.location("materialKind"), (float) planet->material);
m_planetModel->render();
}
@ -293,4 +291,19 @@ namespace endofthejedi {
}
glPolygonMode(GL_FRONT, GL_FILL);
}
void RendererPolygon3d::configureLightningInShader()
{
glm::vec3 c = glm::vec3(1.0, 1.0, 0.8);
glUniform3f(m_shader.location("lightColor"), c.x, c.y, c.z);
glm::vec3 p = glm::vec3(0.3f, 0.4f, 0.0f);
for (const game::Planet *planet : m_state->planets) {
if (planet->material == game::Planet::Material::Sun) {
p = glm::vec3(planet->position, 0.0);
break;
}
}
glUniform3f(m_shader.location("lightPosition"), p.x, p.y, p.z);
}
}

View file

@ -49,6 +49,8 @@ namespace endofthejedi {
void renderTraces();
void configureLightningInShader();
private:
// all models are also here (for easy reloading etc.)
std::vector<PolygonModel*> m_models;

View file

@ -0,0 +1,18 @@
#include "planet.hpp"
namespace game {
glm::vec3 Planet::getColor() const
{
switch (material) {
case Material::Rock: return glm::vec3(0.4, 0.2, 0.1);
case Material::Metal: return glm::vec3(0.1, 0.1, 0.1);
case Material::Sand: return glm::vec3(0.6, 0.6, 0.3);
case Material::Gas: return glm::vec3(0.8, 0.5, 0.2);
case Material::Ice: return glm::vec3(0.7, 0.8, 1.0);
case Material::Water: return glm::vec3(0.2, 0.7, 1.0);
case Material::Sun: return glm::vec3(1.0, 1.0, 0.8);
default: return glm::vec3(1.0, 0.0, 1.0); // diagnostic color
}
}
}

View file

@ -11,37 +11,26 @@ class Planet : public Object {
* Planets are build out of one material.
* TODO: support mixture or multiple material per planet.
*/
enum class Material { Rock, Metal, Sand, Gas, Ice, Water, Sun };
enum class Material { Rock=0, Metal=1, Sand=2, Gas=3, Ice=4, Water=5, Sun=6 };
Planet(const glm::vec2 &pos, int id, float r)
: Planet(pos, id, r, Material::Rock) {}
Planet(const glm::vec2 &pos, int id, float r, Material mat)
: Object(pos, r), id(id), material(mat), seed(rand()) {}
glm::vec3 getColor() const {
switch (material) {
case Material::Rock:
return {x : 0.19, y : 0.19, z : 0.19};
case Material::Metal:
return {x : 0.19, y : 0.19, z : 0.19};
case Material::Sand:
return {x : 0.19, y : 0.19, z : 0.19};
case Material::Gas:
return {x : 0.19, y : 0.19, z : 0.19};
case Material::Ice:
return {x : 0.19, y : 0.19, z : 0.19};
case Material::Water:
return {x : 0.19, y : 0.40, z : 0.72};
case Material::Sun:
return {x : 0.64, y : 0.67, z : 0.19};
}
return {x : 0.1, y : 0.1, z : 0.1};
: Planet(pos, id, r, Material::Rock)
{
}
Planet(const glm::vec2 &pos, int id, float r, Material mat)
: Object(pos, r), id(id), material(mat), seed(rand())
{
}
glm::vec3 getColor() const;
int id;
Material material; // for rendering and physics (can fly through sun and
// outer gas planets)
int seed; // just for rendering variation
// for rendering and physics (can fly through sun and outer gas planets)
Material material;
// just for rendering variation
int seed;
};
}

View file

@ -49,7 +49,12 @@ namespace game {
}
} while(glm::length(pos) < 0.2 && tries++ < 1000);
planets.push_back(new Planet(pos, i, 0.03 + 0.07*util::randf_0_1()));
Planet::Material mat = Planet::Material::Rock;
if (i == 0) {
mat = Planet::Material::Sun;
}
planets.push_back(new Planet(pos, i, 0.03 + 0.07*util::randf_0_1(), mat));
}
}