Mon, 27 Jun 2022 15:50:21 +0300
Adjust date string
#include "vertexprogram.h" #include "document.h" static const char vertexShaderSource[] = R"( #version 330 core const int FRAGSTYLE_Normal = 0; const int FRAGSTYLE_Id = 1; layout (location = 0) in vec3 in_position; layout (location = 1) in vec3 in_color; uniform mat4 view; uniform mat4 projection; uniform mat4 model; smooth out vec3 ex_color; void main() { gl_Position = projection * view * model * vec4(in_position, 1.0); ex_color = in_color; } )"; static const char fragmentShaderSource[] = R"( #version 330 core out vec4 color; smooth in vec3 ex_color; void main(void) { color = vec4(ex_color, 1); } )"; std::vector<glm::vec3> sphere(const int d2) { std::vector<glm::vec3> result; result.reserve(12 * d2 * d2); for (int i = 0; i < d2; ++i) { const float alpha = i * pi<> / d2; const float alpha_2 = (i + 1) * pi<> / d2; for (int j = -d2; j < d2; ++j) { const float beta = j * pi<> / d2; const float beta_2 = (j + 1) * pi<> / d2; const float x1 = cos(beta) * sin(alpha); const float x2 = cos(beta) * sin(alpha_2); const float x3 = cos(beta_2) * sin(alpha_2); const float x4 = cos(beta_2) * sin(alpha); const float z1 = sin(beta) * sin(alpha); const float z2 = sin(beta) * sin(alpha_2); const float z3 = sin(beta_2) * sin(alpha_2); const float z4 = sin(beta_2) * sin(alpha); const float y1 = cos(alpha); const float y2 = cos(alpha_2); result.push_back({x1, y1, z1}); result.push_back({x2, y2, z2}); result.push_back({x3, y2, z3}); result.push_back({x1, y1, z1}); result.push_back({x3, y2, z3}); result.push_back({x4, y1, z4}); } } return result; } VertexProgram::VertexProgram(QObject *parent) : AbstractBasicShaderProgram{parent} { } const char *VertexProgram::vertexShaderSource() const { return ::vertexShaderSource; } const char *VertexProgram::fragmentShaderSource() const { return ::fragmentShaderSource; } const void *VertexProgram::vertexData() const { return this->data.data(); } int VertexProgram::vertexSize() const { return sizeof(Vertex); } int VertexProgram::vertexCount() const { return this->data.size(); } void VertexProgram::setupVertexArrays() { for (int i : {0, 1}) { this->program->enableAttributeArray(i); } const int stride = this->vertexSize(); this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride); this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 3, stride); } GLenum VertexProgram::drawMode() const { return GL_TRIANGLES; } QOpenGLBuffer::UsagePattern VertexProgram::usagePattern() const { return QOpenGLBuffer::DynamicDraw; } void VertexProgram::setFragmentStyle(const FragmentStyle newFragmentStyle) { this->fragmentStyle = newFragmentStyle; } void VertexProgram::build(const VertexMap* vertexMap) { constexpr glm::vec3 color = {0.0, 1.0, 1.0}; this->data.clear(); const std::vector<glm::vec3> sphere = ::sphere(8 / 2); vertexMap->apply([&](const glm::vec3&, const VertexMap::VertexInfo& info) { reserveMore(this->data, sphere.size()); for (const glm::vec3& point : sphere) { const glm::vec3 transformed = glm::scale(info.transform, glm::vec3{0.5, 0.5, 0.5}) * glm::vec4{point, 1}; this->data.push_back({transformed, color}); } }); this->buffer.bind(); this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize()); this->buffer.release(); }