0% found this document useful (0 votes)
11 views7 pages

Computer Graphics

The document outlines a group assignment for a Computer Graphics course, detailing a project that demonstrates how to create an animated circle using the OpenGL graphics library in C++. It includes steps for setting up the environment, defining variables, drawing the circle, and implementing animations through scaling and translation. The final code is provided, showcasing the complete implementation of the animated circle functionality.

Uploaded by

Firaol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Computer Graphics

The document outlines a group assignment for a Computer Graphics course, detailing a project that demonstrates how to create an animated circle using the OpenGL graphics library in C++. It includes steps for setting up the environment, defining variables, drawing the circle, and implementing animations through scaling and translation. The final code is provided, showcasing the complete implementation of the animated circle functionality.

Uploaded by

Firaol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COLLEGE OF IOT

DEPARTMENT OF COMPUTER SCIENCE


COURCE TITTLE: -COMPUTER GRAPHICS
GROUP ASSIGNMENT

NO NAME ID NO
1 FIRAOL BEKELE R/1300/14
2 TOFIK LULU R/3070/14
3 BEREKET MISGANAW R/0817/14
4 AHMED ABAMECHA R/0301/13
5 HERMELA YOHANNES R/3891/14
6 FASIKA FISSEHA R/3382/14
7 ISRAEL DEREJE R/3965/14
8 TAMRAT DEMSIS R/3000/14
9 ABDUHAKIM JEMAL R/3462/14

1|Page
1. Animated circle
1.1. Introduction

This project shows how to draw animated circle using OpenGL graphics library
that run in c++ programming language. Additional to core library OpenGL we use
extra library called GL Utilities Toolkit(glut).

In this project after drawing the circle the circle animated in two ways. First when
program run for the first time the circle “pop-up” (scale from small to big) this is
called scale transformation. Then the circle translates back and forth between two
positions, we use simple animation loop for this.

1.2. Steps

I. Include required headers

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <windows.h>
#include <math.h>
#include <GL/glut.h>
#endif

#include <stdlib.h>

II. Define Global Variable

float scaleFactor = 0.5; // Initial scale


factor
float translationX = 0.0; // Initial X
translation
float translationSpeed = 0.005; // Speed of
translation

III. Calculate Plotting Points Using Polar Coordinates

2|Page
void DrawCircle(float cx, float cy, float r, int
num_segments) {
glBegin(GL_TRIANGLE_FAN);
for (int ii = 0; ii < num_segments; ++ii) {
float theta = 2.0f * 3.1415926f * float(ii) /
float(num_segments);
float x = r * cosf(theta);
float y = r * sinf(theta);
glVertex2f(x + cx, y + cy);
}
glEnd();
}

Code Description:

This code calculate plotting points by using (cx, cy) center of circle and theta that
increase its value until the number of segments defined later reached. It uses polar
coordinate formula to make this happen.

IV. Define state variables

void myInit(void) {
glClearColor(1.0, 1.0, 1.0, 0.0); // white
background
glColor3f(0.3,1,0.5); // green foreground
glMatrixMode(GL_PROJECTION);
glLoadIdentity ();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

V. Draw the Circle

3|Page
Void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(translationX, 0.0, 0.0); // Apply X
translation
glScalef(scaleFactor, scaleFactor, 1.0); // Apply
scale transformation
DrawCircle(0.5, 0.5, 0.2, 100); // Adjust the
parameters as needed
glPopMatrix();
glFlush(); // send all output to the display
}

VI. Set Timer

We need timer for both animations

void Timer(int value) {


translationX += translationSpeed;

if (scaleFactor < 1.0) {


scaleFactor += 0.01; // Increment scale factor
}

if (translationX > 0.2 || translationX < -0.2) {


translationSpeed *= -1.0; // Reverse direction
}

glutPostRedisplay(); // Redraw the scene


glutTimerFunc(16, Timer, 0); // Call Timer function
after 16 ms
}

VII. Initiate glut, create window and display circle created above on window

4|Page
int main (int argc, char *argv[])
{
glutInit(&argc, argv); // initialise the glut library
glutInitWindowSize(500,500); // set size of the window
glutInitWindowPosition(100,100); // position of window
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Animated CIRCLE");
glutDisplayFunc(display); // set display callback
myInit(); // perform other initialisation
glutTimerFunc(0, Timer, 0); // Start the animation timer

glutMainLoop(); // enter the GL event loop

return EXIT_SUCCESS;

1.3. Final code

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <windows.h>
#include <math.h>
#include <GL/glut.h>
#endif

#include <stdlib.h>

float scaleFactor = 0.5; // Initial scale factor


float translationX = 0.0; // Initial X translation

5|Page
float translationSpeed = 0.005; // Speed of translation

void DrawCircle(float cx, float cy, float r, int num_segments) {


glBegin(GL_TRIANGLE_FAN);
for (int ii = 0; ii < num_segments; ++ii) {
float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);
float x = r * cosf(theta);
float y = r * sinf(theta);
glVertex2f(x + cx, y + cy);
}
glEnd();
}

void myInit(void) {
glClearColor(1.0, 1.0, 1.0, 0.0); // white background
glColor3f(0.3,1,0.5); // green foreground
// establish a coordinate system for the image
glMatrixMode(GL_PROJECTION);
glLoadIdentity ();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/* GLUT display callback handler */
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(translationX, 0.0, 0.0); // Apply X translation
glScalef(scaleFactor, scaleFactor, 1.0); // Apply scale transformation
DrawCircle(0.5, 0.5, 0.2, 100); // Adjust the parameters as needed
glPopMatrix();
glFlush(); // send all output to the display
}

void Timer(int value) {


translationX += translationSpeed;
6|Page
if (scaleFactor < 1.0) {
scaleFactor += 0.01; // Increment scale factor
}

if (translationX > 0.2 || translationX < -0.2) {


translationSpeed *= -1.0; // Reverse direction
}

glutPostRedisplay(); // Redraw the scene


glutTimerFunc(16, Timer, 0); // Call Timer function after 16 ms
}

/* Program entry point */


int main (int argc, char *argv[])
{
glutInit(&argc, argv); // initialise the glut library
glutInitWindowSize(500,500); // set size of the window
glutInitWindowPosition(100,100); // position of window
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Animated CIRCLE");
glutDisplayFunc(display); // set display callback
myInit(); // perform other initialisation
glutTimerFunc(0, Timer, 0); // Start the animation timer
glutMainLoop(); // enter the GL event loop
return EXIT_SUCCESS;
}

7|Page

You might also like