Exemple #1
0
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;
}
/**
 * Phong Shading
 */
void PointLight::shade( Ray3D& ray ) {
	Intersection intPoint = ray.intersection;

	// construct vectors
	Vector3D n = intPoint.normal;
	Vector3D de = -ray.dir;
	Vector3D s = get_position() - intPoint.point; // light source
	Vector3D m = ((2 * n.dot(s)) * n) - s; // perfect mirror directions

	// normalize
	n.normalize();
	s.normalize();
	m.normalize();
	de.normalize();

	// do the diffuse shading
	do_diffuse(s, n, ray);

	// do the specular shading
	Colour specular(0, 0, 0);

	double mdde = m.dot(de);
	if ( mdde >= 0 ) {
		specular =
			pow(mdde, intPoint.mat->specular_exp)
			* intPoint.mat->specular * _col_specular;
	}

	if ( ray.shadowed ) {
		ray.col = ray.col + (_shadow_opacity * specular);
	} else {
		ray.col = ray.col + specular;
	}
}
Exemple #3
0
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))));
}
Exemple #4
0
void setup_plastic_with_texture(void)
{
  boost::shared_ptr<shade::shaders::Plastic> specular(new shade::shaders::Plastic(0.7, .3));
  boost::shared_ptr<shade::shaders::ObjectSpace> object_space(new shade::shaders::ObjectSpace);
  boost::shared_ptr<shade::shaders::Texture2D> tex(new shade::shaders::Texture2D);
  tex->texture_unit.set(example::make_texture("examples/pattern.dds"));
  boost::shared_ptr<shade::shaders::UVCoord> uvcoord(new shade::shaders::UVCoord);
  tex->uv = uvcoord;
  specular->color = tex;
  specular->coordinate_system = object_space;
  shader->material = specular;
  {
    shaders::IlluminatedMaterial::LightList::Accessor accessor(specular->lights);

    boost::shared_ptr<shaders::PointLight> light(new shaders::PointLight);
    light->position.set_value(shade::vec3<>(30., 15., 10.));
    light->color.set_value(shade::vec3<>(1., 1., 1.));
    accessor->push_back(light);

    boost::shared_ptr<shaders::PointLight> light2(new shaders::PointLight);
    light2->position.set_value(shade::vec3<>(-15., -3, 0.));
    light2->color.set_value(shade::vec3<>(1., 1., 1.));
    accessor->push_back(light2);
  }
}
Exemple #5
0
boost::shared_ptr<shade::Program> setup_shading(boost::shared_ptr<shade::GLSLWrapper> state)
{
    boost::shared_ptr<shade::shaders::Surface> shader(new shade::shaders::Surface);
    boost::shared_ptr<shade::Program> program(new shade::Program(shader, state));

    boost::shared_ptr<shade::shaders::Plastic> specular(new shade::shaders::Plastic(0.4, .6));
    boost::shared_ptr<shade::shaders::ObjectSpace> object_space(new shade::shaders::ObjectSpace);
    specular->color.set_value(shade::vec4<>(1., 0.4, 0.4, 1.));
    specular->coordinate_system = object_space;
    shader->material = specular;
    {
        shade::shaders::IlluminatedMaterial::LightList::Accessor accessor(specular->lights);

        boost::shared_ptr<shade::shaders::PointLight> light(new shade::shaders::PointLight);
        boost::shared_ptr<shade::shaders::GLLightPosition> gl_light_pos(new shade::shaders::GLLightPosition);
        boost::shared_ptr<shade::shaders::Vec4ToVec3> light_pos(new shade::shaders::Vec4ToVec3);
        gl_light_pos->index.set(1);
        light_pos->value = gl_light_pos;
        light->position = light_pos;
        light->color.set_value(shade::vec3<>(1., 1., 1.));
        accessor->push_back(light);

        boost::shared_ptr<shade::shaders::DirectionalLight> light2(new shade::shaders::DirectionalLight);
        light2->direction.set_value(shade::vec3<>(0., 1., 0.));
        light2->color.set_value(shade::vec3<>(1., 0., 0.));
        accessor->push_back(light2);
    }

    return program;
}
Exemple #6
0
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);
	}
}
Exemple #7
0
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();
}
Exemple #8
0
Spectrum KajiyaKay::f(const Vector3f &wo, const Vector3f &wi) const {
    Spectrum diffuse(0.f), specular(0.f);
    if (!Ks.IsBlack()) {
        // Compute specular Kajiya-Kay term
        Vector3f wh = wi + wo;
        if (!(wh.x == 0 && wh.y == 0 && wh.z == 0)) {
            wh = Normalize(wh);
#if 0
            Float cosThetaH = Dot(wo, wh);
            Float sinThetaH = std::sqrt(std::max((Float)0, (Float)1 - cosThetaH * cosThetaH));
            Float cosThetaO = CosTheta(wo), sinThetaO = SinTheta(wo);
            Float spec = std::pow(cosThetao * cosThetah + sinThetaO * sinThetaH,
                                  exponent);
#else
            Float tdoth = wh.x;
            Float spec = std::pow(
                std::sqrt(std::max((Float)0, (Float)1 - tdoth * tdoth)),
                exponent);
#endif
            specular = spec * Ks;
        }
    }
    // Compute diffuse Kajiya-Kay term
    diffuse = Kd * std::sqrt(std::max((Float)0., (Float)1. - wi.x * wi.x));
    return (InvPi / AbsCosTheta(wi)) * (diffuse + specular);
}
Exemple #9
0
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;
}
Exemple #10
0
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]));
	}
	
}
Exemple #11
0
void setup_plastic(void)
{
    boost::shared_ptr<shade::GLSLTexture> texture(new shade::GLSLTexture(GL_TEXTURE_2D, GL_TEXTURE_2D_ARRAY_EXT));
    texture->bind();
    glTexParameterf(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    Texture texture_data("examples/patterns.dds", 4);
    texture_data.upload();

    boost::shared_ptr<ArrayPlastic> specular(new ArrayPlastic(0.7, .3));
    specular->texture_unit.set(texture);
    boost::shared_ptr<shade::shaders::ObjectSpace> object_space(new shade::shaders::ObjectSpace);
    specular->coordinate_system = object_space;
    boost::shared_ptr<shade::shaders::UVCoord> uvcoord(new shade::shaders::UVCoord);
    specular->uv = uvcoord;
    shader->material = specular;
    {
        shaders::IlluminatedMaterial::LightList::Accessor accessor(specular->lights);

        boost::shared_ptr<shaders::PointLight> light(new shaders::PointLight);
        light->position.set_value(shade::vec3<>(30., 15., 10.));
        light->color.set_value(shade::vec3<>(1., 1., 1.));
        accessor->push_back(light);

        boost::shared_ptr<shaders::PointLight> light2(new shaders::PointLight);
        light2->position.set_value(shade::vec3<>(-15., -3, 0.));
        light2->color.set_value(shade::vec3<>(1., 1., 1.));
        accessor->push_back(light2);
    }
}
Exemple #12
0
	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);
	}
