Exemple #1
0
SceneBuilder::SceneBuilder(char* file)
{
	scene = new Scene();
	pugi::xml_document doc;
	pugi::xml_parse_result result = doc.load_file(file);
	
	if (result) {
		pugi::xml_node nodes = doc.child("scene");
		pugi::xml_node lights = nodes.child("lights");
				
		for (pugi::xml_node light = lights.child("light"); light; light = light.next_sibling("light")) {
			std::cout << "Processing light: " << light << std::endl;
			pugi::xml_node::iterator it = light.begin();
			pugi::xml_node::iterator end = light.end();
			while (it != end) {
				pugi::xml_node position = light.child("position");
				float x = position.attribute("x").as_float();
				float y = position.attribute("y").as_float();
				float z = position.attribute("z").as_float();
				PointLight plight(x,y,z);

				pugi::xml_node color = light.child("color");				
				float r = color.attribute("r").as_float();
				float g = color.attribute("g").as_float();
				float b = color.attribute("b").as_float();
				plight.setColor(r,g,b);

				float intensity = light.child("intensity").attribute("value").as_float();	
				plight.setIntensity(intensity);

				scene->lights.push_back(plight);

				it++;
				
			}
		}
		for (pugi::xml_node node = nodes.child("node"); node; node = node.next_sibling("node")) {
			std::cout << "Processing node: " << node << std::endl;
			pugi::xml_node::iterator it = node.begin();
			pugi::xml_node::iterator end = node.end();
			while (it != end) {
				std::cout << (*it).name() << std::endl;
				//if (strcmp((*it).name(), "plynode")) {
					char* plyfile = (char *)(*it).attribute("file").value();
					std::cout << (*it).attribute("file").value() << std::endl;
					scene->root->addChild(new PlyNode(plyfile));
				//}

				it++;
				
			}
		}
	}
	else
	{
		std::cerr << "XML [" << file << "] parsed with errors. ]\n";
		std::cerr << "Error description: " << result.description() << "\n";
		std::cerr << "Error offset: " << result.offset << " (error at [..." << (file + result.offset) << "]\n\n";
	}
}
Exemple #2
0
int main() 
{
	// init GLFW and GLEW
	glfwInit();
	CVK::useOpenGL33CoreProfile();
	GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Normal Mapping", 0, 0);
	glfwSetWindowPos(window, 100, 50);
	glfwMakeContextCurrent(window);
	glewInit();
	glfwSetWindowSizeCallback(window, resizeCallback);
	glfwSetCharCallback(window, charCallback);

	// setup camera
	camera.setCenter(glm::vec3( 0.0f, 0.0f, 0.0f));
	camera.setRadius(5);
	camera.setNearFar(1.0f, 10.0f);
	CVK::State::getInstance()->setCamera(&camera);

	// setup light
	CVK::Light plight(glm::vec4(0.0f, 1.0, 10.0f, 1.0f), grey, glm::vec3( 0, 0, 0), 1.0f, 0.0f);
	CVK::State::getInstance()->addLight(&plight);

	// setup scene
	CVK::Node* node = new CVK::Node("Cube");
	// create and set teapot geometrie
	CVK::Cube* cube = new CVK::Cube(); 
	node->setGeometry(cube);
	// define material with diffuse color and normal texture
	CVK::Material matTex((char*)RESOURCES_PATH "/normalmapping/diffusemap.png", glm::vec3( 0.5f, 0.5f, 0.5f), glm::vec3( 0.3f, 0.3f, 0.3f), 120.0f);
	matTex.setTexture(CVK::NORMAL_TEXTURE, (char*)RESOURCES_PATH "/normalmapping/normalmap.png");
	CVK::Material matRed(glm::vec3(1.0,0.0,0.0), glm::vec3(1.0,1.0,1.0), 120.0f);
	useColorTexture = false;

	// phong shader
	const char *shadernames0[2] = {SHADERS_PATH "/NormalMapping/Phong.vert", SHADERS_PATH "/NormalMapping/Phong.frag"};
 	CVK::ShaderPhong phongShader = CVK::ShaderPhong( VERTEX_SHADER_BIT|FRAGMENT_SHADER_BIT, shadernames0);
 	//define Scene uniforms (ambient and fog)
	CVK::State::getInstance()->updateSceneSettings(glm::vec3(0.3,0.3,0.3), 0, glm::vec3(1.0,1.0,1.0), 1, 10, 1);
	// normal mapping shader
	const char *shadernames1[2] = {SHADERS_PATH "/NormalMapping/NormalMapping.vert", SHADERS_PATH "/NormalMapping/NormalMapping.frag"};
	ShaderNormalMapping normalMappingShader = ShaderNormalMapping( VERTEX_SHADER_BIT|FRAGMENT_SHADER_BIT, shadernames1);
	//use normal mapping shader
	useNormalMappingShader = false;

	// print infos
	std::cout << "Key s: " << "swap shader" << std::endl;
	std::cout << "Key m: " << "swap material" << std::endl;

	glClearColor(1.0, 1.0, 1.0, 0.0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);         
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  

	while( !glfwWindowShouldClose(window))
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		
		// set active shader
		if(useNormalMappingShader)
			CVK::State::getInstance()->setShader(&normalMappingShader);
		else
			CVK::State::getInstance()->setShader(&phongShader);

		// set material
		if(useColorTexture)
			node->setMaterial(&matTex);
		else
			node->setMaterial(&matRed);
		
		// update camera
		camera.update(window);
		// update view, projection matrix, light uniforms
		CVK::State::getInstance()->getShader()->update();
		// render scene
		node->render();

		glfwSwapBuffers( window);
		glfwPollEvents();
	}
	glfwDestroyWindow(window);
	glfwTerminate();

	// clean up
	delete cube;
	delete node;

	return 0;
}