ObjectNode* genHouseOut(const mat4& initTrans, const mat4& initSkew) {
	ObjectNode* house = new ObjectNode(nullptr, initTrans, initSkew);

	const float height = 0.5;
	house->addChild( *new ObjectNode(	Global::houseCube, translate(0.0, -height, 0.0) *
										distort(1.0, 0.01, 1.0)						) );
	house->addChild( *new ObjectNode(	Global::houseCube, translate(0.0, 0.0, -1.0) *
										distort(1.0, height, 0.01)						) );
	house->addChild( *new ObjectNode(	Global::houseCube, translate(1.0, 0.0, 0.0) *
										distort(0.01, height, 1.0)						) );
	house->addChild( *new ObjectNode(	Global::houseCube, translate(-1.0, 0.0, 0.0) *
										distort(0.01, height, 1.0)						) );
	house->addChild( *new ObjectNode(	Global::houseCube, translate(0.58, 0.0, 1.0) *
										distort(0.42, height, 0.01)					) );
	house->addChild( *new ObjectNode(	Global::houseCube, translate(-0.58, 0.0, 1.0) *
										distort(0.42, height, 0.01)					) );
	house->addChild( *new ObjectNode(	Global::houseCube, translate(0.0, 0.35, 1.0) *
										distort(0.25, 0.3 * height, 0.01)				) );
	house->addChild( *new ObjectNode(	Global::houseCube, translate(-0.29, -0.16, 1.13) *
		rY(M_PI/4.0) * distort(0.2, 0.7 * height, 0.01), rY(M_PI/4.0)	) );
	house->addChild( *new ObjectNode(	Global::housePyramid,
		translate(0.0, 0.85, 0.0) * scale(1.7) * distort(1.0, 0.2, 1.0) *
		rY(M_PI/4.0) * rX(M_PI/2.0),
		distort(1.0, 5.0, 1.0) * rY(M_PI/4.0) * rX(M_PI/2.0)				) );

	return house;
}
ObjectNode* genBranch(int branching, int maxDepth, int density, float scaleFactor, float seasonProb,
	const mat4& initTrans, const mat4& initSkew, int currentDepth)
{
	mat4 tempTrans = distort(0.2, 1.0, 0.2);
	mat4 tempMove = translate(0.0, 1.0, 0.0);
	mat4 tempSkew = distort(5.0, 1.0, 5.0);
	mat4 tempRotate = rX(M_PI/2.0);
	ObjectNode* branch = new ObjectNode(nullptr, initTrans, initSkew);
	branch->addChild(*new ObjectNode(	Global::treeCone, tempMove * tempTrans * tempRotate,
										tempSkew * tempRotate )		);

	if (currentDepth < maxDepth) {
		const int branchFact = branching;
		mat4 tempRotate = rZ(M_PI/6.0);
		mat4 rotMat = rY(2.0 * M_PI / branchFact);
		tempTrans = scale(scaleFactor);
		tempMove = translate(0.0, 1.5, 0.0);
		mat4 twist = rY(M_PI / branchFact) * tempMove;
		mat4 loopTemp = rotMat * tempRotate;

		for (int i = 0; i < branchFact; ++i) {
			loopTemp = rotMat * loopTemp;
			branch->addChild( *genBranch( branching, maxDepth, density, scaleFactor, seasonProb,
				twist * loopTemp * tempTrans, loopTemp, currentDepth + 1 ) );
		}
	}
	
	return branch;
}
ObjectNode* genTree(int branching, int maxDepth, int density, float scaleFactor, float seasonProb, float height,
	const mat4& initTrans, const mat4& initSkew)
{
	ObjectNode* tree = new ObjectNode(nullptr,initTrans, initSkew);

	mat4 tempRotate = rX(M_PI/2.0);
	mat4 tempTrans = distort(1.0, 0.2, 1.0);
	mat4 tempSkew = distort(1.0, 5.0, 1.0);
	mat4 tempMove = translate(0.0, -2.0, 0.0);
	tree->addChild( *new ObjectNode(	Global::treeCone, tempMove * tempTrans * tempRotate,
									tempSkew * tempRotate)	);

	tempTrans = distort(0.3, height, 0.3);
	tempMove[1][3] = height - 2.0;
	tree->addChild( *new ObjectNode(Global::treeCylinder, tempMove * tempTrans * tempRotate, tempRotate) );

	tempMove[1][3] = height * 0.6 + 0.1;
	tree->addChild( *genBranch(branching, maxDepth, density, scaleFactor, seasonProb, tempMove) );

	mat4 rotMat = rY(2.0 * M_PI/4.0);
	tempSkew = rZ(M_PI/4.0);
	for (int i = 0; i < 4; ++i) {
		tempSkew = rotMat * tempSkew;
		tree->addChild( *genBranch(branching, maxDepth, density, scaleFactor, seasonProb,
			tempMove * tempSkew, tempSkew) );
	}

	tree->addChild( *genLeafBunch(M_PI/2.0, 3.0, 0.5, density,
		translate(0.0, height, 0.0) * scale(0.7)) );

	return tree;
}
void init() {
	// Load shaders and use the resulting shader program
    GLuint drawProg = InitShader("Draw.vertex", "Draw.fragment");
	GLuint shadowProg = InitShader("ShadowMap.vertex", "ShadowMap.fragment");
    glUseProgram(drawProg);

	meshSetup(Global::red_opaque, 1.2);

	const mat4 projection = Perspective(Global::FOV, 1.0f, 0.0078125f, 16.0f) * 
		translate(0.0f, 0.0f, 0.75f) * scale(10.0f);

	Global::root = new RenderGraph(
		drawProg,
		shadowProg,
		*genIdyll(5, 1.2f),
		projection
	);
	Global::sun = new RenderGraph(
		drawProg,
		shadowProg,
		*genSun( translate(16.0 * Global::sunVec) * scale(0.4f) *
			rX(M_PI/2.0) ),
		projection
	);

	help();
}
Exemple #5
0
    bool isCollision( std::vector<Room> & rooms, uint32_t x, uint32_t y, uint32_t w, uint32_t h )
    {
        for( std::vector<Room>::iterator rIter = rooms.begin(), end = rooms.end() ; rIter != end ; ++rIter )
        {
            uint32_t & rX( rIter->x );
            uint32_t & rY( rIter->y );
            uint32_t & rW( rIter->w );
            uint32_t & rH( rIter->h );

            bool roomOkay;

            if( (rX + rW + 1 ) < x || rX > (x + w + 1 ) )
            {
                roomOkay = true;
            }
            else if( (rY + rH + 1) < y || rY > (y + h + 1 ) )
            {
                roomOkay = true;
            }
            else
            {
                roomOkay = false;
            }

            if( !roomOkay )
            {
                return true;
            }
        }
        return false;
    }
