Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Big HW 1 ACG

OpenGL – Basic 2D and 3D applications

• For this project you will work in teams of 2-3 people.

• The homework must be uploaded on Moodle respecting the deadline. The


homework must be submitted until the 21st of November 2022, 8:00. No late
submissions will be accepted.

• Two identical projects found in different teams or any approximate project taken
from the Internet will be graded with 0.

• The evaluation of the project will take place during the first lab after the submission
deadline (lab 8). Presence at the lab will be mandatory for presenting your projects.
If you are not present, you will get 0 points for it. Only for exceptional situations
(e.g. proven medical issues presented to us via email), you will be able to present at
a later date (e.g. during the 9th week).

• The final submission will be an archive with your VS project and it should contain a
README file in which you will shortly specify all the functional sections of the hw.
Additionally, if you have parts that don’t work, you may offer solution ideas for a
partial score.

• If you have any questions, please use the special channel created on teams
(course ACG team, to have students from both computer science and electronics
there). Don’t forget to mention your lab assistant (e.g. @Iulia-CristinaStanica) so that
we are notified about your question.

• You MUST use the same structure of an OpenGL program that we used at the labs.
You MUST use modern OpenGL (this includes shaders, vbo, ibo, vao and so on).
Deprecated OpenGL functions are not allowed in your code and will lead to the loss
of all (or most of) the homework’s points!

WRONG (old) example of drawing a square:

glBegin( GL_POLYGON ); /* Begin issuing a polygon */ glColor3f( 0, 1, 0 ); /* Set the current color to green */ glVertex3f( -1, -1,
0 ); /* Issue a vertex */

glVertex3f( -1, 1, 0 ); /* Issue a vertex */

glVertex3f( 1, 1, 0 ); /* Issue a vertex */

glVertex3f( 1, -1, 0 ); /* Issue a vertex */

glEnd(); /* Finish issuing the polygon */

CORRECT (modern) example of drawing a square found in Lab 2


Tasks

Ex 1) 2D “top down shooter” game (5p)


By using C++ and OpenGL (including glfw libraries), write a program that simulates a 2D top
down shooter game. Imagine a simplified version of “Space Asteroids Arcade” (picture below
just for reference, it shouldn’t look like this :) ).

Your program should have the following functionalities:


- Representation of the spaceship with a basic shape of your choice (e.g. triangle)
- Movement of the ship with W A S D
- If the user presses the mouse left click, it will shoot a bullet (e.g. ellipse / circle); you have
the freedom of choosing in which direction you want to spawn the bullet; a hit should be
signaled by a message in the console, as well as its “damage”
- If the user presses the mouse right click, an enemy will be spawned (For maximum points
you have to spawn waves over time and signal the “difficulty increase” in some way, either a
message in the console or by changing the color of the background)
-The enemies will move towards the player and will deal damage to the player on impact and
will disappear.
- The player can’t get out of the screen bounds
- As time passes, the enemies will get stronger (either by how many are spawned or by
frequency of spawning - your choice), and they will require more bullets to be killed (aka they
have more health) and will deal more damage on impact; higher difficulty enemies will have
different colors.

Window creation + correctly rendering basic objects on the screen (ship, bullets,
enemies), with different colors: 1p
Handling user input (keyboard and mouse): 1p
Movement for user, enemies, bullets: 0.5px3 = 1.5p (not respecting screen
boundaries: -0.25p)
Damage on impact and bullet damage (collisions): 0.5p
Progressive difficulty : 0.5p (no of spawned enemies / frequency + damage & health)
Spawning in waves: 0.5p

Bonus task (0.5p): Make the enemies spawn on a circle that is outside the screen
boundaries, from all directions.
Ex 2) Simulate a petanque game in 3D (5p)

Petanque is a French ball-throwing game (balls = “boules”). The objective is to score points
by having boules closer to the target than the opponent after all boules have been thrown.
This is achieved by throwing or rolling boules closer to the small target ball, officially called a
jack. There are two teams (red and blue), each one having three boules. After three rounds,
the team that has the highest number of points wins. By using C++ and OpenGL (including
glfw libraries), write a program that simulates a 3D petanque game.

Obs: You will get no points if you do the game in 2D (top view)!

