Example #1
0
void Weapon::DrawHUD( Screen3D& Screen, MeshManager& MM, FontManager& FM, Camera& Viewer )
{

	HitPlayer = false;

	Screen.Clear(true);
	// Set the matrices to the origin
    D3DXMATRIX matWorld;
    D3DXMatrixIdentity( &matWorld );
    Screen.SetTransform(  matWorld );

//Set render states
					ScreenPtr->D3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
					ScreenPtr->D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
					ScreenPtr->D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
					ScreenPtr->D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
					ScreenPtr->D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
					ScreenPtr->D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
					ScreenPtr->D3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
					ScreenPtr->D3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
					ScreenPtr->D3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
					ScreenPtr->D3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

	//Draw the weapon model
	WeaponFont = FM.GetFont("Lucida Console", 16);
	Screen.D3DDevice->SetRenderState( D3DRS_FOGENABLE, FALSE );

	Matrix ViewMatrix = Viewer.GetLookAtMatrix(  -10 );
	Matrix WorldMatrix;
	Matrix DisplacementMat;
	Matrix DisplacementMat2;
	Matrix ScaleMatrix;


	Screen.SetRenderState(D3DRS_ALPHABLENDENABLE,   TRUE);
	Screen.SetRenderState(D3DRS_SRCBLEND,           D3DBLEND_ONE);
	Screen.SetRenderState(D3DRS_DESTBLEND,          D3DBLEND_ONE);
	Screen.SetRenderState(D3DRS_ALPHATESTENABLE, FALSE );
	Screen.SetRenderState(D3DRS_ZENABLE, FALSE );

	RECT muzzleDim = { Screen.GetWidth()/2, Screen.GetHeight()/2-Screen.GetHeight()/5,Screen.GetWidth()/2+(Screen.GetHeight()/5)*2, Screen.GetHeight()/2+Screen.GetHeight()/5};

	int fade = (int)(MuzzleFlashFade*255.0f);
	Screen.DrawSpriteRect( muzzleDim, MuzzleFlash, true, D3DCOLOR_RGBA( fade, fade, fade, fade ) );
	Screen.SetRenderState(D3DRS_ZENABLE, TRUE );

	Screen.SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
	//Screen.SetTextureMode( TMSphereMap);
	//Screen.SetTexture(GlossMap, 1);


	
	Matrix RMat;
	RMat.Rotate( Viewer.Yaw, Viewer.Pitch, Viewer.Roll);
	ScreenPtr->D3DDevice->MultiplyTransform( D3DTS_TEXTURE1, &RMat.Mat );
		
	//draw
	if(!RightHanded)
	{
		//First, apply a -1 scale to the transform
		//matrix so that the gun draws on the opposite side
		//(since it's left handed). Also, change the 
		//culling order so it doesn't draw inside out.
		Screen.SetRenderState( D3DRS_CULLMODE, D3DCULL_CW);

		DisplacementMat.Translate( 0.10f, 3.1f, -4.3f);
		ScaleMatrix.Scale( -1, 1, 1 );
		D3DXMatrixMultiply(&DisplacementMat.Mat, &DisplacementMat.Mat, &ScaleMatrix.Mat);
		D3DXMatrixMultiply(&ViewMatrix.Mat, &ViewMatrix.Mat, &DisplacementMat.Mat);
		D3DXMatrixInverse(&WorldMatrix.Mat, NULL, &ViewMatrix.Mat);
		Screen.SetTransform(WorldMatrix);


		//Draw the mesh now and return the culling state
		//DisplayMesh.Draw( Screen );
		Screen.SetRenderState(D3DRS_LIGHTING, FALSE );
		MeshA.Draw( Screen, WorldMatrix );
		Screen.SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
		D3DMATRIX OldState;
		Screen.D3DDevice->GetTransform( D3DTS_VIEW, &OldState);
		
		Matrix I;
		Screen.SetViewTransform(I);

		
				
		
		Smoke.MoveSpawn( -1.0f, -1.2f, 9.0f);
		Smoke.Draw( Screen, Viewer );
		
		if(DualWeapons)
		{
			Smoke.MoveSpawn( 1.0f, -1.1f, 8.0f);
			Smoke.Draw( Screen, Viewer );
		}
		
		Screen.D3DDevice->SetTransform( D3DTS_VIEW, &OldState);
       
	}
	else
	{
		DisplacementMat.Translate( -0.10f, 3.1f, -4.3f);
		D3DXMatrixMultiply(&ViewMatrix.Mat, &ViewMatrix.Mat, &DisplacementMat.Mat);
		D3DXMatrixInverse(&WorldMatrix.Mat, NULL, &ViewMatrix.Mat);
		Screen.SetTransform(WorldMatrix);

		//Draw the weapon
		Screen.D3DDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_NONE );
		MeshA.Draw( Screen, WorldMatrix );
		Screen.D3DDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );

		Matrix I;
		Screen.SetViewTransform(I);
				

		Smoke.MoveSpawn( 1.0f, -1.1f, 8.0f);
		Smoke.Draw( Screen, Viewer );

		if(DualWeapons)
		{
			Smoke.MoveSpawn( -1.0f, -1.1f, 8.0f);
			Smoke.Draw( Screen, Viewer );
		}

	}

	//Show the ammo information by the weapon
	Screen.D3DDevice->SetRenderState( D3DRS_FOGENABLE, FALSE );
	ScreenPtr->D3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
	ScreenPtr->D3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
	ScreenPtr->D3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

	Screen.SetRenderState(D3DRS_ZENABLE, FALSE );
	if(ShowAmmoAmount)
	{

		
		TextureHandle hAmmoBox = Screen.GetTextureHandle( "..\\base\\art\\UI\\icons\\ammobox.bmp", "..\\base\\art\\UI\\icons\\ammobox_a.bmp");
		Screen.DrawSprite(  Screen.GetWidth() - 145.0f, (Real)Screen.GetHeight() - 40.0f, Screen.TM.GetTexture( hAmmoBox ));
		WeaponFont->Draw(Screen, Screen.GetWidth() - 105.0f, (Real)Screen.GetHeight() - 32.0f, false, D3DCOLOR_RGBA(255, 255, 255, 255), "%d", ClipAmmo);
		WeaponFont->Draw(Screen, Screen.GetWidth() - 60.0f,  (Real)Screen.GetHeight() - 32.0f, false, D3DCOLOR_RGBA(255, 0, 0, 255), "%d", Ammo);
	}

		
	Screen.TM.SetTextureMode( TMNormal, 0, true);

	//draw the aimer
	if(ShowAmmoAmount)
	{
		Texture* AimerPtr = Screen.TM.GetTexture( Aimer );
		

		Screen.SetRenderState(D3DRS_ALPHABLENDENABLE,   TRUE);
		Screen.SetRenderState(D3DRS_SRCBLEND,           D3DBLEND_SRCALPHA);
		Screen.SetRenderState(D3DRS_DESTBLEND,          D3DBLEND_INVSRCALPHA);
		Screen.SetRenderState(D3DRS_ALPHATESTENABLE, FALSE );

		RECT Dimensions;
		Dimensions.left   = Screen.GetWidth() /2 - (AimerPtr->GetWidth()/2);
		Dimensions.top   = Screen.GetHeight()/2 - (AimerPtr->GetHeight()/2);
		Dimensions.right  = Screen.GetWidth() /2 + AimerPtr->GetWidth()/2-1;
		Dimensions.bottom = Screen.GetHeight()/2 + AimerPtr->GetHeight()/2-1;
	
	
		if(AimerPtr)
			Screen.DrawSprite(Screen.GetWidth()/2 - (AimerPtr->GetWidth()/2), Screen.GetHeight()/2 - (AimerPtr->GetHeight()/2), AimerPtr);


	

		
	}

	ScreenPtr->D3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	ScreenPtr->D3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	ScreenPtr->D3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
	Screen.SetRenderState(D3DRS_ZENABLE, TRUE );
	Screen.SetRenderState( D3DRS_FOGENABLE,  TRUE );

}
Example #2
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("Marcus Piazzola Scene", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
	#ifdef _WINDOWS
		glewInit();
	#endif

	SDL_Event event;
	bool done = false;

	glViewport(0, 0, 640, 360);
	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
	GLuint text1 = LoadTexture("cardSpadesA.png");
	GLuint text2 = LoadTexture("cardHeartsA.png");
	GLuint text3 = LoadTexture("cardDiamondsA.png");

	Matrix projectionMatrix;
	Matrix modelMatrix;
	Matrix viewMatrix;
	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
	float lastFrameTicks = 0.0f;
	glUseProgram(program.programID);

	while (!done) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
				done = true;
			}
		}

		glClear(GL_COLOR_BUFFER_BIT);
		

		float ticks = (float)SDL_GetTicks() / 1000.0f;
		float elapsed = ticks - lastFrameTicks;
		lastFrameTicks = ticks;

		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);
		program.setModelMatrix(modelMatrix);

		//Card 1 (Ace of Hearts)
		float vertices1[] = { -2.0, -0.5, -1.0, -0.5, -1.0, 0.5, -2.0, -0.5, -1.0, 0.5, -2.0, 0.5 };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices1);
		glEnableVertexAttribArray(program.positionAttribute);
		float texCoords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
		glEnableVertexAttribArray(program.texCoordAttribute);
		glBindTexture(GL_TEXTURE_2D, text2);
		glDrawArrays(GL_TRIANGLES, 0, 6);	
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);


		//Card 2 (Ace of Spades)
		modelMatrix.Rotate(lastFrameTicks);
		program.setModelMatrix(modelMatrix);
		modelMatrix.identity();
		float vertices2[] = { -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5 };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices2);
		glEnableVertexAttribArray(program.positionAttribute);
		float texCoords2[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords2);
		glEnableVertexAttribArray(program.texCoordAttribute);
		glBindTexture(GL_TEXTURE_2D, text1);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);


		//Card 3 (Ace of Diamonds)
		program.setModelMatrix(modelMatrix);
		float vertices3[] = { 1.0, -0.5, 2.0, -0.5, 2.0, 0.5, 1.0, -0.5, 2.0, 0.5, 1.0, 0.5 };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices3);	
		glEnableVertexAttribArray(program.positionAttribute);
		float texCoords3[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords3);
		glEnableVertexAttribArray(program.texCoordAttribute);
		glBindTexture(GL_TEXTURE_2D, text3);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.positionAttribute);
		glDisableVertexAttribArray(program.texCoordAttribute);

		SDL_GL_SwapWindow(displayWindow);

	}

	SDL_Quit();
	return 0;
}
Example #3
0
void MeshEntity::OnUpdatePosition()
{
    traceInFast(MeshEntity::OnUpdatePosition);

    Super::OnUpdatePosition();

    for(int i=0; i<MeshLights.Num(); i++)
        MeshLights[i].light->RemoveEntity(this, MeshLights[i].side);
    MeshLights.Clear();

    List<LevelObject*> objects;
    level->GetObjects(bounds.GetTransformedBounds(transform), objects);
    for(int i=0; i<objects.Num(); i++)
    {
        LevelObject *levelObj = objects[i];

        if(levelObj->type == ObjectType_Entity)
        {
            if(!levelObj->ent->IsOf(GetClass(Light)))
                continue;

            Light *light = (Light*)levelObj->ent;
            light->ProcessEntity(this);
        }
    }

    if(bStaticGeometry)
        return;

    Vect position = GetWorldPos()+adjustPos;
    Quat rotation = GetWorldRot()*adjustRot;

    Matrix transform;
    transform.SetIdentity();
    transform.Rotate(rotation.GetInv());
    transform.Translate(-position);

    SH.Clear();
    level->GetSHIndirectLightSamples(GetWorldPos(), indirectLightDist, transform, SH);

    if(shadowDecal)
    {
        Vect shadowRot = -shadowDecal->shadowRot.GetDirectionVector();
        Vect color(0.0f);

        if(SH.Num())
        {
            color += Vect(SH[0]) * shadowRot.x;
            color += Vect(SH[1]) * shadowRot.y;
            color += Vect(SH[2]) * shadowRot.z;
            color += Vect(SH[3]) * fabsf(shadowRot.x);
            color += Vect(SH[4]) * fabsf(shadowRot.y);
            color += Vect(SH[5]) * fabsf(shadowRot.z);
        }
        else
            color = 0.3f;
        shadowDecal->decalColor = Vect4(color);
        shadowDecal->decalColor.w = (color.x+color.y+color.z)/3.0f;
        shadowDecal->decalColor.w = MIN(shadowDecal->decalColor.w*0.5f, 1.0f);
        shadowDecal->decalColor.w = 1.0f-(shadowDecal->decalColor.w*0.8f);
    }

    traceOutFast;
}
Example #4
0
void
InitEffector::PrePhysicsUpdateInternal(float /*deltaTime*/)
{
    if ( ( mAction.get() == 0 ) ||
        (mGameState.get() == 0) ||
        (mAgentAspect.get() == 0)
        )
        {
            return;
        }

    boost::shared_ptr<InitAction> initAction =
        dynamic_pointer_cast<InitAction>(mAction);
    mAction.reset();

    if (initAction.get() == 0)
    {
        GetLog()->Error()
            << "ERROR: (InitEffector) cannot realize an unknown ActionObject\n";
        return;
    }

    // search for the AgentState
    boost::shared_ptr<AgentState> state = static_pointer_cast<AgentState>
        (mAgentAspect->GetChildOfClass("AgentState", true));

    if (state.get() == 0)
    {
        GetLog()->Error()
            << "ERROR: (InitEffector) cannot find AgentState\n";
        return;
    }

    // register the uniform number and team index to the GameStateAspect
    mGameState->RequestUniform
        (state, initAction->GetName(), initAction->GetNumber());

    TTeamIndex team = state->GetTeamIndex();

    // request an initial position for the agent and move it there
    Vector3f pos = mGameState->RequestInitPosition(team);
     // request the initial orientation for the agent and turn
    float angle = mGameState->RequestInitOrientation(team);

    SoccerBase::MoveAndRotateAgent(mAgentAspect, pos, angle);

#if 0
    Matrix mat;
    mat.RotationZ(gDegToRad(angle));

    // agents may be encapsulated in their own collision spaces, so we need
    // to get the parent of the parent of the agent aspect in this case
    boost::shared_ptr<Transform> parent = dynamic_pointer_cast<Transform>
        (mAgentAspect->GetParentSupportingClass("Transform").lock());
    
    
    if (parent.get() == 0)
	{
            GetLog()->Error()
                << "ERROR: (InitEffector) can not get parent of current agent aspect\n";

    	    return;
	}

    Leaf::TLeafList leafList;  

    // get absolute position of the agent
    Vector3f bodyPos = mAgentAspect->GetWorldTransform().Pos();

    // get all the bodies below the parent of the agent aspect
    parent->ListChildrenSupportingClass<Body>(leafList, true);

    if (leafList.size() == 0)
        {
            GetLog()->Error()
                << "ERROR: (InitEffector) agent aspect doesn't have "
                << "children of type Body \n";

            return;
        }

    Leaf::TLeafList::iterator iter = leafList.begin();
    
    // move all the child bodies to the new relative position
    for (iter;
         iter != leafList.end();
         ++iter
         )
        {    
            boost::shared_ptr<Body> childBody =
                dynamic_pointer_cast<Body>(*iter);
                
            Vector3f childPos = childBody->GetPosition();
            Matrix childR = childBody->GetRotation();
            childR = mat*childR;
            Vector3f newPos   = pos + (mat.Rotate(childPos-bodyPos));

            childBody->SetPosition(newPos);
            childBody->SetRotation(childR);
            childBody->SetVelocity(Vector3f(0,0,0));
            childBody->SetAngularVelocity(Vector3f(0,0,0));
        }
#endif
}
Example #5
0
int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
    SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
    glewInit();
