Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <glut.h>
- #include <stdio.h>
- #include <vector>
- #pragma comment (lib, "opengl32.lib")
- #pragma comment (lib, "glu32.lib")
- #pragma comment(lib,"glut32.lib")
- using namespace std;
- double x, y, xOrigin = 0;
- struct type_pair {
- double x;
- double y;
- };
- vector<vector<type_pair>> points;
- int area = 1;
- void renderScene() {
- glPointSize(10);
- glClearColor(0, 0, 0, 0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- /*glBegin(GL_TRIANGLES);
- glVertex3f(-0.5, -0.5, 0.0);
- glVertex3f(0.0, 0.5, 0.0);
- glVertex3f(0.5, -0.5, 0.0);
- glEnd();*/
- for (int j = 0; j < area; j++) {
- glBegin(GL_LINE_LOOP);
- /*glColor3d(1, 0, 0);
- glVertex3d(1, 3, 0);
- glVertex3d(4, 3, 0);
- glColor3d(0, 1, 0);
- glVertex3d(3, 2.7, 0);
- glColor3d(0, 0, 1);
- glVertex3d(2.5, 3.7, 0);*/
- /*glVertex2f(0, 0);
- glVertex2f(0, 0.5);
- glVertex2f(0.5, 0);
- glVertex2f(-0.7, -0.3);*/
- //glPointSize(10);
- //glBegin(GL_POINTS);
- for (int i = 0; i < points[j].size(); i++) {
- glVertex2f(points[j][i].x, points[j][i].y);
- }
- glEnd();
- glBegin(GL_POINTS);
- for (int i = 0; i < points[j].size(); i++) {
- glVertex2f(points[j][i].x, points[j][i].y);
- }
- glEnd();
- }
- glutSwapBuffers();
- }
- void changeSize(int w, int h) {
- //// предупредим деление на ноль
- //// если окно сильно перетянуто будет
- //if (h == 0)
- // h = 1;
- //float ratio = 1.0 * w / h;
- //// используем матрицу проекции
- //glMatrixMode(GL_PROJECTION);
- //// Reset матрицы
- //glLoadIdentity();
- //// определяем окно просмотра
- //glViewport(0, 0, w, h);
- //// установить корректную перспективу.
- //gluPerspective(45, ratio, 1, 1000);
- //// вернуться к модели
- //glMatrixMode(GL_MODELVIEW);
- //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 mouseButton(int button, int state, int x, int y) {
- printf("%d %d %d %d\n", button, state, x, y);
- // только при начале движения, если нажата левая кнопка
- if (button == GLUT_LEFT_BUTTON) {
- // когда кнопка отпущена
- if (state == GLUT_UP) {
- printf("up\n");
- glVertex2f(0, 0);
- }
- else {// state = GLUT_DOWN
- printf("down\n");
- type_pair elem;
- elem.x = (x - 333.0) / 333;
- elem.y = (333.0 - y) / 333;
- if (area != points.size()) {
- /*points.resize(area + 1);*/
- }
- points[area - 1].push_back(elem);
- }
- }
- if (button == GLUT_RIGHT_BUTTON) {
- // когда кнопка отпущена
- if (state == GLUT_UP) {
- printf("up\n");
- }
- else {// state = GLUT_DOWN
- printf("down\n");
- area++;
- points.resize(area);
- }
- }
- glutPostRedisplay();
- }
- void processNormalKeys(unsigned char key, int x, int y) {
- printf("%d %d %d\n", key, x, y);
- if (key == 'r') {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPointSize(10);
- glBegin(GL_POINTS);
- glVertex2f(0, 0);
- glEnd();
- }
- }
- void processSpecialKeys(int key, int x, int y) {
- switch (key) {
- case GLUT_KEY_F1:
- glPointSize(10);
- glBegin(GL_POINTS);
- glVertex2f(0, 0.5);
- glEnd();
- case GLUT_KEY_F2:
- glPointSize(10);
- glBegin(GL_POINTS);
- glVertex2f(0, 0);
- glEnd();
- case GLUT_KEY_F3:
- glPointSize(10);
- glBegin(GL_POINTS);
- glVertex2f(-0.5, 0.5);
- glEnd();
- }
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowPosition(400, 400);
- glutInitWindowSize(666, 666);
- glutCreateWindow("New");
- x = 0.5;
- y = 0;
- points.resize(1);
- //glutReshapeFunc(changeSize);
- glutMouseFunc(mouseButton);
- glutSpecialFunc(processSpecialKeys);
- glutKeyboardFunc(processNormalKeys);
- glutDisplayFunc(renderScene);
- glutMainLoop();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement