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>
- #include <cstdlib>
- #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;
- GLfloat *intrails;
- std::vector<point> shape;
- std::vector<std::vector<int>> edge_table;
- 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;
- intrails[i] = 0;
- }
- }
- void copy_buffer(GLfloat *buff_source, GLfloat *buff_dest)
- {
- for (int i = 0; i < BUFFER_SIZE; i++) {
- buff_dest[i] = buff_source[i];
- }
- }
- void create_buffer()
- {
- pixels = new GLfloat[BUFFER_SIZE];
- intrails = new GLfloat[BUFFER_SIZE];
- }
- 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 Bresenham(int x1, int y1, int const x2, int const y2)
- {
- int delta_x(x2 - x1);
- // if x1 == x2, then it does not matter what we set here
- signed char const ix((delta_x > 0) - (delta_x < 0));
- delta_x = std::abs(delta_x) << 1;
- int delta_y(y2 - y1);
- // if y1 == y2, then it does not matter what we set here
- signed char const iy((delta_y > 0) - (delta_y < 0));
- delta_y = std::abs(delta_y) << 1;
- //plot(x1, y1);
- pixels[PXL_INDX(x1, y1, 0)] = 1;
- if (delta_x >= delta_y)
- {
- // error may go below zero
- int error(delta_y - (delta_x >> 1));
- while (x1 != x2)
- {
- if ((error >= 0) && (error || (ix > 0)))
- {
- error -= delta_x;
- y1 += iy;
- }
- // else do nothing
- error += delta_y;
- x1 += ix;
- //plot(x1, y1);
- pixels[PXL_INDX(x1, y1, 0)] = 1;
- }
- }
- else
- {
- // error may go below zero
- int error(delta_x - (delta_y >> 1));
- while (y1 != y2)
- {
- if ((error >= 0) && (error || (iy > 0)))
- {
- error -= delta_y;
- x1 += ix;
- }
- // else do nothing
- error += delta_x;
- y1 += iy;
- //plot(x1, y1);
- pixels[PXL_INDX(x1,y1,0)] = 1;
- }
- }
- }
- void draw_complex_line(point a, point b)
- {
- if (a.x == b.x && a.y == b.y) return;
- Bresenham(a.x, a.y, b.x, b.y);
- }
- void set_contour()//D
- {
- for (int i = 0; i < shape.size() - 1; i++)
- draw_complex_line(shape[i], shape[i + 1]);
- }
- void draw_contour()//D
- {
- glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RED, GL_FLOAT, (void*)pixels);
- //for (int 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 set_intrails_vector()//F
- {
- int x, y, k, component = 0, height;
- bool inside;
- if (3 == DATA_FORMAT) component = 3;
- copy_buffer(pixels, intrails);
- edge_table.resize(SCREEN_HEIGHT);
- for (y = y_min + 2, k = 0; y <= y_max - 1; y++, k++) {
- inside = false;
- for (x = x_min - 1; x <= x_max + 1; x++) {
- if (pixels[PXL_INDX(x, y, component)] == 1) {
- inside = true;
- while (pixels[PXL_INDX(x, y, component)] == 1){
- edge_table[k].push_back(x);
- x++;
- }
- while (pixels[PXL_INDX(x, y, component)] == 0 && x < x_max && inside) {
- intrails[PXL_INDX(x, y, component)] = 1;
- x++;
- }
- while (pixels[PXL_INDX(x, y, component)] == 1){
- edge_table[k].push_back(x);
- x++;
- }
- }
- }
- }
- }
- void set_intrails_pixels()
- {
- int x, y, i;
- /*
- for (y = 0; y < SCREEN_HEIGHT; y++) {
- for (x = 0; x < SCREEN_WIDTH; x++) {
- for (i = 0; i < edge_table[y].size(); i++) {
- if (edge_table[y][i] == x) {
- intrails[PXL_INDX(x, y, 0)] = 1;
- }
- else {
- intrails[PXL_INDX(x, y, 0)] = 0;
- }
- }
- }
- }*/
- }
- void draw_intrails()//F
- {
- glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RED, GL_FLOAT, (void*)intrails);
- }
- void smooth_circuit()//S
- {
- }
- 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()) {
- set_contour();
- draw_contour();
- }
- }
- if (fill_flag) {
- set_intrails_vector();
- set_intrails_pixels();
- draw_intrails();
- }
- if (anti_aliacing_flag) {
- smooth_circuit();
- }
- if (update_flag) {
- if (draw_flag) {
- set_contour();
- draw_contour();
- }
- if (fill_flag) {
- set_intrails_vector();
- set_intrails_pixels();
- draw_intrails();
- }
- 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 || '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 || '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 || 'F' == key) {
- fill_flag = !fill_flag;
- printf("filling:");
- fill_flag ? printf("ON\n") : printf("OFF\n");
- glutPostRedisplay();
- }
- if ('z' == key || 'Z' == key) {
- if (0 < shape.size()) {
- shape.pop_back();
- figure_pos();
- glutPostRedisplay();
- }
- }
- if ('a' == key || 'A' == key) {
- draw_complex_line(shape[shape.size()-1], shape[0]);
- glutPostRedisplay();
- }
- }
- void initial_state()
- {
- glClearColor(0, 0, 0, 1);
- draw_flag = false;
- fill_flag = false;
- anti_aliacing_flag = false;
- update_flag = false;
- x_max = 0;
- x_min = SCREEN_WIDTH;
- y_max = 0;
- y_min = SCREEN_HEIGHT;
- create_buffer();
- 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