- Display a brown plane (the ground) and one initial small yellow ball (the jack)
- The throwing ball (blue or red, alternatively) is spawned each turn in the initial
position at key press (of your choice) - Figure A.
- By pressing the arrow keys, the user can translate their ball in the terrain’s plane
(left-right only, NOT up-down) before launching it
- When the “P” key is pressed, the throwing ball will be “launched” on the “forward”
direction (translation) and will have a rotation movement around its own center with a
certain intensity of your choice
- Add the camera that we implement together during lab 6 so that we can view the
balls from an appropriate angle (figure A below) and we can translate (and ideally,
also rotate) the camera on all 3 axes
- Calculate the score by finding how many balls of a team are closest to the jack at a
round’s end (3 throws for each team per round); display it in the console (Figures B
and C for explanations)
- Reset the game on key press (“O” key) - this will remove all the blue & red balls and
leave just the jack in the scene

A. First person view in petanque. B. Top view, team red scores one point C. Top view,
team red scores 2 points

BONUS task (1p): Implement a collision detection mechanism (e.g. move one of the enemy
balls if it is hit by the current ball; move the jack and end the game if it is hit).
(variable) Any original & new elements can bring you extra points.
Window creation + rendering the terrain: 0.5p
Balls creation + red/blue color change: 1p (0.75p creation, 0.25p color changes)
Transforms (translation + rotation): 0.5p x 2 = 1p
Score calculation + display: 1p
Handling user input (keyboard): 0.5p
Resetting game & clearing the table: 0.5p
Camera integration: 0.5p
—---------------------------------------------------------------------------------------------------------------------

Obs: Useful link for rendering spheres in OpenGL:


http://www.songho.ca/opengl/gl_sphere.html
!!! If you have issues with creating spheres, you can use other 3D objects (e.g. cubes) and
continue solving the other tasks. You will lose all points for “Ball creation”, but the other
scores won’t be affected.

Explanations for the above link:

1. You have at this link 4 different (mathematical) ways of drawing a sphere. You need just
ONE. Let's focus on the first type (the easiest one) - up until the "Icosphere" example.

2. The implementation is based on dividing the sphere into multiple triangles. Firstly we
define rectangles (ish) by using longitude and latitude. Sectors are from 0 to 360 (all around
the sphere, around the Equator ) and stacks are from -90 to 90 degrees (south to north
pole). We can say how many sectors we want (e.g. 72) and stacks (e.g. 36), which will mean
that the step for each sector will be 360/72, and the step for each stack 180/36 - just don't
hardcode these). These are just some examples, feel free to use other parameters for
having a more / less "rounded" sphere.

3. We define a vertices array (same thing from the lab). This time we can use a vector
(easier to work with it, smth like this std::vector<float>vertices; )

4. By using 2 for loops, we will firstly traverse all stacks and calculate r*cos(angle) and
r*sin(angle) - used in the parametric equations of the sphere, you must have talked about
them when you did Calculus. r*sin(angle) is the z coordinate of a point situated on the
surface of a sphere. And secondly, calculate r*cos(angle)*cos(angle2) and
r*cos(angle)*sin(angle2). These are the x and y coordinates of a point situated on the
surface of a sphere.

5. Add all coordinates to the vertices vector, by calling vertices.push_back(x) etc.

6. Now create similarly the indices vector and put the corresponding indices there. How? By
dividing each "rectangle" into 2 triangles and respecting the counterclockwise order.

Quote: "Each sector in a stack requires 2 triangles. If the first vertex index in the current
stack is k1 and the next stack is k2, then the counterclockwise orders of vertex indices of 2
triangles are; k1 ⟶ k2 ⟶ k1+1 k1+1 ⟶ k2 ⟶ k2+1"

(better to have a look at their drawing)


7. We already have a vbo, ibo, vao in our labs. Just replace the buffer data with stuff like
vertices.size() * sizeof(float) for the size and vertices.data() for the uploaded data. Same for
indices.

8. Bind the vao and change the draw elements function so that it uses the size of the indices
as well.

And... this should be the result (the one in the pic below).

Obs: in their files there are MANY observations / lines of code related to normals / textures.
Simply delete them, we do not need them.

Obs 2: you do not need to bother about ALL the code that was written there (sphere.cpp,
shaders, light, normals ). Just the steps mentioned by me above (and the explanations /
demo code put on their website) should be enough. Basically, the code needed for
generating vertices and indices is the important one.

You might also like