Exemple #1
0
// move object o in the direction it's facing
void moveObject(object *o) {

	// If object is ship, adjust for player controls
	if (o->type == SHIP) {

		// Acceleration
		if (o->state & OBJ_ACCELERATING) {	
			vector acc = scaleVec(&(o->dir), o->spd*dtheta); 
			if (lengthVec(&acc) < MAXSPEED) 
				o->vel = addVec(&(o->vel), &acc);
		}

		// Turning
		if (o->state & OBJ_TURNING_LEFT) {
			turnObject(o, o->turn*dtheta);
		} else if (o->state & OBJ_TURNING_RIGHT) {
			turnObject(o, o->turn*-dtheta);
		}
	}
	
	// If object is gib, rotate it
	if (o->type == GIB) turnObject(o, o->turn*dtheta);

	// translate object and wrap its position if necessary
	vector x = scaleVec(&(o->vel), dtheta);
	o->pos = addVec(&(o->pos), &x);
	wrapPosition(o);
}
Exemple #2
0
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//Model Matrix
	Angel::mat4 model_ = mat4(1.0);
	Angel::vec3 scaleVec(scale_size,scale_size,scale_size);
	//Angel::vec3 transVec(4.0,0.0,0.0);
	Angel::vec3 transVec(u_disp,v_disp,0.0);
	Angel::mat4 scaleMat = Scale(scaleVec);
	Angel::mat4 xRotMat = RotateY(x_angle);
	Angel::mat4 yRotMat = RotateX(y_angle);
	Angel::mat4 transMat = Translate(transVec);
	model_ = scaleMat*xRotMat*yRotMat*transMat;

	//Viewing Matrix
	point4 at(0.0,0.0,0.0,1.0);
	point4 eye(0.0,5.0,3.0,1.0);
	vec4 up(0.0,0.0,1.0,0.0);
	mat4 view_ = LookAt(eye,at,up);

	mat4 modelView_ = model_*view_;
	glUniformMatrix4fv(ModelView,1,GL_TRUE,modelView_);

	glDrawArrays(GL_TRIANGLE_STRIP,0,numVertices);
	
	glutSwapBuffers();
}
Exemple #3
0
void RasterNode::blt(const glm::mat4& transform, const glm::vec2& destSize)
{
    GLContext* pContext = GLContext::getCurrent();
    FRect destRect;
    
    StandardShaderPtr pShader = pContext->getStandardShader();
    float opacity = getEffectiveOpacity();
    pContext->setBlendColor(glm::vec4(1.0f, 1.0f, 1.0f, opacity));
    pShader->setAlpha(opacity);
    if (m_pFXNode) {
        pContext->setBlendMode(m_BlendMode, true);
        m_pFXNode->getTex()->activate(GL_TEXTURE0);
        pShader->setColorModel(0);
        pShader->disableColorspaceMatrix();
        pShader->setGamma(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
        pShader->setPremultipliedAlpha(true);
        pShader->setMask(false);

        FRect relDestRect = m_pFXNode->getRelDestRect();
        destRect = FRect(relDestRect.tl.x*destSize.x, relDestRect.tl.y*destSize.y,
                relDestRect.br.x*destSize.x, relDestRect.br.y*destSize.y);
    } else {
        m_pSurface->activate(getMediaSize());
        pContext->setBlendMode(m_BlendMode, m_pSurface->isPremultipliedAlpha());
        destRect = FRect(glm::vec2(0,0), destSize);
    }
    glm::vec3 pos(destRect.tl.x, destRect.tl.y, 0);
    glm::vec3 scaleVec(destRect.size().x, destRect.size().y, 1);
    glm::mat4 localTransform = glm::translate(transform, pos);
    localTransform = glm::scale(localTransform, scaleVec);
    pShader->setTransform(localTransform);
    pShader->activate();
    m_pSubVA->draw();
}
float Box2DSprite::getCorrectedWidth(){
	glm::vec3 scaleVec(1);
	if(parents.size() > 0){
		scaleVec = childTransform->getScaleVector();
	}
	return width * std::abs(scaleVec.x)*scale;
}
Exemple #5
0
void Objects::insertBegin(const MWWorld::Ptr& ptr)
{
    osg::ref_ptr<osg::Group> cellnode;

    CellMap::iterator found = mCellSceneNodes.find(ptr.getCell());
    if (found == mCellSceneNodes.end())
    {
        cellnode = new osg::Group;
        mRootNode->addChild(cellnode);
        mCellSceneNodes[ptr.getCell()] = cellnode;
    }
    else
        cellnode = found->second;

    osg::ref_ptr<SceneUtil::PositionAttitudeTransform> insert (new SceneUtil::PositionAttitudeTransform);
    cellnode->addChild(insert);

    insert->getOrCreateUserDataContainer()->addUserObject(new PtrHolder(ptr));

    const float *f = ptr.getRefData().getPosition().pos;

    insert->setPosition(osg::Vec3(f[0], f[1], f[2]));

    const float scale = ptr.getCellRef().getScale();
    osg::Vec3f scaleVec(scale, scale, scale);
    ptr.getClass().adjustScale(ptr, scaleVec, true);
    insert->setScale(scaleVec);

    ptr.getRefData().setBaseNode(insert);
}
float Box2DSprite::getCorrectedHeight(){
	glm::vec3 scaleVec(1);
	if(parents.size() > 0){
		scaleVec = childTransform->getScaleVector();
	}
	return height * std::abs(scaleVec.y)*scale;
}
Exemple #7
0
void StGeometryTest::stglDraw(unsigned int ) {
    StGLContext& aCtx = getContext();
    GLint aViewPort[4];
    aCtx.core20fwd->glGetIntegerv(GL_VIEWPORT, aViewPort);
    StGLVec4 transVec(1.0f / (GLfloat )aViewPort[2],
                      1.0f / (GLfloat )aViewPort[3],
                      0.0f, 0.0f);
    StGLVec4 scaleVec(1.0f - 2.0f * transVec.x(),
                      1.0f - 2.0f * transVec.y(),
                      1.0f, 1.0f);

    aCtx.core20fwd->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    aCtx.core20fwd->glEnable(GL_BLEND);
    stProgram.use(aCtx);
    stProgram.setScaleTranslate(aCtx, scaleVec, transVec);

    myGrid.draw(aCtx, stProgram);
    for(size_t aCircleId = 0; aCircleId < 5; ++aCircleId) {
        myCircles[aCircleId].draw(aCtx, stProgram);
    }
    myColors.draw(aCtx, stProgram);
    myBrightness.draw(aCtx, stProgram);

    stProgram.unuse(aCtx);
    aCtx.core20fwd->glDisable(GL_BLEND);
}
Exemple #8
0
// Create asteroid (called at start and whenever an asteroid is destroyed)
void initAsteroid(object *o, vector *p, float r) {
	object A;
	A.verts = (vector*)malloc(sizeof(vector)*13);
	int i;
	for(i = 0; i < 12; i++) {
		vector vi = makeVec(cos(i*pi/6.0), sin(i*pi/6.0));
		A.verts[i] = scaleVec(&vi, r*(0.75+0.25*cos(rand())));
	}
	A.verts[i] = A.verts[0];
	A.size = 13;
	A.pos = copyVec(p);
	A.dir = upVec();
	A.ang = (float)(rand()%10);
	A.turn = 2.0;
	vector vel = makeVec(sin((float)rand()), cos((float)rand()));	
	A.vel = scaleVec(&vel, 2.0+cos(rand()));
	A.spd = 5.0;
	A.type = ASTEROID;
	A.mode = GL_LINE_LOOP;
	A.state = ASTEROID_ACTIVE;
	A.radius = r;
	*o = A;
}
void 
SoXipOverlayTransformBoxManip::scale( SoHandleEventAction* action )
{
	// Extract the rotation matrix from the view matrix
	SbMatrix rotationMatrix = extractRotationMatrix( mXBoundingBox.getInverse() );

	// Get the two control points involved in the computation of the scale
	// matrix (the one initially picked by the user and its opposit control
	// point.
	const SbVec3f& cp3d = mControlPointsCoords->point[ mControlPointId ];
	const SbVec3f& opp_cp3d = mControlPointsCoords->point[ (mControlPointId + 4) % 8 ];

	// Get the projection in the world of the current mouse position 
	SbVec3f p3d;
	getPoint( action, p3d );

	// Project the control points and the mouse world position on a xy plane
	SbVec3f p2d, cp2d, opp_cp2d;
	rotationMatrix.multVecMatrix( cp3d, cp2d );
	rotationMatrix.multVecMatrix( opp_cp3d, opp_cp2d );
	rotationMatrix.multVecMatrix( p3d, p2d );

	SbVec3f refVec = cp2d - opp_cp2d;
	SbVec3f movVec = p2d - opp_cp2d;	

	SbVec3f scaleVec(1, 1, 1);
	for( int i = 0; i < 3; ++ i )
	{
		if( fabs( refVec[i] ) > 0.001 )
			scaleVec[i] = movVec[i] / refVec[i];
	}

	// Compute the 2D scale matrix
	SbMatrix scaleMatrix2D;
	scaleMatrix2D.setScale( scaleVec );

	// Use the opposit control point to center the shape (scale only regarding
	// the picked control point)
	SbMatrix centerMatrix;
	centerMatrix.setTranslate( opp_cp3d );

	mTransformationMatrix =  centerMatrix.inverse()		// Translate a point to the origin
		* rotationMatrix								// Rotate the plane to a xy aligned plane
		* scaleMatrix2D									// Scale the point in this plane
		* rotationMatrix.inverse()						// Rotate back to initial plane
		* centerMatrix;									// Undo the first translation

	transform( mActionNode );
}
Exemple #10
0
void Actor::updateScale()
{
    float scale = mPtr.getCellRef().getScale();
    osg::Vec3f scaleVec(scale,scale,scale);

    mPtr.getClass().adjustScale(mPtr, scaleVec, false);
    mScale = scaleVec;
    mShape->setLocalScaling(toBullet(mScale));

    scaleVec = osg::Vec3f(scale,scale,scale);
    mPtr.getClass().adjustScale(mPtr, scaleVec, true);
    mRenderingScale = scaleVec;

    updateCollisionObjectPosition();
}
Exemple #11
0
// Create laser shot (called each time a laser is shot)
void initLazor(object *o, object *ship) {
	object laser;
	vector* v = (vector*)malloc(sizeof(vector)*2);
	laser.verts = v;
	v->x = 0.0; v->y = 0.0;v++;
	v->x = 0.0; v->y = -0.5;
	laser.size = 2;
	laser.pos = copyVec(&(ship->pos));
	laser.dir = copyVec(&(ship->dir));
	laser.ang = ship->ang;
	laser.turn = 0.0;
	laser.vel = scaleVec(&(ship->dir), 30.0);
	laser.spd = 50.0;
	laser.type = LAZOR;
	laser.mode = GL_LINE_LOOP;
	laser.state = LAZER_ACTIVE;
	*o = laser;
}
Exemple #12
0
void Text3D::computePositions(unsigned int contextID) const
{
    if (_font.valid() == false) return;

    switch(_alignment)
    {
    case LEFT_TOP:      _offset.set(_textBB.xMin(),_textBB.yMax(),_textBB.zMin()); break;
    case LEFT_CENTER:   _offset.set(_textBB.xMin(),(_textBB.yMax()+_textBB.yMin())*0.5f,_textBB.zMin()); break;
    case LEFT_BOTTOM:   _offset.set(_textBB.xMin(),_textBB.yMin(),_textBB.zMin()); break;

    case CENTER_TOP:    _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,_textBB.yMax(),_textBB.zMin()); break;
    case CENTER_CENTER: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,(_textBB.yMax()+_textBB.yMin())*0.5f,_textBB.zMin()); break;
    case CENTER_BOTTOM: _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,_textBB.yMin(),_textBB.zMin()); break;

    case RIGHT_TOP:     _offset.set(_textBB.xMax(),_textBB.yMax(),_textBB.zMin()); break;
    case RIGHT_CENTER:  _offset.set(_textBB.xMax(),(_textBB.yMax()+_textBB.yMin())*0.5f,_textBB.zMin()); break;
    case RIGHT_BOTTOM:  _offset.set(_textBB.xMax(),_textBB.yMin(),_textBB.zMin()); break;

    case LEFT_BASE_LINE:  _offset.set(0.0f,0.0f,0.0f); break;
    case CENTER_BASE_LINE:  _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,0.0f,0.0f); break;
    case RIGHT_BASE_LINE:  _offset.set(_textBB.xMax(),0.0f,0.0f); break;

    case LEFT_BOTTOM_BASE_LINE:  _offset.set(0.0f,-_characterHeight*(_lineCount-1),0.0f); break;
    case CENTER_BOTTOM_BASE_LINE:  _offset.set((_textBB.xMax()+_textBB.xMin())*0.5f,-_characterHeight*(_lineCount-1),0.0f); break;
    case RIGHT_BOTTOM_BASE_LINE:  _offset.set(_textBB.xMax(),-_characterHeight*(_lineCount-1),0.0f); break;
    }

    AutoTransformCache& atc = _autoTransformCache[contextID];
    osg::Matrix& matrix = atc._matrix;


    float scale = _font->getScale();
    osg::Vec3 scaleVec(scale * _characterHeight, scale * _characterHeight / _characterAspectRatio, _characterDepth);
    matrix.makeTranslate(-_offset);
    matrix.postMultScale(scaleVec);
    matrix.postMultRotate(_rotation);
    matrix.postMultTranslate(_position);


    _normal = osg::Matrix::transform3x3(osg::Vec3(0.0f,0.0f,1.0f),matrix);
    _normal.normalize();

    const_cast<Text3D*>(this)->dirtyBound();
}
Exemple #13
0
// Create gibs (called each time an object is destroyed)
// oa: array of objects to put gibs into
// o: object to create gibs form
// flags: state to begin gibs in
void initGib(object *oa, object *o, int flags) {
	int i;
	for (i=0; i<o->size; i++) {
		object gib;
		vector *gv = (vector*)malloc(sizeof(vector)*2);
		gib.verts = gv;
		
		gv->x = o->verts[i].x; gv->y = o->verts[i].y; gv++;
		if (i==o->size-1) {
			gv->x = o->verts[0].x; gv->y = o->verts[0].y;
		} else {
			gv->x = o->verts[i+1].x; gv->y = o->verts[i+1].y;
		}
		vector mid = makeVec((gib.verts[0].x+gib.verts[1].x)/2.0,
						(gib.verts[0].y+gib.verts[1].y)/2.0);
		
		gib.pos = localToWorld(&mid, o);
		
	//	vector dv = subVec(&(gib.pos), &(o->pos));
		gib.verts[0] = subVec(&mid, &(gib.verts[0]));
		gib.verts[1] = subVec(&mid, &(gib.verts[1]));
		gib.size = 2;
		gib.dir = upVec();
		gib.ang = o->ang;
		gib.turn = 1.0+0.5*cos((float)rand());
		vector vel = makeVec(sin((float)rand()), cos((float)rand()));	
		gib.vel = scaleVec(&vel, 0.2+cos(rand()));
		gib.spd = 2.0;
		gib.type = GIB;
		gib.mode = GL_LINES;
		gib.state = flags;
		gib.lifetime = 0.5;
		oa[i] = gib;
	}	

}
Exemple #14
0
vector fromWorld(vector *v) {return scaleVec(v, 1.0/ZOOM);}
Exemple #15
0
vector toWorld(vector *v) {return scaleVec(v, ZOOM);}
Exemple #16
0
/*------------------------------------------------------------------
- Config file format - simplify, don't need xml, but like the structure
{
    "scalars" : {
        "nq" : 3,
        "lrgs" : 4,
        "print" : true
        "t" : 10.0,
        "dt" : 0.1
    },
    "coefficients" : {
        "alpha" : [0.112, 0.234, 0.253],
        "beta" : [0.453, 0.533, -0.732, 0.125, -0.653, 0.752],
        "delta" : [1.0, 1.0, 1.0]
    }
}
------------------------------------------------------------------*/
int main( int argc, char **argv ){
    double *hz, *hhxh;     /* hamiltonian components */
    double *al, *be, *de; 
    fftw_complex *psi;   /* State vector */
    fftw_complex factor;
    double T = 10.0, dt = 0.1;
    uint64_t i, j, k, bcount;
    uint64_t nQ=3, N, L=4, dim;
    int *fft_dims, prnt=0;
    uint64_t testi, testj;
    int dzi, dzj; //TODO: consider using smaller vars for flags and these
    fftw_plan plan;
    
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                         Parse configuration file
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    //TODO: going to need logic to handle incomplete config files
    if( argc < 2 ){
        fprintf( stderr, "Need a json configuration file. Terminating...\n" );
        return 1;
    }

    /* Parse file and populate applicable data structures */
    {
        JSON_Value *root_value = NULL;
        JSON_Object *root_object;
        JSON_Array *array;

        root_value = json_parse_file_with_comments( argv[1] );
        root_object = json_value_get_object( root_value );

        nQ = (uint64_t) json_object_dotget_number( root_object, "scalars.nq" );
        prnt = json_object_dotget_boolean( root_object, "scalars.print" );
        L = (uint64_t) json_object_dotget_number( root_object, "scalars.lrgs" );
        T = json_object_dotget_number( root_object, "scalars.t" );
        dt = json_object_dotget_number( root_object, "scalars.dt" );

        al   = (double *)malloc( nQ*sizeof(double) );
        de   = (double *)malloc( nQ*sizeof(double) );
        be   = (double *)malloc( (nQ*(nQ-1)/2)*sizeof(double) );

        array = json_object_dotget_array( root_object, "coefficients.alpha" );
        if( array != NULL ){
            for( i = 0; i < json_array_get_count(array); i++ ){
                al[i] = -json_array_get_number( array, i );
            }
        }

        array = json_object_dotget_array( root_object, "coefficients.beta" );
        if( array != NULL ){
            for( i = 0; i < json_array_get_count(array); i++ ){
                be[i] = -json_array_get_number( array, i );
            }
        }

        array = json_object_dotget_array( root_object, "coefficients.delta" );
        if( array != NULL ){
            for( i = 0; i < json_array_get_count(array); i++ ){
                de[i] = -json_array_get_number( array, i );
            }
        }

        json_value_free( root_value );
    }

    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        Compute the Hamiltonian and state vector for the simulation
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    /*
        Create state vector and initialize to 1/sqrt(2^n)*(|00...0> + ... + |11...1>)
        TODO: keep track of local size and local base
    */
    dim = 1 << nQ;
    factor = 1.0/sqrt( dim );
    
    fft_dims = (int *)malloc( nQ*sizeof(int) );
    psi  = (fftw_complex *)malloc( (dim)*sizeof(fftw_complex) );
    hz   = (double *)calloc( (dim),sizeof(double) );
    hhxh = (double *)calloc( (dim),sizeof(double) );

    for( i = 0; i < nQ; i++ ){
        fft_dims[i] = 2;
    }

    plan = fftw_plan_dft( nQ, fft_dims, psi, psi, FFTW_FORWARD, FFTW_MEASURE );

    /*
        Assemble Hamiltonian and state vector
    */
    for( k = 0; k < dim; k++ ){
        //TODO: when parallelized, k in dzi test will be ~(k + base)

        bcount = 0;
        for( i = 0; i < nQ; i++ ){
            testi = 1 << (nQ - i - 1);
            dzi = ((k/testi) % 2 == 0) ? 1 : -1;

            hz[k] += al[i] * dzi;
            hhxh[k] += de[i] * dzi;

            for( j = i; j < nQ; j++ ){
                testj = 1 << (nQ - j - 1);
                dzj = ((k/testj) % 2 == 0) ? 1 : -1;

                hz[k] += be[bcount] * dzi * dzj;
                bcount++;
            }
        }
            
        psi[k] = factor;
    }

    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                            Run the Simulation
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    fftw_complex cz, cx;
    double t;
    N = (uint64_t)(T / dt);
    for( i = 0; i < N; i++ ){
        t = i*dt;
        //t0 = (i-1)*dt;

        //Time-dependent coefficients
        cz = (-dt * I)*t/(2.0*T);
        cx = (-dt * I)*(1 - t/T);

        //Evolve system
        expMatTimesVec( psi, hz, cz, dim ); //apply Z part
        fftw_execute( plan );
        expMatTimesVec( psi, hhxh, cx, dim ); //apply X part
        fftw_execute( plan );
        expMatTimesVec( psi, hz, cz, dim ); //apply Z part
        
        /* 
            TODO: can probably get some minor speedup by incorporating this 
                  into expMatTimesVec if needed 
        */
        scaleVec( psi, 1.0/dim, dim );
    }
    fftw_destroy_plan( plan );

    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                        Check solution and clean up
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    //TODO: locally, collect all local largests on one
    //      node, find k largest from that subset
    if( prnt && nQ < 6 ){
        for( i = 0; i < dim; i++ ){
            printf( "psi[%d] = (%f, %f)\t%f\n", 
		    i,
		    creal( psi[i] ), 
		    cimag( psi[i] ), 
		    cabs( psi[i]*psi[i] ) );
        }
    } else {
        uint64_t *largest = (uint64_t *)calloc( L, sizeof(uint64_t) );
        findLargest( largest, psi, dim, L );
        for( i = 0; i < L; ++i ){
            printf( "psi[%d] = (%f, %f)\t%f\n",
		    i,
		    creal( psi[largest[L-1-i]] ), 
		    cimag( psi[largest[L-1-i]] ),
		    cabs( psi[largest[L-1-i]]*psi[largest[L-1-i]] ) );
        }
        free( largest );
    }

    /*
        Free work space.
    */
    fftw_free( psi );
    free( fft_dims );
    free( hz );
    free( hhxh );

    return 0;
}