EnvironmentNode* genHouseIn(const mat4& initTrans, const mat4& initSkew) {
	EnvironmentNode* house = new EnvironmentNode(BoolState(1u, 0u), initTrans, initSkew);
	house->addChild(
		*new LightNode(Lightsource("pointLight", false), translate(0.0, -0.35, -0.87))		);

	const float height = 0.5;
	house->addChild( *new ObjectNode(Global::houseSquare, translate(0.0, -height + 0.02, 0.0) *
									rX(M_PI/2.0), rX(M_PI/2.0)					) );
	house->addChild( *new ObjectNode(Global::houseSquare, translate(0.0, height - 0.02, 0.0) *
									rX(M_PI/2.0), rX(M_PI/2.0)					) );
	house->addChild( *new ObjectNode(	Global::houseSquare, translate(0.0, 0.0, -0.98) *
										distort(1.0, height, 1.0)							) );
	house->addChild( *new ObjectNode(	Global::houseSquare, translate(0.98, 0.0, 0.0) *
		distort(1.0, height, 1.0) * rY(M_PI/2.0), rY(M_PI/2.0)			) );
	house->addChild( *new ObjectNode(	Global::houseSquare, translate(-0.98, 0.0, 0.0) *
		distort(1.0, height, 1.0) * rY(M_PI/2.0), rY(M_PI/2.0)			) );
	house->addChild( *new ObjectNode(	Global::houseSquare, translate(0.58, 0.0, 0.98) *
										distort(0.42, height, 1.0)							) );
	house->addChild( *new ObjectNode(	Global::houseSquare, translate(-0.58, 0.0, 0.98) *
										distort(0.42, height, 1.0)							) );
	house->addChild( *new ObjectNode(	Global::houseSquare, translate(0.0, 0.35, 0.98) *
										distort(0.25, 0.3 * height, 1.0)					) );

	house->addChild( *new ObjectNode(	Global::houseCube, translate(-0.2, 0.0, 0.6) *
										distort(0.01, height, 0.4)						) );
	house->addChild( *new ObjectNode(	Global::houseCube, translate(0.2, 0.0, 0.6) *
										distort(0.01, height, 0.4)						) );
	house->addChild( *new ObjectNode(	Global::houseCube, translate(-0.75, 0.0, 0.2) *
										distort(0.25, height, 0.01)					) );
	house->addChild( *new ObjectNode(	Global::houseCube, translate(0.75, 0.0, 0.2) *
										distort(0.25, height, 0.01)					) );

	house->addChild( *genFirePlace( translate(0.0, -0.35, -0.95) *
		distort(1.5, 1.25, 0.2) * scale(0.25) ));
	house->addChild( *genBed(translate(-0.5, -0.37, 0.85) * scale(0.3)) );
	house->addChild( *genBed(translate(0.65, -0.37, 0.85) * scale(0.3)) );
	house->addChild( *genChair(translate(-0.3, -0.4, -0.35) *
		rY(M_PI/4.0) * scale(0.2), rY(M_PI/4.0)) );
	house->addChild( *genChair(translate(0.25, -0.4, -0.3) *
		rY(M_PI/2.0) * scale(0.2), rY(M_PI/2.0)) );
	house->addChild( *genChair(translate(0.8, -0.4, -0.4) *
		rY(M_PI) * distort(1.0, 1.0, 3.0) * scale(0.2), rY(M_PI)) );

	return house;
}
Direction PlayerState::getRelativeXZDirection(const RealCoords & rc)
{
  const double diffX = rX(rc) - rX(position);
  const double diffZ = rZ(rc) - rZ(position);

  if (std::abs(diffX) > std::abs(diffZ))
  {
    // We compare on the x axis
    if (diffX > 0)  return BLOCK_XPLUS;
    else            return BLOCK_XMINUS;
  }
  else
  {
    // We compare on the z axis
    if (diffZ > 0) return BLOCK_ZPLUS;
    else           return BLOCK_ZMINUS;
  }
}
ObjectNode* genBed(const mat4& initTrans, const mat4& initSkew) {
	ObjectNode* bed = new ObjectNode(nullptr, initTrans, initSkew);

	bed->addChild( *new ObjectNode(Global::furnCube0, translate(0.5, 0.0, 0.0) * distort(0.3, 0.2, 0.4)) );
	bed->addChild( *new ObjectNode(Global::furnCube1, translate(-0.5, 0.0, 0.0) * distort(0.7, 0.2, 0.4)) );
	bed->addChild( *new ObjectNode(	Global::treeCylinder,
		translate(-1.1, -0.3, 0.3) * scale(0.1) * rX(M_PI/2.0),
		rX(M_PI/2.0)) );
	bed->addChild( *new ObjectNode(	Global::treeCylinder, translate(0.7, -0.3, 0.3) *
		scale(0.1) * rX(M_PI/2.0), rX(M_PI/2.0))	);
	bed->addChild( *new ObjectNode(	Global::treeCylinder, translate(0.7, -0.3, -0.3) *
		scale(0.1) * rX(M_PI/2.0), rX(M_PI/2.0))	);
	bed->addChild( *new ObjectNode(	Global::treeCylinder, translate(-1.1, -0.3, -0.3) *
		scale(0.1) * rX(M_PI/2.0), rX(M_PI/2.0))	);

	return bed;
}
ObjectNode* genLeafBunch(float rho, float radius, float prob, int numLeaves,
	const mat4& initTrans, const mat4& initSkew)
{
	ObjectNode* bunch = new ObjectNode(nullptr, initTrans, initSkew);

	mat4 rotMat = MatMath::ID;
	mat4 tempTrans = translate(0.0, radius, 0.0);
	mat4 tempRotate = rY(	randNum(0.0, 2.0 * M_PI) ) *
									rZ(randNum(0.0, rho)	);
	ObjectNode* temp = new ObjectNode(nullptr, tempRotate * tempTrans, tempRotate);
	temp->addChild(*genLeaf(prob));
	bunch->addChild(*temp);
	for (int i = 1; i < numLeaves; ++i) {
		rotMat = rY(	randNum(0.0, 2.0 * M_PI) ) *
								rX( randNum(0.0, M_PI/2.0)	);
		tempRotate = rY(	randNum(0.0, 2.0 * M_PI) ) *
									rX( randNum(0.0, rho)	);
		temp = new ObjectNode(nullptr, tempRotate * tempTrans * rotMat, tempRotate * rotMat);
		temp->addChild(*genLeaf(prob));
		bunch->addChild(*temp);
	}

	return bunch;
}
ObjectNode* genChassis(const mat4& initTrans, const mat4& initSkew, const color4& paint) {
	ObjectNode* chassis = new ObjectNode(nullptr, initTrans, initSkew);
	
	mat4 tempRotate = rX(M_PI/2.0);
	chassis->addChild(	*new ObjectNode(Global::carSphere, distort(2.0, 0.5, 1.0) * tempRotate,
						distort(0.5, 2.0, 1.0) * tempRotate)	);

	chassis->addChild( *new ObjectNode(	Global::carSphere,
		translate(0.3, 0.25, 0.0) * distort(1.25, 0.5, 0.85),
		distort(0.8, 2.0, 1/0.85))	);
	
	mat4 tempSkew = distort(0.35, 0.2, 1.0);
	mat4 tempTrans = translate(-2.0, -1.0, 0.0);
	chassis->addChild( *new ObjectNode(Global::carCube, tempSkew * tempTrans) );
	tempTrans[0][3] = 2.85;
	chassis->addChild( *new ObjectNode(Global::carCube, tempSkew * tempTrans) );

	return chassis;
}
ObjectNode* genChair(const mat4& initTrans, const mat4& initSkew) {
	ObjectNode* chair = new ObjectNode(nullptr, initTrans, initSkew);

	chair->addChild( *new ObjectNode(Global::furnCube1, distort(0.5, 0.2, 0.4)) );
	chair->addChild( *new ObjectNode(	Global::furnCube1, translate(-0.5, 0.5, 0.0) *
		rZ(M_PI/8.0) * distort(0.15, 0.45, 0.4), rZ(M_PI/8.0))		);
	chair->addChild( *new ObjectNode(	Global::furnCube1, translate(0.0, 0.3, 0.45) *
										distort(0.4, 0.1, 0.1))						);
	chair->addChild( *new ObjectNode(	Global::furnCube1, translate(0.0, 0.3, -0.45) *
										distort(0.4, 0.1, 0.1))						);
	chair->addChild( *new ObjectNode(	Global::treeCylinder, translate(-0.4, -0.3, 0.3) *
		scale(0.1) * rX(M_PI/2.0), rX(M_PI/2.0))		);
	chair->addChild( *new ObjectNode(	Global::treeCylinder, translate(0.4, -0.3, 0.3) *
		scale(0.1) * rX(M_PI/2.0), rX(M_PI/2.0))	);
	chair->addChild( *new ObjectNode(	Global::treeCylinder, translate(0.4, -0.3, -0.3) *
		scale(0.1) * rX(M_PI/2.0), rX(M_PI/2.0))	);
	chair->addChild( *new ObjectNode(	Global::treeCylinder, translate(-0.4, -0.3, -0.3) *
		scale(0.1) * rX(M_PI/2.0), rX(M_PI/2.0))	);

	return chair;
}
ObjectNode* genWheel(const mat4& initTrans, const mat4& initSkew) {
	ObjectNode* wheel = new ObjectNode(nullptr, initTrans, initSkew);

	mat4 rotMat = rX(M_PI/2.0);
	mat4 tempTransform = distort(0.1, 0.1, 0.6);
	wheel->addChild( *new ObjectNode(Global::wheelCylinder, rotMat * tempTransform, rotMat, rotateWheel) );
	mat4 tempRotate = rZ(M_PI/4.0);
	wheel->addChild( *new ObjectNode(	Global::wheelCylinder, tempRotate * rotMat * tempTransform,
										tempRotate * rotMat, rotateWheel							) );
	tempRotate = rZ(M_PI/2.0);
	wheel->addChild( *new ObjectNode(	Global::wheelCylinder, tempRotate * rotMat * tempTransform,
										tempRotate * rotMat, rotateWheel							) );
	tempRotate = rZ(-M_PI/4.0);
	wheel->addChild( *new ObjectNode(	Global::wheelCylinder, tempRotate * rotMat *  tempTransform,
										tempRotate * rotMat, rotateWheel							) );

	wheel->addChild( *new ObjectNode(	Global::wheelTube, distort(1.0, 1.0, 0.2), MatMath::ID,
										rotateWheel		) );

	return wheel;
}
void mouseMotion(int p, int q)
//rotates the camera if the left mouse button is pressed down.
//assumes user is clicking and pulling on a plane parallel to the projection plane,
//and approximates the angle of rotation by using the sphere tangent to that plane.
//for the plane at z = -1, that is the unit sphere, along Global::which all arcs are equal
//to the angle that subtends them, meaning that the distance moved by the mouse
//along the tangent plane can be directly plugged in as the angle of rotation
{
	if (Global::leftMouse) {
		float delta_p = 3.0 * Global::FOV * (M_PI/180.0) * (p - Global::pRef) / Global::viewDim;
		float delta_q = 2.5 * Global::FOV * (M_PI/180.0) * (q - Global::qRef) / Global::viewDim;
		mat4 delta_yaw = rY(delta_p);
		Global::pitchTheta =
			(Global::pitchTheta + delta_q > -M_PI/2.0 && Global::pitchTheta + delta_q < M_PI/2.0) ?
				Global::pitchTheta + delta_q
			:	Global::pitchTheta;
		Global::yawMat = delta_yaw * Global::yawMat;
		Global::positMat = delta_yaw * Global::positMat;
		Global::pitchMat = rX(Global::pitchTheta);
		Global::pRef = p, Global::qRef = q;
		glutPostRedisplay();
	}
}
ObjectNode* genRoad(const mat4& initTrans, const mat4& initSkew, float thinness) {
	mat4 tempRotate = rX(-M_PI/2.0);
	return new ObjectNode(Global::roadRing, initTrans * tempRotate, initSkew * tempRotate);
}
ObjectNode* genGround(float height, float size, const mat4& initTrans, const mat4& initSkew) {
	mat4 tempTrans = translate(0.0, -height, 0.0);
	mat4 tempRotate = rX(-M_PI/2.0);
	return new ObjectNode(	Global::groundSquare, initTrans * tempTrans * scale(size) *
							tempRotate, initSkew * tempRotate	);
}
void Vc4Shader::Emit_ShaderOutput_VS(boolean bVS)
{
    assert(this->uShaderType == D3D10_SB_VERTEX_SHADER);
    
    Vc4Register vpm(VC4_QPU_ALU_REG_B, VC4_QPU_WADDR_VPM);

    Vc4Register pos[4]; // X/Y/Z/W.
    for (uint8_t iPos = 0; iPos < this->cOutput; iPos++)
    {
        if (this->OutputRegister[iPos / 4][iPos % 4].GetFlags().position)
        {
            for (uint8_t i = iPos; i < iPos + 4; i++)
            {
                Vc4Register reg = this->OutputRegister[i / 4][i % 4];
                assert(reg.GetFlags().position);
                pos[this->SwizzleMaskToIndex(reg.GetSwizzleMask())] = reg;
            }
            break;
        }
    }

    // Only CS emits raw coordinates.
    if (!bVS)
    {
        // Xc, Yc, Zc, Wc
        for (uint8_t i = 0; i < 4; i++)
        {
            Vc4Register src = pos[i];
            Vc4Instruction Vc4Inst;
            Vc4Inst.Vc4_m_MOV(vpm, src);
            Vc4Inst.Emit(CurrentStorage);
        }
    }

    // calculate 1/W
    {
        // send W to sfu_recip.
        {
            Vc4Register sfu_recip((pos[3].GetMux() == VC4_QPU_ALU_REG_A ? VC4_QPU_ALU_REG_B : VC4_QPU_ALU_REG_A), VC4_QPU_WADDR_SFU_RECIP);
            Vc4Instruction Vc4Inst;
            Vc4Inst.Vc4_a_MOV(sfu_recip, pos[3]);
            Vc4Inst.Emit(CurrentStorage);
        }

        // Issue 2 NOPs to wait result of sfu_recip.
        for (uint8_t i = 0; i < 2; i++)
        {
            Vc4Instruction Vc4Inst;
            Vc4Inst.Emit(CurrentStorage);
        }
    }

    // Now, r4 is 1/W.
    Vc4Register r4(VC4_QPU_ALU_R4);
    
    // Ys/Xs
    {
        // scale by RT dimension (read from uniform). Result in r0/r1.
        {
            for (uint8_t i = 0; i < 2; i++)
            {
                Vc4Register rX(VC4_QPU_ALU_R0 + i, VC4_QPU_WADDR_ACC0 + i); // r0 and r1.
                
                { // divide Xc/Yc by W.
                    Vc4Instruction Vc4Inst;
                    Vc4Inst.Vc4_m_FMUL(rX, pos[i], r4);
                    Vc4Inst.Emit(CurrentStorage);
                }

                { // Scale Xc/Yc with viewport.
                    Vc4Instruction Vc4Inst;
                    Vc4Register unif((rX.GetMux() == VC4_QPU_ALU_REG_A ? VC4_QPU_ALU_REG_B : VC4_QPU_ALU_REG_A), VC4_QPU_RADDR_UNIFORM); // use opossit register file.
                    Vc4Inst.Vc4_m_FMUL(rX, rX, unif);
                    Vc4Inst.Emit(CurrentStorage);
                    {
                        VC4_UNIFORM_FORMAT u;
                        u.Type = (i == 0 ? VC4_UNIFORM_TYPE_VIEWPORT_SCALE_X : VC4_UNIFORM_TYPE_VIEWPORT_SCALE_Y);
                        this->AddUniformReference(u);
                    }
                }
            }
        }

        // Convert r0/r1 to 16bits float and pack them into ra16, then output to vpm.
        {
            Vc4Register ra16(VC4_QPU_ALU_REG_A, 16);

            for (uint8_t i = 0; i < 2; i++)
            {
                Vc4Register rX(VC4_QPU_ALU_R0 + i); // r0 and r1.
                Vc4Instruction Vc4Inst;
                Vc4Inst.Vc4_a_FTOI(ra16, rX); // REUSE ra16 as temp is no longer needed.
                Vc4Inst.Vc4_a_Pack(VC4_QPU_PACK_A_16a + i); // Pack to 16a or 16b.
                Vc4Inst.Emit(CurrentStorage);
            }

            // Issue NOP as ra16 is just written.
            {
                Vc4Instruction Vc4Inst;
                Vc4Inst.Emit(CurrentStorage);
            }

            // Output to vpm.
            {
                Vc4Instruction Vc4Inst;
                Vc4Inst.Vc4_m_MOV(vpm, ra16);
                Vc4Inst.Emit(CurrentStorage);
            }
        }
    }

    // Zs
    { // Zs = Zc / W // TODO: Z offset
        Vc4Instruction Vc4Inst;
        Vc4Inst.Vc4_m_FMUL(vpm, pos[2], r4);
        Vc4Inst.Emit(CurrentStorage);
    }

    // 1/Wc
    {
        // Move result of sfu_recip (come up at r4) to vpm.
        {
            Vc4Instruction Vc4Inst;
            Vc4Inst.Vc4_m_MOV(vpm, r4);
            Vc4Inst.Emit(CurrentStorage);
        }
    }

    // Only VS emits "varying" (everything read from vpm except position data).
    if (bVS)
    {
        for (uint8_t i = 0, iRegUsed = 0; iRegUsed < this->cOutput; i++ )
        {
            Vc4Register src = this->OutputRegister[i / 4][i % 4];
            if (src.GetFlags().valid)
            {
                if (src.GetFlags().linkage)
                {
                    Vc4Instruction Vc4Inst;
                    Vc4Inst.Vc4_m_MOV(vpm, src);
                    // Vc4Inst.Vc4_m_FMUL(vpm, src, r4);
                    Vc4Inst.Emit(CurrentStorage);
                }
                iRegUsed++;
            }
        }
    }
}