Exemplo n.º 1
0
int main(int, char **)
{
    // construct the viewer.
    osgViewer::Viewer viewer;

    // add model to viewer.
    viewer.setSceneData( createShapes() );

    return viewer.run();
}
Exemplo n.º 2
0
  TinkertoyWorldNode(const dart::simulation::WorldPtr& world)
    : dart::gui::osg::RealTimeWorldNode(world),
      mForceCoeff(DefaultForceCoeff),
      mWasSimulating(false)
  {
    mTarget = dart::gui::osg::InteractiveFrame::createShared(
        dart::dynamics::Frame::World());
    getWorld()->addSimpleFrame(mTarget);

    createShapes();
    createInitialToy1();
    createInitialToy2();
    createForceLine();
  }
Exemplo n.º 3
0
int main(int argc, char **argv)
{
    osg::ArgumentParser arguments(&argc,argv);

    // construct the viewer.
    osgViewer::Viewer viewer(arguments);

    // add model to viewer.
    viewer.setSceneData( createShapes(arguments) );

    // add the state manipulator
    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );

    return viewer.run();
}
Exemplo n.º 4
0
void ShapeFinder::start(int newN) {
	int startTime = std::clock();

	std::vector<std::vector<std::vector<bool>>> unique = {};
	std::vector<std::vector<std::vector<bool>>> allRots = {};
	n = std::min(10, std::max(1, newN));

	printf("Shape Finder Started with %i tiles\n", n);

	for (int x = 0; x < n; x++) {
		blank.push_back({});
		for (int y = 0; y < n; y++) {
			blank[x].push_back(false);
		}
	}

	std::vector<std::vector<bool>> grid = blank;
	grid[floor((float)n / 2 - 0.5)][floor((float)n / 2 - 0.5)] = true;

	createShapes(grid, &unique, &allRots);

	printf("n = %i\n", unique.size());

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

		for (int y = 0; y < n; y++) {
			for (int x = 0; x < n; x++) {
				printf("%c", unique[i][y][x] ? 219 : ' ');
			}
			printf("\n");
		}
		printf("\n");

	}

	printf("n = %i\n", unique.size());
	printf("%ims passed\n", std::clock() - startTime);
}
int main(int argc, char *argv[])
#endif
{
#ifdef USE_SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        report("Can't initialize SDL\n");
        return 1;
    }
// TODO fullscreen + get screen size
#ifndef __ANDROID__
    width=height=700;
#else
    width=320;
    height=480;
