-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek3
138 lines (108 loc) · 2.57 KB
/
week3
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <GLFW/glfw3.h>
#include <cstring>
#include <stdlib.h> // srand, rand
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
#include "math.h"
#include <algorithm>
const int width = 640;
const int height = 480;
float* pixels = new float[width*height * 3];
void drawPixel(const int& i, const int& j, const float& red, const float& green, const float& blue)
{
pixels[(i + width* j) * 3 + 0] = red;
pixels[(i + width* j) * 3 + 1] = green;
pixels[(i + width* j) * 3 + 2] = blue;
}
void drawLine(const int& i0, const int& j0, const int& i1, const int& j1, const float& red, const float& green, const float& blue)
{
for (int i = i0; i <= i1; i++)
{
const int j = (j1 - j0)*(i - i0) / (i1 - i0) + j0;
drawPixel(i, j, red, green, blue);
}
}
bool circle(const int x, const int y)
{
const int x_c = 300;
const int y_c = 300;
const int r = 20;
int f = (x - x_c)*(x - x_c) + (y - y_c)*(y - y_c) - r*r;
if (f > 0) return false;
else return true;
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(width, height, "Hello", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
std::fill_n(pixels, width*height * 3, 1.0f);
//#1
drawLine(16, 76, 100, 10, 1.0f, 0.0f, 0.0f);
drawLine(16, 77, 100, 11, 1.0f, 0.0f, 0.0f);
drawLine(16, 78, 100, 12, 1.0f, 0.0f, 0.0f);
//#2
for (int i = 240; i < 320; i++)
{
for (int j = 63; j < 171; j++)
{
if (i == 319)
{
drawPixel(i, j, 0.0f, 0.0f, 0.0f);
}
else if (i == 240)
{
drawPixel(i, j, 0.0f, 0.0f, 0.0f);
}
if (j == 63)
{
drawPixel(i, j, 0.0f, 0.0f, 0.0f);
}
if (j == 170)
{
drawPixel(i, j, 0.0f, 0.0f, 0.0f);
}
}
}
//#3
for (int i = 397; i < 592; i++)
{
for (int j = 63; j < 171; j++)
{
drawPixel(i, j, 0.0f, 1.0f, 0.0f);
}
}
//#4
// //#6
//for (int i = 0; i < 640; i++)
//{
// for (int j = 0; i < 480; j++)
// {
// if(circle(i,j)==true)
// drawPixel(i, j, 0.0f, 1.0f, 1.0f);
// }
//}
glDrawPixels(width, height, GL_RGB, GL_FLOAT, pixels);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}