#endif




    glViewport(0, 0, 640, 360);

    ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");

    Matrix projectionMatrix;
    Matrix modelMatrix;
    Matrix viewMatrix;




    GLuint skeletonTex = loadTexture("skeleton.png");
    GLuint nyanTex = loadTexture("nyan.png");
    GLuint eyeTex = loadTexture("eye.png");

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);



    SDL_Event event;
    bool done = false;
    while (!done) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
                done = true;
            }
            else if (event.type == SDL_KEYDOWN) {
                if (event.key.keysym.scancode == SDL_SCANCODE_SPACE) {
                    angle = 0.0f;
                    std::cout << "Space pressed!" << std::endl;
                }
            }
        }

        float ticks = (float)SDL_GetTicks() / 1000.0f;
        float elapsed = ticks - lastFrameTicks;
        lastFrameTicks = ticks;


        glClearColor(0.5f, 0.5f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        const Uint8 *keys = SDL_GetKeyboardState(NULL);

        if (keys[SDL_SCANCODE_LEFT]) {
            //go left
            std::cout << "Pressing Left" << std::endl;
            angle += elapsed;
        }
        else if (keys[SDL_SCANCODE_RIGHT]) {
            // go right
            std::cout << "Pressing Right" << std::endl;
            angle -= elapsed;
        }



        projectionMatrix.setOrthoProjection(-3.55f, 3.55f, -2.0f, 2.0f, -1.0f, 1.0f);
        //modelMatrix.identity();
        modelMatrix.Rotate(elapsed * 20 * (3.1415926f / 180.0f));

        program.setModelMatrix(modelMatrix);
        program.setProjectionMatrix(projectionMatrix);
        program.setViewMatrix(viewMatrix);

        glUseProgram(program.programID);





        //eye
        loadTexture("eye.png");

        float vertices1[] = {-0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f };
        //float vertices2[] = { -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f };

        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices1);
        glEnableVertexAttribArray(program.positionAttribute);

        float texCoords1[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f};
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords1);
        glEnableVertexAttribArray(program.texCoordAttribute);



        glDrawArrays(GL_TRIANGLES, 0, 12);


        //nyan
        loadTexture("nyan.png");

        float vertices3[] = { 1.5f, -0.5f, 2.5f, 0.5f, 1.5f, 0.5f, 1.5f, -0.5f, 2.5f, -0.5f, 2.5f, 0.5f };


        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices3);
        glEnableVertexAttribArray(program.positionAttribute);

        float texCoords3[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f };

        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords3);
        glEnableVertexAttribArray(program.texCoordAttribute);




        glDrawArrays(GL_TRIANGLES, 0, 12);

        //skeleton
        loadTexture("skeleton.png");

        float vertices5[] = { -1.5f, 0.5f, -1.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -1.5f, 0.5f };
        //float vertices6[] = { -1.5f, 0.5f, -1.5f, -0.5f, -0.5f, 0.5f };

        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices5);
        glEnableVertexAttribArray(program.positionAttribute);

        float texCoords5[] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f };

        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords5);
        glEnableVertexAttribArray(program.texCoordAttribute);



        glDrawArrays(GL_TRIANGLES, 0, 12);

        //test

        /*
        glBegin(GL_TRIANGLES);

        glVertex3f(-1.0f, 1.0f, 0.0f);
        glVertex3f(0.0f, 2.0f, 0.0f);
        glVertex3f(1.0f, 2.0f, 0.0f);

        glEnd();
        */




        glDisableVertexAttribArray(program.positionAttribute);
        glDisableVertexAttribArray(program.texCoordAttribute);

        SDL_GL_SwapWindow(displayWindow);

    }

    SDL_Quit();
    return 0;
}
Example #6
0
int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
	SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
	glewInit();
