# HG changeset patch # User Matti Hamalainen # Date 1575587543 -7200 # Node ID 0ae1ff6096266c1dcbb1ea2f11f2224efd644deb # Parent 9909014498f0de344d42c2479d96c93d04b72472 Implement diffuse setting for models in scenefile. Also refactor parsing/handling of ambient/diffuse/specular values and unify it for both models and lights. diff -r 9909014498f0 -r 0ae1ff609626 dmmodel.cpp --- a/dmmodel.cpp Thu Dec 05 23:52:45 2019 +0200 +++ b/dmmodel.cpp Fri Dec 06 01:12:23 2019 +0200 @@ -624,6 +624,7 @@ DMModel *model = 0; DMLight *light = 0; DMVector4 *ppos = 0, *ppointAt = 0; + DMMaterial *pmat = 0; info.filename = filename; info.nline = info.state = 0; @@ -671,6 +672,7 @@ models.push_back(newmodel); model = &models.back(); model->modelFile = tokens[1]; + pmat = &model->material; info.state = 1; } else @@ -727,17 +729,6 @@ } } else - if (info.state == 1 && key == "specular") - { - DMVector4 val; - val.p.w = 1.0f; - - if (!dmParseVector(info, tokens, 1, val)) - return false; - - model->specular = val; - } - else if (info.state == 1 && key == "shininess") { if (tokens.size() != 2) @@ -746,7 +737,7 @@ "Expected argument for shininess"); } - model->shininess = std::stoi(tokens[1], 0, 0); + model->material.shininess = std::stoi(tokens[1], 0, 0); } else if (key == "light") @@ -763,25 +754,27 @@ light = &lights.back(); ppos = &light->position; ppointAt = &light->pointAt; + pmat = &light->color; info.state = 2; } else - if (info.state == 2 && (key == "ambient" || key == "diffuse" || key == "specular")) + if ((info.state == 1 || info.state == 2) && + (key == "ambient" || key == "diffuse" || key == "specular")) { DMVector4 val; - val.p.w = 1.0f; + val.c.a = 1.0f; if (!dmParseVector(info, tokens, 1, val)) return false; if (key == "ambient") - light->ambient = val; + pmat->ambient = val; else if (key == "diffuse") - light->diffuse = val; + pmat->diffuse = val; else if (key == "specular") - light->specular = val; + pmat->specular = val; } else if (key == "camera") diff -r 9909014498f0 -r 0ae1ff609626 dmmodel.h --- a/dmmodel.h Thu Dec 05 23:52:45 2019 +0200 +++ b/dmmodel.h Fri Dec 06 01:12:23 2019 +0200 @@ -130,18 +130,25 @@ union DMVector4 { struct { float x, y, z, w; } p; + struct { float r, g, b, a; } c; float values[4]; }; +struct DMMaterial +{ + DMVector4 ambient, diffuse, specular; + int shininess; +}; + + struct DMModel { int nvertices, nfaces; std::vector vertices, normals; std::vector faces; - DMVector4 ambient, diffuse, specular; - int shininess; + DMMaterial material; DMVector3 translate, scale, rotate; bool translateSet, scaleSet, rotateSet; @@ -155,18 +162,6 @@ bool loadFromPLY(const std::string &filename); bool loadFromPLY(const std::string &filename, DMPLYFileInfo &info); - void printInfo() - { - printf( - "MODEL: scale <%1.5f, %1.5f, %1.5f>\n" - "MODEL: translate <%1.5f, %1.5f, %1.5f>\n" - "MODEL: rotate <%1.5f, %1.5f, %1.5f>\n" - , - scale.x, scale.y, scale.z, - translate.x, translate.y, translate.z, - rotate.x, rotate.y, rotate.z); - } - DMModel() { nfaces = nvertices = 0; @@ -176,24 +171,28 @@ scale.x = scale.y = scale.z = 0; translateSet = rotateSet = scaleSet = false; - diffuse.p.x = diffuse.p.z = 0.56471f; diffuse.p.y = 0.5f; diffuse.p.w = 1.0f; - specular.p.x = specular.p.y = specular.p.z = 0.8f; specular.p.w = 1.0f; - shininess = 96; + material.diffuse.c.r = material.diffuse.p.z = 0.56471f; + material.diffuse.c.g = 0.5f; + material.diffuse.c.a = 1.0f; + + material.specular.c.r = material.specular.c.g = material.specular.c.b = 0.8f; + material.specular.c.a = 1.0f; + + material.shininess = 96; } }; struct DMLight { - DMVector4 - ambient, diffuse, specular, - position, pointAt; + DMMaterial color; + DMVector4 position, pointAt; DMLight() { - ambient.p.x = ambient.p.y = ambient.p.z = 0.2f; ambient.p.w = 1.0f; - diffuse.p.x = diffuse.p.y = diffuse.p.z = 0.8f; diffuse.p.w = 1.0f; - specular.p.x = specular.p.y = specular.p.z = 0.5f; specular.p.w = 1.0f; + color.ambient.c.r = color.ambient.c.g = color.ambient.p.z = 0.2f; color.ambient.c.a = 1.0f; + color.diffuse.c.r = color.diffuse.c.g = color.diffuse.p.z = 0.8f; color.diffuse.c.a = 1.0f; + color.specular.c.r = color.specular.c.g = color.specular.p.z = 0.5f; color.specular.c.a = 1.0f; position.p.x = 10.0f; position.p.y = 10.0f; diff -r 9909014498f0 -r 0ae1ff609626 gldragon.cpp --- a/gldragon.cpp Thu Dec 05 23:52:45 2019 +0200 +++ b/gldragon.cpp Fri Dec 06 01:12:23 2019 +0200 @@ -247,9 +247,9 @@ // Set the material of the model glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); - glMateriali(GL_FRONT, GL_SHININESS, model.shininess); - glMaterialfv(GL_FRONT, GL_SPECULAR, model.specular.values); - glColor4fv(model.diffuse.values); + glMateriali(GL_FRONT, GL_SHININESS, model.material.shininess); + glMaterialfv(GL_FRONT, GL_SPECULAR, model.material.specular.values); + glColor4fv(model.material.diffuse.values); // Render the model glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); @@ -380,9 +380,9 @@ void dmSetupLight(const int n, const DMLight &light) { glEnable(GL_LIGHT0 + n); - glLightfv(GL_LIGHT0 + n, GL_AMBIENT, light.ambient.values); - glLightfv(GL_LIGHT0 + n, GL_DIFFUSE, light.diffuse.values); - glLightfv(GL_LIGHT0 + n, GL_SPECULAR, light.specular.values); + glLightfv(GL_LIGHT0 + n, GL_AMBIENT, light.color.ambient.values); + glLightfv(GL_LIGHT0 + n, GL_DIFFUSE, light.color.diffuse.values); + glLightfv(GL_LIGHT0 + n, GL_SPECULAR, light.color.specular.values); glLightfv(GL_LIGHT0 + n, GL_POSITION, light.position.values); }