// main
void main(void) {
	vec2 uv = gl_FragCoord.xy / iResolution.xy;
    uv = uv * 2.0 - 1.0;
    uv.x *= iResolution.x / iResolution.y;    
    float time = iGlobalTime * 0.3;
        
    // ray
    vec3 ang = vec3(sin(time*3.0)*0.1,sin(time)*0.2+0.4,time);
    if(iMouse.z > 0.0) ang = vec3(0.0,clamp(2.0-iMouse.y*0.01,-0.3,PI),iMouse.x*0.01);
	mat4 rot = fromEuler(ang);
    
    vec3 ori = vec3(0.0,0.2,time*1.0);
    ori.y += abs(map_detailed(-ori));
    vec3 dir = normalize(vec3(uv.xy,-1.0));
    dir = rotate(normalize(dir),rot);
    
    // tracing
    vec3 p;
    float dens = hftracing(ori,dir,p);
    vec3 dist = p - ori;
    vec3 n = getNormal(p, dot(dist,dist)*EPSILON_NRM);
             
    // color
    vec3 color = sea_color(p,n,dir,dist);
    vec3 light = normalize(vec3(0.0,1.0,0.8));  
    color += vec3(diffuse(n,light,80.0) * SEA_WATER_COLOR) * 0.12; 
    color += vec3(specular(n,light,dir,60.0));  
    
    // post
    color = mix(sky_color(dir),color, clamp(1.0-length(dist)/100.0,0.0,1.0)); 
    color = pow(color,vec3(0.75));
	gl_FragColor = vec4(color,1.0);
}
Exemple #14
0
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);
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
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);
}
Exemple #16
0
fvec scene::get_surface_color( const ray_intersection &r, int reflections ) const {
	fvec surface_color( 4 );
	surface_color( 0 ) = r.to_render->get_color()( 0 ) * 0.3;
	surface_color( 1 ) = r.to_render->get_color()( 1 ) * 0.3;
	surface_color( 2 ) = r.to_render->get_color()( 2 ) * 0.3;
	surface_color( 3 ) = 1.0;

	for ( int i = 0; i < lights.size(); i++ ) {

		// setup lighting calculations
		vec direction( lights.at( i )->get_position() - r.get_point() );
		vec direction_light_source( direction / norm( direction, 2 ) );
		double distance_to_light = norm(
				lights.at( i )->get_position() - r.get_point(), 2 );

		// Detect if casted ray is a shadow
		bool has_shadow = false;
		priority_queue<ray_intersection> shadow_rays = get_ray_intersections(
						ray( r.get_point() + r.get_normal(), direction_light_source ) );
		if ( !shadow_rays.empty() && distance_to_light > shadow_rays.top().get_distance() ) {
			has_shadow = true;
		}

		double dot_light_norm = dot( direction_light_source, r.get_normal() );

		if ( dot_light_norm > 0 ) {

			vec specular(2*dot(r.get_normal(), direction_light_source)
							* r.get_normal() - direction_light_source);

			// add up light values
			for ( int k = 0; k < 3; k++ ) {

				// ambient
				surface_color(k) += lights.at(i)->get_color()(k)
										* r.to_render->get_color()(k);

				if( !has_shadow )
				{
					// fill non-shadows
					surface_color(k) += 0.2 * r.to_render->get_color()(k);

					// specular
					surface_color(k) += r.to_render->get_specular()(k)
											* lights.at(i)->get_color()(k) *
											pow(max(dot(specular, direction), 0.0), 1.0);

					// diffuse
					surface_color(k) += lights.at(i)->get_diffuse()
											* r.to_render->get_diffuse()
											* max(dot(specular, r.get_normal()), 0.0);
				}
			}
		}
	}

	return surface_color;

}
//===========================================================================
void cSpotLight::renderLightSource(cRenderOptions& a_options)
{
	// check if light sources should be rendered
	if ((!a_options.m_enable_lighting) || (!m_enabled))
    {
        // disable OpenGL light source
        glDisable(m_glLightNumber);
        return;
    }

    // enable this light in OpenGL
    glEnable(m_glLightNumber);

    // enable or disable two side light model
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, m_useTwoSideLightModel);

    // compute global position of current light in world
    computeGlobalPositionsFromRoot();

    // set lighting components
	if (a_options.m_rendering_shadow)
	{
		cColorf diffuse((GLfloat)(a_options.m_shadow_light_level * m_diffuse.getR()),
						(GLfloat)(a_options.m_shadow_light_level * m_diffuse.getG()),
						(GLfloat)(a_options.m_shadow_light_level * m_diffuse.getB()));
		cColorf specular(0.0, 0.0, 0.0);
		glLightfv(m_glLightNumber, GL_AMBIENT,  m_ambient.pColor());
		glLightfv(m_glLightNumber, GL_DIFFUSE,  diffuse.pColor() );
		glLightfv(m_glLightNumber, GL_SPECULAR, specular.pColor());
	}
	else
	{	
		glLightfv(m_glLightNumber, GL_AMBIENT,  m_ambient.pColor());
		glLightfv(m_glLightNumber, GL_DIFFUSE,  m_diffuse.pColor() );
		glLightfv(m_glLightNumber, GL_SPECULAR, m_specular.pColor());
	}

    // define the position of the light source. 
    float position[4];
    position[0] = (float)m_globalPos(0) ;
    position[1] = (float)m_globalPos(1) ;
    position[2] = (float)m_globalPos(2) ;
    position[3] = 1.0f; // defines light to be of type positional
    glLightfv(m_glLightNumber, GL_POSITION, (const float *)&position);

    cVector3d dir = m_globalRot.getCol0();
    float direction[4];
    direction[0] = (float)dir(0) ;
    direction[1] = (float)dir(1) ;
    direction[2] = (float)dir(2) ;
    direction[3] = 0.0f;
    glLightfv(m_glLightNumber, GL_SPOT_DIRECTION, (const float *)&direction);

    // set spot exponent
    glLightf(m_glLightNumber, GL_SPOT_EXPONENT, m_spotExponent);   

    // set cutoff angle
    glLightf(m_glLightNumber, GL_SPOT_CUTOFF, m_cutOffAngleDEG); 
}
Exemple #18
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::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);
}
Exemple #20
0
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;
}
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::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);
}
Exemple #23
0
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!
}
//==============================================================================
void cDirectionalLight::renderLightSource(cRenderOptions& a_options)
{
#ifdef C_USE_OPENGL

    // check if light sources should be rendered
    if ((!a_options.m_enable_lighting) || (!m_enabled))
    {
        // disable OpenGL light source
        glDisable(m_glLightNumber);
        return;
    }

    // compute global position of current light in world
    computeGlobalPositionsFromRoot();

    // enable this light in OpenGL
    glEnable(m_glLightNumber);

    // enable or disable two side light model
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, m_useTwoSideLightModel);

    // set lighting components
    if (a_options.m_rendering_shadow)
    {
        cColorf diffuse((GLfloat)(a_options.m_shadow_light_level * m_diffuse.getR()),
                                (GLfloat)(a_options.m_shadow_light_level * m_diffuse.getG()),
                                (GLfloat)(a_options.m_shadow_light_level * m_diffuse.getB()));
        cColorf specular(0.0, 0.0, 0.0);
        glLightfv(m_glLightNumber, GL_AMBIENT,  m_ambient.pColor());
        glLightfv(m_glLightNumber, GL_DIFFUSE,  diffuse.pColor() );
        glLightfv(m_glLightNumber, GL_SPECULAR, specular.pColor());
    }
    else
    {	
        glLightfv(m_glLightNumber, GL_AMBIENT,  m_ambient.pColor());
        glLightfv(m_glLightNumber, GL_DIFFUSE,  m_diffuse.pColor() );
        glLightfv(m_glLightNumber, GL_SPECULAR, m_specular.pColor());
    }

    // disable cutoff angle
    glLightf(m_glLightNumber, GL_SPOT_CUTOFF, 180);

    // define the direction of the light beam. this is a little counter intuitive,
    // but the direction is actually specified with the GL_POSITION command.
    // OpenGL recognizes the light being a directional light source by detecting that
    // value position[3] is equal to zero
    cVector3d dir = getDir();
    float position[4];
    position[0] = (float)-dir(0) ;
    position[1] = (float)-dir(1) ;
    position[2] = (float)-dir(2) ;
    position[3] = 0.0f; // defines light to be of type directional
    glLightfv(m_glLightNumber, GL_POSITION, (const float *)&position);

