Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Computer Graphics
- .2019
- ─
- Name: Arifur Rahman
- ID: 163015004
- Task_01:
- Draw a house using OpenGL.
- Requirements:
- ● Visual Studio IDE with
- ● C++ Development Environment With OpenGL.
- Code Details:
- #include <GL/glut.h>
- void shapes(void) {
- //Drawing House
- glBegin(GL_TRIANGLES);
- glColor3f(0.0, 1.0, 0.0);
- glVertex3f(-0.5, 0.5, 0);
- glVertex3f(0, 1.0, 0);
- glVertex3f(0.5, 0.5, 0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.8, 0.8, 0.8);
- glVertex3f(-0.5, -0.5, 0.0);
- glVertex3f(-0.5, 0.5, 0.0);
- glVertex3f(0.5, 0.5, 0.0);
- glVertex3f(0.5, -0.5, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(0.0, 0.0, 1.0);
- glVertex3f(-0.15, -0.5, 0.0);
- glVertex3f(-0.15, 0.2, 0.0);
- glVertex3f(0.15, 0.2, 0.0);
- glVertex3f(0.15, -0.5, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(-0.45, -0.05, 0.0);
- glVertex3f(-0.45, 0.15, 0.0);
- glVertex3f(-0.25, 0.15, 0.0);
- glVertex3f(-0.25, -0.05, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(0.45, -0.05, 0.0);
- glVertex3f(0.45, 0.15, 0.0);
- glVertex3f(0.25, 0.15, 0.0);
- glVertex3f(0.25, -0.05, 0.0);
- glEnd();
- }
- void display(void) {
- // clearing the window with black color, 1st 3 parameter are for R,G,B. last one for opacity
- glClearColor(0.0, 0.0, 0.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- //viewing transformation
- //glulookat() positions the camera towards the object
- //camera position, camera target, up vector
- gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- shapes();
- glFlush();
- }
- void reshape(int w, int h) {
- // 1st 2 parameters for lower left corner of the viewport rectangle. the default is 0,0
- // the next coordinates are width and height of the viewport
- //Set the viewport to be the entire window
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- // setting the camera
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //perspective transform
- //gluPerspective (30, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
- gluPerspective(30, 1, 1.0, 100.0);
- glMatrixMode(GL_MODELVIEW); //switch back the the model editing mode.
- }
- int main(int argc, char **argv) {
- glutInit(&argc, argv);
- // single buffering.. (double buffering for animation)
- glutInitDisplayMode(GLUT_SINGLE);
- // full screen is 1000,1000
- // this 0,0 or 1000,1000 are world coordinates
- glutInitWindowSize(700, 700);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("A basic OpenGL Window");
- // registering callback functions
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- return 0;
- }
- Output:
- Conclusion:
- We learn about OpenGL triangles, quads commands.
- Task_02:
- Draw a star using OpenGL.
- Requirements:
- ● Visual Studio IDE with
- ● C++ Development Environment with OpenGL.
- Code Details:
- #include <GL/glut.h>
- void shapes (void) {
- /*glColor3f(1.0, 1.0, 1.0); //this will set a color of the square.
- glBegin(GL_LINES); // write shape u want to create
- glVertex3f(-0.5, -0.5, 0.0);
- glVertex3f(-0.5, 0.5, 0.0);
- glVertex3f(0.5, 0.5, 0.0);
- glVertex3f(0.5, -0.5, 0.0);*/
- /*glBegin(GL_POINTS);
- glVertex3f(.5,.5,0);
- glVertex3f(-.5,-.5,0);
- glVertex3f(0,.5,0);
- glVertex3f(.5,0,0);
- glVertex3f(0,0,0);*/
- glBegin(GL_LINE_LOOP);
- glVertex3f(-0.5,0.5,0);
- glVertex3f(0.5,0.5,0);
- glVertex3f(-0.3,-0.2,0);
- glVertex3f(0.0,1,0.0);
- glVertex3f(0.3,-0.2,0);
- /*glBegin(GL_POLYGON);
- glColor3f(1, 0, 0);
- glVertex3f(-0.6, -0.75, 0);
- glColor3f(0, 1, 0);
- glVertex3f(0.6, -0.75, 0);
- glColor3f(0, 0, 1);
- glVertex3f(0, 0.75, 0);*/
- /*glBegin(GL_TRIANGLES);
- glColor3f(0.5, 0.5, 0.5);
- glVertex3f(0, 0, 0);
- glVertex3f(1, .2, 1);
- glVertex3f(-.5, .5, 2);*/
- /*glBegin(GL_QUADS);
- glVertex3f(-0.5, -0.5, 0.0);
- glVertex3f(-0.5, 0.5, 0.0);
- glVertex3f(0.5, 0.5, 0.0);
- glVertex3f(0.5, -0.5, 0.0);*/
- /*glBegin(GL_TRIANGLE_FAN);
- glColor3f(1,0,0);
- glVertex2f(0,0.5);
- glVertex2f(-0.4,0);
- glVertex2f(0.4,0);
- glVertex2f(0,-0.5);
- */
- glEnd();
- }
- void display (void) {
- // clearing the window with black color, 1st 3 parameter are for R,G,B. last one for opacity
- glClearColor (0.0,0.0,0.0,1.0);
- glClear (GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- //viewing transformation
- //glulookat() positions the camera towards the object
- //camera position, camera target, upvector
- gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- shapes();
- glFlush();
- }
- void reshape (int w, int h) {
- // 1st 2 parameters for lower left corner of the viewport rectangle. the default is 0,0
- // the next coordinates are width and hight of the viewport
- //Set the viewport to be the entire window
- glViewport (0, 0, (GLsizei)w, (GLsizei)h);
- // setting the camera
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity ();
- //perspective transform
- //gluPerspective (30, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
- gluPerspective (30, 1, 1.0, 100.0);
- glMatrixMode (GL_MODELVIEW); //switch back the the model editing mode.
- }
- int main (int argc, char **argv) {
- glutInit (&argc, argv);
- glutInitDisplayMode (GLUT_SINGLE); // single buffering.. (double buffering for animation)
- // full screen is 1000,1000
- // this 0,0 or 1000,1000 are world co ordinates
- glutInitWindowSize (700, 700);
- glutInitWindowPosition (100, 100);
- glutCreateWindow ("A basic OpenGL Window");
- // registering callback functions
- glutDisplayFunc (display);
- glutReshapeFunc (reshape);
- glutMainLoop ();
- return 0;
- }
- Output:
- Conclusion:
- We learn about OpenGL line strip commands.
- Task_03:
- Draw a Pyramid using OpenGL.
- Requirements:
- ● Visual Studio IDE with
- ● C++ Development Environment With OpenGL.
- Code Details:
- #include <GL/glut.h>
- void shapes(void) {
- float x1 = 0, y1 = .5;
- float x2 = -0.5, y2 = -0.2;
- float x3 = 0.5, y3 = -0.2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 0, 0);
- glVertex3f(x1, y1, 0);
- glVertex3f(x2, y2, 0);
- glVertex3f(x3, y3, 0);
- glEnd();
- float p1 = (x1 + x2) / 2, q1 = (y1 + y2) / 2;
- float p2 = (x1 + x3) / 2, q2 = (y1 + y3) / 2;
- float p3 = (x2 + x3) / 2, q3 = (y2 + y3) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(0, 1, 0);
- glVertex3f(p1, q1, 0);
- glVertex3f(p2, q2, 0);
- glVertex3f(p3, q3, 0);
- glEnd();
- float m1 = (p1 + x2) / 2, n1 = (q1 + y2) / 2;
- float m2 = (p1 + p3) / 2, n2 = (q1 + q3) / 2;
- float m3 = (x2 + p3) / 2, n3 = (y2 + q3) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(0, 1, 1);
- glVertex3f(m1, n1, 0);
- glVertex3f(m2, n2, 0);
- glVertex3f(m3, n3, 0);
- glEnd();
- float c1 = (x1 + p1) / 2, d1 = (y1 + q1) / 2;
- float c2 = (x1 + p2) / 2, d2 = (y1 + q2) / 2;
- float c3 = (p1 + p2) / 2, d3 = (q1 + q2) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(0, 0, 1);
- glVertex3f(c1, d1, 0);
- glVertex3f(c2, d2, 0);
- glVertex3f(c3, d3, 0);
- glEnd();
- float s1 = (p2 + p3) / 2, t1 = (q2 + q3) / 2;
- float s2 = (p2 + x3) / 2, t2 = (q2 + y3) / 2;
- float s3 = (x3 + p3) / 2, t3 = (y3 + q3) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 0, 1);
- glVertex3f(s1, t1, 0);
- glVertex3f(s2, t2, 0);
- glVertex3f(s3, t3, 0);
- glEnd();
- float a1 = (x1 + c1) / 2, b1 = (y1 + d1) / 2;
- float a2 = (x1 + c2) / 2, b2 = (y1 + d2) / 2;
- float a3 = (c1 + c2) / 2, b3 = (d1 + d2) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 1, .5);
- glVertex3f(a1, b1, 0);
- glVertex3f(a2, b2, 0);
- glVertex3f(a3, b3, 0);
- glEnd();
- float e1 = (p1 + c1) / 2, f1 = (q1 + d1) / 2;
- float e2 = (c1 + c3) / 2, f2 = (d1 + d3) / 2;
- float e3 = (p1 + c3) / 2, f3 = (q1 + d3) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 1, .5);
- glVertex3f(e1, f1, 0);
- glVertex3f(e2, f2, 0);
- glVertex3f(e3, f3, 0);
- glEnd();
- float g1 = (c2 + c3) / 2, h1 = (d2 + d3) / 2;
- float g2 = (c2 + p2) / 2, h2 = (d2 + q2) / 2;
- float g3 = (c3 + p2) / 2, h3 = (d3 + q2) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 1, .5);
- glVertex3f(g1, h1, 0);
- glVertex3f(g2, h2, 0);
- glVertex3f(g3, h3, 0);
- glEnd();
- float i1 = (p1 + m1) / 2, j1 = (q1 + n1) / 2;
- float i2 = (p1 + m2) / 2, j2 = (q1 + n2) / 2;
- float i3 = (m1 + m2) / 2, j3 = (n1 + n2) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 1, .5);
- glVertex3f(i1, j1, 0);
- glVertex3f(i2, j2, 0);
- glVertex3f(i3, j3, 0);
- glEnd();
- float k1 = (s1 + p2) / 2, l1 = (t1 + q2) / 2;
- float k2 = (p2 + s2) / 2, l2 = (q2 + t2) / 2;
- float k3 = (s1 + s2) / 2, l3 = (t1 + t2) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 1, .5);
- glVertex3f(k1, l1, 0);
- glVertex3f(k2, l2, 0);
- glVertex3f(k3, l3, 0);
- glEnd();
- float ax1 = (m1 + x2) / 2, ay1 = (n1 + y2) / 2;
- float ax2 = (m1 + m3) / 2, ay2 = (n1 + n3) / 2;
- float ax3 = (x2 + m3) / 2, ay3 = (y2 + n3) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 1, .5);
- glVertex3f(ax1, ay1, 0);
- glVertex3f(ax2, ay2, 0);
- glVertex3f(ax3, ay3, 0);
- glEnd();
- float bx1 = (m2 + m3) / 2, by1 = (n2 + n3) / 2;
- float bx2 = (m2 + p3) / 2, by2 = (n2 + q3) / 2;
- float bx3 = (m3 + p3) / 2, by3 = (n3 + q3) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 1, .5);
- glVertex3f(bx1, by1, 0);
- glVertex3f(bx2, by2, 0);
- glVertex3f(bx3, by3, 0);
- glEnd();
- float cx1 = (p3 + s1) / 2, cy1 = (q3 + t1) / 2;
- float cx2 = (s1 + s3) / 2, cy2 = (t1 + t3) / 2;
- float cx3 = (p3 + s3) / 2, cy3 = (q3 + t3) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 1, .5);
- glVertex3f(cx1, cy1, 0);
- glVertex3f(cx2, cy2, 0);
- glVertex3f(cx3, cy3, 0);
- glEnd();
- float dx1 = (s2 + s3) / 2, dy1 = (t2 + t3) / 2;
- float dx2 = (s2 + x3) / 2, dy2 = (t2 + y3) / 2;
- float dx3 = (s3 + x3) / 2, dy3 = (t3 + y3) / 2;
- glBegin(GL_TRIANGLES);
- glColor3f(1, 1, .5);
- glVertex3f(dx1, dy1, 0);
- glVertex3f(dx2, dy2, 0);
- glVertex3f(dx3, dy3, 0);
- glEnd();
- }
- void display(void) {
- // clearing the window with black color, 1st 3 parameter are for R,G,B. last one for opacity
- glClearColor(0.0, 0.0, 0.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- //viewing transformation
- //glulookat() positions the camera towards the object
- //camera position, camera target, up vector
- gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- shapes();
- glFlush();
- }
- void reshape(int w, int h) {
- // 1st 2 parameters for lower left corner of the viewport rectangle. the default is 0,0
- // the next coordinates are width and height of the viewport
- //Set the viewport to be the entire window
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- // setting the camera
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //perspective transform
- //gluPerspective (30, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
- gluPerspective(30, 1, 1.0, 100.0);
- glMatrixMode(GL_MODELVIEW); //switch back the the model editing mode.
- }
- int main(int argc, char **argv) {
- glutInit(&argc, argv);
- // single buffering.. (double buffering for animation)
- glutInitDisplayMode(GLUT_SINGLE);
- // full screen is 1000,1000
- // this 0,0 or 1000,1000 are world coordinates
- glutInitWindowSize(700, 700);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("A basic OpenGL Window");
- // registering callback functions
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- return 0;
- }
- Output:
- Conclusion:
- We learn about OpenGL commands.
- Task_04:
- Draw a Chess Board using OpenGL.
- Requirements:
- ● Visual Studio IDE with
- ● C++ Development Environment with OpenGL.
- Code Details:
- #include <GL/glut.h>
- void shapes(void) {
- float y1 = 0.0;
- float y2 = 0.0;
- float y3 = 0.1;
- float y4 = 0.1;
- for (int j = 0; j < 8; j++) {
- float x1 = 0.0;
- float x2 = 0.1;
- float x3 = 0.1;
- float x4 = 0.0;
- for (int i = 0; i < 8; i++) {
- glBegin(GL_QUADS);
- if ((j % 2) != 0) {
- if ((i % 2) != 0) {
- glColor3f(1.0, 1.0, 1.0);
- }
- else {
- glColor3f(0.2, 0.2, 0.2);
- }
- }
- if ((j % 2) == 0) {
- if ((i % 2) != 0) {
- glColor3f(0.2, 0.2, 0.2);
- }
- else {
- glColor3f(1.0, 1.0, 1.0);
- }
- }
- glVertex3f(x1, y1, 0.0);
- glVertex3f(x2, y2, 0.0);
- glVertex3f(x3, y3, 0.0);
- glVertex3f(x4, y4, 0.0);
- glEnd();
- x1 += 0.1;
- x2 += 0.1;
- x3 += 0.1;
- x4 += 0.1;
- }
- y1 += 0.1;
- y2 += 0.1;
- y3 += 0.1;
- y4 += 0.1;
- }
- }
- void display(void) {
- // clearing the window with black color, 1st 3 parameter are for R,G,B. last one for opacity
- glClearColor(0.0, 0.0, 0.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- //viewing transformation
- //glulookat() positions the camera towards the object
- //camera position, camera target, up vector
- gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- shapes();
- glFlush();
- }
- void reshape(int w, int h) {
- // 1st 2 parameters for lower left corner of the viewport rectangle. the default is 0,0
- // the next coordinates are width and height of the viewport
- //Set the viewport to be the entire window
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- // setting the camera
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //perspective transform
- //gluPerspective (30, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
- gluPerspective(30, 1, 1.0, 100.0);
- glMatrixMode(GL_MODELVIEW); //switch back the the model editing mode.
- }
- int main(int argc, char **argv) {
- glutInit(&argc, argv);
- // single buffering.. (double buffering for animation)
- glutInitDisplayMode(GLUT_SINGLE);
- // full screen is 1000,1000
- // this 0,0 or 1000,1000 are world coordinates
- glutInitWindowSize(700, 700);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("A basic OpenGL Window");
- // registering callback functions
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- return 0;
- }
- Output:
- Conclusion:
- We learn about OpenGL line strip commands.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement