Skip to content

Instantly share code, notes, and snippets.

@jameskerr
Created January 7, 2015 06:41
Show Gist options
  • Save jameskerr/719e56414a4fe43b7c48 to your computer and use it in GitHub Desktop.
Save jameskerr/719e56414a4fe43b7c48 to your computer and use it in GitHub Desktop.
Assignment 2 (Primitive Practice)
#include <stdlib.h>
#include <GLUT/glut.h>
#include <math.h>
/*************************
::: HELPER VARIABLES ::: *
*************************/
enum MENU_TYPE {
POINTS,
LINES,
LINE_STRIP,
LINE_LOOP,
TRIANGLES,
TRIANGLE_STRIP,
TRIANGLE_FAN
};
typedef struct Coord_t {
GLfloat x;
GLfloat y;
Coord_t(GLfloat _x, GLfloat _y) : x(_x), y(_y) {}
};
int coords_length = 12;
Coord_t coords[12] = {
Coord_t(1.0, 5.0),
Coord_t(5.0, 1.0),
Coord_t(2.0, 6.0),
Coord_t(6.0, 2.0),
Coord_t(3.0, 7.0),
Coord_t(7.0, 3.0),
Coord_t(4.0, 8.0),
Coord_t(8.0, 4.0),
Coord_t(5.0, 9.0),
Coord_t(9.0, 5.0),
Coord_t(6.0, 10.0),
Coord_t(10.0, 6.0)
};
/************************************
::: HELPER FUNCTIONS DECLARATIONS :::
************************************/
void initWindow();
void initMenu();
void menu(int);
void display();
void loopPoints();
void renderPoints();
void renderPointsUsing(int);
/*****************************
::: MAIN PROGRAM EXECUTION :::
******************************/
int main(int argc, char** argv) {
glutInit(&argc, argv);
initWindow();
initMenu();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
/***************************************
::: HELPER FUNCTIONS IMPLEMENTATIONS :::
***************************************/
void initWindow() {
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 400);
glutInitWindowPosition(0, 0);
glutCreateWindow("Primitives");
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0.0, 20.0, 0.0, 12.0);
glColor3f(1.0, 0.0, 0.0);
}
void initMenu() {
glutCreateMenu(menu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutAddMenuEntry("Points", POINTS);
glutAddMenuEntry("Lines", LINES);
glutAddMenuEntry("Line Strip", LINE_STRIP);
glutAddMenuEntry("Line Loop", LINE_LOOP);
glutAddMenuEntry("Triangles", TRIANGLES);
glutAddMenuEntry("Triangle Strip", TRIANGLE_STRIP);
glutAddMenuEntry("Triangle Fan", TRIANGLE_FAN);
}
void menu(int item) {
glClear(GL_COLOR_BUFFER_BIT);
renderPoints();
switch (item) {
case POINTS:
renderPointsUsing(GL_POINTS);
break;
case LINES:
renderPointsUsing(GL_LINES);
break;
case LINE_STRIP:
renderPointsUsing(GL_LINE_STRIP);
break;
case LINE_LOOP:
renderPointsUsing(GL_LINE_LOOP);
break;
case TRIANGLES:
renderPointsUsing(GL_TRIANGLES);
break;
case TRIANGLE_STRIP:
renderPointsUsing(GL_TRIANGLE_STRIP);
break;
case TRIANGLE_FAN:
renderPointsUsing(GL_TRIANGLE_FAN);
break;
default:
break;
}
glFlush();
}
void display() {
glPointSize(5);
glLineWidth(5);
renderPoints();
glFlush();
}
void renderPoints() {
glBegin(GL_POINTS);
for(int i = 0; i < coords_length; ++i) {
glVertex2f(coords[i].x, coords[i].y);
}
glEnd();
}
void renderPointsUsing(int primitive) {
glBegin(primitive);
for(int i = 0; i < coords_length; ++i) {
glVertex2f(coords[i].x + 10.0, coords[i].y);
}
glEnd();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment