Пример #1
0
void Surface::createPolyhedron(rvulong type)
{
  Vector normal, snormal, tmpvec;
  int i;

  ph.type   =  ( (nvecs == 4) ? 1 : 0 )
    | ( type & (~1));

  ph.surface= 0;


  /******* BOUNDING BOX *********/
  ph.bbox.be_empty();
  
  for(i=0;i<nvecs;i++)
    ph.bbox.fit_into(vecs[i]);

  ph.bbox.add_safety();


  /********* MASTER PLANE *******/
  normal=planarNormal(); /* normal on surface */
  createPlane(&ph.plane[0], normal, vecs[1]);


  /********* BOUNDING PLANES ******/
  for(i=0;i<nvecs;i++)
    {
      tmpvec = vecs[(i+1) % nvecs] - vecs[i];
      snormal=Vector(normal, tmpvec);
      createPlane(&ph.plane[i+1], snormal, vecs[i]);
    };
}
Пример #2
0
void RenderableDebugPlane::update(const UpdateData& data) {
    if (_shader->isDirty())
        _shader->rebuildFromFile();

    if (_planeIsDirty)
        createPlane();
}
// Descr: evident
// Implementation details: See gtest/samples for GTest syntax and usage
TEST(GeometryTest, GetNormal)
{

	Atom atom1, atom2, atom3;
	atom1.x = 3.2;
	atom1.y = 4.6;
	atom1.z = 0.2;
	atom2.x = 7.1;
	atom2.y = 1.4;
	atom2.z = 10.0;
	atom3.x = 0.0;
	atom3.y = 0.0;
	atom3.z = 0.0;

    Plane testPlane = createPlane(atom1, atom2, atom3);

    Point expected = createPoint(45.72, -30.58, -28.18);

    Point test = getNormal(testPlane);

    test.x = ((double) ((int) (test.x * 100))) / 100.0;
    test.y = ((double) ((int) (test.y * 100))) / 100.0;
    test.z = ((double) ((int) (test.z * 100))) / 100.0;

    EXPECT_NEAR(expected.x, test.x, .01);
    EXPECT_NEAR(expected.y, test.y, .01);
    EXPECT_NEAR(expected.z, test.z, .01);

}
Пример #4
0
void testGetNormal()
{
    cout << "Testing GetNormal" <<endl;   
    Atom atom1, atom2, atom3;
    atom1.x = 3.2;
    atom1.y = 4.6;
    atom1.z = 0.2;
    atom2.x = 7.1;
    atom2.y = 1.4;
    atom2.z = 10.0;
    atom3.x = 0.0;
    atom3.y = 0.0;
    atom3.z = 0.0;

    Plane testPlane = createPlane(atom1, atom2, atom3);

    Point expected = createPoint(45.72, -30.58, -28.18);

    Point test = getNormal(testPlane);
    
    test.x = ((double) ((int) (test.x * 100))) / 100.0;
    test.y = ((double) ((int) (test.y * 100))) / 100.0;
    test.z = ((double) ((int) (test.z * 100))) / 100.0;
    assert(test.x == expected.x);
    assert(test.y == expected.y);
    assert(test.z == expected.z);

    cout << "Testing GetNormal Completed\n" <<endl;
}
////////////////////////////////////////////////////////////////////////////////
// SoftShadowsRenderer::createGeometry()
////////////////////////////////////////////////////////////////////////////////
bool SoftShadowsRenderer::createGeometry()
{
    return createPlane(
        m_groundIndexBuffer,
        m_groundVertexBuffer,
        m_groundRadius,
        m_groundHeight);
}
Пример #6
0
NS_CC_BEGIN

bool Frustum::initFrustum(const Camera* camera)
{
    _initialized = true;
    createPlane(camera);
    return true;
}
bool ScreenSpaceFramebuffer::initialize(){
    _originalViewportSize = OsEng.windowWrapper().currentWindowResolution();

    createPlane();
    createShaders();
    createFragmentbuffer();

    return isReady();
}
Пример #8
0
bool ScreenSpaceImage::initialize() {
    _originalViewportSize = OsEng.windowWrapper().currentWindowResolution();

    createPlane();
    createShaders();
    updateTexture();

    return isReady();
}
Пример #9
0
void readGeometry(scene_data* scene, const boost::property_tree::ptree& pt)
{
	boost::property_tree::ptree::const_iterator iter = pt.begin();
	for (; iter != pt.end(); ++iter)
	{
		std::string name = iter->first;
		geometry* geom;
		if (name == "cube")
			geom = createBox();
		else if (name == "tetrahedron")
			geom = createTetrahedron();
		else if (name == "pyramid")
			geom = createPyramid();
		else if (name == "disk")
		{
			int slices = iter->second.get_child("slices").get_value<int>();
			geom = createDisk(slices);
		}
		else if (name == "cylinder")
		{
			int slices = iter->second.get_child("slices").get_value<int>();
			int stacks = iter->second.get_child("stacks").get_value<int>();
			geom = createCylinder(stacks, slices);
		}
		else if (name == "sphere")
		{
			int slices = iter->second.get_child("slices").get_value<int>();
			int stacks = iter->second.get_child("stacks").get_value<int>();
			geom = createSphere(stacks, slices);
		}
		else if (name == "torus")
		{
			float radius = iter->second.get_child("radius").get_value<float>();
			int slices = iter->second.get_child("slices").get_value<int>();
			int stacks = iter->second.get_child("stacks").get_value<int>();
			geom = createTorus(radius, stacks, slices);
		}
		else if (name == "plane")
		{
			int width = iter->second.get_child("width").get_value<int>();
			int depth = iter->second.get_child("depth").get_value<int>();
			geom = createPlane(width, depth);
		}
		else if (name == "sierpinski")
		{
			int divisions = iter->second.get_child("divisions").get_value<int>();
			geom = createSierpinski(divisions);
		}
		else
		{
			std::cerr << "Error - Geometry type not recognised: " << name << std::endl;
			exit(EXIT_FAILURE);
		}
		scene->geometry[name] = geom;
	}
}
void Segmentation::doSegmentation(pcl::PointCloud<pcl::PointXYZRGBA>::Ptr &cloud)
{
    //MainPlane groundMainPlane;
    // Variables
    isComputed = false ;
    struct timeval tbegin,tend;
    double texec=0.0;

    // Start timer
    gettimeofday(&tbegin,NULL);

    pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloudFiltered(new pcl::PointCloud<pcl::PointXYZRGBA>);
    //pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloudFilteredCopy(new pcl::PointCloud<pcl::PointXYZRGBA>);
    _mainCloud.reset(new pcl::PointCloud<pcl::PointXYZRGBA>);
    (void) passthroughFilter(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0, cloud, cloudFiltered); // 3.0, 3.0, -0.2, 2.0, -4.0, 4.0
    pcl::copyPointCloud(*cloudFiltered, *_mainCloud);

    pcl::PointCloud<pcl::PointXYZRGBA>::Ptr ground (new pcl::PointCloud<pcl::PointXYZRGBA>);
    pcl::PointCloud<pcl::PointXYZRGBA>::Ptr Hullground (new pcl::PointCloud<pcl::PointXYZRGBA>);

    std::vector<pcl::PointIndices> vectorInliers(0);
    std::vector <pcl::ModelCoefficients> vectorCoeff(0);
    std::vector < pcl::PointCloud<pcl::PointXYZRGBA>::Ptr > vectorCloudinliers(0);
    std::vector < pcl::PointCloud<pcl::PointXYZRGBA>::Ptr > vectorHull(0);
    std::vector < pcl::PointCloud<pcl::PointXYZRGBA>::Ptr > vectorCluster(0);

    pcl::ModelCoefficients::Ptr coefficientsGround (new pcl::ModelCoefficients);
    pcl::PointIndices::Ptr inliersGround (new pcl::PointIndices);

    //    pcl::VoxelGrid< pcl::PointXYZRGBA > sor;
    //    sor.setInputCloud (_mainCloud);
    //    sor.setLeafSize (0.01f, 0.01f, 0.01f);
    //    sor.filter (*_mainCloud);
    //(void) findGround(_mainCloud,_ground);
    //isComputed = true;
    if(findGround(_mainCloud,_ground))
    {
        Eigen::Vector3f gravityVector(_ground.getCoefficients()->values[0], _ground.getCoefficients()->values[1], _ground.getCoefficients()->values[2]);

        //(void) findOtherPlanesRansac(_mainCloud,gravityVector,vectorCoeff , vectorCloudinliers,vectorHull);

        //(void) regionGrowing(vectorCloudinliers, vectorCluster);
        euclidianClustering(_mainCloud,vectorCloudinliers);
        ransac(vectorCloudinliers,gravityVector,vectorCluster );
        createPlane(vectorCluster);
        // End timer
    }

    gettimeofday(&tend,NULL);
    // Compute execution time
    texec = ((double)(1000*(tend.tv_sec-tbegin.tv_sec)+((tend.tv_usec-tbegin.tv_usec)/1000)))/1000;
    std::cout << "time : " << texec << std::endl;
}
Пример #11
0
void Frustum::calculatePlanes(const glm::vec3 &pos, const glm::vec3 &dir, const glm::vec3 &up)
{
    glm::vec3 X, Y, Z, nc, fc;
    Z = glm::normalize(-dir);
    X = glm::normalize(glm::cross(up,Z));
    Y = glm::cross(Z,X);
    
    nc = pos - Z * near;
    fc = pos - Z * far;
    
    ntl = nc + Y * nh - X * nw;
	ntr = nc + Y * nh + X * nw;
	nbl = nc - Y * nh - X * nw;
	nbr = nc - Y * nh + X * nw;
    
	ftl = fc + Y * fh - X * fw;
	ftr = fc + Y * fh + X * fw;
	fbl = fc - Y * fh - X * fw;
	fbr = fc - Y * fh + X * fw;
    
    plane[PTOP] = createPlane(ntr,ntl,ftl);
	plane[PBOTTOM] = createPlane(nbl,nbr,fbr);
	plane[PLEFT] = createPlane(ntl,nbl,fbl);
	plane[PRIGHT] = createPlane(nbr,ntr,fbr);
	plane[PNEAR] = createPlane(ntl,ntr,nbr);
	plane[PFAR] = createPlane(ftr,ftl,fbl);
    
    std::cout<<"Z "<<Z.x<<" "<<Z.y<<" "<<Z.z<<std::endl;
    
    for(int i=0; i<6; i++)
    {
    	std::cout<<plane[i].x<<" "<<plane[i].y<<" "<<plane[i].z<<std::endl;
    }
}
		//-----------------------------------------------------------------------
		bool PhysXBridge::initNx(Real gravityY)
		{
			if (!mPhysicsSDK)
			{
				// Initialize PhysicsSDK
				NxPhysicsSDKDesc desc;
				NxSDKCreateError errorCode = NXCE_NO_ERROR;
				mPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, 0, new PhysXLogging(), desc, &errorCode);
				if(!mPhysicsSDK) 
				{
					EXCEPT(Exception::ERR_INTERNAL_ERROR, "PU: Cannot initialise the PhysX SDK.", "PhysXBridge::initNx");
				}

				mPhysicsSDK->setParameter(NX_SKIN_WIDTH, 0.05f);
			}

			// Create a scene
			if (!mScene)
			{
				NxSceneDesc sceneDesc;
				sceneDesc.gravity = NxVec3(0.0f, gravityY, 0.0f);
				mScene = mPhysicsSDK->createScene(sceneDesc);
				if(!mScene)
				{
					EXCEPT(Exception::ERR_INTERNAL_ERROR, "PU: Cannot create a PhysX Scene.", "PhysXBridge::initNx");
				}

				// Set the contact report
				mScene->setUserContactReport(&mPhysXContactReport);

				// Set default material
				NxMaterial* defaultMaterial = mScene->getMaterialFromIndex(0);
				defaultMaterial->setRestitution(0.0f);
				defaultMaterial->setStaticFriction(0.5f);
				defaultMaterial->setDynamicFriction(0.5f);

				// Create ground plane
				createPlane();
			}

			// Create a controller
			ControllerManager& controllerManager = ControllerManager::getSingleton(); 
			ControllerValueRealPtr physXBridgeUpdateValue(PU_NEW PhysXBridgeUpdateValue(this));
			mTimeController = controllerManager.createFrameTimePassthroughController(physXBridgeUpdateValue);

			return true;
		}
Пример #13
0
void 
WaterRenderer::
init()
{
    // initialize parent
    TrackballViewer::init();

	m_water = *createPlane();
	m_skybox = *createCube();
	
	waterColor = Vector4(0.1,0.2,0.3,0.5);

	watch.start();
	startingTime = watch.stop();

	// set camera to look at world coordinate center
    sceneSize = 4;
	set_scene_pos(Vector3(0.0, 0.0, 0.0), sceneSize);
	isRadial = false;
	amplitude1 = 0.01f;
	wavelength1 = 2.0f;
	speed1 = 1.0f;
	direction1 = Vector2(1, -1);

	amplitude2 = 1.0f;
	wavelength2 = 8.0f;
	speed2 = 2.0f;
	direction2 = Vector2(1, 0);

	amplitude3 = 0.5f;
	wavelength3 = 15.0f;
	speed3 = 1.0f;
	direction3 = Vector2(0, 1);
	
	amplitude4 = 15;
	wavelength4 = 30;
	speed4 = 3.0;
	direction4 = Vector2(1,0);

	time4 = -1000;

	generateCubeMap();
	
	// load shaders
	m_skyShader.create("skybox.vs", "skybox.fs");
	m_waterShader.create("water.vs", "water.fs");
}
Пример #14
0
void GECube::generate(float width, float height, float depth, unsigned int segments_x, unsigned segments_y, unsigned int segments_z)
{
	// Vertex and index mesh count.
	m_vertexCount = ((segments_x + 1) * (segments_y + 1) + (segments_z + 1) * (segments_y + 1) + (segments_x + 1) * (segments_z + 1)) * 16;
	m_indexCount = (segments_x * segments_y + segments_z * segments_y + segments_x * segments_z) * 12;

	// Generate the data holders.
	m_vertexBuffer = new float[m_vertexCount];
	m_indexBuffer = new unsigned int[m_indexCount];

	float xOrigin = 1.0 / 2.0f;
	float yOrigin = 1.0 / 2.0f;
	float zOrigin = 1.0 / 2.0f;

	unsigned int vertexOffset = 0;
	unsigned int indexOffset = 0;

	// Top
	createPlane(1.0f, 1.0, segments_x, segments_z, yOrigin, { 0.0f, 1.0f, 0.0f }, 0, 0);

	// Bottom
	vertexOffset = (segments_x + 1) * (segments_z + 1);
	indexOffset = segments_x * segments_z;
	createPlane(1.0f, 1.0f, segments_x, segments_z, yOrigin, { 0.0f, -1.0f, 0.0f }, vertexOffset, indexOffset);

	// Left
	vertexOffset = (segments_x + 1) * (segments_z + 1) * 2;
	indexOffset = segments_x * segments_z * 2;
	createPlane(1.0f, 1.0f, segments_z, segments_y, xOrigin, { -1.0f, 0.0f, 0.0f }, vertexOffset, indexOffset);

	//Right
	vertexOffset = (segments_x + 1) * (segments_z + 1) * 2 + (segments_z + 1) * (segments_y + 1);
	indexOffset = segments_x * segments_z * 2 + segments_z * segments_y;
	createPlane(1.0f, 1.0f, segments_z, segments_y, xOrigin, { 1.0f, 0.0f, 0.0f }, vertexOffset, indexOffset);

	//Back
	vertexOffset = ((segments_x + 1) * (segments_z + 1) + (segments_z + 1) * (segments_y + 1)) * 2;
	indexOffset = (segments_x * segments_z + segments_z * segments_y) * 2;
	createPlane(1.0f, 1.0f, segments_x, segments_y, zOrigin, { 0.0f, 0.0f, -1.0f }, vertexOffset, indexOffset);

	//Back
	vertexOffset = ((segments_x + 1) * (segments_z + 1) + (segments_z + 1) * (segments_y + 1)) * 2 + (segments_x + 1) * (segments_y + 1);
	indexOffset = (segments_x * segments_z + segments_z * segments_y) * 2 + segments_x * segments_y;
	createPlane(1.0f, 1.0f, segments_x, segments_y, zOrigin, { 0.0f, 0.0f, 1.0f }, vertexOffset, indexOffset);

	m_scale = { width, height, depth };
	m_scaleChanged = true;

	m_width = width;
	m_height = height;
	m_depth = depth;

	generateBuffers();
}
Пример #15
0
Graphics::Graphics(HWND outputWindow, int width, int height) : width(width), height(height) {
	initializeDirect3d11App(outputWindow, width, height);

	light.diffuse = 0.6;
	light.ambient = XMFLOAT4(0.3, 0.3, 0.3, 0.0);
	light.position = XMFLOAT3(3.0, 7.0, 10.0);

	Model *bunny = loadObjModel("resources\\bunny.3dobj", 0.01);
	Model *cube = createCube();
	Model *allModel = concatModel(createCube(), bunny, 3, 0, 0);

	model = bunny;

	initCamera();

	initPass();
	initDebugPass();

	XMMATRIX trans = XMMatrixTranslation(3, 0, 0);
	XMFLOAT4X4 transMatrix;
	XMStoreFloat4x4(&transMatrix, trans);
	pass->setWorldMatrix(transMatrix);

	skyMapPass = new SkyMapPass(device, context, camera, defaultRenderTarget);
	skyMapPass->init("resources\\skymap.dds", width, height);

	renderDepthPass = new RenderDepthPass(device, context, camera, depthRenderTarget);
	renderDepthPass->init(allModel, &light, width, height);

	shadowMapPass = new ShadowMapPass(device, context, camera, defaultRenderTarget);
	shadowMapPass->init(createPlane(), &light, "resources\\grass.jpg", depthRenderTarget->getTexture(), width, height);

	normalMapPass = new NormalMapPass(device, context, camera, defaultRenderTarget);
	normalMapPass->init(createCube(), "resources\\brick_bump.jpg", "resources\\brick.jpg", &light, width, height);

	// instances
	InstanceType* instances = new InstanceType[2];
	instances[0].offset = XMFLOAT3(1, 0, 0);
	instances[1].offset = XMFLOAT3(5, 0, 0);

	ModelInstance *twoCube = new ModelInstance(*createCube(), instances, 2);
	instancesPass = new InstancesPass(device, context, camera, defaultRenderTarget);
	instancesPass->init(twoCube, width, height);
}
Пример #16
0
 shared_ptr<TexturedMesh> GeometryFactory::getPrimitive(String name)
 {
     auto it = primitive_map.find(name);
     
     if(it != primitive_map.end())
         return it->second;
     
     // else create
     if (name == "plane")
         primitive_map[name] = createPlane();
     else if (name == "boundingBox")
         primitive_map[name] = createBoundingBox();
     else if (name == "box")
         primitive_map[name] = createBox();
     
     it = primitive_map.find(name);
     assert(it != primitive_map.end());
     return it->second;
 }
