Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // glfw_30.cpp : Defines the entry point for the console application.
- #include "stdafx.h"
- #include <time.h>
- #define _USE_MATH_DEFINES
- #include <cmath>
- #include <GL/glew.h>
- #include <GL\glut.h>
- #include <GLFW/glfw3.h>
- #include <vector>
- #include <algorithm>
- #define SCREEN_POS_X 100
- #define SCREEN_POS_Y 100
- #define SCREEN_WIDTH 800
- #define SCREEN_HEIGHT 600
- #define DATA_FORMAT 1
- #define BUFFER_SIZE SCREEN_WIDTH*SCREEN_HEIGHT*DATA_FORMAT
- #define PXL_INDX(X,Y,COMP) ((Y*SCREEN_WIDTH+X)*DATA_FORMAT+COMP)
- typedef struct {
- int x, y;
- } point;
- bool draw_flag, fill_flag, anti_aliacing_flag, update_flag;
- int x_max, x_min, y_max, y_min;
- GLfloat *pixels;
- std::vector<point> shape;
- std::vector<std::vector<int>> enge_table;
- bool myfunction(point i, point j) { return (i.y<j.y); }
- void changeSize(int w, int h)
- {
- // Prevent a divide by zero, when window is too short
- // (you cant make a window of zero width).
- if (0 == h)
- h = 1;
- float ratio = w * 1.0 / h;
- // Use the Projection Matrix
- glMatrixMode(GL_PROJECTION);
- // Reset Matrix
- glLoadIdentity();
- // Set the viewport to be the entire window
- glViewport(0, 0, w, h);
- // Set the correct perspective.
- gluOrtho2D(0, (GLdouble)w, 0, (GLdouble)h);
- // Get Back to the Modelview
- glMatrixMode(GL_MODELVIEW);
- }
- void clear_buffer()
- {
- for (int i = 0; i < BUFFER_SIZE; i++) pixels[i] = 0;
- }
- void draw_simple_line(point a, point b)
- {
- if (a.x == b.x && a.y == b.y) return;
- glColor3f(1, 0, 0);
- glBegin(GL_LINES);
- glVertex2f(a.x, a.y);
- glVertex2f(b.x, b.y);
- glEnd();
- }
- void draw_complex_line(point a, point b)
- {
- if (a.x == b.x && a.y == b.y) return;
- int i, j;
- int dx = abs(a.x - b.x);
- int dy = abs(a.y - b.y);
- }
- void draw_circuit()
- {
- int i, j;
- for (i = 0; i < shape.size() - 1; i++) {
- draw_simple_line(shape[i], shape[i + 1]);
- }
- draw_simple_line(shape[0], shape[shape.size()-1]);
- }
- void fill_circuit()
- {
- int i, j, component = 0;
- if (3 == DATA_FORMAT) component = 3;
- clear_buffer();
- for (i = 0; i < SCREEN_HEIGHT; i++) {
- for (j = 0; j < SCREEN_WIDTH; j++) {
- if (i > y_min && i < y_max && j > x_min && j < x_max) {
- pixels[PXL_INDX(j, i, component)] = 1;
- }
- }
- }
- glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RED, GL_FLOAT, (void*)pixels);
- }
- void smooth_circuit()
- {
- }
- void figure_pos()
- {
- int max_x = 0, min_x = SCREEN_WIDTH;
- int max_y = 0, min_y = SCREEN_HEIGHT;
- for (int i = 0; i < shape.size(); i++) {
- if (shape[i].x > max_x) max_x = shape[i].x;
- if (shape[i].x < min_x) min_x = shape[i].x;
- if (shape[i].y > max_y) max_y = shape[i].y;
- if (shape[i].y < min_y) min_y = shape[i].y;
- }
- x_max = max_x;
- x_min = min_x;
- y_max = max_y;
- y_min = min_y;
- }
- void renderScene(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- if (draw_flag) {
- if (0 != shape.size())
- draw_circuit();
- }
- if (fill_flag) {
- fill_circuit();
- }
- if (anti_aliacing_flag) {
- smooth_circuit();
- }
- if (update_flag) {
- if (draw_flag) draw_circuit();
- if (fill_flag) fill_circuit();
- if (anti_aliacing_flag) smooth_circuit();
- }
- glutSwapBuffers();
- }
- void mouseButton(int button, int state, int x, int y)
- {
- if (GLUT_LEFT_BUTTON == button) {
- point a;
- if (GLUT_DOWN == state) {
- a.x = x;
- a.y = SCREEN_HEIGHT - y;
- printf("%d %d\n", a.x, a.y);
- shape.push_back(a);
- figure_pos();
- glutPostRedisplay();
- }
- if (GLUT_UP == state) {
- glutPostRedisplay();
- }
- }
- }
- void keyboardButton(unsigned char key, int x, int y)
- {
- if ('d' == key) {
- draw_flag = !draw_flag;
- printf("x_min=%d x_max=%d\ny_min=%d y_max=%d\n", x_min, x_max, y_min, y_max);
- printf("drawing:");
- draw_flag ? printf("ON\n") : printf("OFF\n");
- glutPostRedisplay();
- }
- if ('s' == key) {
- anti_aliacing_flag = !anti_aliacing_flag;
- anti_aliacing_flag ? printf("pc_mode:ON\n") : printf("console_mode:ON\n");
- glutPostRedisplay();
- }
- if ('f' == key) {
- fill_flag = !fill_flag;
- printf("filling:");
- fill_flag ? printf("ON\n") : printf("OFF\n");
- glutPostRedisplay();
- }
- if ('z' == key) {
- shape.pop_back();
- figure_pos();
- glutPostRedisplay();
- }
- }
- void initial_state()
- {
- glClearColor(1, 1, 1, 1);
- draw_flag = true;
- fill_flag = false;
- anti_aliacing_flag = false;
- update_flag = false;
- x_max = 0;
- x_min = SCREEN_WIDTH;
- y_max = 0;
- y_min = SCREEN_HEIGHT;
- pixels = new GLfloat[BUFFER_SIZE];
- clear_buffer();
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(SCREEN_POS_X, SCREEN_POS_Y);
- glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
- glutCreateWindow("Anthony's Project: Lab 4");
- glutDisplayFunc(renderScene);
- glutReshapeFunc(changeSize);
- glutIdleFunc(renderScene);
- glutMouseFunc(mouseButton);
- glutKeyboardFunc(keyboardButton);
- initial_state();
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement