Wed, 25 May 2022 20:36:34 +0300
Fix pick() picking from weird places on the screen with high DPI scaling
glReadPixels reads data from the frame buffer, which contains data after
high DPI scaling, so any reads to that need to take this scaling into account
/* * 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 <http://www.gnu.org/licenses/>. */ #pragma once #include "main.h" #include "gl/common.h" #include "types/boundingbox.h" #include <glm/gtc/type_ptr.hpp> #include <QMap> #include <QSet> #include <QOpenGLVertexArrayObject> #include <QOpenGLBuffer> #include <QOpenGLShaderProgram> #include <QOpenGLExtraFunctions> class Model; class DocumentManager; namespace gl { struct ModelShaders { struct Vertex { glm::vec3 position; glm::vec4 color; glm::vec3 normal; glm::int32 id; glm::int32 selected = 0; }; bool initialized = false; struct ShaderObject { std::unique_ptr<QOpenGLShaderProgram> program = nullptr; QOpenGLShaderProgram* pickSceneProgram = nullptr; QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer}; QOpenGLVertexArrayObject vertexArray; std::vector<Vertex> cachedData; std::size_t vertexCount; } shaderObjects[gl::NUM_POLYGON_TYPES]; }; void build( ModelShaders* shaders, Model *model, const ldraw::ColorTable& colorTable, DocumentManager* context, const RenderPreferences& preferences); void initializeModelShaders(ModelShaders* modelShaders); void bindModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass); void releaseModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass); void setModelShaderSelectedObjects(gl::ModelShaders* shaders, const QSet<ldraw::id_t>& ids); std::size_t vertexCount(const ModelShaders *shaders, gl::ArrayClass arrayClass); ldraw::id_t idFromColor(const std::array<GLubyte, 3>& data); template<typename T> void setShaderUniform(gl::ModelShaders* shaders, const char* uniformName, T&& value) { for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) { shader.program->bind(); const int location = glfunc.glGetUniformLocation(shader.program->programId(), uniformName); Q_ASSERT(location != -1); shader.program->setUniformValue(location, std::forward<T>(value)); shader.program->release(); } } template<typename Float, glm::qualifier Prec> void setShaderUniformMatrix( gl::ModelShaders* shaders, const char* uniformName, const glm::mat<4, 4, Float, Prec>& value) { const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value)); setShaderUniform(shaders, uniformName, *array); } BoundingBox boundingBoxForModel(Model* model, DocumentManager* context); } #define CHECK_GL_ERROR() { checkGLError(__FILE__, __LINE__); } void checkGLError (QString file, int line);