/*
* LDForge: LDraw parts authoring CAD
* Copyright (C) 2013 - 2018 Teemu Piippo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#define GL_GLEXT_PROTOTYPES
#include
#include
#include
#include "gl/compiler.h"
#include "documentmanager.h"
#include "invert.h"
#include "ring.h"
static const char* vertexShaderSource = R"(
#version 330 core
layout(location=0) in vec3 position;
layout(location=1) in vec4 color;
out vec4 vColor;
uniform mat4 CameraTransformation;
void main()
{
vColor = color;
gl_Position = projection * modelview * CameraTransformation * vec4(position, 1.0);
}
)";
static const char* fragmentShaderSource = R"(
#version 330 core
in vec4 vColor;
out vec4 fColor;
void main()
{
fColor = vColor;
}
)";
gl::Compiler::Compiler(const ColorTable& colorTable, QObject* parent) :
QObject{parent},
colorTable{colorTable}
{
}
gl::Compiler::~Compiler()
{
}
void gl::Compiler::initialize()
{
if (not this->initialized)
{
this->initializeOpenGLFunctions();
for (int i = 0; i < gl::NUM_ARRAY_CLASSES; i += 1)
{
auto& object = this->glObjects[i];
object.program = new QOpenGLShaderProgram;
object.program->create();
object.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource);
object.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource);
object.program->link();
object.program->bind();
object.buffer.create();
object.buffer.bind();
object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
object.vertexArray.create();
object.vertexArray.bind();
object.program->enableAttributeArray(0);
object.program->enableAttributeArray(1);
object.program->setAttributeBuffer(0, GL_FLOAT, 0, 3);
object.program->setAttributeBuffer(1, GL_FLOAT, 3, 4);
object.vertexArray.release();
object.buffer.release();
object.program->release();
}
this->initialized = true;
}
}
void gl::Compiler::build(Model* model, DocumentManager* context)
{
this->boundingBox = {};
std::vector vboData[gl::NUM_ARRAY_CLASSES];
const std::vector polygons = model->getPolygons(context);
for (const gl::Polygon& polygon : polygons)
{
this->buildPolygon(polygon, vboData);
}
for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
{
auto& buffer = this->glObjects[arrayId].buffer;
auto& vector = vboData[arrayId];
this->storedVboSizes[arrayId] = vector.size();
buffer.bind();
buffer.allocate(vector.data(), static_cast(vector.size() * sizeof vector[0]));
buffer.release();
}
}
gl::ArrayClass classifyPolygon(const gl::Polygon& polygon)
{
switch (polygon.type)
{
case gl::Polygon::EdgeLine:
return gl::ArrayClass::Lines;
case gl::Polygon::Triangle:
return gl::ArrayClass::Triangles;
case gl::Polygon::Quadrilateral:
return gl::ArrayClass::Quads;
case gl::Polygon::ConditionalEdge:
return gl::ArrayClass::ConditionalLines;
}
return gl::ArrayClass::Lines;
}
[[maybe_unused]]
static QColor colorFromId(linetypes::Id id)
{
// Calculate a color based from this index. This method caters for
// 16777216 objects. I don't think that will be exceeded anytime soon.
const int r = (id.value / 0x10000) % 0x100;
const int g = (id.value / 0x100) % 0x100;
const int b = id.value % 0x100;
return {r, g, b};
}
void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector* vboData)
{
const gl::ArrayClass vboClass = classifyPolygon(polygon);
//std::vector& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})];
std::vector& vertexBuffer = vboData[static_cast(vboClass)];
/*
std::vector& normalsBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::Normals})];
auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices());
reserveMore(normalsBuffer, polygon.numPolygonVertices() * 3_z);
for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
{
const Point3D& v1 = vertexRing[i - 1];
const Point3D& v2 = vertexRing[i];
const Point3D& v3 = vertexRing[i + 1];
const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized();
for (const GLfloat coord : {normal.x(), normal.y(), normal.z()})
normalsBuffer.push_back(coord);
}
*/
reserveMore(vertexBuffer, polygon.numPolygonVertices() * 7);
const QColor color = this->getColorForPolygon(polygon);
// Transform vertices so that they're suitable for GL rendering
for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
{
Point3D& point = polygon.vertices[i];
polygon.vertices[i].y = -polygon.vertices[i].y;
polygon.vertices[i].z = -polygon.vertices[i].z;
this->boundingBox.consider(polygon.vertices[i]);
vertexBuffer.push_back(static_cast(point.x));
vertexBuffer.push_back(static_cast(point.y));
vertexBuffer.push_back(static_cast(point.z));
vertexBuffer.push_back(static_cast(color.redF()));
vertexBuffer.push_back(static_cast(color.greenF()));
vertexBuffer.push_back(static_cast(color.blueF()));
vertexBuffer.push_back(static_cast(color.alphaF()));
}
}
QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon)
{
QColor color;
// For normal colors, use the polygon's color.
if (polygon.color == colors::main)
{
color = {255, 255, 64}; // mainColorRepresentation();
}
else if (polygon.color == colors::edge)
{
// Edge color is black, unless we have a dark background, in which case lines need to be bright.
color = Qt::black; //luma(config::backgroundColor()) > 40 ? Qt::black : Qt::white;
}
else
{
// Not main or edge color, use the polygon's color as is.
color = this->colorTable[polygon.color].faceColor;
}
return color;
}
Point3D gl::Compiler::modelCenter() const
{
return boxCenter(this->boundingBox);
}
double gl::Compiler::modelDistance() const
{
return longestMeasure(this->boundingBox);
}
void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass)
{
auto& object = this->glObjects[static_cast(arrayClass)];
object.vertexArray.bind();
object.program->bind();
}
void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass)
{
auto& object = this->glObjects[static_cast(arrayClass)];
object.program->release();
object.vertexArray.release();
}
std::size_t gl::Compiler::vboSize(const gl::ArrayClass arrayClass) const
{
return this->storedVboSizes[static_cast(arrayClass)];
}
int gl::vboIndex(const VboAddress vboAddress)
{
return static_cast(vboAddress.vboClass) * gl::numVboSubclasses
+ static_cast(vboAddress.vboSubclass);
}