-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox256glwidget.cpp
More file actions
170 lines (160 loc) · 5.47 KB
/
box256glwidget.cpp
File metadata and controls
170 lines (160 loc) · 5.47 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "box256glwidget.h"
#include <QtMath>
#include <QOpenGLFunctions>
#include <iostream>
Box256GLWidget::Box256GLWidget(Box256Machine *m)
: qTimer(), time(0), gridVertBuffer(QOpenGLBuffer::VertexBuffer), numGridVerts(0),numPixelVerts(0), machine(m)
{
qTimer.start(1000/60,this);
}
Box256GLWidget::~Box256GLWidget()
{
qTimer.stop();
}
void Box256GLWidget::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
QString vertShaderStr = "#version 330 core\n"
"layout (location = 0) in vec2 aPos;\n"
"void main()\n"
"{"
" gl_Position = vec4(aPos.xy, 0.0, 1.0);\n"
"}";
QString gridFragShaderStr = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{"
" FragColor = vec4(1.0f);\n"
"}";
QString pixelVertShaderStr = "#version 330 core\n"
"layout (location = 0) in vec2 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"out vec3 pixColor;\n"
"void main()\n"
"{"
" gl_Position = vec4(aPos.xy, 0.0, 1.0);\n"
" pixColor = aColor;\n"
"}";
QString pixelFragShaderStr = "#version 330 core\n"
"in vec3 pixColor;\n"
"out vec4 FragColor;\n"
"void main()\n"
"{"
" FragColor = vec4(pixColor, 1.0f);\n"
"}";
gridShader.addShaderFromSourceCode(QOpenGLShader::Vertex,vertShaderStr);
gridShader.addShaderFromSourceCode(QOpenGLShader::Fragment,gridFragShaderStr);
gridShader.link();
gridVertBuffer.create();
gridVertBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
pixShader.addShaderFromSourceCode(QOpenGLShader::Vertex,pixelVertShaderStr);
pixShader.addShaderFromSourceCode(QOpenGLShader::Fragment,pixelFragShaderStr);
pixShader.link();
pixVertBuffer.create();
pixVertBuffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
gridVertBuffer.bind();
QVector<float> verticesVec;
float s = 0.5f / 0xF;
for(int i=0x0;i<=0xF;i++)
{
float x = i*2*s - 0.5f;
for(int j=0x0;j<=0xF;j++)
{
float y = j*2*s - 0.5f;
#define vx(f) verticesVec.push_back(f+x); numGridVerts++
#define vy(f) verticesVec.push_back(f+y); numGridVerts++
vx(-s);vy(-s);vx(-s);vy(s);
vx(-s);vy(s);vx(s);vy(s);
vx(s);vy(s);vx(s);vy(-s);
vx(s);vy(-s);vx(-s);vy(-s);
#undef vx
#undef vy
}
}
gridVertBuffer.allocate(verticesVec.data(),verticesVec.size() * static_cast<int>(sizeof(float)) );
updatePixels();
}
void Box256GLWidget::updatePixels()
{
numPixelVerts=0;
pixVertBuffer.bind();
QVector<float> verticesVec;
float s = 0.5f / 0xF;
for(BOXBYTE i=0x0;i<=0xF;i++)
{
float x = i*2*s - 0.5f;
for(BOXBYTE j=0x0;j<=0xF;j++)
{
float y = j*2*s - 0.5f;
BOXBYTE color = machine->getPixel(j*0x10 + i);
#define setColor(red,green,blue) r=red/255.0f;g=green/255.0f;b=blue/255.0f
float r=0,g=0,b=0;
switch (color % 0x10) {
case 0x0:{setColor(0,0,0);break;}
case 0x1:{setColor(31,46,80);break;}
case 0x2:{setColor(116,50,83);break;}
case 0x3:{setColor(61,129,84);break;}
case 0x4:{setColor(159,88,65);break;}
case 0x5:{setColor(94,87,80);break;}
case 0x6:{setColor(194,195,199);break;}
case 0x7:{setColor(253,241,233);break;}
case 0x8:{setColor(233,65,91);break;}
case 0x9:{setColor(242,165,67);break;}
case 0xA:{setColor(255,249,93);break;}
case 0xB:{setColor(109,221,100);break;}
case 0xC:{setColor(84,175,247);break;}
case 0xD:{setColor(128,121,153);break;}
case 0xE:{setColor(237,133,170);break;}
case 0xF:{setColor(247,205,176);break;}
}
#undef setColor
#define vx(f) verticesVec.push_back(f+x); numPixelVerts++
#define vy(f) verticesVec.push_back(f-y); numPixelVerts++
#define vc verticesVec.push_back(r);verticesVec.push_back(g);verticesVec.push_back(b);numPixelVerts+=3
vx(-s);vy(-s);vc;vx(-s);vy(s);vc;vx(s);vy(s);vc;
vx(-s);vy(-s);vc;vx(s);vy(-s);vc;vx(s);vy(s);vc;
#undef vc
#undef vx
#undef vy
}
}
pixVertBuffer.allocate(verticesVec.data(),verticesVec.size() * static_cast<int>(sizeof(float)) );
}
void Box256GLWidget::resizeGL(int w, int h)
{
glViewport( 0, 0, w, qMax( h, 1 ) );
}
void Box256GLWidget::drawGrid()
{
gridVertBuffer.bind();
gridShader.bind();
gridShader.setAttributeBuffer("aPos",GL_FLOAT,0,2);
gridShader.enableAttributeArray("aPos");
glDrawArrays(GL_LINES, 0, numGridVerts);
}
void Box256GLWidget::drawPixels()
{
pixVertBuffer.bind();
pixShader.bind();
pixShader.setAttributeBuffer("aPos",GL_FLOAT,0,2,5*sizeof(float));
pixShader.enableAttributeArray("aPos");
pixShader.setAttributeBuffer("aColor",GL_FLOAT,2*sizeof(float),3,5*sizeof(float));
pixShader.enableAttributeArray("aColor");
glDrawArrays(GL_TRIANGLES, 0, numPixelVerts);
}
void Box256GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
updatePixels();
drawPixels();
drawGrid();
}
void Box256GLWidget::timerEvent(QTimerEvent * timer)
{
if (timer->timerId() == qTimer.timerId()){
time++;
update();
}
}