//     const SDL_VideoInfo* vinfo=SDL_GetVideoInfo();    
//     width = vinfo->current_w;
//     height = vinfo->current_h;
//     report("Detected %dx%d resolution.\n",width,height);
#endif

    window = SDL_CreateWindow("Bezier Fragment Shader Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

    if(window == NULL)
    {
        report("Can't create window: %s\n", SDL_GetError());
        return -1;
    }

    glcontext = SDL_GL_CreateContext(window);

    if(glcontext == NULL)
    {
        report("Can't create context: %s\n", SDL_GetError());
        return -1;
    }

    SDL_GL_MakeCurrent(window, glcontext);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
#else
    glutInit(&argc,argv);
    glutInitWindowSize(700,700);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Bezier Fragment Shader Demo");
#endif
    

    glViewport(0, 0, width, height);

    glClearColor(0.0, 0.0, 0.0, 0.0);

    glEnable(GL_DEPTH_TEST);

    fshader = loadShader("bezier.glsl",GL_FRAGMENT_SHADER);
    vshader = loadShader("bezier-vertex.glsl",GL_VERTEX_SHADER);

    if (!(fshader&&vshader))
    {
        report("One of shaders failed, aborting.\n");
        return -1;
    }

    program = glCreateProgram();
    glAttachShader(program, fshader);
    glAttachShader(program, vshader);
    glBindAttribLocation(program, 0, "vertexPos");
    glBindAttribLocation(program, 1, "bezCoordsAttr");
    glLinkProgram(program);

    glGetProgramiv(program, GL_LINK_STATUS, &linked);

    if (!linked)
    {
        report("Can't link the shader\n");
        return -1;
    }

    glUseProgram(program);

    glUniform1i(glGetUniformLocation(program, "drawFill"), 1);
    glUniform1i(glGetUniformLocation(program, "useBezier"), 1);
    glUniform1i(glGetUniformLocation(program, "drawStroke"), 1);

#ifndef __ANDROID__
//     glEnableClientState(GL_VERTEX_ARRAY); // Why don't they work like glEnable(A|B) did before? or am I dumb?
//     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
#endif

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    createShapes();
    
#ifdef USE_SDL
    int running = 1;
    timemark=SDL_GetTicks();

    while(running)
    {
        if ( SDL_PollEvent(&event) > 0 )
        {
//         SDL_WaitEvent(&event);

            switch(event.type)
            {
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        #ifdef __ANDROID__
                        case SDLK_AC_SEARCH:
                        #endif  
                        case SDLK_F1: performanceReport(); break;
                        
                        #ifndef __ANDROID__
                            case SDLK_ESCAPE: running = 0;          break;
                        #else
                            case SDLK_AC_BACK: running = 0;         break;
                        #endif

                        case SDLK_LEFT:   Camera.beta += M_PI / 36; camera(); break;
                        case SDLK_RIGHT:  Camera.beta -= M_PI / 36; camera(); break;
                        case SDLK_UP:    Camera.alpha += M_PI / 36; camera(); break;
                        case SDLK_DOWN:  Camera.alpha -= M_PI / 36; camera(); break;
                        default: keyb(event.key.keysym.scancode);   break;
                    }
                break;

                case SDL_MOUSEWHEEL:
                    Camera.dist*= (event.wheel.y < 0)? 1.1 : 0.9;
                    camera();
                break;

                case SDL_MOUSEMOTION:
                    if(event.motion.state == 1) motion(event.motion.xrel, event.motion.yrel);
                break;

                // Note, the first frame flickers, TODO workaround
                // TODO: track the real sequence of WINDOWEVENT_ENTER and WINDOWEVENT_SIZE_CHANGED events 
                case SDL_WINDOWEVENT:
                    if (event.window.event==SDL_WINDOWEVENT_SIZE_CHANGED)
                        size(event.window.data1, event.window.data2);
                    camera();
                break;

                // handle touch events here

                case SDL_QUIT:
                    running = 0;
                break;
            }
        }

        draw();
    }
    
    performanceReport();
    
    SDL_GL_MakeCurrent(NULL, NULL);
    SDL_GL_DeleteContext(glcontext);
    SDL_DestroyWindow(window);
    SDL_Quit();
#else
    glutReshapeFunc(size);
    glutDisplayFunc(draw);
    glutIdleFunc(draw);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutSpecialFunc(keybs);
    glutKeyboardFunc(keyb);
    glutTimerFunc(TIMER_RESOLUTION,timer,0);
    glutMainLoop();