#endif
	float lastFrameTicks = 0.0f;
	glViewport(0, 0, 640, 360);
	ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
	Matrix projectionMatrix;
	Matrix modelMatrix;
	modelMatrix.Rotate(180 * (3.1415926 / 180.0));
	size_t angle = 0;
	Matrix viewMatrix;
	projectionMatrix.setOrthoProjection(-3.55, 3.55, -2.0f, 2.0f, -1.0f, 1.0f);
	SDL_Event event;
	bool done = false;
	glClearColor(0.4f, 0.2f, 0.8f, 1.0f);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	while (!done) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
				done = true;
			}
		}
		program.setModelMatrix(modelMatrix);
		program.setProjectionMatrix(projectionMatrix);
		program.setViewMatrix(viewMatrix);
		glUseProgram(program.programID);

		glClear(GL_COLOR_BUFFER_BIT);
		
		glEnableVertexAttribArray(program.positionAttribute);
		glEnableVertexAttribArray(program.texCoordAttribute);
		float vertices[] = { 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f};
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, vertices);
		LoadTexture("smiley1.png");
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.texCoordAttribute);
		glDisableVertexAttribArray(program.positionAttribute);
		
		glEnableVertexAttribArray(program.positionAttribute);
		glEnableVertexAttribArray(program.texCoordAttribute);
		float vertices2[] = { 2.0f, 2.0f, 1.0f, 1.0f, 2.0f, 1.0f, 2.0f, 2.0f, 1.0f, 2.0f, 1.0f, 1.0f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices2);
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, vertices2);
		LoadTexture("smiley2.png");
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.texCoordAttribute);
		glDisableVertexAttribArray(program.positionAttribute);
		
		glEnableVertexAttribArray(program.positionAttribute);
		glEnableVertexAttribArray(program.texCoordAttribute);
		float vertices3[] = { 3.0f, 1.0f, 2.0f, 0.0f, 3.0f, 0.0f, 3.0f, 1.0f, 2.0f, 1.0f, 2.0f, 0.0f };
		glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices3);
		glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, vertices3);
		LoadTexture("smiley3.png");
		glDrawArrays(GL_TRIANGLES, 0, 6);
		glDisableVertexAttribArray(program.texCoordAttribute);
		glDisableVertexAttribArray(program.positionAttribute);

		float ticks = (float)SDL_GetTicks() / 100.0f;
		float elapsed = ticks - lastFrameTicks;
		lastFrameTicks = ticks;
		angle += elapsed;
		modelMatrix.Rotate(angle * .1);
		SDL_GL_SwapWindow(displayWindow);
	}

	SDL_Quit();
	return 0;
}
//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
			{
				Graphics graphics(hdc);

				Color color;
				color.SetFromCOLORREF(params.m_ccBackground.rgbResult);
				graphics.Clear(color);

				HFONT fnIndirect = CreateFontIndirect(params.m_cf.lpLogFont);
				Font font(hdc, fnIndirect);
				if (params.m_bAntialiasing)
					graphics.SetSmoothingMode(SmoothingModeHighQuality);
				
				Matrix matrix;
				
				RectF rect;
				graphics.MeasureString(params.m_text.c_str(), params.m_text.length(), &font, params.m_pStart, &rect);
				matrix.Translate(params.m_pStart.X + rect.Width / 2, params.m_pStart.Y + rect.Height / 2);
				matrix.Rotate(params.m_fRotAngle);
				matrix.Scale(params.m_fScale, params.m_fScale, MatrixOrderAppend);
				graphics.MultiplyTransform(&matrix);

				StringFormat strformat;
				GraphicsPath path;
				FontFamily fnFamily;
				font.GetFamily(&fnFamily);
				path.AddString(params.m_text.c_str(), params.m_text.length(), &fnFamily, font.GetStyle(), font.GetSize(), PointF(-rect.Width / 2, -rect.Height / 2), strformat.GenericTypographic());

				color.SetFromCOLORREF(params.m_ccCircuit.rgbResult);
				Pen pen(color, 3);
				pen.SetLineJoin(LineJoinRound);

				graphics.DrawPath(&pen, &path);

				color.SetFromCOLORREF(params.m_ccFill.rgbResult);
				SolidBrush brush(color);
				graphics.FillPath(&brush, &path);
							
				Region region(&path);
				hRgn = region.GetHRGN(&graphics);

				graphics.ResetTransform();

				RECT rc;
				GetWindowRect(hWnd, &rc);

				POINT pt;
				pt.x = rc.left;
				pt.y = rc.top;
				ScreenToClient(hWnd, &pt);

				matrix.Translate((REAL)-pt.x, (REAL)-pt.y, MatrixOrderAppend);
				region.Transform(&matrix);
				
				HRGN hRgnFr = region.GetHRGN(&graphics);
				OffsetRect(&rc, -rc.left, -rc.top);

				/*HRGN hRgnWnd = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);

				CombineRgn(hRgn, hRgn, hRgnWnd, RGN_XOR);
				CombineRgn(hRgnFr, hRgnFr, hRgnWnd, RGN_XOR);*/
				SetWindowRgn(hWnd, params.m_bNonRectRg ? hRgnFr : NULL, TRUE);
			}
			EndPaint(hWnd, &ps);
        }
        break;
	case WM_LBUTTONDOWN:
		{
			int xPos = GET_X_LPARAM(lParam);
			int yPos = GET_Y_LPARAM(lParam);
				
			if (PtInRegion(hRgn, xPos, yPos))
			{
				DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_TEXT_PARAMS), hWnd, TextParams);
				InvalidateRect(hWnd, NULL, TRUE);
			}
		}
		break;
	case WM_ERASEBKGND:
		return TRUE;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
Example #8
0
	int main(int argc, char *argv[])
	{
		SDL_Init(SDL_INIT_VIDEO);
		displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
		SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
		SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
		glewInit();
#endif

		SDL_Event event;
		//glClearColor(0.4f, 0.2f, 0.4f, 1.0f);
		bool done = false;


		glViewport(0, 0, 640, 360);
		ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl", RESOURCE_FOLDER"fragment_textured.glsl");
		speed = 0.1;
		//music initilization
		Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096);
		Mix_Chunk *laserSound;
		laserSound = Mix_LoadWAV("laser1.wav");
		Mix_Chunk *explosionSound;
		explosionSound = Mix_LoadWAV("explosion1.wav");
		Mix_Music *music;
		music = Mix_LoadMUS("BoxCat_Games_-_10_-_Epic_Song.mp3");
		Mix_PlayMusic(music, -1);

		//texture initilization
		GLuint text = LoadTexture("font1.png");
		GLuint spriteSheetTexture = LoadTexture("sheet.png");
		int enemycount = 36;
		//player sprite
		SheetSprite mySprite(&program, spriteSheetTexture, 112.0f / 1024.0f, 791.0f / 1024.0f, 112.0f / 1024.0f, 75.0f /
			1024.0f, 0.3);
		SheetSprite enemyt1(&program, spriteSheetTexture, 423.0f / 1024.0f, 728.0f / 1024.0f, 93.0f / 1024.0f, 84.0f /
			1024.0f, 0.25);
		SheetSprite enemyt2(&program, spriteSheetTexture, 120.0f / 1024.0f, 604.0f / 1024.0f, 104.0f / 1024.0f, 84.0f /
			1024.0f, 0.25);
		SheetSprite enemyt3(&program, spriteSheetTexture, 144.0f / 1024.0f, 156.0f / 1024.0f, 103.0f / 1024.0f, 84.0f /
			1024.0f, 0.25);
		SheetSprite playerlaser(&program, spriteSheetTexture, 856.0f / 1024.0f, 421.0f / 1024.0f, 9.0f / 1024.0f, 54.0f /
			1024.0f, 0.2);
		SheetSprite enemylaser(&program, spriteSheetTexture, 858.0f / 1024.0f, 230.0f / 1024.0f, 9.0f / 1024.0f, 54.0f /
			1024.0f, 0.2);
		float lastFrameTicks = 0.0f;
		float pxpos = 0;
		float enemyydisp = 0;//enemy y displacement
		float distanceToTravel = 1.2;
		float bulletSpeed = 2.7;
		bool canShoot = true;
		bool enemyShoot = true;
		Matrix projectionMatrix;
		Matrix modelMatrix;
		Matrix viewMatrix;
		Entity playerBullet(100, 100, 0, playerlaser, 1, 1);
		Entity enemyBullet(-100, -100, 0, enemylaser, 1, 1);
	
		vector<Entity> enemies;
		for (int i = 0; i < 36; i++){
			SheetSprite* enemysprite = &enemyt1;
			if (i > 11 && i < 24){ enemysprite = &enemyt2; }
			else if (i > 23){ enemysprite = &enemyt3; }
			Entity enemy(-3.2 + ((i % 12) / 2.0f), 1.8 - (i / 12) / 3.0f, M_PI, *enemysprite, 10, 10);
			enemies.push_back(enemy);
		}
		projectionMatrix.setOrthoProjection(-3.55f, 3.55f, -2.0f, 2.0f, -1.0f, 1.0f);
		glUseProgram(program.programID);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		enum{ START_SCREEN, GAME_SCREEN,WEND_SCREEN,LEND_SCREEN };
		int state = START_SCREEN;
		while (!done) {
			while (SDL_PollEvent(&event)) {
				if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
					done = true;
				}


			}
			glClear(GL_COLOR_BUFFER_BIT);
			const Uint8 *keys = SDL_GetKeyboardState(NULL);
			float vertices[] = { -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5 };
			float texCoords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
			program.setProjectionMatrix(projectionMatrix);
			program.setViewMatrix(viewMatrix);
			switch (state){

			case START_SCREEN:
				//text
				modelMatrix.identity();
				//modelMatrix.Scale(.6, .6, 0);
				modelMatrix.Translate(-1.5, 1, 0);
				program.setModelMatrix(modelMatrix);



				//glBindTexture(GL_TEXTURE_2D, ship2texB);

				glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
				glEnableVertexAttribArray(program.positionAttribute);

				glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
				glEnableVertexAttribArray(program.texCoordAttribute);
				DrawText(&program, text, "SPACE INVADERS", .5f, -.27f);
				modelMatrix.identity();

				modelMatrix.Translate(-1.5, -0.33, 0);
				program.setModelMatrix(modelMatrix);



				DrawText(&program, text, "press Enter to begin", .4f, -.25f);


				//glDrawArrays(GL_TRIANGLES, 0, 6);
				glDisableVertexAttribArray(program.positionAttribute);
				glDisableVertexAttribArray(program.texCoordAttribute);
				if (keys[SDL_SCANCODE_RETURN]){
					lastFrameTicks = (float)SDL_GetTicks() / 1000.0f;//contains start time of the program so that ticks starts at zero rather than actual time since start of the program
					state = GAME_SCREEN;
					
				}
				break;
			case WEND_SCREEN:

				//text
				modelMatrix.identity();
				//modelMatrix.Scale(.6, .6, 0);
				modelMatrix.Translate(-1.5, 1, 0);
				program.setModelMatrix(modelMatrix);




				glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
				glEnableVertexAttribArray(program.positionAttribute);

				glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
				glEnableVertexAttribArray(program.texCoordAttribute);
				DrawText(&program, text, "YOU WIN!", .5f, -.27f);
				modelMatrix.identity();

				modelMatrix.Translate(-1.5, -0.33, 0);
				program.setModelMatrix(modelMatrix);



				DrawText(&program, text, "press Enter to close", .4f, -.25f);


				//glDrawArrays(GL_TRIANGLES, 0, 6);
				glDisableVertexAttribArray(program.positionAttribute);
				glDisableVertexAttribArray(program.texCoordAttribute);
				if (keys[SDL_SCANCODE_RETURN]){
					done = true;
				}
				break;
			case LEND_SCREEN:

				//text
				modelMatrix.identity();
				//modelMatrix.Scale(.6, .6, 0);
				modelMatrix.Translate(-1.5, 1, 0);
				program.setModelMatrix(modelMatrix);




				glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
				glEnableVertexAttribArray(program.positionAttribute);

				glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
				glEnableVertexAttribArray(program.texCoordAttribute);
				DrawText(&program, text, "YOU LOST.", .5f, -.27f);
				modelMatrix.identity();

				modelMatrix.Translate(-1.5, -0.33, 0);
				program.setModelMatrix(modelMatrix);



				DrawText(&program, text, "press Enter to close", .4f, -.25f);


				//glDrawArrays(GL_TRIANGLES, 0, 6);
				glDisableVertexAttribArray(program.positionAttribute);
				glDisableVertexAttribArray(program.texCoordAttribute);
				if (keys[SDL_SCANCODE_RETURN]){
					done = true;
				}
				break;

			case GAME_SCREEN:
				float ticks = (float)SDL_GetTicks() / 1000.0f ;
				float elapsed = ticks - lastFrameTicks;
				lastFrameTicks = ticks;

				if (keys[SDL_SCANCODE_RIGHT]){
					if (pxpos < 3.3){
						pxpos += elapsed*distanceToTravel;
					}
				}
				if (keys[SDL_SCANCODE_LEFT]){
					if (pxpos > -3.3){
						pxpos -= elapsed*distanceToTravel;
					}

				}
				if (keys[SDL_SCANCODE_SPACE]){
					if (canShoot){
						canShoot = false;
						playerBullet.x = pxpos;
						playerBullet.y = -1.8;
						Mix_PlayChannel(-1, laserSound, 0);

						/*playerBullet.x = enemies[3].x;
						playerBullet.y = enemies[3].y; */
					}

				}
				//player rendering
				modelMatrix.identity();

				modelMatrix.Translate(pxpos, -1.8, 0);
				//modelMatrix.Scale(1.5, 1.5, 0);
				program.setModelMatrix(modelMatrix);
				mySprite.Draw();

				//enemy rendering
				for (int j = 0; j < 36; j++){
					if (!enemies[j].dead){
						modelMatrix.identity();
						enemies[j].x += elapsed * speed;
						if (enemies[j].x > 3.3){
							speed *= -1;
							for (int k = 0; k < enemies.size(); k++){
								enemies[k].y -= .2;
							}
						}
						else if (enemies[j].x < -3.3){
							speed *= -1;
							for (int k = 0; k < enemies.size(); k++){
								enemies[k].y -= .2;
							}
						}
						if (!((enemies[j].y - .1) >= (-1.8 + .1) || (enemies[j].y + .1) <= (-1.8 - .1) || (enemies[j].x - .1) >= (pxpos + .1) || (enemies[j].x + .1) <= (pxpos - .1))){
							state = LEND_SCREEN;
						}
						if (rand() % 500 == 1 && enemyShoot== true){ 
							enemyShoot = false;
							enemyBullet.x = enemies[j].x;
							enemyBullet.y = enemies[j].y;
						}
						modelMatrix.Translate(enemies[j].x, enemies[j].y, 0);
						modelMatrix.Rotate(enemies[j].rotation);
						program.setModelMatrix(modelMatrix);
						enemies[j].Draw();
					}
				}
				//player bullet rendering
				playerBullet.y += elapsed * bulletSpeed;
				if (playerBullet.y > 2){
					canShoot = true;
				}
				for (int j = 0; j < enemies.size(); j++){
					if (playerBullet.collide(enemies[j])&& enemies[j].dead == false){
						playerBullet.x = 100;
						playerBullet.y = 100;
						enemies[j].dead = true;
						enemycount--;
						Mix_PlayChannel(-1, explosionSound, 0);

						if (speed > 0){ speed += 0.02; }
						else{ speed -= 0.02; }
						canShoot = true;
						//done = true;
					}
					
				}
				if (playerBullet.collide(enemyBullet)){
					playerBullet.x = 100;
					playerBullet.y = 100;
					enemyBullet.x = -100;
					enemyBullet.y = -100;
					canShoot = true;
					enemyShoot = true;
				}
				if (enemycount == 0){
					state = WEND_SCREEN;
				}
				modelMatrix.identity();

				modelMatrix.Translate(playerBullet.x, playerBullet.y , 0);
				program.setModelMatrix(modelMatrix);
				playerBullet.Draw();


				//enemy bullet
				enemyBullet.y -= bulletSpeed* elapsed;
				if (enemyBullet.y < -2){
					enemyBullet.y =  -100;
					enemyShoot = true;
				}
				if (!((enemyBullet.y - .1) >= (-1.8 + .1) || (enemyBullet.y + .1) <= (-1.8 - .1) || (enemyBullet.x - .1) >= (pxpos + .1) || (enemyBullet.x + .1) <= (pxpos - .1))){
					Mix_PlayChannel(-1, explosionSound, 0);
					state = LEND_SCREEN;
				}
				modelMatrix.identity();

				modelMatrix.Translate(enemyBullet.x, enemyBullet.y, 0);
				modelMatrix.Rotate(M_PI);
				program.setModelMatrix(modelMatrix);
				enemyBullet.Draw();




				break;

				
			}

			



			SDL_GL_SwapWindow(displayWindow);
		}

		SDL_Quit();
		return 0;
	}
