| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "solidpipeline.h" |
| 7 |
|
| 8 |
#include "avogadrogl.h" |
| 9 |
#include "camera.h" |
| 10 |
#include "shader.h" |
| 11 |
#include "shaderprogram.h" |
| 12 |
|
| 13 |
#include "solid_vs.h" |
| 14 |
|
| 15 |
#include "solid_first_fs.h" |
| 16 |
|
| 17 |
#include <iostream> |
| 18 |
|
| 19 |
#include <cmath> |
| 20 |
|
| 21 |
namespace Avogadro::Rendering { |
| 22 |
|
| 23 |
class SolidPipeline::Private |
| 24 |
{ |
| 25 |
public: |
| 26 |
Private() {} |
| 27 |
|
| 28 |
void attachStage(ShaderProgram& prog, const GLchar* nameRGB, GLuint texRGB, |
| 29 |
const GLchar* nameDepth, GLuint texDepth, int w, int h) |
| 30 |
{ |
| 31 |
prog.bind(); |
| 32 |
GLuint programID; |
| 33 |
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&programID); |
| 34 |
|
| 35 |
GLuint attrRGB = glGetUniformLocation(programID, nameRGB); |
| 36 |
glActiveTexture(GL_TEXTURE0 + 1); |
| 37 |
glBindTexture(GL_TEXTURE_2D, texRGB); |
| 38 |
glUniform1i(attrRGB, 1); |
| 39 |
|
| 40 |
GLuint attrDepth = glGetUniformLocation(programID, nameDepth); |
| 41 |
glActiveTexture(GL_TEXTURE0 + 2); |
| 42 |
glBindTexture(GL_TEXTURE_2D, texDepth); |
| 43 |
glUniform1i(attrDepth, 2); |
| 44 |
|
| 45 |
prog.setUniformValue("width", float(w)); |
| 46 |
prog.setUniformValue("height", float(h)); |
| 47 |
} |
| 48 |
|
| 49 |
GLuint defaultFBO; |
| 50 |
GLuint renderFBO; |
| 51 |
GLuint renderTexture; |
| 52 |
GLuint depthTexture; |
| 53 |
GLuint screenVAO; |
| 54 |
GLuint screenVBO; |
| 55 |
ShaderProgram firstStageShaders; |
| 56 |
Shader screenVertexShader; |
| 57 |
Shader firstFragmentShader; |
| 58 |
}; |
| 59 |
|
| 60 |
static const GLfloat s_fullscreenQuad[] = { |
| 61 |
-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, |
| 62 |
-1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, |
| 63 |
}; |
| 64 |
|
| 65 |
void initializeFramebuffer(GLuint* outFBO, GLuint* texRGB, GLuint* texDepth) |
| 66 |
{ |
| 67 |
glGenFramebuffers(1, outFBO); |
| 68 |
glBindFramebuffer(GL_FRAMEBUFFER, *outFBO); |
| 69 |
|
| 70 |
glGenTextures(1, texRGB); |
| 71 |
glBindTexture(GL_TEXTURE_2D, *texRGB); |
| 72 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 73 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 74 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 75 |
*texRGB, 0); |
| 76 |
|
| 77 |
glGenTextures(1, texDepth); |
| 78 |
glBindTexture(GL_TEXTURE_2D, *texDepth); |
| 79 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 80 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 81 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 82 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 83 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, |
| 84 |
*texDepth, 0); |
| 85 |
} |
| 86 |
|
| 87 |
SolidPipeline::SolidPipeline() |
| 88 |
: m_pixelRatio(1.0f), m_aoEnabled(false), m_dofStrength(1.0f), |
| 89 |
m_dofPosition(1.0), m_dofEnabled(false), m_fogPosition(1.0), |
| 90 |
m_backgroundColor(0, 0, 0, 0), m_fogEnabled(true), m_aoStrength(1.0f), |
| 91 |
m_fogStrength(1.0f), m_edEnabled(false), m_edStrength(1.0f), m_width(0), |
| 92 |
m_height(0), d(new Private) |
| 93 |
{ |
| 94 |
} |
| 95 |
|
| 96 |
SolidPipeline::~SolidPipeline() |
| 97 |
{ |
| 98 |
delete d; |
| 99 |
} |
| 100 |
|
| 101 |
void SolidPipeline::initialize() |
| 102 |
{ |
| 103 |
initializeFramebuffer(&d->renderFBO, &d->renderTexture, &d->depthTexture); |
| 104 |
|
| 105 |
// Create VAO for fullscreen quad (required for OpenGL Core Profile) |
| 106 |
glGenVertexArrays(1, &d->screenVAO); |
| 107 |
glBindVertexArray(d->screenVAO); |
| 108 |
|
| 109 |
glGenBuffers(1, &d->screenVBO); |
| 110 |
glBindBuffer(GL_ARRAY_BUFFER, d->screenVBO); |
| 111 |
glBufferData(GL_ARRAY_BUFFER, sizeof(s_fullscreenQuad), s_fullscreenQuad, |
| 112 |
GL_STATIC_DRAW); |
| 113 |
|
| 114 |
// Set up vertex attribute for the fullscreen quad |
| 115 |
glEnableVertexAttribArray(0); |
| 116 |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); |
| 117 |
|
| 118 |
glBindVertexArray(0); |
| 119 |
|
| 120 |
d->screenVertexShader.setType(Shader::Vertex); |
| 121 |
d->screenVertexShader.setSource(solid_vs); |
| 122 |
if (!d->screenVertexShader.compile()) |
| 123 |
std::cout << d->screenVertexShader.error() << std::endl; |
| 124 |
|
| 125 |
d->firstFragmentShader.setType(Shader::Fragment); |
| 126 |
d->firstFragmentShader.setSource(solid_first_fs); |
| 127 |
if (!d->firstFragmentShader.compile()) |
| 128 |
std::cout << d->firstFragmentShader.error() << std::endl; |
| 129 |
|
| 130 |
d->firstStageShaders.attachShader(d->screenVertexShader); |
| 131 |
d->firstStageShaders.attachShader(d->firstFragmentShader); |
| 132 |
if (!d->firstStageShaders.link()) |
| 133 |
std::cout << d->firstStageShaders.error() << std::endl; |
| 134 |
|
| 135 |
// here is the end of the code that needs to be compared |
| 136 |
} |
| 137 |
|
| 138 |
void SolidPipeline::begin() |
| 139 |
{ |
| 140 |
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&d->defaultFBO); |
| 141 |
glBindFramebuffer(GL_FRAMEBUFFER, d->renderFBO); |
| 142 |
GLenum drawBuffersList[1] = { GL_COLOR_ATTACHMENT0 }; |
| 143 |
glDrawBuffers(1, drawBuffersList); |
| 144 |
|
| 145 |
GLfloat tmp[5]; |
| 146 |
glGetFloatv(GL_COLOR_CLEAR_VALUE, tmp); |
| 147 |
glGetFloatv(GL_DEPTH_CLEAR_VALUE, tmp + 4); |
| 148 |
glClearColor(0.0, 0.0, 0.0, 0.0); |
| 149 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 150 |
glClearColor(tmp[0], tmp[1], tmp[2], tmp[3]); |
| 151 |
glClearDepth(tmp[4]); |
| 152 |
} |
| 153 |
|
| 154 |
void SolidPipeline::end() |
| 155 |
{ |
| 156 |
// Bind VAO for fullscreen quad (required for OpenGL Core Profile) |
| 157 |
glBindVertexArray(d->screenVAO); |
| 158 |
|
| 159 |
// Draw to screen |
| 160 |
if (glIsFramebuffer(d->defaultFBO)) { |
| 161 |
glBindFramebuffer(GL_FRAMEBUFFER, d->defaultFBO); |
| 162 |
GLenum drawBuffersList[1] = { GL_COLOR_ATTACHMENT0 }; |
| 163 |
glDrawBuffers(1, drawBuffersList); |
| 164 |
} else { |
| 165 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 166 |
glDrawBuffer(GL_BACK); |
| 167 |
} |
| 168 |
d->attachStage(d->firstStageShaders, "inRGBTex", d->renderTexture, |
| 169 |
"inDepthTex", d->depthTexture, m_width, m_height); |
| 170 |
d->firstStageShaders.setUniformValue("inAoEnabled", |
| 171 |
m_aoEnabled ? 1.0f : 0.0f); |
| 172 |
d->firstStageShaders.setUniformValue("inDofEnabled", |
| 173 |
m_dofEnabled ? 1.0f : 0.0f); |
| 174 |
d->firstStageShaders.setUniformValue( |
| 175 |
"inDofStrength", m_dofEnabled ? (m_dofStrength * 100.0f) : 0.0f); |
| 176 |
d->firstStageShaders.setUniformValue("inDofPosition", |
| 177 |
((m_dofPosition) / 10.0f)); |
| 178 |
d->firstStageShaders.setUniformValue("inAoStrength", m_aoStrength); |
| 179 |
d->firstStageShaders.setUniformValue("inEdStrength", m_edStrength); |
| 180 |
d->firstStageShaders.setUniformValue("inFogEnabled", |
| 181 |
m_fogEnabled ? 1.0f : 0.0f); |
| 182 |
d->firstStageShaders.setUniformValue("inFogStrength", |
| 183 |
m_fogEnabled ? m_fogStrength : 0.0f); |
| 184 |
d->firstStageShaders.setUniformValue("inFogPosition", m_fogPosition); |
| 185 |
d->firstStageShaders.setUniformValue("fogR", (m_backgroundColor[0]) / 255.0f); |
| 186 |
d->firstStageShaders.setUniformValue("fogG", (m_backgroundColor[1]) / 255.0f); |
| 187 |
d->firstStageShaders.setUniformValue("fogB", (m_backgroundColor[2]) / 255.0f); |
| 188 |
glDrawArrays(GL_TRIANGLES, 0, 6); |
| 189 |
glBindVertexArray(0); |
| 190 |
} |
| 191 |
|
| 192 |
void SolidPipeline::adjustOffset(const Camera& cam) |
| 193 |
{ |
| 194 |
|
| 195 |
// The numbers used in calculations are random. |
| 196 |
// They help define an offset with the projection-matrix |
| 197 |
// to make the fog dynamic as the molecule moves away |
| 198 |
// from the camera or come closer. |
| 199 |
Eigen::Matrix4f projectView = cam.projection().matrix(); |
| 200 |
|
| 201 |
float project = ((((5000 + projectView(2, 3) * 1000) / 6) + 55) * 100); |
| 202 |
|
| 203 |
float offSet = 0.000102337 * pow(project, 2) - 3.84689 * project + 36182.2; |
| 204 |
if (project >= 21018.106 && project < 21595.588) { |
| 205 |
offSet = 2.63129 * project - 54768.4; |
| 206 |
} else if (project >= 21595.588) { |
| 207 |
offSet = 9.952 * project - 212865; |
| 208 |
} |
| 209 |
d->firstStageShaders.setUniformValue("uoffset", offSet); |
| 210 |
} |
| 211 |
|
| 212 |
void SolidPipeline::resize(int width, int height) |
| 213 |
{ |
| 214 |
m_width = width * m_pixelRatio; |
| 215 |
m_height = height * m_pixelRatio; |
| 216 |
|
| 217 |
glBindTexture(GL_TEXTURE_2D, d->renderTexture); |
| 218 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_width, m_height, 0, GL_RGBA, |
| 219 |
GL_UNSIGNED_BYTE, nullptr); |
| 220 |
|
| 221 |
glBindTexture(GL_TEXTURE_2D, d->depthTexture); |
| 222 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_width, m_height, 0, |
| 223 |
GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, nullptr); |
| 224 |
} |
| 225 |
|
| 226 |
void SolidPipeline::setPixelRatio(float ratio) |
| 227 |
{ |
| 228 |
m_pixelRatio = ratio; |
| 229 |
} |
| 230 |
|
| 231 |
} // End namespace Avogadro::Rendering |