Cs602... Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

CS602 -Computer Graphics

Assignment No.2 Solution

Code:

#include <GL/glut.h>

#include <cmath>

#include <ctime

#include <string>

/Window dimensions

const int WINDOW WIDTH = 600;

const int WINDOW HEIGHT=600;

/ User ID

const char* userlD= "Bc180404611":

/ Function to convert degrees to radians

float degToRad(float degrees) {

return degrees * M PI/ 180.0f;

/ Function to draw a string (used for displaying the ID)

void drawString(float x, float y, const char* str) {

glRasterPos2f(x, y);

for (const char* c = str; *c !="\0; Llc) {

glutBitmapCharacter(GLUT BITMAP HELVETICA 18, *c):


}
}

void drawClockFace()

/ Draw the circle

glColor3f(1.0, 1.0, 1.0);


glBegin(GL LINE L0OP);

for (int i =0; i< 360; ++i){

float theta- degToRad(i);


glVertex2f(cos(theta), sin(theta);

glEnd);

H Draw thc hour and minutc ticks

for (int i=0; i<60; ++i) {

float theta - degToRad(i * 6); // 360 /60

float xl= cos(theta):

float yl = sin(theta):

float x2 = (i %5 -0) ? x1 *0.9f: x1 * 0.95f; // Longer ticks for hours


float y2 - (i %5 ==0) ?yl *0.9f: yl * 0.95f;
glBegin(GL_ LINES):

glVertex2fix , yl); glVertex2f(x2, y2); junaid Qazi

glEnd();

/ Function to draw the clock hands

void drawClockHands(int hour, int minute, int second)

/ Draw the second hand

glColor3f(1.0, 0.0, 0.0);


float secondAngle - degToRad(90 - second * 6);
glBegin(GL LINES):
glVertex2f(0.0, 0.0);

glVcrtex2f(0.9f* cos(sccondAnglc), 0.9f* sin(sccondAnglc));


glEnd);

/ Draw the minute hand

glColor3f(0.0, 1.0, 0.0);

float minuteAngle = degToRad(90 - (minute *6 I second * 0.1)):

glBegin(GL LINES);

glVertex2f(0.0, 0.0); e azi


glVertex2f0.8f* cos(minuteAngle), 0.3f* sin(minuteAngle);

glEnd);

Draw the hour hand

glColor3 f(0.0, 0.0, 1.0);

float hourAngle degToRad(90 - (hour * 30 + minute * 0.5);


glBegin(GL_ LINES);
glVertex2f(0.0, 0.0);

glVertex2f(0.6f* cos(hourAngle), 0.6f * sin(hourAngle)):


glEnd);

/ Function to display the clock and the user ID

void display() {

giClear(GL_ COLOR BUFFER BIT);

glLoadldentity0;

glScalef(0.8f, 0.8f, 1.0f); // Center the clock

drawClockFace);

/ Get the current time

time t now - fime(0):

tm* utcTime gmtime(&now),

utcTime->m hournveit fo PakinsodORGde

Standard Time (U

mktime(utcTime); // Normalize the time structure

int hour = utcTime->tm hour % 12; / Convert to 12-hour format


int minute =utcTime->tm min;

int second = utcTime->tm sec;

drawClockHands(hour, minute, second);

I/ Display the user ID


glLoadldentity0;
glColor3f(1.0, 1.0, 1.0);
drawString(-0,98f, 0.9f, uscrlD);

glutSwapBuffers();

/ Timer function to update the display every second

void timer(int value) {

glutPostRedisplay():
/ Function to initialize OpenGL settings

void initOpenGL) {

glClearColor(0.0, 0,0, 0,0, 0,0):

glMatrixMode(GL PROJECTION);

glLoadldentity0;

gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

glMatrixMode(GL_ MODELVIEW):

Main function

int main(int argc, char** argv)

glutinit(&argc, argv);

glutlnitDisplayMode(GLUT _DOUBLE | GLUT_ RGB);

glutlnitWindowSize(WINDOW WIDTH, WINDOW HEIGHT):

glutCreateWindow("Analog Clock");

initOpenGLO:
unwtsey naid Oazi

glutTimerFunc(0, timer, 0);

glutMainlLoop0;

return 0;

Prat Cle beug

include cGL/glut

id Oa:

-Erronr

You might also like