Example #9
0
int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 360, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
    SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
    glewInit();
#endif
    
    
    
    //setup
    glViewport(0, 0, 640, 360);
    
    ShaderProgram program(RESOURCE_FOLDER"vertex_textured.glsl",RESOURCE_FOLDER"fragment_textured.glsl");
    
    //textures
    GLuint medalTexture = LoadTexture("flat_medal1.png");
    
    Matrix medalProjectionMatrix;
    Matrix medalModelMatrix;
    Matrix medalViewMatrix;
    
    medalProjectionMatrix.setOrthoProjection(-3.55,3.55,-2.0f,2.0f,-1.0f, 1.0f);
    
    GLuint smileyTexture = LoadTexture("happy.gif");
    
    Matrix smileyProjectionMatrix;
    Matrix smileyModelMatrix;
    Matrix smileyViewMatrix;
    
    smileyProjectionMatrix.setOrthoProjection(-3.55,3.55,-2.0f,2.0f,-1.0f, 1.0f);
    
    
    GLuint diceTexture = LoadTexture("dieWhite_border1.png");
    
    
    Matrix diceProjectionMatrix;
    Matrix diceModelMatrix;
    Matrix diceViewMatrix;
    
    diceProjectionMatrix.setOrthoProjection(-3.55,3.55,-2.0f,2.0f,-1.0f, 1.0f);

    
    glUseProgram(program.programID);
    
    
    //x, y, z locations
    
    float medalX = 0.0f;
    float medalY = 0.0f;
    float medalZ = 0.0f;
    
    float smileyX = -0.8f;
    float smileyY = -0.8f;
    float smileyZ = 0.0f;

    
    
    
    
    float lastFrameTicks = 0.0f;

    
    SDL_Event event;
    bool done = false;
    while (!done) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
                done = true;
            }
        }
        
        glClear(GL_COLOR_BUFFER_BIT);
        
        
        //get the time
        
        float ticks = (float)SDL_GetTicks()/1000.0f;
        float elapsed = ticks - lastFrameTicks;
        lastFrameTicks = ticks;
        

        //generate the medal
        
        program.setModelMatrix(medalModelMatrix);
        program.setProjectionMatrix(medalProjectionMatrix);
        program.setViewMatrix(medalViewMatrix);
        
        glBindTexture(GL_TEXTURE_2D, medalTexture);
        
        float vertices[] = {-0.2, -0.2, 0.2, -0.2, 0.2, 0.2, -0.2, -0.2, 0.2, 0.2, -0.2, 0.2};
        
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
        glEnableVertexAttribArray(program.positionAttribute);
        
        float texCoord[] = {0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0};
        
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoord);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        
        glDisableVertexAttribArray(program.positionAttribute);
        glDisableVertexAttribArray(program.texCoordAttribute);
        
        float angleRotation = angleRotation + elapsed * (0.1*3.14159621/180);
        
        medalModelMatrix.Rotate(angleRotation);
        
        
        
        //generate the happy face texture
        program.setModelMatrix(smileyModelMatrix);
        program.setProjectionMatrix(smileyProjectionMatrix);
        program.setViewMatrix(smileyViewMatrix);
        
        glBindTexture(GL_TEXTURE_2D, smileyTexture);
        
        //start the texture in the center of the screen and then translate it down by 0.5
        float smileyVertex[] = {-0.2, -0.2, 0.2, -0.2, 0.2, 0.2, -0.2, -0.2, 0.2, 0.2, -0.2, 0.2};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, smileyVertex);
        glEnableVertexAttribArray(program.positionAttribute);
        
        float smileyTexCoord[] = {0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0};
        
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, smileyTexCoord);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        glDisableVertexAttribArray(program.positionAttribute);
        glDisableVertexAttribArray(program.texCoordAttribute);
        
        smileyProjectionMatrix.identity();
        smileyProjectionMatrix.Translate(0, -0.5, 0.0);
        
        float smileyXTranslate = elapsed*1;
        smileyX = smileyX + smileyXTranslate;
        
        if (smileyX > 1.0)
        {
            smileyX = -smileyX;
        }
        
        smileyProjectionMatrix.Translate(smileyX,0, 0);
        
        
        
        //generate the dice
        program.setModelMatrix(diceModelMatrix);
        program.setProjectionMatrix(diceProjectionMatrix);
        program.setViewMatrix(diceViewMatrix);
        
        glBindTexture(GL_TEXTURE_2D, diceTexture);
        
        //start the texture in the center of the screen and then translate it up by 1.0
        float diceVertices[] = {-0.2, -0.2, 0.2, -0.2, 0.2, 0.2, -0.2, -0.2, 0.2, 0.2, -0.2, 0.2};
        
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, diceVertices);
        glEnableVertexAttribArray(program.positionAttribute);
        
        float dicetexCoord[] = {0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0};
        
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, dicetexCoord);
        glEnableVertexAttribArray(program.texCoordAttribute);
        
        
        glDrawArrays(GL_TRIANGLES, 0, 6);
        
        
        glDisableVertexAttribArray(program.positionAttribute);
        glDisableVertexAttribArray(program.texCoordAttribute);
        
        float diceScale = 1+sin(ticks);
        
        std::cout << diceScale << std::endl;
        diceModelMatrix.identity();
        diceModelMatrix.Translate(0.0, 1.0, 0.0);
        diceModelMatrix.Scale(diceScale, diceScale, 0.0);
        
        SDL_GL_SwapWindow(displayWindow);
        
    }
    
    SDL_Quit();
    return 0;
}
Example #10
0
bool CGdiPlusImage::DrawImage(CDC* pDC, CRect& rcDraw, CRect& rcCanvas)
{
    //´´½¨ÆÁÄ»
    ASSERT(pDC!=NULL); 
    if ( !pDC ) return false; 

    Bitmap bmp(rcCanvas.Width(), rcCanvas.Height()); 
    Graphics* pGraphics = Graphics::FromImage(&bmp); 
    if ( !pGraphics ) return false;

    //GraphicsContainer Containter = pGraphics->BeginContainer();
    pGraphics->SetSmoothingMode(SmoothingModeHighQuality); 

    SolidBrush bgbrush(Color(238, 243, 250));
    pGraphics->FillRectangle(&bgbrush, 0, 0, rcCanvas.Width(), rcCanvas.Height());
     
    if ( m_pImage )
    {
        //»ñÈ¡ÊôÐÔ 
        INT nImageWidth  = m_pImage->GetWidth();
        INT nImageHeight = m_pImage->GetHeight(); 


        float fMin = 1.0F;  
        bool bZoomOut = false; 
        int nXSrc = 0;
        int nYSrc = 0;  
         
        //¹¹ÔìλÖÃ
        RectF rcDrawRect;  
        rcDrawRect.X = rcDraw.left;
        rcDrawRect.Y = rcDraw.top;
        rcDrawRect.Width = rcDraw.Width();
        rcDrawRect.Height = rcDraw.Height(); 

        // »­±Ê
        Pen pen(Color(255, 0, 0), 2); 

        Matrix mtr;
        mtr.Translate(rcDrawRect.X + rcDrawRect.Width / 2, rcDrawRect.Y + rcDrawRect.Height / 2);
        mtr.Rotate(m_nRotateAngle);
        mtr.Translate(-(rcDrawRect.X + rcDrawRect.Width / 2), -(rcDrawRect.Y + rcDrawRect.Height / 2));

        pGraphics->SetTransform(&mtr); 

        //»æ»­Í¼Ïñ 
        pGraphics->DrawImage(
            m_pImage, rcDrawRect,
            nXSrc, nYSrc,
            (REAL)nImageWidth - nXSrc * 2, (REAL)nImageHeight - nYSrc * 2, UnitPixel);  
         

        for ( int i = 0; i < m_vvecPoints.size(); ++ i )
        {
            int nCount = m_vvecPoints[i].size(); 
            Point *pts = new Point[nCount];

            for ( int j = 0; j < nCount; ++ j )
            {
                pts[ j ] = m_vvecPoints[ i ][ j ]; 
                pts[ j ].X += rcDraw.left;
                pts[ j ].Y += rcDraw.top; 
            }  

            //for ( int j = 0; j < nCount - 1; ++ j )
            { 
               // pGraphics->DrawLine(&pen, pts[ j ], pts[ j +1 ]); 
            } 

  
                pGraphics->DrawLines(&pen, pts, nCount);
                //pGraphics->DrawPolygon(&pen, pts, nCount); 
        


            delete[] pts;
            pts = NULL; 
        }


        pGraphics->ResetTransform();   
        //pGraphics->EndContainer(Containter);

    } // if ( m_pImage )

    // ´ÓÄڴ濽±´ÖÁÉ豸
    Graphics graphicsreal(pDC->GetSafeHdc()); 
    graphicsreal.DrawImage(&bmp, 0, 0, rcCanvas.Width(), rcCanvas.Height()); 
    delete pGraphics;
    pGraphics = NULL;

    return true;
}
Example #11
0
void D3D10System::DrawSpriteExRotate(Texture *texture, DWORD color, float x, float y, float x2, float y2, float degrees, float u, float v, float u2, float v2, float texDegrees)
{
    if(!curPixelShader)
        return;

    if(!texture)
    {
        AppWarning(TEXT("Trying to draw a sprite with a NULL texture"));
        return;
    }

    HANDLE hColor = curPixelShader->GetParameterByName(TEXT("outputColor"));

    if(hColor)
        curPixelShader->SetColor(hColor, color);

    //------------------------------
    // crop positional values

    Vect2 totalSize = Vect2(x2-x, y2-y);
    Vect2 invMult   = Vect2(totalSize.x < 0.0f ? -1.0f : 1.0f, totalSize.y < 0.0f ? -1.0f : 1.0f);
    totalSize.Abs();

    if(y2-y < 0) {
        float tempFloat = curCropping[1];
        curCropping[1] = curCropping[3];
        curCropping[3] = tempFloat;
    }

    if(x2-x < 0) {
        float tempFloat = curCropping[0];
        curCropping[0] = curCropping[2];
        curCropping[2] = tempFloat;
    }

    x  += curCropping[0] * invMult.x;
    y  += curCropping[1] * invMult.y;
    x2 -= curCropping[2] * invMult.x;
    y2 -= curCropping[3] * invMult.y;

    //------------------------------
    // crop texture coordinate values

    float cropMult[4];
    cropMult[0] = curCropping[0]/totalSize.x;
    cropMult[1] = curCropping[1]/totalSize.y;
    cropMult[2] = curCropping[2]/totalSize.x;
    cropMult[3] = curCropping[3]/totalSize.y;

    Vect2 totalUVSize = Vect2(u2-u, v2-v);
    u  += cropMult[0] * totalUVSize.x;
    v  += cropMult[1] * totalUVSize.y;
    u2 -= cropMult[2] * totalUVSize.x;
    v2 -= cropMult[3] * totalUVSize.y;

    //------------------------------
    // draw

    VBData *data = spriteVertexBuffer->GetData();
    data->VertList[0].Set(x,  y,  0.0f);
    data->VertList[1].Set(x,  y2, 0.0f);
    data->VertList[2].Set(x2, y,  0.0f);
    data->VertList[3].Set(x2, y2, 0.0f);

    if (!CloseFloat(degrees, 0.0f)) {
        List<Vect> &coords = data->VertList;

        Vect2 center(x+totalSize.x/2, y+totalSize.y/2);

        Matrix rotMatrix;
        rotMatrix.SetIdentity();
        rotMatrix.Rotate(AxisAngle(0.0f, 0.0f, 1.0f, RAD(degrees)));

        for (int i = 0; i < 4; i++) {
            Vect val = coords[i]-Vect(center);
            val.TransformVector(rotMatrix);
            coords[i] = val;
            coords[i] += Vect(center);
        }
    }

    List<UVCoord> &coords = data->UVList[0];
    coords[0].Set(u,  v);
    coords[1].Set(u,  v2);
    coords[2].Set(u2, v);
    coords[3].Set(u2, v2);

    if (!CloseFloat(texDegrees, 0.0f)) {
        Matrix rotMatrix;
        rotMatrix.SetIdentity();
        rotMatrix.Rotate(AxisAngle(0.0f, 0.0f, 1.0f, -RAD(texDegrees)));

        Vect2 minVal = Vect2(0.0f, 0.0f);
        for (int i = 0; i < 4; i++) {
            Vect val = Vect(coords[i]);
            val.TransformVector(rotMatrix);
            coords[i] = val;
            minVal.ClampMax(coords[i]);
        }

        for (int i = 0; i < 4; i++)
            coords[i] -= minVal;
    }

    spriteVertexBuffer->FlushBuffers();

    LoadVertexBuffer(spriteVertexBuffer);
    LoadTexture(texture);

    Draw(GS_TRIANGLESTRIP);
}
Example #12
0
int main(int argc, char *argv[]) {
//    Initialize SDL
    SDL_Init(SDL_INIT_VIDEO);
    displayWindow = SDL_CreateWindow("Homework 1", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(displayWindow);
    SDL_GL_MakeCurrent(displayWindow, context);
#ifdef _WINDOWS
    glewInit();
#endif
    
//    Define shader program
    ShaderProgram program(RESOURCE_FOLDER "vertex_textured.glsl", RESOURCE_FOLDER "fragment_textured.glsl");
    glViewport(0, 0, 800, 600);
    
//    Load textures
    GLuint emoji = LoadTexture("emoji.png");
    GLuint alien = LoadTexture("p1_front.png");
    GLuint medal = LoadTexture("flat_medal6.png");
//    Enable blending
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glUseProgram(program.programID);
    
//    Initialize local variables
    float lastFrameTicks = 0.0f;
    float angle = 0.0f;
    float X,Y,moveX, moveY = 0.0;
    
    SDL_Event event;
    bool done = false;
    while (!done) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
                done = true;
            }
        }
//        Define matrixes
        program.setProjectionMatrix(projectionMatrix);
        program.setModelMatrix(modelMatrix);
        program.setViewMatrix(viewMatrix);
        
//        Calculate elapsed time
        float ticks = (float)SDL_GetTicks()/1000.0f;
        float elapsed = ticks - lastFrameTicks;
        lastFrameTicks = ticks;
        
//        Calculate angle based on elapsed time
        angle += elapsed;
        if (X < .35 && Y < .35) {
            X += elapsed/2;
            Y += elapsed/2;
        }
        else if (X < -.35 && Y < -.35) {
            X -= elapsed/2;
            Y -= elapsed/2;
        }
        
//        Initialize keyboard state
        const Uint8 *keys = SDL_GetKeyboardState(NULL);
        
//        Clear and set color
        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        
//        Draw alien texture
//        glActiveTexture(GL_TEXTURE1);
        modelMatrix.identity();
        modelMatrix.Scale(.2, .2, 1.0);
        program.setModelMatrix(modelMatrix);
        glBindTexture(GL_TEXTURE_2D, alien);
        float vertices1[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
        float texCoords1[] = {0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices1);
        glEnableVertexAttribArray(program.positionAttribute);
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords1);
        glEnableVertexAttribArray(program.texCoordAttribute);
        glEnable(GL_TEXTURE_2D);
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glDisableVertexAttribArray(program.positionAttribute);
        glDisableVertexAttribArray(program.texCoordAttribute);
        
//        Draw medal texture
//        glActiveTexture(GL_TEXTURE2);
        modelMatrix.identity();
        modelMatrix.Scale(.05, .05, 1.0);
        modelMatrix.Translate(0, -.95, 0);
        program.setModelMatrix(modelMatrix);
        glBindTexture(GL_TEXTURE_2D, medal);
        float vertices2[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
        float texCoords2[] = {0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices2);
        glEnableVertexAttribArray(program.positionAttribute);
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords2);
        glEnableVertexAttribArray(program.texCoordAttribute);
        glEnable(GL_TEXTURE_2D);
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glDisableVertexAttribArray(program.positionAttribute);
        glDisableVertexAttribArray(program.texCoordAttribute);
        
//        Draw emoji texture
//        glActiveTexture(GL_TEXTURE0);
        modelMatrix.identity();
        modelMatrix.Scale(X*2, Y*2, 1.0);
        modelMatrix.Rotate((angle*100.0f) * M_PI/180);
        if(keys[SDL_SCANCODE_SPACE]) {
            modelMatrix.identity();
            modelMatrix.Scale(X*2, Y*2, 1.0);
        }
        modelMatrix.Translate(X*1.5, Y*1.5, 0);
        program.setModelMatrix(modelMatrix);
        glBindTexture(GL_TEXTURE_2D, emoji);
        float vertices[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5};
        float texCoords[] = {0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0};
        glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
        glEnableVertexAttribArray(program.positionAttribute);
        glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
        glEnableVertexAttribArray(program.texCoordAttribute);
        glEnable(GL_TEXTURE_2D);
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glDisableVertexAttribArray(program.positionAttribute);
        glDisableVertexAttribArray(program.texCoordAttribute);
        
        SDL_GL_SwapWindow(displayWindow);
    }
    
    SDL_Quit();
    return 0;
}