예제 #1
0
void ShaderProgram::autoRegisterUniforms()
{

  unsigned int size=m_shaders.size();
  const std::string *source;
  std::vector<std::string> lines;

  boost::char_separator<char> sep(" \t\r\n");
  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;

  for(unsigned int i=0; i<size; ++i)
  {
    /// first grab all of the shader source for this program
    source=m_shaders[i]->getShaderSource();
    // and split on new lines
    boost::split(lines, *source, boost::is_any_of("\n\r"));

    // now we loop for the strings and tokenize looking for the uniform keyword
    // or the #define keyword
    std::vector<std::string>::iterator start=lines.begin();
    std::vector<std::string>::iterator end=lines.end();
    std::map<std::string,int> defines;

    while(start!=end)
    {
      // have we got #define
      if(start->find("#define") !=std::string::npos)
      {
        int value;
        std::string define;
        if( parseHashDefine(*start,define,value) )
         {
          defines[define]=value;
        }
      }
      // see if we have uniform in the string
      else if (start->find("uniform") !=std::string::npos)
      {
        parseUniform(*start,defines);
      }
      // got to the next line
      ++start;
     }
  }
}
예제 #2
0
bool Material::parseShader(Pass* pass, Properties* shaderProperties)
{
    // vertexShader
    const char* vertShader = getOptionalString(shaderProperties, "vertexShader", nullptr);

    // fragmentShader
    const char* fragShader = getOptionalString(shaderProperties, "fragmentShader", nullptr);

    // compileTimeDefines
    const char* compileTimeDefines = getOptionalString(shaderProperties, "defines", "");

    if (vertShader && fragShader)
    {
        auto glProgramState = GLProgramState::getOrCreateWithShaders(vertShader, fragShader, compileTimeDefines);
        pass->setGLProgramState(glProgramState);

        // Parse uniforms only if the GLProgramState was created
        auto property = shaderProperties->getNextProperty();
        while (property)
        {
            if (isValidUniform(property))
            {
                parseUniform(glProgramState, shaderProperties, property);
            }

            property = shaderProperties->getNextProperty();
        }

        auto space = shaderProperties->getNextNamespace();
        while (space)
        {
            const char* name = space->getNamespace();
            if (strcmp(name, "sampler") == 0)
            {
                parseSampler(glProgramState, space);
            }
            space = shaderProperties->getNextNamespace();
        }
    }

    return true;
}
예제 #3
0
//Creates a TriangleMesh instance from provided xml data
TriangleMesh Visitor::loadMeshFromXml(pugi::xml_node& resource, Node& container)
{
	bool static_mesh = false;
	Shader mesh_shader;
	Texture mesh_texture;
	pugi::xml_node scenario_texture = resource.child("Texture");

	if (resource.attribute("StaticObject"))
	{
		static_mesh = true;
	}

	if (resource.child("Texture") || resource.child("Texture3D") || resource.child("FrameBufferTexture"))
		for (pugi::xml_node_iterator textures = resource.begin(); textures != resource.end(); ++textures)
		{
			//mesh_texture = loadTextureFromXml(*textures);
			mesh_texture = loadTextureFromXml(resource.child("Texture"));
			//if (mesh_texture != nullptr)
			break;
		}
	else
		mesh_texture = scene_objects.getTextureByKey(resource.attribute("Texture").as_string());

	std::string fs_path = resource.attribute("FSPath").as_string();
	std::string vs_path = resource.attribute("VSPath").as_string();
	mesh_shader = scene_objects.getShaderByKey(vs_path.append(fs_path));

	std::string obj_path = resource.attribute("ObjFile").as_string();
	//mesh_buffers = scene_objects.getBufferByKey(obj_path);
	pugi::xml_attribute instance = resource.attribute("InstanceCount");

	vec3 position;
	vec3 velocity;

	//Now gather all information for uniforms to the shaders
	for (pugi::xml_node_iterator uniforms = resource.begin(); uniforms != resource.end(); ++uniforms)
	{
		if (std::strcmp(uniforms->name(), "UniformBlock") == 0)
		{
			UniformBlock to_add = synthesizeBlock(*uniforms);
			mesh_shader.addUniformBlock(to_add);
		}
		if (std::strcmp(uniforms->name(), "Uniform") == 0)
		{
			ShaderParameter uniform = parseUniform(*uniforms);
			if (strcmp(uniform.name.c_str(), "velocity") == 0)
			{
				velocity.x = uniform.client_data[0];
				velocity.y = uniform.client_data[1];
				velocity.z = uniform.client_data[2];
			}
			else
				mesh_shader.addUniform(uniform);

			if (strcmp(uniform.name.c_str(), "translation") == 0)
			{
				position.x = uniform.client_data[0];
				position.y = uniform.client_data[1];
				position.z = uniform.client_data[2];
			}
		}
	}


	//Now loop over all nodes to determine which blocks the shader should bind to 
	for (pugi::xml_node_iterator uniforms = resource.begin(); uniforms != resource.end(); ++uniforms)
	{
		if (std::strcmp(uniforms->name(), "ShaderUniforms") == 0)
		{
			std::string block_name = uniforms->attribute("Name").as_string();
			int block_index = uniforms->attribute("BindingPoint").as_int();
		}
	}

	//Lava.png
	//dry_clay.jpg
	//Redbrick.jpg
	//rock04.jpg
	//Rosewood.jpg

	//Checkered Marble.png
	//Ocean Texture.png
	//Stonewall.jpg
	//Ambient Green.jpg
	//Red Pattern.jpg
	//Green Wall.jpg
	//Red Fuzzy Pattern.jpg
	//Brick Circle Pattern.jpg
	ObjFileReader* mesh_reader = scene_objects.getFileReader(obj_path);
	int num_buffers = mesh_reader->getNumSubMeshes();
	Texture second = scene_objects.getTextureByKey("Lava.png");
	Texture third = scene_objects.getTextureByKey("Redbrick.jpg");
	Texture fourth = scene_objects.getTextureByKey("Rosewood.jpg");
	Texture fifth = scene_objects.getTextureByKey("Checkered Marble.png");

	Texture sixth = scene_objects.getTextureByKey("Lava.png");
	Texture seventh = scene_objects.getTextureByKey("Ocean Texture.png");
	Texture eighth= scene_objects.getTextureByKey("Stonewall.jpg");
	Texture ninth = scene_objects.getTextureByKey("Ambient Green.jpg");
	Texture tenth = scene_objects.getTextureByKey("Red Pattern.jpg");
	Texture eleventh = scene_objects.getTextureByKey("Brick Circle Pattern.jpg");
	Texture twelfth = scene_objects.getTextureByKey("Green Wall.jpg");
	Texture thirteenth = scene_objects.getTextureByKey("Brick Circle Pattern.jpg");


	for (int i = 0; i < num_buffers; i++)
	{
		BufferManager mesh_buffers;
		TriangleMesh to_return;

		mesh_buffers = mesh_reader->getPrimitive(i);

		//if (instance)
		//	to_return = TriangleMesh(instance.as_int());
		//else
		if (i % 13 == 0)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, mesh_texture, static_mesh);
		else if (i % 13 == 1)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, second, static_mesh);
		else if (i % 13 == 2)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, third, static_mesh);
		else if (i % 13 == 3)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, fourth, static_mesh);
		else if (i % 13 == 4)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, fifth, static_mesh);
		else if (i % 13 == 5)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, sixth, static_mesh);
		else if (i % 13 == 6)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, seventh, static_mesh);
		else if (i % 13 == 7)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, eighth, static_mesh);
		else if (i % 13 == 8)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, ninth, static_mesh);
		else if (i % 13 == 9)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, tenth, static_mesh);
		else if (i % 13 == 10)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, eleventh, static_mesh);
		else if (i % 13 == 11)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, twelfth, static_mesh);
		else if (i % 13 == 12)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, thirteenth, static_mesh);


		//to_return.setPosition(position);
		to_return.setVelocity(velocity);

		container.meshes.push_back(to_return);
	}
	TriangleMesh dummy;
	return dummy;
}