-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingpong.cpp
139 lines (109 loc) · 3.9 KB
/
pingpong.cpp
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
#include <cstdlib>
#include <ctime>
#include "pingpong.h"
#include "shader_util.h"
PingPong::PingPong(GLuint width, GLuint height) : mWidth(width), mHeight(height)
{
/* Create our framebuffer */
glGenFramebuffers(1, &mFbo);
glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
createFrontAndBackTextures();
/* Setup shaders */
mShaderProgram = linkShaderProgram("vertex_shader.glsl",
"test_shader.glsl");
srand(static_cast<unsigned int>(time(nullptr)));
}
PingPong::~PingPong()
{
glDeleteTextures(2, mColorAttachments);
}
void PingPong::update(GLuint vao)
{
glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
mFront, 0);
glViewport(0, 0, mWidth, mHeight);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(mShaderProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mBack);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, static_cast<void*>(nullptr));
glBindVertexArray(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Swap textures
GLuint temp = mBack;
mBack = mFront;
mFront = temp;
}
GLuint PingPong::getFront()
{
return mFront;
}
void PingPong::createFrontAndBackTextures()
{
GLuint* textureData = new GLuint[mWidth * mHeight];
for(GLuint row = 0; row < mHeight; row++)
{
for(GLuint col = 0; col < mWidth; col += 2)
{
GLuint state = 0;
GLfloat r = static_cast<float>(rand()) / RAND_MAX;
if(r < 0.33f)
{
state = 0xFF000064;
}
else if(r >= 0.33f && r < 0.66f)
{
state = 0xFF000001;
}
else
{
state = 0xFF000000;
}
if(row % 2)
{
if(col == 0)
{
textureData[mWidth * row] = state;
textureData[mWidth * row + mWidth - 1] = state;
}
else if(col < mWidth - 1)
{
textureData[mWidth * row + col - 1] = state;
textureData[mWidth * row + col] = state;
}
}
else
{
textureData[mWidth * row + col] = state;
textureData[mWidth * row + col + 1] = state;
}
}
}
glGenTextures(1, &mColorAttachments[0]);
glBindTexture(GL_TEXTURE_2D, mColorAttachments[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI, mWidth, mHeight, 0, GL_RGBA_INTEGER,
GL_UNSIGNED_BYTE, textureData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures(1, &mColorAttachments[1]);
glBindTexture(GL_TEXTURE_2D, mColorAttachments[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI, mWidth, mHeight, 0, GL_RGBA_INTEGER,
GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
delete[] textureData;
/* Set front and back */
mBack = mColorAttachments[0];
mFront = mColorAttachments[1];
}