Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Solution

The document contains C++ code for OpenGL programs that render various 2D and 3D shapes like lines, triangles, quads, polygons, a cylinder, cube, and multiple viewports. The programs initialize OpenGL, set display callbacks that clear the screen and draw objects with the appropriate colors, and contain main functions that initialize GLUT and enter the main loop.

Uploaded by

c.ronaldo2012777
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Solution

The document contains C++ code for OpenGL programs that render various 2D and 3D shapes like lines, triangles, quads, polygons, a cylinder, cube, and multiple viewports. The programs initialize OpenGL, set display callbacks that clear the screen and draw objects with the appropriate colors, and contain main functions that initialize GLUT and enter the main loop.

Uploaded by

c.ronaldo2012777
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment 1

#include <GL/glut.h>
void RenderScene(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 1.0);
glBegin(GL_LINES);
{
glVertex2f(0, 0);
glVertex2f(400, 400);
}
glEnd();
glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutCreateWindow("Simple");
glutInitWindowSize(500, 500);
glutDisplayFunc(RenderScene);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glutMainLoop();
return 0;
}

#include <GL/glut.h>

void displayCB(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex2f(25, 300);
glVertex2f(30, 350);
glVertex2f(60, 400);
glEnd();
glFlush();
}

int main(int argc, char *argv[])


{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Triangle");
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho(0.0f, 450, 450,0, -1.0f, 1.0f);
glutDisplayFunc(displayCB);
glutMainLoop();

return 0;
}

#include <GL/glut.h>

void displayCB(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_QUADS);
glVertex2f(50, 50);
glVertex2f(50, 200);
glVertex2f(200, 200);
glVertex2f(200, 50);
glEnd();
glFlush();
}

int main(int argc, char *argv[])


{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Quad");
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho(-500.0f, 500, 500, -500.0f, -1.0f, 1.0f);
glutDisplayFunc(displayCB);
glutMainLoop();

return 0;
}

#include <GL/glut.h>

void displayCB(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0,5,0);
glVertex3f(0, 0, 0);
glVertex3f(10, -5, 0);
glVertex3f(20, 0, 0);
glVertex3f(20, 5, 0);
glVertex3f(10, 10, 0);
glEnd();
glFlush();
}

int main(int argc, char *argv[])


{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("hexagon");
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho(-35.0f, 35.0f, 35.0f, -35.0f, 1.0f, -1.0f);
glutDisplayFunc(displayCB);
glutMainLoop();

return 0;
}

#include <GL/glut.h>

void displayCB(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(-20, 0);
glVertex2f(0, 40);
glVertex2f(20, 0);
glVertex2f(60, -20);
glVertex2f(20, -40);
glVertex2f(0, -80);
glVertex2f(-20, -40);
glVertex2f(-60, -20);
glEnd();
glFlush();
}

int main(int argc, char *argv[])


{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("star");
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho(-470.0f, 470, 470, -470.0f, -1.0f, 1.0f);
glutDisplayFunc(displayCB);
glutMainLoop();

return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <GL/glut.h>
#include<cmath>
#define PI 22.0f/7.0f
void Renderscene(void)
{
GLfloat angle, x, y, step;
step = PI / 100;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-3, 0.0f, 1.0f, 0.0f);
glRotatef(-25, 1.0f, 0.0f, 0.0f);
glRotatef(90, 0.0f, 1.0f, 1.0f);
glColor3f(0.5f, 0.5f, 0.5f);
glBegin(GL_TRIANGLE_FAN);
{
glVertex3f(0, 0, 1);
for (angle = 0.0f; angle <= (2.0f*PI); angle += step)
{
glColor3f(0, 1, 1);
x = 2.0f*sin(angle);
y = 2.0f*cos(angle);
glVertex3f(x, y, 1.0f);
}

}
glEnd();
glBegin(GL_TRIANGLE_FAN);
{
glVertex3f(0, 0, -1);
for (angle = 0.0f; angle <= (2.0f*PI); angle += step)
{
glColor3f(0, 1, 1);
x = 2.0f*sin(angle);
y = 2.0f*cos(angle);
glVertex3f(x, y, -1.0f);
}

}
glEnd();
glBegin(GL_QUAD_STRIP);
{
for (angle = 0.0f; angle <= (2.0f*PI); angle += step)
{
glColor3f(0, 1, 1);
x = 2.0f*sin(angle);
y = 2.0f*cos(angle);
glVertex3f(x, y, 1.0f);
glVertex3f(x, y, -1.0f);

}
}
glEnd();

glFlush();
}
int main(int argc, char *argv[])
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
glutCreateWindow("cylinder");
glutInitWindowSize(7,7);
glutDisplayFunc(Renderscene);
glClearColor(1.0, 1.0, 1.0, 1.0);
glShadeModel(GL_FLAT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-3.0f, 3, 3, -3.0f, 3.0f, -3.0f);
glutMainLoop();
return 0;
}
#include <GL/glut.h>
void drawcube(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
gluLookAt(150, 50, 60, 0, 0, 0, 0, 1, 0);
glBegin(GL_QUADS);
glColor3f(1,1,0);
glVertex3f(30, -30, 30);
glVertex3f(30, 30, 30);
glVertex3f(-30, 30, 30);
glVertex3f(-30, -30, 30);

glEnd();
glBegin(GL_QUADS);
glColor3f(1, 1, 0);
glVertex3f(30, -30, -30);
glVertex3f(30, 30, -30);
glVertex3f(-30, 30, -30);
glVertex3f(-30, -30, -30);
glEnd();
glEnd();
glBegin(GL_QUADS);
glColor3f(1, 1, 0);
glVertex3f(30, -30, 30);
glVertex3f(30, -30, -30);
glVertex3f(30, 30, -30);
glVertex3f(30, 30, 30);
glEnd();
glBegin(GL_QUADS);
glColor3f(1, 1, 0);
glVertex3f(-30, -30, 30);
glVertex3f(-30, -30, -30);
glVertex3f(-30, 30, -30);
glVertex3f(-30, 30, 30);
glEnd();
glBegin(GL_QUADS);
glColor3f(1, 1, 0);
glVertex3f(30, 30, 30);
glVertex3f(30, 30, -30);
glVertex3f(-30, 30, -30);
glVertex3f(-30, 30, 30);
glEnd();
glBegin(GL_QUADS);
glColor3f(1, 1, 0);
glVertex3f(30, -30, 30);
glVertex3f(30, -30, -30);
glVertex3f(-30, -30, -30);
glVertex3f(-30, -30, 30);
glEnd();
glutSwapBuffers();
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("cube");
glutDisplayFunc(drawcube);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glClearColor(1, 1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutMainLoop();
return 0;
}
#include <GL/glut.h>
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glViewport(0, 0, 200, 400);
glColor3f(1, 1, 1);
glRectf(-60, 120, 60, -120);

glColor3f(1, 0, 0);
glViewport(200, 400, 200, 400);
glColor3f(1, 1, 1);
glRectf(-60, 120, 60, -120);

glColor3f(1, 0, 0);
glViewport(0, 400, 200, 400);
glColor3f(1, 1, 1);
glRectf(-60, 120, 60, -120);

glColor3f(1, 0, 0);
glViewport(200, 0, 200, 400);
glColor3f(1, 1, 1);
glRectf(-60, 120, 60, -120);
glFlush();
}
void SR(void)
{
glClearColor(1, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100, 100, -200, 200, 1, -1);

}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(400, 800);
glutCreateWindow("simple");
glutDisplayFunc(render);
SR();
glutMainLoop();
return 0;
}

You might also like