#endif
    
    return 0;
}
WorldLinearCastMultithreadedDemo::WorldLinearCastMultithreadedDemo(hkDemoEnvironment* env)
	: hkDefaultPhysicsDemo(env),
	m_time(0),
	m_semaphore(0,1000)
{
	const WorldLinearCastMultithreadedDemoVariant& variant = g_WorldLinearCastMultithreadedDemoVariants[m_variantId];

	//
	// Setup the camera.
	//
	{
		hkVector4 from(0.0f, 10.0f, 30.0f);
		hkVector4 to  (0.0f, 0.0f, 0.0f);
		hkVector4 up  (0.0f, 1.0f, 0.0f);
		setupDefaultCameras(env, from, to, up);
	}

	//
	// Create the world.
	//
	{
		hkpWorldCinfo info;
		{
			info.m_gravity.set(0.0f, -9.81f, 0.0f);	
			info.setBroadPhaseWorldSize( 100.0f );
			info.m_collisionTolerance = .001f;
			info.m_simulationType = hkpWorldCinfo::SIMULATION_TYPE_MULTITHREADED;
		}

		m_world = new hkpWorld(info);
		m_world->lock();

		//	Register collision agents
		hkpAgentRegisterUtil::registerAllAgents(m_world->getCollisionDispatcher());

		setupGraphics();
	}

	
	//
	// Create the underlying landscape
	//
	hkAabb aabb;
	{
		hkVector4 scaling(1.0f, 0.3f, 1.0f );
		m_groundShapeFactory.setScaling( scaling );
		m_groundShapeFactory.setTriangleRadius( 0.0f );

		hkpMoppBvTreeShape* groundShape = m_groundShapeFactory.createMoppShapeForSpu();
		{
			hkpRigidBodyCinfo groundInfo;
			groundInfo.m_shape = groundShape;
			groundInfo.m_motionType = hkpMotion::MOTION_FIXED;
			hkpRigidBody* b = new hkpRigidBody(groundInfo);
			
			b->getCollidable()->getShape()->getAabb( b->getTransform(), 0.0f, aabb );

			m_world->addEntity(b);
			b->removeReference();
		}
		groundShape->removeReference();
	}

	//	Create a selection of shapes
	hkArray<hkpShape*> shapes;
	createShapes( shapes );

	// We would like to ensure that each object is placed on the surface of the landscape such
	// that it is not penetrating and is resting on the surface. To do this we make use of world 
	// linear casting which will allow us to shape cast our objects against the landscape.
	// Using the results of this cast we can easily position our objects in the desired manner.
	{
		hkPseudoRandomGenerator random(501);
		int xRes = 8;
		int yRes = 8;
		for (int i = 0; i < xRes * yRes; i ++ )
		{
			// create a fixed rigid body info with a random orientation
			hkpRigidBodyCinfo ci;
			ci.m_shape = shapes[ random.getRandChar( shapes.getSize() ) ];
			ci.m_motionType = hkpMotion::MOTION_FIXED;
			random.getRandomRotation( ci.m_rotation );

			// place it above the plane
			ci.m_position.set(  (1.8f * aabb.m_max(0)) * (-.5f + (i%xRes)/ float(xRes)), 
								aabb.m_max(1) + 10.0f,
								(1.8f * aabb.m_max(2)) * (-.5f + (i/xRes)/ float(yRes))  );
			
			// create the rigid body
			hkpRigidBody* b = new hkpRigidBody(ci);


			// To perform the linear casting we use a hkpLinearCastInput structure and
			// set the end position of the cast (the start position is given by the
			// location of our object). We then create a hkpClosestCdPointCollector
			// to gather the results of the cast and then ask the world to perform
			// the cast:
			{
				hkpLinearCastInput li;
				li.m_to.set( ci.m_position(0), aabb.m_min(1), ci.m_position(2) );

				hkpClosestCdPointCollector hitCollector;
				m_world->linearCast( b->getCollidable(), li, hitCollector );

				//	Check for hits
				if ( hitCollector.hasHit() )
				{
					hkVector4 position; position.setInterpolate4( ci.m_position, li.m_to, hitCollector.getHitContact().getDistance() );
					b->setPosition( position );
				}
			}

			m_world->addEntity( b );
			m_rocksOnTheFloor.pushBack( b );
		}
	}

	hkPseudoRandomGenerator generator(3245);

	// Create query objects and phantoms from shapes
	buildQueryObects( shapes, m_queryObjects );


	//	Remove shape references
	{
		for (int i = 0; i < shapes.getSize(); i++)
		{ 
			shapes[i]->removeReference();
			shapes[i] = 0;
		}
	}

	m_world->unlock();

	hkpCollisionQueryJobQueueUtils::registerWithJobQueue(m_jobQueue);

	// Special case for this demo variant: we do not allow the # of active SPUs to drop to zero as this can cause a deadlock.
	if ( variant.m_demoType == WorldLinearCastMultithreadedDemoVariant::MULTITHREADED_ON_SPU ) m_allowZeroActiveSpus = false;

	// register the default addCdPoint() function; you are free to register your own implementation here though
	hkpFixedBufferCdPointCollector::registerDefaultAddCdPointFunction();

}
Exemplo n.º 7
0
void ShapeFinder::createShapes(std::vector<std::vector<bool>> grid, std::vector<std::vector<std::vector<bool>>>* unique, std::vector<std::vector<std::vector<bool>>>* allRots) {
	if (unique->size() >= counts[n - 1])
		return;

	int relTiles[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
	std::vector<std::pair<int, int>> tiles;

	for (int x = 0; x < n; x++) {
		for (int y = 0; y < n; y++) {
			if (grid[y][x] == true) {
				for (int i = 0; i < 4; i++) {

					if (x + relTiles[i][0] >= 0 && x + relTiles[i][0] < n) {
						if (y + relTiles[i][1] >= 0 && y + relTiles[i][1] < n) {
							if (!grid[y + relTiles[i][1]][x + relTiles[i][0]]) {

								bool stop = false;
								for (int v = 0; v < tiles.size(); v++) {
									if (tiles[v].second == x + relTiles[i][0] && tiles[v].first == y + relTiles[i][1]) {
										stop = true;
										break;
									}
								}

								if (!stop) {
									tiles.push_back({ y + relTiles[i][1], x + relTiles[i][0] });
								}
							}
						}
					}
				}
			}
		}
	}
	
	for (int i = 0; i < tiles.size(); i++) {
		std::vector<std::vector<bool>> newGrid = grid;
		newGrid[tiles[i].first][tiles[i].second] = true;

		int tileNumber = 0;
		for (int x = 0; x < n; x++) {
			for (int y = 0; y < n; y++) {
				if (newGrid[y][x])
					tileNumber++;
			}
		}

		if (tileNumber == n) {
			int topmostTile = n, leftmostTile = n;

			for (int y = 0; y < n; y++) {
				for (int x = 0; x < n; x++) {
					if (newGrid[y][x]) {
						if (x < leftmostTile)
							leftmostTile = x;
						if (y < topmostTile)
							topmostTile = y;
					}
				}
			}

			for (int y = topmostTile; y < n; y++) {
				for (int x = leftmostTile; x < n; x++) { 
					if (newGrid[y][x]) {
						newGrid[y][x] = false;
						newGrid[y - topmostTile][x - leftmostTile] = true;
					}
				}
			}

			bool stop = false;
			for (int ii = 0; ii < allRots->size(); ii++) {
				if (newGrid == (*allRots)[ii]) {
					stop = true;
					break;
				}
			}

			if (stop) continue;

			unique->push_back(newGrid);
			allRots->push_back(newGrid);

			printf("Unique Shapes Found: %i\n", unique->size());

			for (int ii = 0; ii < 3; ii++)
				allRots->push_back(rotate(newGrid, ii));
		} else {
			createShapes(newGrid, unique, allRots);
		}
	}
}
Exemplo n.º 8
0
ShapeQueryDemo::ShapeQueryDemo(hkDemoEnvironment* env)
	:	hkDefaultPhysicsDemo(env), m_time(0)
{

	// Setup the camera.
	{
		hkVector4 from(0.0f, 10.0f, 30.0f);
		hkVector4 to  (0.0f, 0.0f, 0.0f);
		hkVector4 up  (0.0f, 1.0f, 0.0f);
		setupDefaultCameras(env, from, to, up);
	}

	// Create the world.
	{
		hkpWorldCinfo info;
		
		info.m_gravity.set(0.0f, -9.81f, 0.0f);	
		info.setBroadPhaseWorldSize( 100.0f );
		info.m_collisionTolerance = .001f;
		m_world = new hkpWorld(info);
		m_world->lock();

		setupGraphics();
	}

	//	Register collision agents
	hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() );

	
	//
	// Create the underlying landscape
	//
	hkAabb aabb;
	{
		hkVector4 scaling(1.0f, 0.3f, 1.0f );
		m_groundShapeFactory.setScaling( scaling );
		m_groundShapeFactory.setTriangleRadius( 0.0f );

		hkpMoppBvTreeShape* groundShape = m_groundShapeFactory.createMoppShape();
		{
			hkpRigidBodyCinfo groundInfo;
			groundInfo.m_shape = groundShape;
			groundInfo.m_motionType = hkpMotion::MOTION_FIXED;
			hkpRigidBody* b = new hkpRigidBody(groundInfo);
			
			b->getCollidable()->getShape()->getAabb( b->getTransform(), 0.0f, aabb );

			m_world->addEntity(b);
			b->removeReference();
		}
		groundShape->removeReference();
	}

	//	Create a selection of shapes
	hkArray<hkpShape*> shapes;
	createShapes( shapes );

	// We would like to ensure that each object is placed on the surface of the landscape such
	// that it is not penetrating and is resting on the surface. To do this we make use of world 
	// linear casting which will allow us to shape cast our objects against the landscape.
	// Using the results of this cast we can easily position our objects in the desired manner.
	{
		hkPseudoRandomGenerator random(501);
		int xRes = 8;
		int yRes = 8;
		for (int i = 0; i < xRes * yRes; i ++ )
		{
			// create a fixed rigid body info with a random orientation
			hkpRigidBodyCinfo ci;
			ci.m_shape = shapes[ random.getRandChar( shapes.getSize() ) ];
			ci.m_motionType = hkpMotion::MOTION_FIXED;
			random.getRandomRotation( ci.m_rotation );

			// place it above the plane
			ci.m_position.set(  (1.8f * aabb.m_max(0)) * (-.5f + (i%xRes)/ float(xRes)), 
								aabb.m_max(1) + 10.0f,
								(1.8f * aabb.m_max(2)) * (-.5f + (i/xRes)/ float(yRes))  );
			
			// create the rigid body
			hkpRigidBody* b = new hkpRigidBody(ci);


			// To perform the linear casting we use a hkpLinearCastInput structure and
			// set the end position of the cast (the start position is given by the
			// location of our object). We then create a hkpClosestCdPointCollector
			// to gather the results of the cast and then ask the world to perform
			// the cast:
			{
				hkpLinearCastInput li;
				li.m_to.set( ci.m_position(0), aabb.m_min(1), ci.m_position(2) );

				hkpClosestCdPointCollector hitCollector;
				m_world->linearCast( b->getCollidable(), li, hitCollector );

				//	Check for hits
				if ( hitCollector.hasHit() )
				{
					hkVector4 position; position.setInterpolate4( ci.m_position, li.m_to, hitCollector.getHitContact().getDistance() );
					b->setPosition( position );
				}
			}

			m_world->addEntity( b );
			m_fixedSmallBodies.pushBack( b );
		}
	}

	hkPseudoRandomGenerator generator(3245);

	// Create query objects and phantoms from our shapes
	if (m_variantId == 0)
	{
		// Only AABB phantoms are needed for the raycast variant
		buildAabbPhantoms( m_world, 10, m_rayCastPhantoms, generator );
	}
	else
	{
		// Create query objects and phantoms from shapes
		buildQueryObects( shapes, m_queryObjects );
		buildSimplePhantoms( m_world, shapes, m_simplePhantoms, generator );
		buildCachingPhantoms( m_world, shapes, m_cachingPhantoms, generator );
	}


	//	Remove shape references
	{
		for (int i = 0; i < shapes.getSize(); i++)
		{ 
			shapes[i]->removeReference();
			shapes[i] = 0;
		}
	}

	m_world->unlock();
}