std::vector<std::shared_ptr<Material>> ModelLoader::loadMaterials(const aiScene* scene, std::map<std::string, std::shared_ptr<Texture>> textures) { std::vector<std::shared_ptr<Material>> output; for (unsigned int materialIndex = 0; materialIndex < scene->mNumMaterials; ++materialIndex) { std::shared_ptr<BasicLightingMaterial> currentGeneratedMaterial(std::make_shared<BasicLightingMaterial>()); const aiMaterial *material = scene->mMaterials[materialIndex]; aiColor4D aiDiffuse; aiColor4D aiSpecular; aiColor4D aiAmbient; aiColor4D aiEmission; glm::vec4 diffuse(1.0f, 1.0f, 1.0f, 1.0f); glm::vec4 specular(0.0f, 0.0f, 0.0f, 0.0f); glm::vec4 ambient(0.0f, 0.0f, 0.0f, 1.0f); glm::vec4 emission(0.0f, 0.0f, 0.0f, 0.0f); float shininess; float shininessStrength; unsigned int max = 1; aiString texturePath; std::shared_ptr<Texture> texture(std::make_shared<Texture>()); if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, &aiDiffuse)) { diffuse = aiColor4DToGlmVec4(aiDiffuse); } if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, &aiSpecular)) { specular = aiColor4DToGlmVec4(aiSpecular); } if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &aiAmbient)) { ambient = aiColor4DToGlmVec4(aiAmbient); } if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_EMISSIVE, &aiEmission)) { emission = aiColor4DToGlmVec4(aiEmission); } aiGetMaterialFloatArray(material, AI_MATKEY_SHININESS, &shininess, &max); aiGetMaterialFloatArray(material, AI_MATKEY_SHININESS_STRENGTH, &shininessStrength, &max); shininess *= shininessStrength; if (AI_SUCCESS == material->GetTexture(aiTextureType_DIFFUSE, 0, &texturePath)) { std::string path(texturePath.data); texture = textures[path]; } currentGeneratedMaterial->setDiffuse(diffuse); currentGeneratedMaterial->setSpecular(specular); currentGeneratedMaterial->setAmbient(ambient); currentGeneratedMaterial->setEmission(emission); currentGeneratedMaterial->setShininess(shininess); currentGeneratedMaterial->setTexture(texture); output.push_back(currentGeneratedMaterial); } return output; }
//-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RenderStateMaterial_FF::applyOpenGL(OpenGLContext* oglContext) const { Color4f ambient(m_ambient, m_alpha); Color4f diffuse(m_diffuse, m_alpha); Color4f emission(m_emission, m_alpha); Color4f specular(m_specular, m_alpha); CVF_ASSERT(ambient.isValid()); CVF_ASSERT(diffuse.isValid()); CVF_ASSERT(emission.isValid()); CVF_ASSERT(specular.isValid()); CVF_ASSERT(Math::valueInRange(m_shininess, 0.0f, 128.0f)); if (m_enableColorMaterial) { glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); } else { glDisable(GL_COLOR_MATERIAL); } glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient.ptr()); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse.ptr()); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular.ptr()); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission.ptr()); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, m_shininess); // Try this to be nice if lighting is off glColor3fv(diffuse.ptr()); CVF_CHECK_OGL(oglContext); }
void pgModel::Render( BtU32 screenIndex ) { // Set the light direction static BtFloat x, y, z; z = -0.05f; y = 0.05f; x = 0.02f; MtVector3 v3LightDirection( x, y, z ); v3LightDirection.GetNormalise(); MtVector3 ambient(0.5f, 0.5f, 0.5f); //MtVector3 ambient(0.3f, 0.3f, 0.3f); //MtVector3 ambient(0, 0, 0); m_pShader->SetFloats( RsHandles_Light0Direction, &v3LightDirection.x, 3 ); // Set the lights ambient level m_pShader->SetFloats( RsHandles_LightAmbient, &ambient.x, 3 ); // Apply the shader m_pShader->Apply(); if( ( screenIndex > 1 ) && ( screenIndex <= 18 ) ) { m_pModel->Render(); } if( screenIndex == 20 ) { m_pModel->Render(); } /* BtChar text[32]; sprintf( text, "Sin-angle %.2f", sinAngle ); HlFont::RenderHeavy( MtVector2( 0, 0 ), text, MaxSortOrders-1 ); */ }
bool Planet::processArguments(int argc, const char **argv) { if(argc == 0) return true; if(argc != 8 && argc != 11) { Console->printf( "planet: planetBmpName az inc shadowsT/F flareT/F R G B [ambR ambG ambB]"); return false; } bool shadows = false; if (argv[3][0] == 't' || argv[3][0] == 'T' || argv[3][0] == '1') shadows = true; bool flare = true; if (argv[4][0] == 'f' || argv[4][0] == 'F' || argv[4][0] == '0') flare = false; ColorF intensity; intensity.red = atof(argv[5]); intensity.green = atof(argv[6]); intensity.blue = atof(argv[7]); ColorF ambient(0.0f, 0.0f, 0.0f); if (argc == 11) { ambient.red = atof(argv[8]); ambient.green = atof(argv[9]); ambient.blue = atof(argv[10]); } set(argv[0], atof(argv[1]), atof(argv[2]), shadows, flare, intensity, ambient); return true; }
void Viewer::attrib_buffers(QGLViewer* viewer) { QMatrix4x4 mvpMatrix; QMatrix4x4 mvMatrix; double mat[16]; viewer->camera()->getModelViewProjectionMatrix(mat); for(int i=0; i < 16; i++) { mvpMatrix.data()[i] = (float)mat[i]; } viewer->camera()->getModelViewMatrix(mat); for(int i=0; i < 16; i++) { mvMatrix.data()[i] = (float)mat[i]; } // define material QVector4D ambient(0.25f, 0.20725f, 0.20725f, 0.922f); QVector4D diffuse( 1.0f, 0.829f, 0.829f, 0.922f ); QVector4D specular( 0.6f, 0.6f, 0.6f, 1.0f ); QVector4D position(0.0f,0.0f,1.0f,1.0f ); GLfloat shininess = 11.264f; rendering_program.bind(); mvpLocation = rendering_program.uniformLocation("mvp_matrix"); mvLocation = rendering_program.uniformLocation("mv_matrix"); colorLocation = rendering_program.uniformLocation("color"); lightLocation[0] = rendering_program.uniformLocation("light_pos"); lightLocation[1] = rendering_program.uniformLocation("light_diff"); lightLocation[2] = rendering_program.uniformLocation("light_spec"); lightLocation[3] = rendering_program.uniformLocation("light_amb"); lightLocation[4] = rendering_program.uniformLocation("spec_power"); rendering_program.setUniformValue(lightLocation[0], position); rendering_program.setUniformValue(lightLocation[1], diffuse); rendering_program.setUniformValue(lightLocation[2], specular); rendering_program.setUniformValue(lightLocation[3], ambient); rendering_program.setUniformValue(lightLocation[4], shininess); rendering_program.setUniformValue(mvpLocation, mvpMatrix); rendering_program.setUniformValue(mvLocation, mvMatrix); rendering_program.release(); rendering_program_points.bind(); mvpLocation_points = rendering_program_points.uniformLocation("mvp_matrix"); colorLocation_points = rendering_program_points.uniformLocation("color"); rendering_program_points.setUniformValue(mvpLocation_points, mvpMatrix); rendering_program_points.release(); }
void AreaLight::shade(Ray3D &ray) { // Lighting is purely additive, and will be divided by number of samples, so don't clamp. Colour ambient(ray.intersection.mat->ambient); ambient = ambient * _col_ambient; ray.col = ray.col + ambient; if (ray.in_shadow) { // Don't shade the ray beyond its ambient component. return; } // Calculate the diffuse component. Vector3D s = (this->get_position() - ray.intersection.point); s.normalize(); Vector3D n = ray.intersection.normal; n.normalize(); Colour diffuse = max(0, s.dot(n)) * ray.intersection.mat->diffuse; diffuse = diffuse * _col_diffuse; // Calculate the specular component. Vector3D b = (ray.origin - ray.intersection.point); b.normalize(); Vector3D r = -1 * s + 2 * (n.dot(s) * n); r.normalize(); Colour specular = pow(max(0, r.dot(b)), ray.intersection.mat->specular_exp) * ray.intersection.mat->specular; specular = specular * _col_specular; ray.col = ray.col + diffuse + specular; }
void GLModel::AddMaterials(aiMaterial** materials, unsigned int numMaterials) { this->materials->resize(numMaterials); for ( unsigned int i = 0; i < numMaterials; ++i ) { aiMaterial &material = *(materials[i]); aiColor3D ambient(0.0f, 0.0f, 0.0f); aiColor3D diffuse(0.0f, 0.0f, 0.0f); aiColor3D specular(0.0f, 0.0f, 0.0f); float shininess = 10.0f; float intensity = 1.0f; float diffuseBlend = 1.0f; std::cout<<"diffuseBlend "<<diffuseBlend<<std::endl; aiString name; material.Get(AI_MATKEY_COLOR_DIFFUSE, diffuse); material.Get(AI_MATKEY_COLOR_AMBIENT, ambient); material.Get(AI_MATKEY_COLOR_SPECULAR, specular); material.Get(AI_MATKEY_SHININESS, shininess); material.Get(AI_MATKEY_TEXBLEND_DIFFUSE(0), diffuseBlend); material.Get(AI_MATKEY_SHININESS_STRENGTH, intensity); material.Get(AI_MATKEY_NAME, name); Material mat; mat.diffuse = glm::vec4(diffuse.r, diffuse.g, diffuse.b, 1.0f); mat.ambient = glm::vec4(ambient.r, ambient.g, ambient.b, 1.0f); mat.specular = glm::vec4(specular.r, specular.g, specular.b, 1.0f); mat.shininess = shininess / 5.0f; mat.intensity = 1.0f + intensity; mat.diffuseBlend = diffuseBlend; //int numTextures = material.GetTextureCount(aiTextureType_DIFFUSE); aiString texPath; this->textures->resize(this->textures->size() + 1 + 1); if ( material.Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE,0), texPath) == AI_SUCCESS ) { std::string location; if( this->path == "./") location = this->path + texPath.data; else location = this->path + "/" + texPath.data; std::cout << "Texture at " << location << std::endl; GLTexture texture(name.C_Str(), GL_TEXTURE_2D, location.c_str()); if (!texture.Load()) { printf("Error loading texture '%s'\n", location.c_str()); } this->textures->at(i) = std::pair<bool, GLTexture>(true, texture); } if(std::string(name.C_Str()) != std::string("DefaultMaterial") || numMaterials == 1) this->materials->at(i) = std::pair<aiString, Material>(name, mat); } }
Material* createMaterialFromMtl(tinyobj::material_t mtl) { glm::vec3 ambient(mtl.ambient[0], mtl.ambient[1], mtl.ambient[2]); glm::vec3 diffuse(mtl.diffuse[0], mtl.diffuse[1], mtl.diffuse[2]); glm::vec3 specular(mtl.specular[0], mtl.specular[1], mtl.specular[2]); return new Material(ambient, diffuse, specular, mtl.shininess); }
void Scene::loadLights(XMLDocument& doc){ XMLElement *lights = doc.FirstChildElement("scene")->FirstChildElement("lights"); for (const XMLElement* light = lights->FirstChildElement(); light != NULL; light = light->NextSiblingElement()){ glm::vec3 position( light->FirstChildElement("position")->FindAttribute("x")->FloatValue(), light->FirstChildElement("position")->FindAttribute("y")->FloatValue(), light->FirstChildElement("position")->FindAttribute("z")->FloatValue()); glm::vec3 ambient( light->FirstChildElement("ambient")->FindAttribute("x")->FloatValue(), light->FirstChildElement("ambient")->FindAttribute("y")->FloatValue(), light->FirstChildElement("ambient")->FindAttribute("z")->FloatValue()); glm::vec3 diffuse( light->FirstChildElement("diffuse")->FindAttribute("x")->FloatValue(), light->FirstChildElement("diffuse")->FindAttribute("y")->FloatValue(), light->FirstChildElement("diffuse")->FindAttribute("z")->FloatValue()); glm::vec3 specular( light->FirstChildElement("specular")->FindAttribute("x")->FloatValue(), light->FirstChildElement("specular")->FindAttribute("y")->FloatValue(), light->FirstChildElement("specular")->FindAttribute("z")->FloatValue()); float constant = light->FirstChildElement("constant")->FindAttribute("value")->FloatValue(); float linear = light->FirstChildElement("linear")->FindAttribute("value")->FloatValue(); float quadratic = light->FirstChildElement("quadratic")->FindAttribute("value")->FloatValue(); Light lightContent(position, ambient, diffuse, specular, constant, linear, quadratic); _lights.push_back(lightContent); } }
Color shiny_surface(RContext *rc) { Color ce = environment_map(rc->m->tinfo, reflect_dir(rc->v, rc->n)); return c_add(c_scale(rc->m->ka, ambient(rc)), c_scale(rc->m->ks, c_add(ce, specular(rc)))); }
void Light::renderSelf(glm::mat4 combined_transform) { glm::vec3 ambient(0.1f,0,0); glm::vec3 diffuse(0.2f); glm::vec3 specular(0.2f); GLfloat shininess = 80.f; GLuint ambient_loc, diffuse_loc, specular_loc, shininess_loc; //for fs GLuint light_pos;//for vs for (auto it = shader_groups.begin(); it != shader_groups.end(); ++it) { auto program = (*it)->getProgram(); (*it)->use(); ambient_loc = glGetUniformLocation(program, "kambient"); glUniform3fv(ambient_loc, 1, glm::value_ptr(ambient)); diffuse_loc = glGetUniformLocation(program, "kdiffuse"); glUniform3fv(diffuse_loc, 1, glm::value_ptr(diffuse)); specular_loc = glGetUniformLocation(program, "kspecular"); glUniform3fv(specular_loc, 1, glm::value_ptr(specular)); shininess_loc = glGetUniformLocation(program, "shininess"); glUniform1fv(shininess_loc, 1,(const float *) &shininess); light_pos = glGetUniformLocation(program, "light_pos"); glUniform3fv(light_pos, 1, glm::value_ptr(combined_transform[3])); } }
ofxLight::ofxLight(){ bool anyLight = false; for(int i=0; i<8; i++){ if(!(bool)glIsEnabled(GL_LIGHT0 +i)){ lightEnum = GL_LIGHT0 + i; lightNum = i; anyLight = true; break; } } if(!anyLight){ std::cout << "error creating light, maybe you're exceding the maximum 8 lights allowed?" << std::endl; return; } ambient(0, 0, 0); diffuse(0, 0, 0); specular(0, 0, 0); on(); if(!(bool)glIsEnabled(GL_LIGHTING)){ ofxLightsOn(); } isSpot = false; }
Color ray_shade(int level, Real w, Ray v, RContext *rc, Object *ol) { Inode *i = ray_intersect(ol, v); if (i != NULL) { Light *l; Real wf; Material *m = i->m; Vector3 p = ray_point(v, i->t); Cone recv = cone_make(p, i->n, PIOVER2); Color c = c_mult(m->c, c_scale(m->ka, ambient(rc))); rc->p = p; for (l = rc->l; l != NULL; l = l->next) if ((*l->transport)(l, recv, rc) && (wf = shadow(l, p, ol)) > RAY_WF_MIN) c = c_add(c, c_mult(m->c, c_scale(wf * m->kd * v3_dot(l->outdir,i->n), l->outcol))); if (level++ < MAX_RAY_LEVEL) { if ((wf = w * m->ks) > RAY_WF_MIN) { Ray r = ray_make(p, reflect_dir(v.d, i->n)); c = c_add(c, c_mult(m->s, c_scale(m->ks, ray_shade(level, wf, r, rc, ol)))); } if ((wf = w * m->kt) > RAY_WF_MIN) { Ray t = ray_make(p, refract_dir(v.d, i->n, (i->enter)? 1/m->ir: m->ir)); if (v3_sqrnorm(t.d) > 0) { c = c_add(c, c_mult(m->s, c_scale(m->kt, ray_shade(level, wf, t, rc, ol)))); } } } inode_free(i); return c; } else { return BG_COLOR; } }
void OBJXArchive::createMapOfMaterials() { m_mapOfMaterials.clear(); if (m_mtlFile.str().length() > 0) { m_mtlFile.seekg(ios::beg); string line = ""; Material m; while (getline(m_mtlFile, line)) { if ( (line.find("newmtl ") != string::npos) && (line.length() > 8) ) { // Store old material description. if (m.getName() != "Undefined") { m_mapOfMaterials[m.getName()] = m; } // Start next material description. m = Material(line.substr(7)); } if ( (line.find("Ns ") != string::npos) && (line.length() > 4) ) { stringstream shininess(line.substr(3)); double s; shininess >> s; m.setShininess(s); } if ( (line.find("Ka ") != string::npos) && (line.length() > 4) ) { stringstream ambient(line.substr(3)); double x; double y; double z; ambient >> x; ambient >> y; ambient >> z; m.setAmbient(Point3(x, y, z)); }
int gr_render_cmd(lua_State* L) { GRLUA_DEBUG_CALL; gr_node_ud* root = (gr_node_ud*)luaL_checkudata(L, 1, "gr.node"); luaL_argcheck(L, root != 0, 1, "Root node expected"); const char* filename = luaL_checkstring(L, 2); int width = luaL_checknumber(L, 3); int height = luaL_checknumber(L, 4); glm::vec3 eye; glm::vec3 view, up; get_tuple(L, 5, &eye[0], 3); get_tuple(L, 6, &view[0], 3); get_tuple(L, 7, &up[0], 3); double fov = luaL_checknumber(L, 8); double ambient_data[3]; get_tuple(L, 9, ambient_data, 3); glm::vec3 ambient(ambient_data[0], ambient_data[1], ambient_data[2]); luaL_checktype(L, 10, LUA_TTABLE); int light_count = lua_rawlen(L, 10); int aaMode = luaL_checknumber(L, 11); int partition = luaL_checknumber(L, 12); int threadCount = luaL_checknumber(L, 13); //Error checking if(threadCount <= 0) threadCount = 1; if(partition < 2) partition = 2; if(aaMode < 0 || aaMode > 2) aaMode = 0; luaL_argcheck(L, light_count >= 1, 10, "Tuple of lights expected"); std::list<Light*> lights; for (int i = 1; i <= light_count; i++) { lua_rawgeti(L, 10, i); gr_light_ud* ldata = (gr_light_ud*)luaL_checkudata(L, -1, "gr.light"); luaL_argcheck(L, ldata != 0, 10, "Light expected"); lights.push_back(ldata->light); lua_pop(L, 1); } Image im( width, height, 3 ); a4_render( root->node, im, eye, view, up, fov, ambient, lights, aaMode, partition, threadCount); im.savePng( filename ); return 0; }
void Scene_c3t3_item::draw(CGAL::Three::Viewer_interface* viewer) const { if (!are_buffers_filled) { compute_elements(); initialize_buffers(viewer); } vaos[Facets]->bind(); program = getShaderProgram(PROGRAM_WITH_LIGHT); attrib_buffers(viewer, PROGRAM_WITH_LIGHT); program->bind(); //program->setAttributeValue("colors", this->color()); viewer->glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(positions_poly.size() / 3)); program->release(); vaos[Facets]->release(); if(spheres_are_shown) { vaos[Spheres]->bind(); program_sphere->bind(); //ModelViewMatrix used for the transformation of the camera. QMatrix4x4 mvp_mat; // ModelView Matrix used for the lighting system QMatrix4x4 mv_mat; GLdouble d_mat[16]; GLint is_both_sides = 0; viewer->camera()->getModelViewProjectionMatrix(d_mat); //Convert the GLdoubles matrices in GLfloats for (int i=0; i<16; ++i){ mvp_mat.data()[i] = GLfloat(d_mat[i]); } viewer->camera()->getModelViewMatrix(d_mat); for (int i=0; i<16; ++i) mv_mat.data()[i] = GLfloat(d_mat[i]); QVector4D position(0.0f,0.0f,1.0f, 1.0f ); QVector4D ambient(0.4f, 0.4f, 0.4f, 0.4f); // Diffuse QVector4D diffuse(1.0f, 1.0f, 1.0f, 1.0f); // Specular QVector4D specular(0.0f, 0.0f, 0.0f, 1.0f); viewer->glGetIntegerv(GL_LIGHT_MODEL_TWO_SIDE, &is_both_sides); program_sphere->setUniformValue("mvp_matrix", mvp_mat); program_sphere->setUniformValue("mv_matrix", mv_mat); program_sphere->setUniformValue("light_pos", position); program_sphere->setUniformValue("light_diff",diffuse); program_sphere->setUniformValue("light_spec", specular); program_sphere->setUniformValue("light_amb", ambient); program_sphere->setUniformValue("spec_power", 51.8f); program_sphere->setUniformValue("is_two_side", is_both_sides); viewer->glDrawArraysInstanced(GL_TRIANGLES, 0, static_cast<GLsizei>(s_vertex.size()/3), static_cast<GLsizei>(s_radius.size())); program_sphere->release(); vaos[Spheres]->release(); } }
MaterialPtr Material::Pearl() { float openGLFactor=128.0; glm::vec3 ambient(0.25, 0.20725, 0.20725); glm::vec3 diffuse(1.0, 0.829, 0.829); glm::vec3 specular(0.296648, 0.296648, 0.296648); float shininess = openGLFactor*0.088; return std::make_shared<Material>(ambient, diffuse, specular, shininess); }
MaterialPtr Material::Emerald() { float openGLFactor=128.0; glm::vec3 ambient(0.0215, 0.1745, 0.0215); glm::vec3 diffuse(0.07568, 0.61424, 0.07568); glm::vec3 specular(0.633, 0.727811, 0.633); float shininess = openGLFactor*0.6; return std::make_shared<Material>(ambient, diffuse, specular, shininess); }
MaterialPtr Material::Bronze() { float openGLFactor=128.0; glm::vec3 ambient(0.2125, 0.1275, 0.054); glm::vec3 diffuse(0.714, 0.4284, 0.18144); glm::vec3 specular(0.393548, 0.271906, 0.166721); float shininess = openGLFactor*0.2; return std::make_shared<Material>(ambient, diffuse, specular, shininess); }
Color textured_plastic(RContext *rc) { Color c, ct = texture_map(rc->m->tinfo, rc->t); c = c_add(c_mult(ct, c_add(c_scale(rc->m->ka, ambient(rc)), c_scale(rc->m->kd, diffuse(rc)))), c_mult(rc->m->s, c_scale(rc->m->ks, specular(rc)))); return c; }
void Scene::_parsePointLight(uint id){ float vec[4]; _checkToken( "PointLight"); _nextToken( ); _checkToken( "{"); _nextToken( ); _checkToken("position"); for( int i = 0; i < 3; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } Point3 position(vec); _nextToken( ); _checkToken("ambientColor"); for( int i = 0; i < 4; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } RGBAColor ambient(vec); _nextToken( ); _checkToken( "diffuseColor"); for( int i = 0; i < 4; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } RGBAColor diffuse(vec); _nextToken( ); _checkToken("specularColor"); for( int i = 0; i < 4; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } RGBAColor specular(vec); _nextToken( ); _checkToken("constant_attenuation"); _nextToken( ); float constant_attenuation = _parseFloat( ); _nextToken( ); _checkToken("linear_attenuation"); _nextToken( ); float linear_attenuation = _parseFloat( ); _nextToken( ); _checkToken("quadratic_attenuation"); _nextToken( ); float quadratic_attenuation = _parseFloat( ); _nextToken( ); _checkToken( "}"); // Fill me in! }
Material::Material() { ambient(1,1,1,1); diffuse(1,1,1,1); emissive(1,1,1,1); specular(1,1,1,1); m_shininess = 100; m_texture = -1; m_pTextureFilename = 0; }
int gr_render_cmd(lua_State* L) { GRLUA_DEBUG_CALL; gr_node_ud* root = (gr_node_ud*)luaL_checkudata(L, 1, "gr.node"); luaL_argcheck(L, root != 0, 1, "Root node expected"); const char* filename = luaL_checkstring(L, 2); int width = luaL_checknumber(L, 3); int height = luaL_checknumber(L, 4); Point3D eye; Vector3D view, up; get_tuple(L, 5, &eye[0], 3); get_tuple(L, 6, &view[0], 3); get_tuple(L, 7, &up[0], 3); double fov = luaL_checknumber(L, 8); double ambient_data[3]; get_tuple(L, 9, ambient_data, 3); Colour ambient(ambient_data[0], ambient_data[1], ambient_data[2]); luaL_checktype(L, 10, LUA_TTABLE); int light_count = luaL_getn(L, 10); luaL_argcheck(L, light_count >= 1, 10, "Tuple of lights expected"); std::list<Light*> lights; for (int i = 1; i <= light_count; i++) { lua_rawgeti(L, 10, i); gr_light_ud* ldata = (gr_light_ud*)luaL_checkudata(L, -1, "gr.light"); luaL_argcheck(L, ldata != 0, 10, "Light expected"); lights.push_back(ldata->light); lua_pop(L, 1); } double aperature = luaL_checknumber(L, 11); double dof = luaL_checknumber(L, 12); a4_render(root->node, filename, width, height, eye, view, up, fov, ambient, lights, aperature, dof); return 0; }
void SetOpenGlDefaultMaterial() { glm::vec4 ambient( 0.2, 0.2, 0.2, 1.0 ); glm::vec4 specular( 0.0, 0.0, 0.0, 1.0 ); glm::vec4 emissive( 0.0, 0.0, 0.0, 1.0 ); glm::vec4 diffuse( 0.0, 0.0, 0.0, 1.0 ); GLint shininess_value = 0; glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ); glMateriali ( GL_FRONT_AND_BACK, GL_SHININESS, shininess_value ); glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.x ); glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.x ); glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.x ); glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.x ); }
void all_start_here(t_bunny_ini *ini, t_data *data) { data->map->spawn[0] = mygetnbr(bunny_ini_get_field (ini, "level", "start_position", 0)) + 0.5; data->map->spawn[1] = mygetnbr(bunny_ini_get_field (ini, "level", "start_position", 1)) + 0.5; data->map->spawn[2] = mygetnbr(bunny_ini_get_field (ini, "level", "start_position", 2)); data->pl->pp_y = mygetnbr(bunny_ini_get_field(ini, "level", "start_position", 0)) + 0.5; data->pl->pp_x = mygetnbr(bunny_ini_get_field(ini, "level", "start_position", 1)) + 0.5; data->pl->angle = mygetnbr(bunny_ini_get_field(ini, "level", "start_position", 2)); ambient(data); }
VType *CopyMesh(INDICES &indices, LptaSkin::ID &skinId, const aiScene *scene, MANAGERS &managers) { VType *vertices = new VType(); vector<LptaSkin::ID> skinIds; for (unsigned int matIdx = 0; matIdx < scene->mNumMaterials; ++matIdx) { aiMaterial *mat = scene->mMaterials[matIdx]; aiColor4D diffuse(0.0f, 0.0f, 0.0f, 0.0f); aiColor4D ambient(0.0f, 0.0f, 0.0f, 0.0f); aiColor4D specular(0.0f, 0.0f, 0.0f, 0.0f); aiColor4D emissive(0.0f, 0.0f, 0.0f, 0.0f); float specularPower = 0.0f; mat->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse); mat->Get(AI_MATKEY_COLOR_AMBIENT, ambient); mat->Get(AI_MATKEY_COLOR_SPECULAR, specular); mat->Get(AI_MATKEY_COLOR_EMISSIVE, emissive); mat->Get(AI_MATKEY_SHININESS, specularPower); aiGetMaterialColor(mat, AI_MATKEY_COLOR_EMISSIVE, &emissive); LptaMaterial::ID materialId = managers.material.AddOrRetrieveMaterial( LptaColor(diffuse.r, diffuse.g, diffuse.b, diffuse.a), LptaColor(ambient.r, ambient.g, ambient.b, diffuse.a), LptaColor(specular.r, specular.g, specular.b, specular.a), LptaColor(emissive.r, emissive.g, emissive.b, emissive.a), specularPower ); LptaSkin::TEXTURE_IDS textureIds; // only diffuse texture is supported for now for (unsigned int texIdx = 0; texIdx < mat->GetTextureCount(aiTextureType_DIFFUSE) && texIdx < LptaSkin::MAX_TEXTURES; ++texIdx) { aiString filepath; mat->GetTexture(aiTextureType_DIFFUSE, texIdx, &filepath); LptaTexture::ID textureId = managers.texture.AddOrRetrieveTexture(filepath.C_Str()); textureIds.push_back(textureId); } LptaSkin::ID skinId = managers.skin.AddSkin(materialId, textureIds, false); skinIds.push_back(skinId); } for (unsigned int meshIdx = 0; meshIdx < scene->mNumMeshes; ++meshIdx) { const aiMesh *mesh = scene->mMeshes[meshIdx]; CopyAlgorithm::Copy(*vertices, indices, *mesh, vertices->GetNumVertices()); skinId = skinIds.at(mesh->mMaterialIndex); } return vertices; }
void PointLight::shade(Ray3D& ray) { // TODO: implement this function to fill in values for ray.col // using phong shading. Make sure your vectors are normalized, and // clamp colour values to 1.0. // // It is assumed at this point that the intersection information in ray // is available. So be sure that traverseScene() is called on the ray // before this function. // Shade the ambient component. Colour ambient(ray.intersection.mat->ambient); ambient = ambient * _col_ambient; // Ray colour shouldn't be darker than its brightest ambient component. if (ray.col[0] < ambient[0] && ray.col[1] < ambient[1] && ray.col[2] < ambient[2]) { ray.col = ambient; } else if (ray.in_shadow) { // Raytracer's shader will add back in the ambient term. ray.col = Colour(0, 0, 0); } if (ray.in_shadow) { // Don't shade the ray beyond its ambient component. return; } // Calculate the diffuse component. Vector3D s = (this->get_position() - ray.intersection.point); s.normalize(); Vector3D n = ray.intersection.normal; n.normalize(); Colour diffuse = max(0, s.dot(n)) * ray.intersection.mat->diffuse; diffuse = diffuse * _col_diffuse; // Calculate the specular component. Vector3D b = (ray.origin - ray.intersection.point); b.normalize(); Vector3D r = -1 * s + 2 * (n.dot(s) * n); r.normalize(); Colour specular = pow(max(0, r.dot(b)), ray.intersection.mat->specular_exp) * ray.intersection.mat->specular; specular = specular * _col_specular; ray.col = ray.col + diffuse + specular; ray.col.clamp(); }
static void SetMaterial (const dVector& color) { #ifdef COMPILE_OPENGL SetDefaultLight (); dVector ambient(0.0f, 0.0f, 0.0f, 0.0f); glMaterialfv(GL_FRONT, GL_DIFFUSE, &color.m_x); glMaterialfv(GL_FRONT, GL_SPECULAR, &ambient.m_x); glMaterialfv(GL_FRONT, GL_AMBIENT, &ambient.m_x); glMaterialfv(GL_FRONT, GL_EMISSION, &ambient.m_x); glMaterialf(GL_FRONT, GL_SHININESS, 0.0f); glDisable(GL_TEXTURE_2D); glEnable(GL_CULL_FACE); glCullFace (GL_BACK); glFrontFace (GL_CCW); glShadeModel (GL_SMOOTH); #endif }
//============================================================================= // シェーダパラメーターの設定 void TitleWeaponUnit::SettingShaderParameter() { // カメラ取得 Camera* camera = _application->GetCameraManager()->GetCurrentCamera(); // 行列の作成 algo::CreateWVP(_matrix_world_view_projection, _world.matrix, camera); // ライトの方向作成 D3DXVECTOR4 light_direction(0.2f, -0.8f, 0.5f, 0.f); D3DXVECTOR4 ambient(0.97f, 0.8f, 0.75f, 1.f); D3DXVECTOR4 eye(camera->GetVectorEye(), 0.f); for( u32 shader_index = 0; shader_index < _shader_size; ++shader_index ) { // シェーダの設定 _shader[shader_index].SetWorldViewProjection(_matrix_world_view_projection); _shader[shader_index].SetWorld(_world.matrix); _shader[shader_index].SetLightDirection(light_direction); _shader[shader_index].SetEyePosition(eye); _shader[shader_index].SetWorld(_world.matrix); } }
void SurfacePropertiesCS::parsePhong( TiXmlElement* techniqueRoot ) { // create a material float power = 2; if ( techniqueRoot->FirstChildElement( "shininess" ) ) { const char* valStr = techniqueRoot->FirstChildElement( "shininess" )->FirstChildElement( "float" )->GetText(); sscanf_s( valStr, "%f", &power ); } Color ambient( 0.7f, 0.7f, 0.7f, 1 ); if ( techniqueRoot->FirstChildElement( "ambient" ) ) { const char* valStr = techniqueRoot->FirstChildElement( "ambient" )->FirstChildElement( "color" )->GetText(); sscanf_s( valStr, "%f %f %f %f", &ambient.r, &ambient.g, &ambient.b, &ambient.a ); } Color diffuse( 0.7f, 0.7f, 0.7f, 1 ); if ( techniqueRoot->FirstChildElement( "diffuse" ) ) { const char* valStr = techniqueRoot->FirstChildElement( "diffuse" )->FirstChildElement( "color" )->GetText(); sscanf_s( valStr, "%f %f %f %f", &diffuse.r, &diffuse.g, &diffuse.b, &diffuse.a ); } Color specular( 0.9f, 0.9f, 0.9f, 1 ); if ( techniqueRoot->FirstChildElement( "specular" ) ) { const char* valStr = techniqueRoot->FirstChildElement( "specular" )->FirstChildElement( "color" )->GetText(); sscanf_s( valStr, "%f %f %f %f", &specular.r, &specular.g, &specular.b, &specular.a ); } Color emissive( 0, 0, 0 ); if ( techniqueRoot->FirstChildElement( "emission" ) ) { const char* valStr = techniqueRoot->FirstChildElement( "emission" )->FirstChildElement( "color" )->GetText(); sscanf_s( valStr, "%f %f %f %f", &emissive.r, &emissive.g, &emissive.b, &emissive.a ); } m_surfaceProperties = new SurfaceProperties( ambient, diffuse, specular, emissive, power ); }