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

Assignment01

The document contains source code for drawing and manipulating a star shape in OpenGL. The code includes: 1) Drawing a basic 5 pointed star shape 2) Allowing translation of the star by taking user input for Tx and Ty values 3) Allowing rotation of the star by taking a user input angle value 4) Drawing the reflections of the star shape across the x, y, and origin axes.

Uploaded by

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

Assignment01

The document contains source code for drawing and manipulating a star shape in OpenGL. The code includes: 1) Drawing a basic 5 pointed star shape 2) Allowing translation of the star by taking user input for Tx and Ty values 3) Allowing rotation of the star by taking a user input angle value 4) Drawing the reflections of the star shape across the x, y, and origin axes.

Uploaded by

Geerbani Shashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

01.

Star Shape Drawing Source Code

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

#include <stdlib.h>

static void display(void) {


glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.22,0.99,0.11);
glBegin(GL_POLYGON);
glVertex2f(2,2);
glVertex2f(2,-2);
glVertex2f(-2,-2);
glVertex2f(-2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(5,0);
glVertex2f(2,2);
glVertex2f(2,-2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0,5);
glVertex2f(-2,2);
glVertex2f(2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-5,0);
glVertex2f(-2,-2);
glVertex2f(-2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0,-5);
glVertex2f(2,-2);
glVertex2f(-2,-2);
glEnd();
glFlush();
}

void init(){
glClearColor(0.0,0.0,1.0,1.0);
glOrtho(-20,20,-20,20,-20,20);
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 480);
glutInitWindowPosition (200, 200);
glutCreateWindow ("Star Shape 2018331502");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------

02. Translation of Star Shape by taking user input of Tx & Ty Source Code

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

#include <stdlib.h>
#include<bits/stdc++.h>
using namespace std;

float Tx = 0.0;
float Ty = 0.0;

void translation(void)
{

glBegin(GL_POLYGON);
glVertex2f(2,2);
glVertex2f(2,-2);
glVertex2f(-2,-2);
glVertex2f(-2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(5,0);
glVertex2f(2,2);
glVertex2f(2,-2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0,5);
glVertex2f(-2,2);
glVertex2f(2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-5,0);
glVertex2f(-2,-2);
glVertex2f(-2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0,-5);
glVertex2f(2,-2);
glVertex2f(-2,-2);
glEnd();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.22,0.99,0.11);
glLoadIdentity();
glTranslatef(Tx,Ty,0);
translation();
glFlush();

void reshape(int w, int h)


{
glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-15,15,-15,15,-15,15);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 480);
glutInitWindowPosition (200, 200);
cout<<"Enter translation values for Tx and Ty: ";
cin>>Tx>>Ty;
glutCreateWindow ("Star translation 2018331502");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

-----------------------------------------------------------------------------------
-----------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------

03. Rotations of Star Shape by taking user input of angle source code

#include <windows.h>
#include <iostream>
#include <stdlib.h>

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

using namespace std;

//Called when a key is pressed


void handleKeypress(unsigned char key, int x, int y)
{
switch (key)
{
case 27: //Escape key
exit(0);
}
}

//Initializes 3D rendering
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL); //Enable color
glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //Change the background to sky blue
}

//Called when the window is resized


void handleResize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

float _angle;
float _cameraAngle = 0.0f;

//Draws the 3D scene


void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, -5.0f);

glPushMatrix();
glTranslatef(0.0f, -1.0f, 0.0f);
glRotatef(_angle, 0.0f, 0.0f, -1.0f);

glBegin(GL_POLYGON);
glVertex2f(0.2,0.2);
glVertex2f(0.2,-0.2);
glVertex2f(-0.2,-0.2);
glVertex2f(-0.2,0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.5,0.0);
glVertex2f(0.2,0.2);
glVertex2f(0.2,-0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.0,0.5);
glVertex2f(-0.2,0.2);
glVertex2f(0.2,0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-0.5,0.0);
glVertex2f(-0.2,-0.2);
glVertex2f(-0.2,0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.0,-0.5);
glVertex2f(0.2,-0.2);
glVertex2f(-0.2,-0.2);
glEnd();
glPopMatrix();

glutSwapBuffers();
}

void update(int value)


{
_angle += 2.0f;
if (_angle > 360)
{
_angle -= 360;
}

glutPostRedisplay(); ////Tell GLUT that the scene has changed


glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv)


{
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);

cout<<"Enter the rotation angle of the star: ";


cin>>_angle;
//Create the window
glutCreateWindow("Star Rotations 2018331502");
initRendering();

//Set handler functions


glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);

glutTimerFunc(25, update, 0); //Add a timer

glutMainLoop();
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------

04. Reflections of Star Shape Source Code

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

#include <stdlib.h>
#include<bits/stdc++.h>
using namespace std;

void reflections() {
glBegin(GL_POLYGON);
glVertex2f(3, 6); // Top right
glVertex2f(5, 6); // Top left
glVertex2f(5, 4); // Bottom left
glVertex2f(3, 4); // Bottom right
glEnd();

glBegin(GL_POLYGON);
glVertex2f(4, 7.5);
glVertex2f(5, 6);
glVertex2f(3, 6);
glEnd();

glBegin(GL_POLYGON);
glVertex2f(5, 6);
glVertex2f(6.5, 5);
glVertex2f(5, 4);
glEnd();

glBegin(GL_POLYGON);
glVertex2f(5, 4);
glVertex2f(4, 2.5);
glVertex2f(3, 4);
glEnd();

glBegin(GL_POLYGON);
glVertex2f(3, 4);
glVertex2f(1.5, 5);
glVertex2f(3, 6);
glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity();

reflections();

// Reflections in all quadrants


glPushMatrix();
glScalef(-1.0, 1.0, 1.0); // Reflect horizontally
glColor3f(1.0, 0.0, 0.0); // Red color
reflections();
glPopMatrix();

glPushMatrix();
glScalef(1.0, -1.0, 1.0); // Reflect vertically
glColor3f(0.0, 1.0, 0.0); // Green color
reflections();
glPopMatrix();

glPushMatrix();
glScalef(-1.0, -1.0, 1.0); // Reflect both horizontally and vertically
glColor3f(0.0, 0.0, 1.0); // Blue color
reflections();
glPopMatrix();

glFlush();
}

void reshape(int w, int h) {


glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, -10, 10);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow("Star Reflection 2018331502");

glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();

return 0;
}

You might also like