Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Killian Mills, 11368701
- Compile:
- MinGW Windows:
- gcc Assignment2.c -o run glut32.lib -lopengl32 -lglu32
- Ubuntu:
- gcc -o run Assignment2.c -lglut -lGLU -lGL -lm
- */
- #include <GL/gl.h>
- #include <GL/glext.h>
- #include <GL/glut.h>
- #include <stdio.h>
- GLfloat xx1,xx2,yy1,yy2,xx3,yy3;
- GLfloat firstx, firsty;
- GLfloat hh;
- int action;
- int mainView;
- float transparent = 1.0;
- float brightness = 0.0;
- float red = 1.0;
- float green = 1.0;
- float blue = 1.0;
- GLfloat colourPicker[3];
- int xAxis[99];
- int yAxis[99];
- int polyLimit;
- void Display(void)
- {
- glClearColor(0.0,0.0,0.0,1.0);
- glColor4f(red, green, blue,transparent);
- glFlush();
- return;
- }
- //Point
- void DisplayPoint(){
- glBegin(GL_POINTS);
- glVertex2f(xx1,yy1);
- glEnd();
- glFlush();
- return;
- }
- //Line
- void DisplayLine(){
- glBegin(GL_LINES);
- glVertex2f(xx1,yy1);
- glVertex2f(xx2,yy2);
- glEnd();
- glFlush();
- return;
- }
- //Rectangle
- void DisplayRectangle(){
- glBegin(GL_POLYGON);
- glVertex2f(xx1,yy1);
- glVertex2f(xx1,yy2);
- glVertex2f(xx2,yy2);
- glVertex2f(xx2,yy1);
- glEnd();
- glFlush();
- return;
- }
- //Triangle
- void DisplayTriangle(){
- glBegin(GL_POLYGON);
- glVertex2f(xx1,yy1);
- glVertex2f(xx2,yy2);
- glVertex2f(xx3,yy3);
- glEnd();
- glFlush();
- return;
- }
- //Polygon
- void DisplayPolygon(){
- int counter=0;
- glBegin(GL_POLYGON);
- while(counter < polyLimit){ // display to limit of polygon
- glVertex2f(xAxis[counter],yAxis[counter]);
- counter++;
- }
- glEnd();
- glFlush();
- return;
- }
- // mouse listener for each shape
- void MouseDraw(GLint button, GLint state, GLint x, GLint y)
- {
- static int first=1;
- static int count=0;
- if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
- {
- //Point
- if(action == 0){
- xx1=x;
- yy1=hh-y;
- DisplayPoint();
- }
- //Line
- if(action == 1){
- if (first)
- {
- xx1=x;
- yy1=hh-y;
- }
- else
- {
- xx2=x;
- yy2=hh-y;
- DisplayLine();
- }
- first = !first;
- }
- // Rectangle
- if(action == 2){
- if (first)
- {
- xx1=x;
- yy1=hh-y;
- }
- else
- {
- xx2=x;
- yy2=hh-y;
- DisplayRectangle();
- }
- first = !first;
- }
- // Triangle
- if(action == 3){
- if(first==1){
- xx1=x;
- yy1=hh-y;
- first = 2;
- }
- else if(first==2){
- xx2=x;
- yy2=hh-y;
- first = 3;
- }
- else{
- xx3=x;
- yy3=hh-y;
- DisplayTriangle();
- first = 1;
- }
- }
- // Polygon
- if(action == 4){
- xAxis[count] =x;
- yAxis[count] =hh-y;
- count++;
- if(count == polyLimit){
- DisplayPolygon();
- count=0;
- }
- }
- }
- return;
- }
- //pick colour using mouse location on colour window
- void pickColour(GLint button, GLint state, GLint x, GLint y){
- if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON){
- xx1=x;
- yy1=hh-y;
- glReadPixels(xx1, yy1, 1, 1, GL_RGB, GL_FLOAT, colourPicker);
- glutSetWindow(mainView);
- red = colourPicker[0];
- green = colourPicker[1];
- blue = colourPicker[2];
- glColor4f(red, green, blue, transparent);
- glFlush();
- }
- return;
- }
- void MyReshape(GLsizei w, GLsizei h)
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0,(GLfloat)w,0.0,(GLfloat)h);
- glViewport(0,0,w,h);
- hh=h;
- return;
- }
- // display colours on seperate window
- // from : https://en.wikibooks.org/wiki/OpenGL_Programming/Basics/Color
- void displayColour() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBegin(GL_QUADS);
- glColor3f(0.5f, 0.0f, 1.0f); // make this vertex purple
- glVertex2f(-0.75, 0.75);
- glColor3f(1.0f, 0.0f, 0.0f); // make this vertex red
- glVertex2f(-0.75, -0.75);
- glColor3f(0.0f, 1.0f, 0.0f); // make this vertex green
- glVertex2f(0.75, -0.75);
- glColor3f(1.0f, 1.0f, 0.0f); // make this vertex yellow
- glVertex2f(0.75, 0.75);
- glEnd();
- glutSwapBuffers();
- }
- // basic colours, change red, green, blue
- void ColourMenu(int op){
- switch(op) {
- case('r'):
- red = 1.0;
- green = 0.0;
- blue = 0.0;
- glColor4f(red, green, blue, transparent); // red
- break;
- case('g'):
- red = 0.0;
- green = 1.0;
- blue = 0.0;
- glColor4f(red, green, blue, transparent); // green
- break;
- case('b'):
- red = 0.0;
- green = 0.0;
- blue = 1.0;
- glColor4f(red, green, blue, transparent); // blue
- break;
- case('w'):
- red = 1.0;
- green = 1.0;
- blue = 1.0;
- glColor4f(red, green, blue, transparent); // white
- break;
- case('v'):
- red = 0.0;
- green = 0.0;
- blue = 0.0;
- glColor4f(red, green, blue, transparent); // black
- break;
- }
- }
- // change transparency, keep red, green, blue same
- void transparentMenu(int op){
- switch(op) {
- case(10):
- transparent = 1.0;
- glColor4f(red, green, blue, transparent); // high
- break;
- case(20):
- transparent = 0.8;
- glColor4f(red, green, blue, transparent);
- break;
- case(30):
- transparent = 0.6;
- glColor4f(red, green, blue, transparent);
- break;
- case(40):
- transparent = 0.4;
- glColor4f(red, green, blue, transparent);
- break;
- case(50):
- transparent = 0.2;
- glColor4f(red, green, blue, transparent); // low
- break;
- }
- }
- // change brightness, add or subtract from red green and blue
- void brightMenu(int op){
- brightness += ((double)op)/10;
- if(brightness > 1.0) {
- brightness = 1.0;
- }
- else if(brightness < -1.0) {
- brightness = -1.0;
- }
- glColor4f(red+brightness,green+brightness,blue+brightness,transparent); // add brightness
- glFlush();
- }
- //assign limit to polygon
- void polygonMenu(int op){
- polyLimit = op;
- action = 4;
- }
- //main menu options
- void menu(int op){
- switch(op) {
- case(0):
- action = 0; // point
- break;
- case(1):
- action = 1; // line
- break;
- case(2):
- action = 2; // rectangle
- break;
- case(3):
- action = 3; // triangle
- break;
- case(6):
- glClear(GL_COLOR_BUFFER_BIT); // clear
- glFlush();
- }
- }
- int main(int argc, char **argv)
- {
- // Draw Window
- glutInit(&argc, argv);
- glutInitWindowSize(500,500);
- glutInitWindowPosition(500,200);
- mainView = glutCreateWindow("Assignment2: Paint");
- glutReshapeFunc(MyReshape);
- glutMouseFunc(MouseDraw);
- glutDisplayFunc(Display);
- glClear(GL_COLOR_BUFFER_BIT);
- glPointSize(5);
- glLineWidth(5);
- // Colour Menu
- int colourList = glutCreateMenu(ColourMenu);
- glutAddMenuEntry("Red", 'r');
- glutAddMenuEntry("Green", 'g');
- glutAddMenuEntry("Blue", 'b');
- glutAddMenuEntry("White", 'w');
- glutAddMenuEntry("Black", 'v');
- // transparent Menu
- int transparentList = glutCreateMenu(transparentMenu);
- glutAddMenuEntry("1.0", 10);
- glutAddMenuEntry("0.8", 20);
- glutAddMenuEntry("0.6", 30);
- glutAddMenuEntry("0.4", 40);
- glutAddMenuEntry("0.2", 50);
- // transparent Menu
- int brightList = glutCreateMenu(brightMenu);
- glutAddMenuEntry("Increase", 1);
- glutAddMenuEntry("Decrease", -1);
- // polygon Menu
- int polyList = glutCreateMenu(polygonMenu);
- glutAddMenuEntry("3", 3);
- glutAddMenuEntry("4", 4);
- glutAddMenuEntry("5", 5);
- glutAddMenuEntry("6", 6);
- glutAddMenuEntry("7", 7);
- glutAddMenuEntry("8", 8);
- glutAddMenuEntry("9", 9);
- glutAddMenuEntry("10", 10);
- // Main Menu
- glutCreateMenu(menu);
- glutAddMenuEntry("Point", 0);
- glutAddMenuEntry("Line", 1);
- glutAddMenuEntry("Rectangle", 2);
- glutAddMenuEntry("Triangle", 3);
- glutAddSubMenu("Polygon", polyList);
- glutAddSubMenu("Colour", colourList);
- glutAddSubMenu("Transparency", transparentList);
- glutAddSubMenu("Brightness", brightList);
- glutAddMenuEntry("Clear", 6);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- // Enable Blending
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- // Colour Window
- glutInitWindowPosition(1100,200);
- glutCreateWindow("Colours");
- glutDisplayFunc(displayColour);
- glutMouseFunc(pickColour);
- //Start Looping
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement