-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyPainter2D.h
109 lines (82 loc) · 2.03 KB
/
MyPainter2D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once
#include <GLFW/glfw3.h>
#include <iostream>
class MyPainter2D
{
public:
GLFWwindow* window;
const int width;
const int height = 480;
// color = (red, green, blue)
float *pixels;
MyPainter2D() //default constructor
:window(nullptr), pixels(nullptr), width(640)
{
}
~MyPainter2D()//destructor
{
std::cout << "destructor MyPainter2D" << std::endl;
if (pixels != nullptr)
delete pixels;
glfwTerminate();
}
void initialize()
{
/* Initialize the library */
if (!glfwInit())
{
std::cout << "glfw failed" << std::endl;
exit(1);//end this process
}
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(width, height, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
std::cout << "window failed" << std::endl;
exit(1);
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
// color = (red, green, blue)
pixels = new float[width * height * 3];
}
void drawOnePixel(const int& x, const int& y, const float& red, const float& green, const float& blue)
{
pixels[(x + width*y) * 3 + 0] = red;
pixels[(x + width*y) * 3 + 1] = green;
pixels[(x + width*y) * 3 + 2] = blue;
}
void drawLine(const int& x_s, const int& y_s, const int& x_e, const int& y_e, const float& red, const float& green, const float& blue)
{
if (x_e == x_s)
{
for (int y = y_s; y <= y_e; y++)
drawOnePixel(x_s, y, red, green, blue);
}
else
{
for (int i = x_s; i <= x_e; i++)
{
const int j = (y_e - y_s)*(i - x_s) / (x_e - x_s) + y_s;
drawOnePixel(i, j, red, green, blue);
}
}
}
bool ShouldCloseWindow()
{
return (bool)glfwWindowShouldClose(window);
}
void preProcessing()
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
}
void postProcessing()
{
/* Swap front and back buffers */
glfwSwapBuffers(my_painter.window);
/* Poll for and process events */
glfwPollEvents();
}
};