#endif
}
Exemple #25
0
inline vector3f get_specular(double *spc, primitive *pri, ray &r, kdtree &kdtree, int depth) {
	vector3f specular(0.0, 0.0, 0.0);
	if (spc[0] + spc[1] + spc[2] > eps)
	{
		vector3f surface_normal(pri->get_normal(r.origin));
		ray specular_ray(r.origin, r.direction - 2 * (r.direction * surface_normal) * surface_normal);
		specular = map_mul(get_ray_trace(specular_ray, kdtree, depth + 1), vector3f(spc));
	}
	return specular;
}
Exemple #26
0
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;
}
Exemple #27
0
void Viewer::attrib_buffers(CGAL::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 diffuse( 0.9f,
                     0.9f,
                     0.9f,
                     0.9f );

  QVector4D specular( 0.0f,
                      0.0f,
                      0.0f,
                      1.0f );


  QVector4D position((bb.xmax()-bb.xmin())/2, (bb.ymax()-bb.ymin())/2,bb.zmax(), 0.0 );
  GLfloat shininess =  1.0f;

  rendering_program.bind();
  mvpLocation[0] = rendering_program.uniformLocation("mvp_matrix");
  mvLocation = rendering_program.uniformLocation("mv_matrix");
  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[0], mvpMatrix);
  rendering_program.setUniformValue(mvLocation, mvMatrix);

  rendering_program.release();
  rendering_program_p_l.bind();
  mvpLocation[1] = rendering_program_p_l.uniformLocation("mvp_matrix");
  colorLocation = rendering_program_p_l.uniformLocation("color");
  rendering_program.setUniformValue(mvpLocation[1], mvpMatrix);
  rendering_program_p_l.release();
}
Exemple #28
0
void	shade(t_raytracer *ray, t_vec dir)
{
	if (ray->shade > 0)
	{
		ray->l = sub_vec(ray->tmp->pos, ray->pi);
		ray->l = norm(ray->l);
		if (ray->ret->diffuse > 0)
		{
			ray->dot = DOT(ray->n, ray->l);
			if (ray->dot > 0)
				ray->color = dot_positif(ray);
		}
		specular(ray, dir);
	}
}
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 );

}
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;
}