Beispiel #1
0
void Demo::renderPartGPU(){
	
	CVK::Node* partGPUNode = new CVK::Node("partGPU");
	partGPUNode->setGeometry(partGeometry);
	partGPUNode->setMaterial(partMaterial);

	for (int j = 0; j < world->getAllPartNum(); j++){
		glm::vec3 temP = cuda->h_uVOpPos[j];	//h_uVOpPos
		//cout << temP.y << endl;
		partGPUNode->setModelMatrix(glm::translate(glm::mat4(1.0f), temP));
		partGPUNode->render();
	}
}
Beispiel #2
0
void CVK::Node::load(std::string path)
{
	// load a scene with ASSIMP
	Assimp::Importer importer;
	const aiScene* scene = importer.ReadFile(path, aiProcess_GenSmoothNormals | aiProcess_Triangulate);
	if(scene)
		printf("SUCCESS: Loaded Model from Path: \"%s\"\n", path.c_str());
	else 
	{
		printf("ERROR: Loading failed from Path: \"%s\"\n", path.c_str());
		return;
	}

	std::vector<CVK::Material*> materials;
	
	// load all materials in this file
	for(unsigned int i=0; i < scene->mNumMaterials; i++)
	{
		aiMaterial* mat = scene->mMaterials[i];

		aiColor3D diffColor (0.f,0.f,0.f);
		mat->Get(AI_MATKEY_COLOR_DIFFUSE, diffColor);
		aiColor3D specColor (0.f,0.f,0.f);
		mat->Get(AI_MATKEY_COLOR_SPECULAR, specColor);
		float shininess = 0.0f;
		mat->Get(AI_MATKEY_SHININESS, shininess);
		glm::vec3 diffuseColor(diffColor.r, diffColor.g, diffColor.b);
		glm::vec3 specularColor(specColor.r, specColor.g, specColor.b);

		aiString fileName;  // filename
		mat->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), fileName);
		std::string s = path.substr(0, path.rfind("/")) + "/";
		s += fileName.data;
		
		//materials.push_back(new CVK::Material(glm::vec3(0.0,1.0,0.0), glm::vec3(0.0,0.0,1.0), 10));
		if (fileName.length>0) 
			materials.push_back(new CVK::Material( const_cast<char*> ( s.c_str() ), 1.f, 1.f, specularColor, shininess));
			//should set kd and ks!!
		else 
			materials.push_back(new CVK::Material(diffuseColor, specularColor, shininess));
	}
	
	// load all meshes in this file
	for(unsigned int i=0; i < scene->mNumMeshes; i++)
	{
		aiMesh* mesh = scene->mMeshes[i];

		CVK::Geometry* geometry = new CVK::Geometry();
		
		// load geometry information of the current mesh
		for(unsigned int vCount = 0; vCount < mesh->mNumVertices; vCount++)
		{
			// vertices
			aiVector3D v = mesh->mVertices[vCount];
			geometry->getVertices()->push_back( glm::vec4(v.x, v.y, v.z, 1.0f));

			// normals
			if (mesh->HasNormals())
			{
				v = mesh->mNormals[vCount];
				geometry->getNormals()->push_back( glm::vec3(v.x, v.y, v.z));
			}

			// texture coordinates
			if (mesh->HasTextureCoords(0))
			{
				v = mesh->mTextureCoords[0][vCount];
				geometry->getUVs()->push_back( glm::vec2(v.x, v.y));
			}
		}

		for(unsigned int fCount = 0; fCount < mesh->mNumFaces; fCount++)
		{
			aiFace f = mesh->mFaces[fCount];
			// index numbers
			for(unsigned int iCount = 0; iCount < f.mNumIndices; iCount++)
			{
				geometry->getIndex()->push_back(f.mIndices[iCount]);
			}
		}

		geometry->createBuffers();
		
		// new child node with the geometry and a connection to material
		CVK::Node* child = new CVK::Node();
		child->setGeometry(geometry);
		child->setMaterial(materials[mesh->mMaterialIndex]);
		addChild(child);
	}
}
Beispiel #3
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;
}
Beispiel #4
0
void Demo::run(){

	// Init GLFW and GLEW
	glfwInit();
	CVK::useOpenGL33CoreProfile();
	GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "RBPE-Demo", 0, 0);
	glfwSetWindowPos( window, 100, 50);
	glfwMakeContextCurrent(window);

	glewInit();

	float pr = world->getPartRadius();
	float temp = pr * 3;
	cubeGeometry = new CVK::Cube(temp);
	partGeometry = new CVK::Sphere(pr);
	//material setzten, geht aber nur bei node, also in VO
	cubeMaterial = new CVK::Material((char*)RESOURCES_PATH "/cv_logo.bmp", black, grey, 100.0f);
	//cubeMaterial = new CVK::Material(red, white, 100.0f);	//!for transparency!
	//cubeMaterial->setAlpha(0.5);							//!for transparency!
	partMaterial = new CVK::Material(blue, white, 100.0f);

	//plane für boden
	plane = new CVK::Plane();
	CVK::Node* planeNode = new CVK::Node("Plane");
	CVK::Material mat_brick((char*)RESOURCES_PATH "/brick.bmp", black, darkgrey, 100.0f);
	planeNode->setGeometry(plane);
	planeNode->setMaterial(&mat_brick);
	planeNode->setModelMatrix(glm::rotate(glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, 0)), glm::vec3(10)), -90.0f, glm::vec3(1, 0, 0)));	//0.4=partdurchmesser
	demo->sceneRoot->addChild(planeNode);
	demo->partRoot->addChild(planeNode);

	camera->setCenter( glm::vec3( 0.0f, 0.0f, 0.0f));
	camera->setRadius( 30);
	camera->setNearFar( 1.0f, 100.0f);

	glfwSetWindowSizeCallback( window, resizeCallback);

	initScene();

	//load, compile and link Shader
	const char *shadernames[2] = {SHADERS_PATH "/Phong.vert", SHADERS_PATH "/Phong.frag"};
	CVK::ShaderPhong phongShader( VERTEX_SHADER_BIT|FRAGMENT_SHADER_BIT, shadernames);

	//OpenGL parameters
	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);

	CVK::State::getInstance()->setCamera( camera);

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

	//define Scene uniforms (ambient and fog)
	CVK::State::getInstance()->updateSceneSettings( darkgrey, 0, white, 1, 10, 1);
	CVK::State::getInstance()->setShader(&phongShader);

	//init cuda
	if(isGPU == true){
		cuda->initCUDA();
	}

	while( !glfwWindowShouldClose(window)){

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		//time->startFrame();

		stepSimulation(duration);

		//Update Camera
		camera->update(window);

		//update shader and render
		phongShader.update();

		if (renderPart == false){
			//render würfel
			updateVOs();
			sceneRoot->render();
		}
		if (renderPart == true){
			//render partikel
			if (isGPU == true){
				updateVOs();
				planeNode->render();
				renderPartGPU();
			}
			else{
				partRoot->render();
			}
		}

		glfwSwapBuffers(window);
		glfwPollEvents();

		//time->endFrame();

		int fps = time->updateFPS();
		char title[64];
		sprintf_s(title, "Rigid Body | %d fps", fps);
		glfwSetWindowTitle(window, title);
	}
	cuda->~Cuda();	//free cuda stuff

	glfwDestroyWindow( window);
	glfwTerminate();
}