Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simple programming assignment using OpenGL with GLUT from a C program. The basic

ID: 3666582 • Letter: S

Question

Simple programming assignment using OpenGL with GLUT from a C program.

The basic program will read a sequence of pairs of integers from a file, with each pair specifying one endpoint/vertex (x and y coordinates) of a straight line segment.

There will, of course, be an even number of pairs of integers in the input file, and there will always be at least two pair (yielding a single line segment).

There will never be more than 100 line segments specified in the input file.

All endpoint x and y coordinate values will fit in a 32-bit integer (but don’t forget that some of these may be negative).

The program should also arrange for there to be an unused “border” region around the bounding box that encloses all the line segments.

The border size should be five percent (5%) of the corresponding bounding box dimension, or 25 pixels, whichever is larger.

The size of the window your program creates should be at least 500 by 500 pixels, but it can be as large as you want.

The steps the program should perform are as follows:
- Open the input file. The name of the input file will be specified on the command line.
- Read the number of line segments (an integer ns, at least 1, but no larger than 100).
- Read and save the pair of endpoints (x, y coordinates) for each end of each line segment.
- Determine the integer bounding box for the line segments.
- Based on the window size you use, determine the appropriate scale factor to use when drawing
the lines, and determine the border size. The scale factor should be selected so that the bounding
box fits inside the window and is as large as possible (given the border). Note that the same scale
factor should be used in the x and y dimensions.
- Create the window and draw the lines.

The background for the window should be white, and the lines should be black.

Explanation / Answer

#include <iostream>

#include <GL/glut.h>

/* process menu option 'op' */

void menu(int op) {

  switch(op) {

  case 'Q':

  case 'q':

    exit(0);

  }

}

/* executed when a regular key is pressed */

void keyboardDown(unsigned char key, int x, int y) {

  switch(key) {

  case 'Q':

  case 'q':

  case 27:   // ESC

    exit(0);

  }

}

/* executed when a regular key is released */

void keyboardUp(unsigned char key, int x, int y) {

}

/* executed when a special key is pressed */

void keyboardSpecialDown(int k, int x, int y) {

}

/* executed when a special key is released */

void keyboardSpecialUp(int k, int x, int y) {

}

/* reshaped window */

void reshape(int width, int height) {

  GLfloat fieldOfView = 90.0f;

  glViewport (0, 0, (GLsizei) width, (GLsizei) height);

  glMatrixMode (GL_PROJECTION);

  glLoadIdentity();

  gluPerspective(fieldOfView, (GLfloat) width/(GLfloat) height, 0.1, 500.0);

  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity();

}

/* executed when button 'button' is put into state 'state' at screen position ('x', 'y') */

void mouseClick(int button, int state, int x, int y) {

}

/* executed when the mouse moves to position ('x', 'y') */

void mouseMotion(int x, int y) {

}

/* render the scene */

void draw() {

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity();

  /* render the scene here */

  glFlush();

  glutSwapBuffers();

}

/* executed when program is idle */

void idle() {

}

/* initialize OpenGL settings */

void initGL(int width, int height) {

  reshape(width, height);

  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

  glClearDepth(1.0f);

  glEnable(GL_DEPTH_TEST);

  glDepthFunc(GL_LEQUAL);

}

/* initialize GLUT settings, register callbacks, enter main loop */

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

   

  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

  glutInitWindowSize(800, 600);

  glutInitWindowPosition(100, 100);

  glutCreateWindow("Perspective's GLUT Template");

  // register glut call backs

  glutKeyboardFunc(keyboardDown);

  glutKeyboardUpFunc(keyboardUp);

  glutSpecialFunc(keyboardSpecialDown);

  glutSpecialUpFunc(keyboardSpecialUp);

  glutMouseFunc(mouseClick);

  glutMotionFunc(mouseMotion);

  glutReshapeFunc(reshape);

  glutDisplayFunc(draw);

  glutIdleFunc(idle);

  glutIgnoreKeyRepeat(true); // ignore keys held down

  // create a sub menu

  int subMenu = glutCreateMenu(menu);

  glutAddMenuEntry("Do nothing", 0);

  glutAddMenuEntry("Really Quit", 'q');

  // create main "right click" menu

  glutCreateMenu(menu);

  glutAddSubMenu("Sub Menu", subMenu);

  glutAddMenuEntry("Quit", 'q');

  glutAttachMenu(GLUT_RIGHT_BUTTON);

  initGL(800, 600);

  glutMainLoop();

  return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote