Exemplo n.º 1
0
void gameRender()
{
    // update simulation
    float dt = g_timer.getElapsedMs() * 0.001f;
    g_timer.reset();

    // filter motion so you slow down gradually when you lift your finger off the screen
    const float kChangeFactor = 0.1f;
    g_playerPosChange = g_playerPosChangeTarget * kChangeFactor + g_playerPosChange * (1.0f - kChangeFactor);
    g_playerAngleChange = g_playerAngleChangeTarget * kChangeFactor + g_playerAngleChange * (1.0f - kChangeFactor);

    // update engine sound
    g_engineSound->setPaused(g_playerPosChange == 0.0f);
    float normSpeed = fabs(g_playerPosChange / kPosChangeMax);
    g_engineSound->setPitchShift(normSpeed*8);
    g_engineSound->setVolume(normSpeed * 0.4f);

    // update player position
    Vector3 forwardDir(sinf(g_playerAngle), 0.0f, cosf(g_playerAngle));
    Vector3 dr;
    Vector3::multiply(forwardDir, g_playerPosChange*dt, dr);
    Vector3::add(g_playerPos, dr, g_playerPos);
    g_playerAngle += g_playerAngleChange * dt;

    // keep player angle in [0..2pi]
    if (g_playerAngle > 2.0f*M_PI)
    {
        g_playerAngle -= 2.0f*M_PI;
    }
    else if (g_playerAngle < 0.0f)
    {
        g_playerAngle += 2.0f*M_PI;
    }

    // test for hits
    for (int i = 0; i < kNumCubes; ++i)
    {
        if (g_cubes[i]->hitTest(g_playerPos))
        {
            g_pointSound->play();
            placeCube(g_cubes[i]);
        }
    }

    // render
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    float fov = 0.9f;
    float near = 0.1f;
    float far = 100.0f;
    float aspect = (float) g_width / g_height;

    float top = tanf(fov*0.5) * near;
    float bottom = -top;
    float left = aspect * bottom;
    float right = aspect * top;

    glFrustumf(left, right, bottom, top, near, far);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // for landscape orientation
    glRotatef(90.0f, 0.0f, 0.0f, 1.0f);

    Vector3 eye;
    Vector3 v;
    Vector3::multiply(forwardDir, -3.0f, v);
    Vector3::add(g_playerPos, v, eye);
    eye.y = 1.0f;

    Vector3::multiply(forwardDir, 1.0f, v);
    Vector3 center;
    Vector3::add(g_playerPos, v, center);

    gluLookAt(eye.x, eye.y, eye.z,
              center.x, center.y, center.z,
              0.0f, 1.0f, 0.0f);

    CkSound::set3dListenerPosition(
            eye.x, eye.y, eye.z,
            center.x, center.y, center.z,
            0.0f, 1.0f, 0.0f);

    g_ship->setTransform(g_playerPos, g_playerAngle * 180.0f / M_PI + 180.0f);
    g_ship->draw();

    for (int i = 0; i < kNumCubes; ++i)
    {
        g_cubes[i]->draw(dt);
    }

    g_ground->draw();

    CkMixer::getMaster()->setVolume(1.0f);

    CkUpdate();
}
Exemplo n.º 2
0
void SeleneDev::CTestNode::Render()
{
	Selene::CGraphics::SVertex pyramidVertices[3];
	Selene::CGraphics::SVertex boxVertices[4];
	Selene::CGraphics::SVertex squaresVertices[12];

	Selene::Color pyramidColor(1.0f, 1.0f, 1.0f, 1.0f);
	Selene::Color boxColor(0.5f, 0.5f, 0.2f, 0.5f);
	Selene::Color squaresColor(1.0f, 1.0f, 1.0f, 0.5f);

	pyramidVertices[0].m_Color = pyramidColor;
	pyramidVertices[0].m_TexCoords = Selene::Vector2(0.0f, 0.0f);
	pyramidVertices[0].m_Coords = Selene::Vector3(0.0f, 1.0f, 0.0f);
	pyramidVertices[0].m_Coords += m_MoonPos;

	pyramidVertices[1].m_Color = pyramidColor;
	pyramidVertices[1].m_TexCoords = Selene::Vector2(0.0f, 1.0f);
	pyramidVertices[1].m_Coords = Selene::Vector3(-1.0f, -1.0f, 0.0f);
	pyramidVertices[1].m_Coords += m_MoonPos;

	pyramidVertices[2].m_Color = pyramidColor;
	pyramidVertices[2].m_TexCoords = Selene::Vector2(1.0f, 1.0f);
	pyramidVertices[2].m_Coords = Selene::Vector3(1.0f, -1.0f, 0.0f);
	pyramidVertices[2].m_Coords += m_MoonPos;


	boxVertices[0].m_Color = boxColor;
	boxVertices[0].m_TexCoords = Selene::Vector2(0.0f, 0.0f);
	boxVertices[0].m_Coords = Selene::Vector3(-1.0f, 1.0f, 0.0f);
	boxVertices[0].m_Coords += m_BoxPos;

	boxVertices[1].m_Color = boxColor;
	boxVertices[1].m_TexCoords = Selene::Vector2(1.0f, 0.0f);
	boxVertices[1].m_Coords = Selene::Vector3(1.0f, 1.0f, 0.0f);
	boxVertices[1].m_Coords += m_BoxPos;

	boxVertices[2].m_Color = boxColor;
	boxVertices[2].m_TexCoords = Selene::Vector2(1.0f, 1.0f);
	boxVertices[2].m_Coords = Selene::Vector3(1.0f, -1.0f, 0.0f);
	boxVertices[2].m_Coords += m_BoxPos;

	boxVertices[3].m_Color = boxColor;
	boxVertices[3].m_TexCoords = Selene::Vector2(0.0f, 1.0f);
	boxVertices[3].m_Coords = Selene::Vector3(-1.0f, -1.0f, 0.0f);
	boxVertices[3].m_Coords += m_BoxPos;

	Selene::Vector3 bottomOffset(0.0f, -0.5f, 0.0f);

	squaresVertices[0].m_Color = squaresColor;
	squaresVertices[0].m_TexCoords = Selene::Vector2(0.0f, 0.0f);
	squaresVertices[0].m_Coords = Selene::Vector3(-5.0f, 0.0f, 5.0f);
	squaresVertices[0].m_Coords += m_SquaresPos + bottomOffset;

	squaresVertices[1].m_Color = squaresColor;
	squaresVertices[1].m_TexCoords = Selene::Vector2(1.0f, 0.0f);
	squaresVertices[1].m_Coords = Selene::Vector3(5.0f, 0.0f, 5.0f);
	squaresVertices[1].m_Coords += m_SquaresPos + bottomOffset;

	squaresVertices[2].m_Color = squaresColor;
	squaresVertices[2].m_TexCoords = Selene::Vector2(1.0f, 1.0f);
	squaresVertices[2].m_Coords = Selene::Vector3(5.0f, 0.0f, -5.0f);
	squaresVertices[2].m_Coords += m_SquaresPos + bottomOffset;

	squaresVertices[3].m_Color = squaresColor;
	squaresVertices[3].m_TexCoords = Selene::Vector2(0.0f, 1.0f);
	squaresVertices[3].m_Coords = Selene::Vector3(-5.0f, 0.0f, -5.0f);
	squaresVertices[3].m_Coords += m_SquaresPos + bottomOffset;

	squaresVertices[4].m_Color = squaresColor;
	squaresVertices[4].m_TexCoords = Selene::Vector2(0.0f, 0.0f);
	squaresVertices[4].m_Coords = Selene::Vector3(-5.0f, 0.0f, -5.0f);
	squaresVertices[4].m_Coords += m_SquaresPos;

	squaresVertices[5].m_Color = squaresColor;
	squaresVertices[5].m_TexCoords = Selene::Vector2(1.0f, 0.0f);
	squaresVertices[5].m_Coords = Selene::Vector3(5.0f, 0.0f, -5.0f);
	squaresVertices[5].m_Coords += m_SquaresPos;

	squaresVertices[6].m_Color = squaresColor;
	squaresVertices[6].m_TexCoords = Selene::Vector2(1.0f, 1.0f);
	squaresVertices[6].m_Coords = Selene::Vector3(5.0f, 10.0f, -5.0f);
	squaresVertices[6].m_Coords += m_SquaresPos;

	squaresVertices[7].m_Color = squaresColor;
	squaresVertices[7].m_TexCoords = Selene::Vector2(0.0f, 1.0f);
	squaresVertices[7].m_Coords = Selene::Vector3(-5.0f, 10.0f, -5.0f);
	squaresVertices[7].m_Coords += m_SquaresPos;

	squaresVertices[8].m_Color = squaresColor;
	squaresVertices[8].m_TexCoords = Selene::Vector2(0.0f, 0.0f);
	squaresVertices[8].m_Coords = Selene::Vector3(-5.0f, 0.0f, -5.0f);
	squaresVertices[8].m_Coords += m_SquaresPos;

	squaresVertices[9].m_Color = squaresColor;
	squaresVertices[9].m_TexCoords = Selene::Vector2(1.0f, 0.0f);
	squaresVertices[9].m_Coords = Selene::Vector3(-5.0f, 10.0f, -5.0f);
	squaresVertices[9].m_Coords += m_SquaresPos;

	squaresVertices[10].m_Color = squaresColor;
	squaresVertices[10].m_TexCoords = Selene::Vector2(1.0f, 1.0f);
	squaresVertices[10].m_Coords = Selene::Vector3(-5.0f, 10.0f, 5.0f);
	squaresVertices[10].m_Coords += m_SquaresPos;

	squaresVertices[11].m_Color = squaresColor;
	squaresVertices[11].m_TexCoords = Selene::Vector2(0.0f, 1.0f);
	squaresVertices[11].m_Coords = Selene::Vector3(-5.0f, 0.0f, 5.0f);
	squaresVertices[11].m_Coords += m_SquaresPos;

	// coordinate axes
	Selene::CGraphics::SVertex xLineVertices[3];
	Selene::CGraphics::SVertex yLineVertices[2];
	Selene::CGraphics::SVertex zLineVertices[2];

	xLineVertices[0].m_Color = Selene::Color(1.0f, 0.0f, 0.0f, 1.0f);
	xLineVertices[0].m_Coords = Selene::Vector3(0.0f, 0.0f, 0.0f);
	xLineVertices[1].m_Color = Selene::Color(1.0f, 0.0f, 0.0f, 1.0f);
	xLineVertices[1].m_Coords = Selene::Vector3(5.0f, 0.0f, 0.0f);
	xLineVertices[2].m_Color = Selene::Color(1.0f, 0.0f, 0.0f, 1.0f);
	xLineVertices[2].m_Coords = Selene::Vector3(0.0f, 0.2f, 0.0f);


	yLineVertices[0].m_Color = Selene::Color(0.0f, 1.0f, 0.0f, 1.0f);
	yLineVertices[0].m_Coords = Selene::Vector3(0.0f, 0.0f, 0.0f);
	yLineVertices[1].m_Color = Selene::Color(0.0f, 1.0f, 0.0f, 1.0f);
	yLineVertices[1].m_Coords = Selene::Vector3(0.0f, 5.0f, 0.0f);

	zLineVertices[0].m_Color = Selene::Color(0.0f, 0.0f, 1.0f, 1.0f);
	zLineVertices[0].m_Coords = Selene::Vector3(0.0f, 0.0f, 0.0f);
	zLineVertices[1].m_Color = Selene::Color(0.0f, 0.0f, 1.0f, 1.0f);
	zLineVertices[1].m_Coords = Selene::Vector3(0.0f, 0.0f, 5.0f);


	//glLoadIdentity();
	//glTranslatef(-1.5f, 0.0f, 0.0f);
	//glRotatef(m_TriangleAngle, 0.0f, 1.0f, 0.0f);

	m_pGraphics->EnableAlphaBlend();
	m_pGraphics->SetAlphaBlendMode(Selene::CGraphics::SBM_SRC_ALPHA,
								   Selene::CGraphics::DBM_ONE_MINUS_SRC_ALPHA);

	m_pGraphics->SetPrimitiveMode(Selene::CGraphics::PM_TRIANGLES);
	//m_pGraphics->SetPrimitiveMode(Selene::CGraphics::PM_LINES);
	m_pGraphics->SetTexture(m_pBoxTex);
	m_pGraphics->StartPrimitives();
	m_pGraphics->Draw(pyramidVertices, 3);
	m_pGraphics->EndPrimitives();

	//glLoadIdentity();
	//glTranslatef(1.5f, 0.0f, 0.0f);
	//glRotatef(m_QuadAngle, 1.0f, 1.0f, 1.0f);

	m_pGraphics->SetPrimitiveMode(Selene::CGraphics::PM_QUADS);
	//m_pGraphics->SetPrimitiveMode(Selene::CGraphics::PM_LINES);
	m_pGraphics->SetTexture(m_pMoonTex);
	m_pGraphics->StartPrimitives();
	m_pGraphics->Draw(boxVertices, 4);
	m_pGraphics->EndPrimitives();

	m_pGraphics->SetPrimitiveMode(Selene::CGraphics::PM_QUADS);
	m_pGraphics->SetTexture(m_pSquaresTex);
	// hack
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, m_pFont->GetTextureID());
	glBegin(Selene::CGraphics::PM_QUADS);
	// end hack
	//m_pGraphics->StartPrimitives();
	m_pGraphics->Draw(squaresVertices, 12);
	m_pGraphics->EndPrimitives();

	//m_pGraphics->SetPrimitiveMode(Selene::CGraphics::PM_TRIANGLES);
	//m_pGraphics->SetTexture(NULL);
	//m_pGraphics->StartPrimitives();
	//m_pGraphics->Draw(xLineVertices, 3);
	//m_pGraphics->EndPrimitives();


	// object rotation test
	Selene::Vector3 rightDir(1.0f, 0.0f, 0.0f);
	Selene::Vector3 upDir(0.0f, 1.0f, 0.0f);
	Selene::Vector3 forwardDir(0.0f, 0.0f, 1.0f);

	Selene::Vector3 lookAt(0.0f, 0.0f, 1.0f);
	Selene::Vector3 pos(0.0f, 0.0f, 0.0f);
	Selene::Vector3 up(0.0f, 1.0f, 0.0f);

	//*
	lookAt = Selene::Vector3(0.0f, 0.0, 0.0f);
	pos = Selene::Vector3(3.0f, 0.0, 4.0f);
	up = Selene::Vector3(0.0f, 1.0, 0.0f);
	//*/

	forwardDir = lookAt - pos;
	forwardDir *= -1.0f;
	forwardDir.Normalize();
	rightDir = up.Cross(forwardDir);
	rightDir.Normalize();
	upDir = forwardDir.Cross(rightDir);
	upDir.Normalize();

	rightDir.Normalize();
	upDir.Normalize();
	forwardDir.Normalize();

	Selene::Matrix4 rotationMatrix;
	//rotationMatrix.m[0][0] = rightDir.m_X;
	//rotationMatrix.m[0][1] = rightDir.m_Y;
	//rotationMatrix.m[0][2] = rightDir.m_Z;
	//rotationMatrix.m[0][3] = 0.0f;

	//rotationMatrix.m[1][0] = upDir.m_X;
	//rotationMatrix.m[1][1] = upDir.m_Y;
	//rotationMatrix.m[1][2] = upDir.m_Z;
	//rotationMatrix.m[1][3] = 0.0f;

	//rotationMatrix.m[2][0] = forwardDir.m_X;
	//rotationMatrix.m[2][1] = forwardDir.m_Y;
	//rotationMatrix.m[2][2] = forwardDir.m_Z;
	//rotationMatrix.m[2][3] = 0.0f;

	//rotationMatrix.m[3][0] = 0.0f;
	//rotationMatrix.m[3][1] = 0.0f;
	//rotationMatrix.m[3][2] = 0.0f;
	//rotationMatrix.m[3][3] = 1.0f;

	rotationMatrix.m[0][0] = rightDir.m_X;
	rotationMatrix.m[0][1] = upDir.m_X;
	rotationMatrix.m[0][2] = forwardDir.m_X;
	rotationMatrix.m[0][3] = 0.0f;

	rotationMatrix.m[1][0] = rightDir.m_Y;
	rotationMatrix.m[1][1] = upDir.m_Y;
	rotationMatrix.m[1][2] = forwardDir.m_Y;
	rotationMatrix.m[1][3] = 0.0f;

	rotationMatrix.m[2][0] = rightDir.m_Z;
	rotationMatrix.m[2][1] = upDir.m_Z;
	rotationMatrix.m[2][2] = forwardDir.m_Z;
	rotationMatrix.m[2][3] = 0.0f;

	rotationMatrix.m[3][0] = 0.0f;
	rotationMatrix.m[3][1] = 0.0f;
	rotationMatrix.m[3][2] = 0.0f;
	rotationMatrix.m[3][3] = 1.0f;

	//rotationMatrix.Transpose();

	//*
	xLineVertices[0].m_Coords *= rotationMatrix;
	xLineVertices[1].m_Coords *= rotationMatrix;
	yLineVertices[0].m_Coords *= rotationMatrix;
	yLineVertices[1].m_Coords *= rotationMatrix;
	zLineVertices[0].m_Coords *= rotationMatrix;
	zLineVertices[1].m_Coords *= rotationMatrix;

	/*/
	Selene::CCamera* pCamera = m_pGraphics->GetActiveCamera();
	float dist = 3.0f;
	xLineVertices[1].m_Coords = pCamera->GetRightDir() * dist;
	yLineVertices[1].m_Coords = pCamera->GetUpDir() * dist;
	zLineVertices[1].m_Coords = pCamera->GetLookDir() * dist;
	//*/

	m_pGraphics->SetPrimitiveMode(Selene::CGraphics::PM_LINES);
	m_pGraphics->SetTexture(NULL);
	m_pGraphics->StartPrimitives();
	m_pGraphics->Draw(xLineVertices, 2);
	m_pGraphics->Draw(yLineVertices, 2);
	m_pGraphics->Draw(zLineVertices, 2);
	m_pGraphics->EndPrimitives();


	CSceneNode::Render();
}