|
|
@@ -79,17 +79,19 @@ struct LightingProg: public GlProgram { |
|
|
|
~LightingProg() { deinit(); } |
|
|
|
|
|
|
|
GLint inverseCamera = uniformLoc("inverseCamera"); |
|
|
|
GLint camera = uniformLoc("camera"); |
|
|
|
GLint lightPos = uniformLoc("lightPos"); |
|
|
|
GLint lightLevel = uniformLoc("lightLevel"); |
|
|
|
GLint opacityTex = uniformLoc("opacityTex"); |
|
|
|
GLint vertex = attribLoc("vertex"); |
|
|
|
|
|
|
|
GLuint vbo; |
|
|
|
|
|
|
|
static constexpr GLfloat vertexes[] = { |
|
|
|
-1.0f, -1.0f, // pos 0: top left |
|
|
|
-1.0f, 1.0f , // pos 1: bottom left |
|
|
|
1.0f, 1.0f, // pos 2: bottom right |
|
|
|
1.0f, 1.0f, // pos 2: bottom right |
|
|
|
-1.0f, 1.0f, // pos 1: bottom left |
|
|
|
1.0f, 1.0f, // pos 2: bottom right |
|
|
|
1.0f, 1.0f, // pos 2: bottom right |
|
|
|
1.0f, -1.0f, // pos 3: top right |
|
|
|
-1.0f, -1.0f, // pos 0: top left |
|
|
|
}; |
|
|
@@ -100,6 +102,8 @@ struct LightingProg: public GlProgram { |
|
|
|
glVertexAttribPointer(vertex, 2, GL_FLOAT, GL_FALSE, 0, (void *)0); |
|
|
|
glEnableVertexAttribArray(vertex); |
|
|
|
glCheck(); |
|
|
|
|
|
|
|
glUniform1i(opacityTex, 0); |
|
|
|
} |
|
|
|
|
|
|
|
void disable() { |
|
|
@@ -357,17 +361,27 @@ struct RendererState { |
|
|
|
GLuint opacityFramebuffer = 0; |
|
|
|
GLuint opacityTex = 0; |
|
|
|
GLuint atlasTex = 0; |
|
|
|
GLuint ditherTex = 0; |
|
|
|
SwanCommon::Vec2 atlasTexSize; |
|
|
|
}; |
|
|
|
|
|
|
|
// Bayer dither pattern |
|
|
|
static const unsigned char ditherPattern[] = { |
|
|
|
0, 32, 8, 40, 2, 34, 10, 42, |
|
|
|
48, 16, 56, 24, 50, 18, 58, 26, |
|
|
|
12, 44, 4, 36, 14, 46, 6, 38, |
|
|
|
60, 28, 52, 20, 62, 30, 54, 22, |
|
|
|
3, 35, 11, 43, 1, 33, 9, 41, |
|
|
|
51, 19, 59, 27, 49, 17, 57, 25, |
|
|
|
15, 47, 7, 39, 13, 45, 5, 37, |
|
|
|
63, 31, 55, 23, 61, 29, 53, 21, |
|
|
|
}; |
|
|
|
|
|
|
|
Renderer::Renderer(): state_(std::make_unique<RendererState>()) { |
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
|
|
glEnable(GL_BLEND); |
|
|
|
glCheck(); |
|
|
|
|
|
|
|
glGenTextures(1, &state_->atlasTex); |
|
|
|
glCheck(); |
|
|
|
|
|
|
|
glGenFramebuffers(1, &state_->lightingFramebuffer); |
|
|
|
glCheck(); |
|
|
|
glGenTextures(1, &state_->lightingTex); |
|
|
@@ -377,6 +391,21 @@ Renderer::Renderer(): state_(std::make_unique<RendererState>()) { |
|
|
|
glCheck(); |
|
|
|
glGenTextures(1, &state_->opacityTex); |
|
|
|
glCheck(); |
|
|
|
|
|
|
|
glGenTextures(1, &state_->atlasTex); |
|
|
|
glCheck(); |
|
|
|
|
|
|
|
glGenTextures(1, &state_->ditherTex); |
|
|
|
glCheck(); |
|
|
|
glBindTexture(GL_TEXTURE_2D, state_->ditherTex); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
|
|
glTexImage2D( |
|
|
|
GL_TEXTURE_2D, 0, GL_LUMINANCE, 8, 8, 0, GL_LUMINANCE, |
|
|
|
GL_UNSIGNED_BYTE, ditherPattern); |
|
|
|
glCheck(); |
|
|
|
} |
|
|
|
|
|
|
|
Renderer::~Renderer() { |
|
|
@@ -425,17 +454,19 @@ void Renderer::draw(const RenderCamera &cam) { |
|
|
|
glTexImage2D( |
|
|
|
GL_TEXTURE_2D, 0, GL_RGBA, cam.size.x, cam.size.y, 0, |
|
|
|
GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
|
|
glCheck(); |
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, state_->opacityTex); |
|
|
|
glCheck(); |
|
|
|
glTexImage2D( |
|
|
|
GL_TEXTURE_2D, 0, GL_RGBA, cam.size.x, cam.size.y, 0, |
|
|
|
GL_TEXTURE_2D, 0, GL_RGBA, cam.size.x / 2, cam.size.y / 8, 0, |
|
|
|
GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|
|
|
glCheck(); |
|
|
|
} |
|
|
|
|
|
|
@@ -483,6 +514,10 @@ void Renderer::draw(const RenderCamera &cam) { |
|
|
|
glClear(GL_COLOR_BUFFER_BIT); |
|
|
|
glCheck(); |
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0); |
|
|
|
glBindTexture(GL_TEXTURE_2D, state_->ditherTex); |
|
|
|
glCheck(); |
|
|
|
|
|
|
|
glBlendFunc(GL_ONE, GL_ONE); |
|
|
|
lightingProg.enable(); |
|
|
|
glUniformMatrix3fv(lightingProg.inverseCamera, 1, GL_TRUE, invCamMat.data()); |