Revamp color system, again. Moving color components to scene objects instead

This commit is contained in:
2019-03-19 20:11:52 +01:00
parent 50e3b60c28
commit 3a6181f239
5 changed files with 111 additions and 73 deletions
+29 -31
View File
@@ -172,20 +172,19 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
boxNode->shininess = 20;
boxNode->displacementCoefficient = 40;
rootNode->children.push_back(boxNode);
sphereNode = createSceneNode();
sphereNode->setTexture(&t_cobble_diff, &t_cobble_normal);
sphereNode->setMesh(&sphere);
sphereNode->scale *= 2;
sphereNode->scale.z *= 20;
//lightNode[1]->children.push_back(sphereNode);
*/
sphereNode = createSceneNode();
//sphereNode->setTexture(&t_cobble_diff, &t_cobble_normal);
sphereNode->setMesh(&sphere);
sphereNode->position = {500, 500, 100};
sphereNode->scale *= 15;
sphereNode->diffuse_color;
sphereNode->setMaterial(Material().reflection_mapped(&t_reflection, 0.5).no_colors().no_texture_reset(), true);
//rootNode->children.push_back(sphereNode);
lightNode[0]->position = {-600, 1400, 800};
lightNode[0]->color_emissive = vec3(0.35);
lightNode[0]->color_diffuse = vec3(0.6);
lightNode[0]->color_specular = vec3(0.1);
lightNode[0]->attenuation = vec3(1.0, 0.0, 0.0);
lightNode[0]->attenuation = vec3(1.8, 0.0, 0.0);
textNode = createSceneNode();
@@ -302,27 +301,23 @@ void renderNode(SceneNode* node, Gloom::Shader* parent_shader = default_shader)
// coordinates in MV space
vec3 position; // MV
vec3 attenuation;
vec3 color_emissive;
vec3 color_diffuse;
vec3 color_specular;
vec3 color;
bool is_spot;
vec3 spot_target; // MV
float spot_cuttof_angle;
vec3 spot_direction; // MV, must be normalized
float spot_cuttof_cos;
void push_to_shader(Gloom::Shader* shader, uint id) {
#define L(x) shader->location("light[" + std::to_string(id) + "]." #x)
#define V(x) glUniform3fv(L(x), 1, glm::value_ptr(x))
glUniform1i (L(is_spot) , is_spot);
glUniform1f (L(spot_cuttof_angle), spot_cuttof_angle);
glUniform1f (L(spot_cuttof_cos), spot_cuttof_cos);
V(position);
V(spot_target);
V(spot_direction);
V(attenuation);
V(color_emissive);
V(color_diffuse);
V(color_specular);
#undef v
#undef l
V(color);
#undef V
#undef L
}
};
static Light lights[N_LIGHTS];
@@ -346,16 +341,21 @@ void renderNode(SceneNode* node, Gloom::Shader* parent_shader = default_shader)
glUniformMatrix4fv(s->location("MV") , 1, GL_FALSE, glm::value_ptr(node->MV));
glUniformMatrix4fv(s->location("MVnormal"), 1, GL_FALSE, glm::value_ptr(node->MVnormal));
glUniform2fv(s->location("uvOffset") , 1, glm::value_ptr(node->uvOffset));
glUniform4fv(s->location("basecolor") , 1, glm::value_ptr(node->basecolor));
glUniform3fv(s->location("diffuse_color") , 1, glm::value_ptr(node->diffuse_color));
glUniform3fv(s->location("emissive_color"), 1, glm::value_ptr(node->emissive_color));
glUniform3fv(s->location("specular_color"), 1, glm::value_ptr(node->specular_color));
glUniform1f( s->location("opacity"), node->opacity);
glUniform1f( s->location("shininess"), node->shininess);
glUniform1f( s->location("reflexiveness"), node->reflexiveness);
glUniform1f( s->location("displacementCoefficient"), node->displacementCoefficient);
glUniform1ui(s->location("isTextured"), node->isTextured);
glUniform1ui(s->location("isColorMapped"), node->isColorMapped);
glUniform1ui(s->location("isVertexColored"), node->isVertexColored);
glUniform1ui(s->location("isNormalMapped"), node->isNormalMapped);
glUniform1ui(s->location("isDisplacementMapped"), node->isDisplacementMapped);
glUniform1ui(s->location("isReflectionMapped"), node->isReflectionMapped);
glUniform1ui(s->location("isIlluminated"), node->isIlluminated);
glUniform1ui(s->location("isInverted"), node->isInverted);
if (node->isTextured) glBindTextureUnit(0, node->diffuseTextureID);
if (node->isNormalMapped) glBindTextureUnit(1, node->normalTextureID);
if (node->isDisplacementMapped) glBindTextureUnit(2, node->displacementTextureID);
@@ -368,12 +368,10 @@ void renderNode(SceneNode* node, Gloom::Shader* parent_shader = default_shader)
uint id = node->lightID;
lights[id].position = vec3(node->MV * vec4(vec3(0.0), 1.0));
lights[id].is_spot = node->nodeType == SPOT_LIGHT;
lights[id].spot_target = node->rotation; // already MV space, todo: change this
lights[id].spot_cuttof_angle = glm::sin(node->spot_cuttof_angle);
lights[id].spot_direction = node->spot_direction; // MV space
lights[id].spot_cuttof_cos = node->spot_cuttof_cos;
lights[id].attenuation = node->attenuation;
lights[id].color_emissive = node->color_emissive;
lights[id].color_diffuse = node->color_diffuse;
lights[id].color_specular = node->color_specular;
lights[id].color = node->light_color;
lights[id].push_to_shader(s, id);
break;
}
+39 -16
View File
@@ -43,14 +43,20 @@ struct SceneNode {
vertexArrayObjectID = cache[mesh];
VAOIndexCount = mesh->indices.size();
isColorMapped = ! mesh->colors.empty();
isVertexColored = ! mesh->colors.empty();
}
void setTexture(const PNGImage* diffuse, const PNGImage* normal=nullptr, const PNGImage* displacement=nullptr) {
void setTexture(
const PNGImage* diffuse,
const PNGImage* normal=nullptr,
const PNGImage* displacement=nullptr,
const PNGImage* reflection=nullptr,
bool texture_reset=true) {
static map<const PNGImage*, int> cache;
assert(vertexArrayObjectID==-1);
isTextured = false;
isNormalMapped = false;
isDisplacementMapped = false;
if (texture_reset){
isTextured = false;
isNormalMapped = false;
isDisplacementMapped = false;
}
if (diffuse) {
if (cache.find(diffuse) == cache.end())
@@ -73,18 +79,32 @@ struct SceneNode {
isDisplacementMapped = true;
}
}
void setMaterial(const Material& mat, bool recursive=false) {
if (!mat.ignore_diffuse) diffuse_color = mat.diffuse_color;
if (!mat.ignore_emissive) emissive_color = mat.emissive_color;
if (!mat.ignore_specular) specular_color = mat.specular_color;
if (!mat.ignore_specular) shininess = mat.shininess;
setTexture(
mat.diffuse_texture,
mat.normal_texture,
mat.displacement_texture,
mat.reflection_texture,
mat.texture_reset
);
if (recursive) for (SceneNode* child : children)
child->setMaterial(mat, true);
}
// this node
SceneNodeType nodeType;
vector<SceneNode*> children;
// light specific:
uint lightID = -1;
vec3 color_emissive = vec3(0.0);
vec3 color_diffuse = vec3(0.0);
vec3 color_specular = vec3(0.0);
vec3 attenuation = vec3(1.0, 0.0, 0.001); // 1 / (x + y*l + z*l*l)
float spot_cuttof_angle = glm::radians(1.5); // radians
uint lightID = -1;
vec3 light_color = vec3(1.0);
vec3 attenuation = vec3(1.0, 0.0, 0.001); // 1 / (x + y*l + z*l*l)
vec3 spot_direction = vec3(0.0); // in MV space, must be normalized
float spot_cuttof_cos = glm::cos(glm::radians(1.5));
SceneNode* targeted_by = nullptr; // spot will follow this node
// The node's position and rotation relative to its parent
@@ -97,9 +117,12 @@ struct SceneNode {
int vertexArrayObjectID = -1;
uint VAOIndexCount = 0;
// textures
float shininess = 10.0; // specular power
vec4 basecolor = vec4(1.0);
// textures and materials
float opacity = 1.0;
float shininess = 1.0; // specular power
vec3 diffuse_color = vec3(1.0);
vec3 emissive_color = vec3(0.5);
vec3 specular_color = vec3(0.2);
vec2 uvOffset = vec2(0.0, 0.0); // specular power
uint diffuseTextureID;
uint normalTextureID;
@@ -108,7 +131,7 @@ struct SceneNode {
// shader flags
bool isTextured = false;
bool isColorMapped = false;
bool isVertexColored = false;
bool isNormalMapped = false;
bool isDisplacementMapped = false;
bool isIlluminated = true;
+1 -1
View File
@@ -51,7 +51,7 @@ uint generateBuffer(const Mesh &mesh, bool doAddTangents) {
glEnableVertexAttribArray(3);
}
if (doAddTangents && !mesh.textureCoordinates.empty())
if (doAddTangents || !mesh.textureCoordinates.empty())
addTangents(vaoID, mesh);
return vaoID;