Simple programming assignment using OpenGL with GLUT from a C program. The basic
ID: 3670264 • 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 <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h
void initGL() {
// Set "clearing" or background color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.8f, 0.1f);
glVertex2f(-0.2f, 0.1f);
glVertex2f(-0.2f, 0.7f);
glVertex2f(-0.8f, 0.7f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex2f(-0.7f, -0.6f);
glVertex2f(-0.1f, -0.6f);
glVertex2f(-0.1f, 0.0f);
glVertex2f(-0.7f, 0.0f);
glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
glVertex2f(-0.9f, -0.7f);
glColor3f(1.0f, 1.0f, 1.0f); // White
glVertex2f(-0.5f, -0.7f);
glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
glVertex2f(-0.5f, -0.3f);
glColor3f(1.0f, 1.0f, 1.0f); // White
glVertex2f(-0.9f, -0.3f);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex2f(0.1f, -0.6f);
glVertex2f(0.7f, -0.6f);
glVertex2f(0.4f, -0.1f);
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(0.3f, -0.4f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex2f(0.9f, -0.4f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex2f(0.6f, -0.9f);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex2f(0.4f, 0.2f);
glVertex2f(0.6f, 0.2f);
glVertex2f(0.7f, 0.4f);
glVertex2f(0.6f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.3f, 0.4f);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("Vertex, Primitive & Color");
glutInitWindowSize(320, 320);
glutInitWindowPosition(50, 50);
glutDisplayFunc(display);
initGL();
glutMainLoop();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.