Пример #17
0
bool RenderableDebugPlane::initialize() {
    glGenVertexArrays(1, &_quad); // generate array
    glGenBuffers(1, &_vertexPositionBuffer); // generate buffer
    createPlane();

    if (_shader == nullptr) {
        // Plane Program

        RenderEngine& renderEngine = OsEng.renderEngine();
        _shader = renderEngine.buildRenderProgram("PlaneProgram",
            "${MODULE_BASE}/shaders/plane_vs.glsl",
            "${MODULE_BASE}/shaders/plane_fs.glsl"
            );
        if (!_shader)
            return false;
    }


    return isReady();
}
Пример #18
0
osg::Node* CSulClouds::create()
{
	/*
	// create perlin noise cloud
	m_rTexImage = new CSulTexImage( 256, 256 );

	CSulPerlinNoise2D* pPerlin = new CSulPerlinNoise2D;
	pPerlin->CreateArray2D( m_rTexImage, 100.0f, 128, 0, 255 ); 
	m_rTexImage->Process();
*/

	m_rGroup = new osg::Group;	
	m_rGroup->setName( "CSulClouds" );


	/*
	// debug create screen aligned quad to see clouds
	CSulScreenAlignedQuad* pQuad = new CSulScreenAlignedQuad( osg::Vec3(128,128,0), 256, 256, 800, 600 );
	pQuad->setTexture( m_rTexImage->getTexture() );
	pGroup->addChild( pQuad->getProjection() );
*/

	osg::Node* pGeodeCloud = createPlane( m_size, getHeight() );

	CSulTransToEye* mXform = new CSulTransToEye;
	mXform->IgnoreZ( true );
	mXform->setCullingActive(false);
	mXform->addChild( pGeodeCloud );

	m_rGroup->addChild( mXform );
/*
	// to keep the texture at the correct position and wind
	osg::TexMat* texMat = new osg::TexMat();
	osg::StateSet* stateset = m_rPlane->getOrCreateStateSet();
	stateset->setTextureAttribute( 0, texMat );
	m_rPlane->setCullCallback( new CSulCloudPlaneCullback( texMat, m_size ) );
*/

	return m_rGroup;
}
	//---------------------------------------------------------------------
	bool PrefabFactory::createPrefab(Mesh* mesh)
	{
		const String& resourceName = mesh->getName();

		if(resourceName == "Prefab_Plane")
		{
			createPlane(mesh);
			return true;
		}
		else if(resourceName == "Prefab_Cube")
		{
			createCube(mesh);
			return true;
		}
		else if(resourceName == "Prefab_Sphere")
		{
			createSphere(mesh);
			return true;
		}
	
		return false;
	}
Пример #20
0
void initialise()
{
	srand(time(NULL));

	glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
	projection = glm::perspective(glm::degrees(glm::quarter_pi<float>()),
								  800.0f/600.0f,
								  0.1f,
								  10000.0f);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_VERTEX_ARRAY);

	if (!eff.addShader("multi_light.vert", GL_VERTEX_SHADER))
		exit(EXIT_FAILURE);
	if (!eff.addShader("multi_light.frag", GL_FRAGMENT_SHADER))
		exit(EXIT_FAILURE);
	if (!eff.create())
		exit(EXIT_FAILURE);

	light.data.ambient = glm::vec4(0.0f, 0.0f, 0.1f, 1.0f);
	light.data.diffuse = glm::vec4(0.0f, 0.0f, 0.2f, 1.0f);
	light.data.specular = glm::vec4(0.0f, 0.0f, 0.5f, 1.0f);
	light.data.lightDir = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f);
	light.create();

	spot_light_data spot;
	spot.ambient = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
	spot.diffuse = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
	spot.specular = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
	spot.lightPos = glm::vec4(0.0f, 2.0f, 0.0f, 1.0f);
	spot.attenuation = glm::vec4(0.05f, 0.02f, 0.01f, 1.0f);
	spot.lightDir = glm::vec4(0.0f, -1.0f, 0.0f, 1.0f);
	spot.power = 1.0f;
	spot.dist = 10.0f;
	dynamic.addSpot(spot);

	point_light_data point[4];
	glm::vec4 cols[4];
	cols[0] = glm::vec4(0.2f, 0.0f, 0.0f, 1.0f);
	cols[1] = glm::vec4(0.0f, 0.2f, 0.0f, 1.0f);
	cols[2] = glm::vec4(0.0f, 0.0f, 0.2f, 1.0f);
	cols[3] = glm::vec4(0.2f, 0.2f, 0.2f, 1.0f);
	for (int i = 0; i < 4; ++i)
	{
		point[i].ambient = cols[i];
		point[i].diffuse = cols[i];
		point[i].specular = cols[i];
		point[i].attenuation = glm::vec4(0.02f, 0.01f, 0.005f, 1.f);
		point[i].dist = 10.0f;
	}
	point[0].lightPos = glm::vec4(-2.0f, 2.0f, -2.0f, 1.0f);
	point[1].lightPos = glm::vec4(2.0f, 2.0f, -2.0f, 1.0f);
	point[2].lightPos = glm::vec4(-2.0f, 2.0f, 2.0f, 1.0f);
	point[3].lightPos = glm::vec4(2.0f, 2.0f, 2.0f, 1.0f);
	for (int i = 0; i < 4; ++i)
		dynamic.addPoint(point[i]);

	dynamic.create();

	plane.geometry = createPlane(20, 20);
	glm::vec4 colour(0.1f, 0.1f, 0.1f, 1.0f);
	plane.material = new material();
	plane.material->data.emissive = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
	plane.material->data.ambient = colour;
	plane.material->data.diffuse = colour;
	plane.material->data.specular = colour;
	plane.material->data.shininess = 2.0f;
	plane.material->create();

	sphere.geometry = createSphere(20, 20);
	sphere.material = new material();
	sphere.material->data.emissive = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
	sphere.material->data.shininess = 1.0f;
	sphere.material->create();
	sphere.transform.position = glm::vec3(0.0f, 2.0f, 0.0f);
	sphere.transform.scale = glm::vec3(0.1f, 0.1f, 0.1f);

	geometry* geom = createBox();
	for (int i = 0; i < 4; ++i)
	{
		box[i].geometry = geom;
		glm::vec4 colour(0.1f * i, 0.6f - (0.1f * i), (float)rand() / (float)RAND_MAX, 1.0f);
		box[i].material = new material;
		box[i].material->data.emissive = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
		box[i].material->data.ambient = colour;
		box[i].material->data.diffuse = colour;
		box[i].material->data.specular = colour;
		box[i].material->data.shininess = i + 1;
		box[i].material->create();
		box[i].transform.scale = glm::vec3(0.25f, 0.25f, 0.25f);
	}
	box[0].transform.position = glm::vec3(-2.0f, 0.25f, -2.0f);
	box[1].transform.position = glm::vec3(2.0f, 0.25f, -2.0f);
	box[2].transform.position = glm::vec3(-2.0f, 0.25f, 2.0f);
	box[3].transform.position = glm::vec3(2.0f, 0.25f, 2.0f);
}
Пример #21
0
int main(int argc, char** argv)
{
  // parse command
  unsigned int pos = 1;
  unsigned int mask = (argc >= 2) ? 0 : 0xFFFF;
  while(pos < argc)
  {
    unsigned int sceneNumber = atoi(argv[pos++]);
    mask |= 1 << sceneNumber;
  }


  // Initialize
  phong p32(32.0f);
  phong p64(64.0f);
  phong p16(16.0f);
  diffuse d;
  constantAlbedo red(color(1.0f, 0.0f, 0.0f));
  constantAlbedo green(color(0.0f, 1.0f, 0.0f));
  constantAlbedo blue(color(0.0f, 0.0f, 1.0f));
  constantAlbedo white(color(1.0f, 1.0f, 1.0f));

  std::vector<triangle> sphere = createSphere( vec3d(0.0f, 0.0f, 0.0f), 1.0f, 16, 16 );
  std::vector<triangle> plane = createPlane( vec3d(-1.0f, -1.0f, 0.0f), vec3d(0.0f, 2.0f, 0.0f), vec3d(2.0f, 0.0f, 0.0f) );

  material redMaterial;
  redMaterial.addComponent( reflectanceComponent(&red, &d) );
  redMaterial.addComponent( reflectanceComponent(&white, &p32) );

  material greenMaterial;
  greenMaterial.addComponent( reflectanceComponent(&green, &d) );
  greenMaterial.addComponent( reflectanceComponent(&white, &p64) );

  material blueMaterial;
  blueMaterial.addComponent( reflectanceComponent(&blue, &d) );
  blueMaterial.addComponent( reflectanceComponent(&white, &p16) );

  material whiteMaterial;
  whiteMaterial.addComponent( reflectanceComponent(&white, &d) );

  triangleMesh redSphere(sphere, redMaterial);
  triangleMesh greenSphere(sphere, greenMaterial);
  triangleMesh blueSphere(sphere, blueMaterial);
  triangleMesh whitePlane(plane, whiteMaterial);

  sceneGraphObject redSphereObject(redSphere);
  sceneGraphObject greenSphereObject(greenSphere);
  sceneGraphObject blueSphereObject(blueSphere);
  sceneGraphObject whitePlaneObject(whitePlane);

  camera cam( vec3d(0.0f, 0.0f, 5.0f),
	      vec3d(0.0f, 0.0f, -1.0f),
	      vec3d(0.0f, 1.0f, 0.0f),
	      35.0f * M_PI / 180.0f,
	      512, 512 );

  directionalLightsource ls( color(1.0f, 1.0f, 1.0f), vec3d(0.0f, -1.0f, -1.0f) );
  directionalLightsource frontal( color(1.0f, 1.0f, 1.0f), vec3d(0.0f, 0.0f, -1.0f) );


  /////////////////////////////////////////////////////

  if((mask & 2) != 0)
  {
    sceneGraphNode sg1;
    sg1.addChildNode(redSphereObject);
  
    std::cout << "Generating Image 1." << std::endl;
    image result1 = generateImage(cam, sg1, ls);
    result1.save("hw4-result1.ppm");
  }


  /////////////////////////////////////////////////////

  if((mask & 4) != 0)
  {
    sceneGraphNode sg2node1(translation3d(vec3d(+1.0f, 0.0f, 0.0f)));
    sg2node1.addChildNode(redSphereObject);

    sceneGraphNode sg2node2(translation3d(vec3d(-1.0f, 0.0f, 0.0f)));
    sg2node2.addChildNode(blueSphereObject);

    sceneGraphNode sg2;
    sg2.addChildNode(sg2node1);
    sg2.addChildNode(sg2node2);

    std::cout << "Generating Image 2." << std::endl;
    image result2 = generateImage(cam, sg2, ls);
    result2.save("hw4-result2.ppm");
  }


  /////////////////////////////////////////////////////

  if((mask & 8) != 0)
  {
    sceneGraphNode sg3node1(scale3d(2.0f, 0.5f, 0.5f));
    sg3node1.addChildNode(greenSphereObject);

    sceneGraphNode sg3;
    sg3.addChildNode(sg3node1);
  
    std::cout << "Generating Image 3." << std::endl;
    image result3 = generateImage(cam, sg3, ls);
    result3.save("hw4-result3.ppm");
  }


  /////////////////////////////////////////////////////

  if((mask & 16) != 0)
  {
    sceneGraphNode sg2node1(translation3d(vec3d(+1.0f, 0.0f, 0.0f)));
    sg2node1.addChildNode(redSphereObject);

    sceneGraphNode sg2node2(translation3d(vec3d(-1.0f, 0.0f, 0.0f)));
    sg2node2.addChildNode(blueSphereObject);

    sceneGraphNode sg2;
    sg2.addChildNode(sg2node1);
    sg2.addChildNode(sg2node2);

    sceneGraphNode sg4node1(rotationY3d(M_PI));
    sg4node1.addChildNode(sg2);

    sceneGraphNode sg4;
    sg4.addChildNode(sg4node1);
  
    std::cout << "Generating Image 4." << std::endl;
    image result4 = generateImage(cam, sg4, ls);
    result4.save("hw4-result4.ppm");
  }


  /////////////////////////////////////////////////////

  if((mask & 32) != 0)
  {
    sceneGraphNode sg5node0(scale3d(0.5f, 0.5f, 0.5f));
    sceneGraphNode sg5node1(rotationY3d(0.5 * M_PI));
    sceneGraphNode sg5node2(rotationX3d(0.5 * M_PI));
    sceneGraphNode sg5node3(translation3d(vec3d(0.0f, 2.0f, 0.0f)));
    sceneGraphNode sg5node4(rotationX3d(0.5 * M_PI));
    sceneGraphNode sg5node5(translation3d(vec3d(0.0f, 2.0f, 0.0f)));
    sceneGraphNode sg5node6(rotationX3d(0.5 * M_PI));
    sceneGraphNode sg5node7(translation3d(vec3d(0.0f, 2.0f, 0.0f)));
    sceneGraphNode sg5node8(rotationX3d(0.5 * M_PI));
    sceneGraphNode sg5node9(translation3d(vec3d(0.0f, 2.0f, 0.0f)));

    sceneGraphNode sg5;
    sg5.addChildNode(sg5node0);
    sg5node0.addChildNode(sg5node1);
    sg5node1.addChildNode(sg5node2);
    sg5node2.addChildNode(sg5node3);
    sg5node3.addChildNode(redSphereObject);
    sg5node3.addChildNode(sg5node4);
    sg5node4.addChildNode(sg5node5);
    sg5node5.addChildNode(greenSphereObject);
    sg5node5.addChildNode(sg5node6);
    sg5node6.addChildNode(sg5node7);
    sg5node7.addChildNode(blueSphereObject);
    sg5node7.addChildNode(sg5node8);
    sg5node8.addChildNode(sg5node9);
    sg5node9.addChildNode(blueSphereObject);
    
    std::cout << "Generating Image 5." << std::endl;
    image result5 = generateImage(cam, sg5, ls);
    result5.save("hw4-result5.ppm");
  }


  /////////////////////////////////////////////////////

  if((mask & 64) != 0)
  {
    sceneGraphNode cubeside( translation3d(vec3d(0.0f, 0.0f, 1.0f)) );
    cubeside.addChildNode(whitePlaneObject);

    sceneGraphNode cubeNode1( rotationY3d(0.0f / 2.0f * M_PI) );
    cubeNode1.addChildNode(cubeside);
    sceneGraphNode cubeNode2( rotationY3d(1.0f / 2.0f * M_PI) );
    cubeNode2.addChildNode(cubeside);
    sceneGraphNode cubeNode3( rotationY3d(2.0f / 2.0f * M_PI) );
    cubeNode3.addChildNode(cubeside);
    sceneGraphNode cubeNode4( rotationY3d(3.0f / 2.0f * M_PI) );
    cubeNode4.addChildNode(cubeside);
    sceneGraphNode cubeNode5( rotationX3d(0.5f * M_PI) );
    cubeNode5.addChildNode(cubeside);
    sceneGraphNode cubeNode6( rotationX3d(-0.5f * M_PI) );
    cubeNode6.addChildNode(cubeside);
    
    sceneGraphNode cube;
    cube.addChildNode(cubeNode1);
    cube.addChildNode(cubeNode2);
    cube.addChildNode(cubeNode3);
    cube.addChildNode(cubeNode4);
    cube.addChildNode(cubeNode5);
    cube.addChildNode(cubeNode6);
    
    sceneGraphNode sg6( rotation3d(0.25*M_PI, vec3d(1.0f, 0.75f, 0.5f)) );
    sg6.addChildNode(cube);
  
    std::cout << "Generating Image 6." << std::endl;
    image result6 = generateImage(cam, sg6, frontal);
    result6.save("hw4-result6.ppm");
  }

  // Done.
  return 0;
}
Пример #22
0
// ===================================================================
void ShapeNode::drawShape()
{
    pthread_mutex_lock(&sceneMutex);

	// remove the old shape:
	if (this->getAttachmentNode()->containsNode(shapeGeode.get()))
	{
		this->getAttachmentNode()->removeChild(shapeGeode.get());
		shapeGeode = NULL;
	}

	// only draw shape if context matches host or context is empty
    // Have to use to a string "NULL" because OSC cannot do empty strings
	//bool drawOnThisHost = (not spinApp::Instance().getContext()->isServer()) 
    //        and (this->getContextString() == getHostname() or this->getContextString() == "NULL");

	bool drawOnThisHost = ((this->getContextString() == spinApp::Instance().getUserID()) or
	                       (this->getContextString() == "NULL"));

	
	// TODO: this should only be added if this application is a graphical renderer.
	// There is no point to actually add the memory of the ShapeDrawable for apps
	// that do not need to use it!
	// ACTUALLY, the server needs to know about the geometry to compute 
	// intersections, determine bounding regions, radius, etc...
	//if (shape && !ignoreOnThisHost)
	if (shape and drawOnThisHost)
	{
		osg::TessellationHints* hints = new osg::TessellationHints;
		hints->setDetailRatio(GENERIC_SHAPE_RESOLUTION);

		if (billboard)
		{
			osg::Billboard *b = new osg::Billboard();
			switch (billboard)
			{
				case POINT_EYE:
					b->setMode(osg::Billboard::POINT_ROT_EYE);
					break;
				case STAY_UP:
					b->setMode(osg::Billboard::AXIAL_ROT);
					b->setAxis(osg::Vec3(0.0f,0.0f,1.0f));
					b->setNormal(osg::Vec3(0.0f,1.0f,0.0f));
					break;
                default:
                    break;
			}
			shapeGeode = b;
			
		} else {
			shapeGeode = new osg::Geode();
		}



		if (shape==PLANE) // OSG doesn't support planes
		{
			shapeGeode->addDrawable(createPlane(AS_UNIT_SCALE * .5, _color));
		}

		else {
			osg::ShapeDrawable *shapeDrawable;
			if (shape==SPHERE)
			{
				shapeDrawable = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),AS_UNIT_SCALE*.5), hints);
			}
			else if (shape==BOX)
			{
				shapeDrawable = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f), AS_UNIT_SCALE), hints);
			}
			else if (shape==CYLINDER)
			{
				shapeDrawable = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,0.0f), AS_UNIT_SCALE*.25,AS_UNIT_SCALE), hints);
			}
			else if (shape==CAPSULE)
			{
				shapeDrawable = new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(0.0f,0.0f,0.0f), AS_UNIT_SCALE*.25,AS_UNIT_SCALE), hints);
			}
			else if (shape==CONE)
			{
				shapeDrawable = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f,0.0f,0.0f), AS_UNIT_SCALE*.25,AS_UNIT_SCALE), hints);
			}
			else {
				return;
			}
			shapeGeode->addDrawable(shapeDrawable);
			shapeDrawable->setColor(_color);
		}




		/*
		switch (shape)
		{
			case SPHERE:
				shapeGeode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),AS_UNIT_SCALE*.5), hints));
				break;
			case BOX:
				shapeGeode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f), AS_UNIT_SCALE), hints));
				break;
			case CYLINDER:
				shapeGeode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,0.0f), AS_UNIT_SCALE*.25,AS_UNIT_SCALE), hints));
				break;
			case CAPSULE:
				shapeGeode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(0.0f,0.0f,0.0f), AS_UNIT_SCALE*.25,AS_UNIT_SCALE), hints));
				break;
			case CONE:
				shapeGeode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f,0.0f,0.0f), AS_UNIT_SCALE*.25,AS_UNIT_SCALE), hints));
				break;
			case PLANE:
				shapeGeode->addDrawable(createPlane(AS_UNIT_SCALE * .5));
				break;
			default:
				shapeGeode = NULL;
		}
		*/

	}


	if (shapeGeode.valid())
	{
		// wireframe test
		//osgFX::Scribe* scribe = new osgFX::Scribe();
		//modelGroup->addChild(scribe);
		//scribe->addChild(shapeGeode.get());
		// end wireframe test
		
		osg::StateSet *ss = shapeGeode->getOrCreateStateSet();
		ss->setMode( GL_BLEND, osg::StateAttribute::ON );
		ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

		// if this shape has a stateset, then lighting is defined there.
		// Otherwise, we have an internal lighting parameter for the shape:
		if (!stateset->s_thing)
		{
			if (lightingEnabled) ss->setMode( GL_LIGHTING, osg::StateAttribute::ON );
			else ss->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
		}
		
		this->getAttachmentNode()->addChild(shapeGeode.get());
		shapeGeode->setName(string(id->s_name) + ".shapeGeode");
		optimizer.optimize(shapeGeode.get()); // ?
	}

	pthread_mutex_unlock(&sceneMutex);

	
	//drawTexture();
	updateStateSet();

}
Пример #23
0
int main (int argc, const char **argv, const char **env) {
  Light *light = malloc(sizeof(Light));

  light->location = vectorCreate(0.0, 10.0, 10.0);
  light->color = vectorCreate(1.0, 1.0, 1.0);
  light->intensity = 1.0;

  Light **lights = calloc(3, sizeof(Light*));

  lights[0] = light;

  light = malloc(sizeof(Light));

  light->location = vectorCreate(0.0, 3.0, 10.0);
  light->color = vectorCreate(1.0, 0.4, 1.0);
  light->intensity = 1.0;

  lights[1] = light;

  Primitive **primitives = calloc(10,sizeof(Primitive *));

  primitives[0] = createSphere((vector){3.0, 7.0, 2.0}, 2.0);
  primitives[1] = createSphere((vector){0.0, 10.0, 1.9}, 1.9);
  primitives[2] = createPlane(vectorCreate(0.0, 0.0, 1.0), vectorCreate(0.0, 0.0, 0.0));

  primitives[0]->material = malloc(sizeof(Material));
  primitives[1]->material = malloc(sizeof(Material));
  primitives[2]->material = malloc(sizeof(Material));

  primitives[0]->material->color = vectorCreate(1.0, 1.0, 1.0);
  primitives[1]->material->color = vectorCreate(1.0, 1.0, 1.0);
  primitives[2]->material->color = vectorCreate(0.0, 0.7, 0.0);

  primitives[0]->material->specular = 0.7;
  primitives[1]->material->specular = 0.3;
  primitives[2]->material->specular = 0.0;

  primitives[0]->material->diffuse = 1.0;
  primitives[1]->material->diffuse = 0.5;
  primitives[2]->material->diffuse = 1.0;

  primitives[0]->material->reflection = 0.5;
  primitives[1]->material->reflection = 0.5;
  primitives[2]->material->reflection = 0.0;
  
  primitives[0]->material->refraction = 0.0;
  primitives[1]->material->refraction = 1.1;
  primitives[2]->material->refraction = 0.0;
  
  primitives[0]->material->transparency = 0.0;
  primitives[1]->material->transparency = 0.7;
  primitives[2]->material->transparency = 0.0;
  
  primitives[3] = createPlane(vectorCreate(0.0, -1.0, 0.0), vectorCreate(0.0, 15.0, 0.0));
  primitives[3]->material = malloc(sizeof(Material));
  
  primitives[3]->material->color = vectorCreate(0.3, 0.2, 7.0);
  
  primitives[3]->material->specular = 0.0;
  primitives[3]->material->diffuse  = 1.0;
  primitives[3]->material->reflection = 0.0;
  primitives[3]->material->refraction = 0.0;
  primitives[3]->material->transparency = 0.0;

  vector camera = vectorCreate(0.0, -5.0, 5.0);

  png_uint_32 height = HEIGHT, width = WIDTH;

  png_bytepp imageBuffer = malloc(sizeof(png_bytep) * height);
  for (png_uint_32 i = 0; i < height; i++) {
    imageBuffer[i] = malloc(sizeof(png_byte) * 3 * width);
    for (png_uint_32 j = 0; j < width * 3; j++) {
      imageBuffer[i][j] = 0;
    }
  }
  
  int x,y;

  for (y = 0; y < HEIGHT; y++) {
    
    for (x = 0; x < WIDTH; x++) {
      Line *ray = createLine(camera, x, y);

      vector color = rayTrace(ray, primitives, lights, 1.0);
      
      free(ray);
      
      if (color.x >= 1.0) color.x = 1.0;
      if (color.y >= 1.0) color.y = 1.0;
      if (color.z >= 1.0) color.z = 1.0;
      
      imageBuffer[y][x * 3] = color.x * 255;
      imageBuffer[y][x * 3 + 1] = color.y * 255;
      imageBuffer[y][x * 3 + 2] = color.z * 255;
    }
  
  }
  
  writePNG(imageBuffer, width, height, 0);
    
  for (png_uint_32 i = 0; i < height; i++) {
    free(imageBuffer[i]);
  }    
  free(imageBuffer);

  return 0;
}