Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdlib.h>
- #include "glut.h"
- #include <vector>
- #include <array>
- #include <fstream>
- #include <iostream>
- GLint Width = 512, Height = 512;
- bool load = false;
- int vp_width = 512;
- int vp_height = 512;
- std::array<int, 2> currentPt;
- std::vector<std::array<int, 2>> pts;
- bool closed = false;
- int input = 0;
- void DisplayDefault()
- {
- if (!(load)) {
- glClearColor(0, 0.58823, 0.53333, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3ub(205, 220, 57);
- glBegin(GL_LINE_STRIP);
- std::ifstream input_file("polygonCoordinates.txt");
- if (input_file) {
- std::istream_iterator<int> start(input_file), end;
- std::vector<int> coordinates(start, end);
- std::cout << "Read " << coordinates.size() << " numbers" << std::endl;
- for (int i = 0; i<coordinates.size(); i += 2)
- pts.push_back(std::array<int, 2>{coordinates[i], coordinates[i + 1]});
- }
- else {
- std::cout << "polygonCoordinates.txt is Empty" << std::endl;
- pts.push_back(std::array<int, 2>{72, 323});
- pts.push_back(std::array<int, 2>{73, 252});
- pts.push_back(std::array<int, 2>{73, 252});
- pts.push_back(std::array<int, 2>{112, 251});
- pts.push_back(std::array<int, 2>{112, 251});
- pts.push_back(std::array<int, 2>{128, 275});
- pts.push_back(std::array<int, 2>{128, 275});
- pts.push_back(std::array<int, 2>{96, 225});
- pts.push_back(std::array<int, 2>{96, 225});
- pts.push_back(std::array<int, 2>{124, 224});
- pts.push_back(std::array<int, 2>{124, 224});
- pts.push_back(std::array<int, 2>{154, 271});
- pts.push_back(std::array<int, 2>{154, 271});
- pts.push_back(std::array<int, 2>{143, 253});
- pts.push_back(std::array<int, 2>{143, 253});
- pts.push_back(std::array<int, 2>{253, 253});
- pts.push_back(std::array<int, 2>{253, 253});
- pts.push_back(std::array<int, 2>{221, 296});
- pts.push_back(std::array<int, 2>{221, 296});
- pts.push_back(std::array<int, 2>{220, 275});
- pts.push_back(std::array<int, 2>{220, 275});
- pts.push_back(std::array<int, 2>{237, 274});
- pts.push_back(std::array<int, 2>{237, 274});
- pts.push_back(std::array<int, 2>{221, 297});
- pts.push_back(std::array<int, 2>{221, 297});
- pts.push_back(std::array<int, 2>{108, 296});
- pts.push_back(std::array<int, 2>{108, 296});
- pts.push_back(std::array<int, 2>{95, 324});
- pts.push_back(std::array<int, 2>{95, 324});
- pts.push_back(std::array<int, 2>{73, 323});
- }
- for (int i = 0; i<pts.size(); i++)
- glVertex3f(pts[i][0], pts[i][1], 0);
- glEnd();
- glFinish();
- load = true;
- }
- }
- void Reshape(GLint w, GLint h)
- {
- load = false;
- Width = w; Height = h;
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, w, 0, h, -1.0, 1.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void Mouse_move(int x, int y)
- {
- currentPt = std::array<int, 2>{x, vp_height - y};
- glutPostRedisplay();
- }
- void Save()
- {
- std::ofstream output_file("polygonCoordinates.txt", std::ofstream::out);
- for (int i = 0; i<pts.size(); i++) {
- if (i == 0)
- output_file << (float)pts[i][0] << " " << (float)pts[i][1] << std::endl;
- else if (i<pts.size() - 1) {
- output_file << (float)pts[i][0] << " " << (float)pts[i][1] << std::endl;
- output_file << (float)pts[i][0] << " " << (float)pts[i][1] << std::endl;
- }
- else
- output_file << (float)pts[i][0] << " " << (float)pts[i][1] << std::endl;
- glVertex2f((float)pts[i][0], (float)pts[i][1]);
- }
- output_file.flush();
- output_file.close();
- }
- void Draw_polygon_line(int button, int state, int x, int y)
- {
- if (input == 1) {
- currentPt = std::array<int, 2>{x, vp_height - y};
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
- {
- if (closed)
- pts.clear();
- closed = false;
- pts.push_back(currentPt);
- }
- if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
- closed = true;
- std::cout << pts.size() << ") " << currentPt[0] << ", " << currentPt[1] << std::endl;
- Save();
- }
- }
- void DrawYourself() {
- glClearColor(0.6196, 0.6196, 0.6196, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- if (!pts.empty()) {
- glBegin(GL_LINE_STRIP);
- for (int i = 0; i<pts.size(); i++) {
- glVertex2f((float)pts[i][0], (float)pts[i][1]);
- }
- auto &endPt = closed ? pts.front() : currentPt;
- glVertex2f((float)endPt[0], (float)endPt[1]);
- glEnd();
- glFinish();
- }
- glutSwapBuffers();
- }
- void Keyboard(unsigned char key, int x, int y)
- {
- std::cout << "Key pressed: " << key << std::endl;
- int speed = 5;
- switch (key) {
- case 'd':
- for (int i = 0; i<pts.size(); i++)
- pts[i][0] = pts[i][0] + speed;
- break;
- case 'a':
- for (int i = 0; i<pts.size(); i++)
- pts[i][0] = pts[i][0] - speed;
- break;
- case 'w':
- for (int i = 0; i<pts.size(); i++)
- pts[i][1] = pts[i][1] + speed;
- break;
- case 's':
- for (int i = 0; i<pts.size(); i++)
- pts[i][1] = pts[i][1] - speed;
- break;
- default:
- break;
- }
- glClearColor(0, 0.58823, 0.53333, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3ub(205, 220, 57);
- glBegin(GL_LINE_STRIP);
- for (int i = 0; i < pts.size(); i++)
- glVertex3f(pts[i][0], pts[i][1], 0);
- glEnd();
- glFinish();
- }
- int main(int argc, char *argv[]) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB);
- glutInitWindowSize(Width, Height);
- glutCreateWindow("Computer Graphics #6");
- glutReshapeFunc(Reshape);
- glutMouseFunc(Draw_polygon_line);
- glLineWidth(3.0f);
- std::cout << "1-Draw by yourself" << std::endl;
- std::cout << "2-Display Default Polygon (Control buttons (W, S, A, D))" << std::endl;
- std::cin >> input;
- switch (input) {
- case 1:
- glutDisplayFunc(DrawYourself);
- DrawYourself();
- break;
- case 2:
- glutDisplayFunc(DisplayDefault);
- break;
- default:
- return 0;
- }
- glutPassiveMotionFunc(Mouse_move);
- glMatrixMode(GL_PROJECTION);
- glutKeyboardFunc(Keyboard);
- glOrtho(0.0f, (float)vp_width, 0.0f, (float)vp_height, -1.0, 1.0);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment