Darshan Nayak

Full Stack Developer

OpenGL Game

Languages/Libraries: C++, OpenGL, glut

This application was a culmination of 5 assignments for CS809 (Interactive Entertainment Software) at University of Regina.

Features: Procedurally Generated Game World, Game Physics with Gravity, Collision Detection, Friction, and Traction, and AI components like Path Finding for NPCs using A* and Meet-in-Middle search algorithms.

Game.cpp

Following function prepares a square texture for Red Disks in the game world.

						
void initRedRockHeightmapDisplayList(int currDiskNumber)	//initializes Heightmap for Red Disk initializes Heightmap for Red Disk 
{
	redRockHeightMap_list[currDiskNumber].begin();
	glEnable(GL_TEXTURE_2D);
	//TextureManager::activate("Rainbow.bmp");
	TextureManager::activate("Models/DiskA.bmp");
	glColor3d(1.0, 1.0, 1.0);
	for (unsigned int x0 = 0; x0 < REDROCK_HEIGHTMAP_SIZE; x0++)
	{
		unsigned int x1 = x0 + 1;
		float tex_x0 = (float)(x0) / REDROCK_HEIGHTMAP_SIZE;
		float tex_x1 = (float)(x1) / REDROCK_HEIGHTMAP_SIZE;
		glBegin(GL_TRIANGLE_STRIP);
		for (unsigned int z = 0; z <= REDROCK_HEIGHTMAP_SIZE; z++)
		{
			float tex_z = (float)(z) / REDROCK_HEIGHTMAP_SIZE;
			glTexCoord2d(tex_x1, tex_z);
			glVertex3d(x1, redRockHeights[x1][z], z);
			glTexCoord2d(tex_x0, tex_z);
			glVertex3d(x0, redRockHeights[x0][z], z);
		}
		glEnd();
	}
	glDisable(GL_TEXTURE_2D);
	redRockHeightMap_list[currDiskNumber].end();
}
						
                  

Following code snippet shows how the skybox, the player character, and a red disk element is displayed in the game world.

						
//FUNCTION SNIPPET; NOT THE WHOLE CODE OF THE FUNCTION
void Game::displayGame(int w,int h)	//displays the game with complete world, player character, and pickups
{
	//SKYBOX; THE BACKGROUND OF THE GAME WORLD
	glPushMatrix();
		glTranslated(camPos.x, camPos.y + 0.4, camPos.z);
		//glScaled(600.0, 600.0, 600.0);
		glDepthMask(GL_FALSE);
		skybox_list.draw();
		glDepthMask(GL_TRUE);
	glPopMatrix();

	//PLAYER; THE PLAYER'S MODEL
	glPushMatrix();
		glTranslated(playerPosVector.x, playerPosVector.y, playerPosVector.z);
		glRotated(-90 + playerAngleDeg, 0.0, 1.0, 0.0);
		/*cout << playerStandingOnDisk << '\n';*/
		player_list.draw();
	glPopMatrix();

	//GAME WORLD CONSISTS OF DISKS; WE ARE GENERATING THEM BELOW
	for (int currDiskNumber = 0;currDiskNumber < totalDisks;currDiskNumber++)
	{
		glPushMatrix();
			glTranslated(disk[currDiskNumber].xPos, disk[currDiskNumber].yPos, disk[currDiskNumber].zPos);
			glScaled(disk[currDiskNumber].radius, 1.0, disk[currDiskNumber].radius);
			if (disk[currDiskNumber].surfaceType == 1)	//draw disks
			{
				redRock_list.draw();
				glPushMatrix();
					double scale = sqrtf(2) / REDROCK_HEIGHTMAP_SIZE;
					glTranslated(-REDROCK_HEIGHTMAP_SIZE / 2 * scale, ZERO, -REDROCK_HEIGHTMAP_SIZE / 2 * scale);
					glScaled(scale, 1.0, scale);
					//redRockHeightMap_list.draw();
					redRockHeightMap_list[currDiskNumber].draw();
				glPopMatrix();
			}
		glPopMatrix();
	}
			
}
						
                  

Following code snippet shows how the player character is updated (jumping/falling-off) in a gravity simulated game world.

						
//FUNCTION SNIPPET; NOT THE WHOLE CODE OF THE FUNCTION
void Game::updatePlayer()	//function updates the player chatacter
{
		else//fallen off disk; considered jumping
		{
			isPlayerInAir = true;
			playerVelocityVector.y -= 9.8*FIXED_DELTA_TIME;//simulating gravity
		}
	}
	else//jumping actively
	{
		playerVelocityVector.y -= 9.8*FIXED_DELTA_TIME;
		if (((playerPosVector.y - ZERO_POINT_8) < heightMapHeightUnderPlayer) && playerStandingOnDisk != -1)	//standing on disk
		{
			if (wasPlayerWithinDiskAreaLastFrame)
			{
				//landing
				isPlayerInAir = false;
				playerPosVector.y = ZERO_POINT_8 + heightMapHeightUnderPlayer;
				playerVelocityVector.y = 0.0;
			}
			else//hit side of disk, must fall straight down
			{
				playerPosVector -= (playerVelocityVector * FIXED_DELTA_TIME);
				playerVelocityVector.x = 0.0;
				playerVelocityVector.z = 0.0;
			}
		}
	}
	playerPosVector += (playerVelocityVector * FIXED_DELTA_TIME);
}