mirror of
https://github.com/lucaspalomodevelop/meshlab.git
synced 2026-03-15 00:54:38 +00:00
commit
add44e8a76
@ -1130,6 +1130,8 @@ void MainWindow::updateSharedContextDataAfterFilterExecution(int postcondmask,in
|
||||
delete fauxaction;
|
||||
/****************************************************************************************************/
|
||||
}
|
||||
if (!mm->cm.textures.empty())
|
||||
updateTexture(mm->id());
|
||||
foreach(GLArea* gla,mvc->viewerList)
|
||||
{
|
||||
if (gla != NULL)
|
||||
|
||||
@ -349,6 +349,11 @@ std::map<std::string, QVariant> FilterLayerPlugin::applyFilter(
|
||||
destModel->updateDataMask(currentModel);
|
||||
tri::Append<CMeshO, CMeshO>::Mesh(destModel->cm, currentModel->cm);
|
||||
|
||||
for (std::string& tex: destModel->cm.textures) {
|
||||
QString fullPath = currentModel->pathName() + QDir::separator() + QString(tex.c_str());
|
||||
tex = fullPath.toStdString();
|
||||
}
|
||||
|
||||
log("Duplicated current model to layer %i", md.meshList.size());
|
||||
|
||||
// init new layer
|
||||
|
||||
@ -29,27 +29,9 @@
|
||||
#include <QImage>
|
||||
#include <QFileInfo>
|
||||
|
||||
#include <QOpenGLContext>
|
||||
|
||||
OpenGLFunctionsHandle GetOpenGLFunctionsHandle()
|
||||
{
|
||||
QOpenGLContext *currentContext = QOpenGLContext::currentContext();
|
||||
if (!currentContext) {
|
||||
LOG_ERR << "OpenGL context not initialized";
|
||||
std::exit(-1);
|
||||
}
|
||||
OpenGLFunctionsHandle glFuncs = currentContext->versionFunctions<OpenGLFunctionsVersion>();
|
||||
if (!glFuncs) {
|
||||
LOG_ERR << "Could not obtain required OpenGL context version";
|
||||
exit(-1);
|
||||
}
|
||||
return glFuncs;
|
||||
}
|
||||
|
||||
void CheckGLError()
|
||||
{
|
||||
OpenGLFunctionsHandle glFuncs = GetOpenGLFunctionsHandle();
|
||||
GLenum error = glFuncs->glGetError();
|
||||
GLenum error = glGetError();
|
||||
if (error != GL_NO_ERROR)
|
||||
{
|
||||
std::stringstream ss;
|
||||
@ -79,53 +61,51 @@ std::string ReadShader(const char *path)
|
||||
|
||||
uint32_t CompileShaders(const char **vs_text, const char **fs_text)
|
||||
{
|
||||
OpenGLFunctionsHandle glFuncs = GetOpenGLFunctionsHandle();
|
||||
|
||||
GLint status;
|
||||
char infoLog[1024] = {0};
|
||||
|
||||
GLuint vs = glFuncs->glCreateShader(GL_VERTEX_SHADER);
|
||||
glFuncs->glShaderSource(vs, 1, vs_text, NULL);
|
||||
glFuncs->glCompileShader(vs);
|
||||
glFuncs->glGetShaderInfoLog(vs, 1024, NULL, infoLog);
|
||||
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vs, 1, vs_text, NULL);
|
||||
glCompileShader(vs);
|
||||
glGetShaderInfoLog(vs, 1024, NULL, infoLog);
|
||||
if (*infoLog) {
|
||||
LOG_DEBUG << infoLog;
|
||||
memset(infoLog, 0, 1024);
|
||||
}
|
||||
glFuncs->glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
|
||||
glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
|
||||
if (status == GL_FALSE) {
|
||||
LOG_ERR << "Vertex shader compilation failed";
|
||||
}
|
||||
|
||||
GLuint fs = glFuncs->glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glFuncs->glShaderSource(fs, 1, fs_text, NULL);
|
||||
glFuncs->glCompileShader(fs);
|
||||
glFuncs->glGetShaderInfoLog(fs, 1024, NULL, infoLog);
|
||||
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fs, 1, fs_text, NULL);
|
||||
glCompileShader(fs);
|
||||
glGetShaderInfoLog(fs, 1024, NULL, infoLog);
|
||||
if (*infoLog) {
|
||||
LOG_DEBUG << infoLog;
|
||||
memset(infoLog, 0, 1024);
|
||||
}
|
||||
glFuncs->glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
|
||||
glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
|
||||
if (status == GL_FALSE) {
|
||||
LOG_ERR << "Fragment shader compilation failed";
|
||||
}
|
||||
|
||||
GLuint program = glFuncs->glCreateProgram();
|
||||
glFuncs->glAttachShader(program, vs);
|
||||
glFuncs->glAttachShader(program, fs);
|
||||
glFuncs->glLinkProgram(program);
|
||||
glFuncs->glValidateProgram(program);
|
||||
glFuncs->glGetProgramInfoLog(program, 1024, NULL, infoLog);
|
||||
GLuint program = glCreateProgram();
|
||||
glAttachShader(program, vs);
|
||||
glAttachShader(program, fs);
|
||||
glLinkProgram(program);
|
||||
glValidateProgram(program);
|
||||
glGetProgramInfoLog(program, 1024, NULL, infoLog);
|
||||
if (*infoLog) {
|
||||
LOG_DEBUG << infoLog;
|
||||
}
|
||||
glFuncs->glGetProgramiv(program, GL_LINK_STATUS, &status);
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &status);
|
||||
if (status == GL_FALSE) {
|
||||
LOG_ERR << "Shader program link failed";
|
||||
}
|
||||
|
||||
glFuncs->glDeleteShader(vs);
|
||||
glFuncs->glDeleteShader(fs);
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
|
||||
CheckGLError();
|
||||
|
||||
|
||||
@ -25,13 +25,9 @@
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include <QOpenGLFunctions_4_1_Core>
|
||||
|
||||
using OpenGLFunctionsVersion = QOpenGLFunctions_4_1_Core;
|
||||
using OpenGLFunctionsHandle = OpenGLFunctionsVersion*;
|
||||
|
||||
OpenGLFunctionsHandle GetOpenGLFunctionsHandle();
|
||||
#include <GL/glew.h>
|
||||
|
||||
|
||||
/* Prints the last OpenGL error code */
|
||||
|
||||
@ -55,7 +55,6 @@ bool TextureObject::AddImage(std::string path)
|
||||
|
||||
void TextureObject::Bind(int i)
|
||||
{
|
||||
OpenGLFunctionsHandle glFuncs = GetOpenGLFunctionsHandle();
|
||||
ensure(i >= 0 && i < (int) texInfoVec.size());
|
||||
// load texture from qimage on first use
|
||||
if (texNameVec[i] == 0) {
|
||||
@ -65,25 +64,25 @@ void TextureObject::Bind(int i)
|
||||
QImage glimg = img.convertToFormat(QImage::Format_ARGB32);
|
||||
img = glimg;
|
||||
}
|
||||
glFuncs->glGenTextures(1, &texNameVec[i]);
|
||||
glGenTextures(1, &texNameVec[i]);
|
||||
|
||||
Mirror(img);
|
||||
glFuncs->glBindTexture(GL_TEXTURE_2D, texNameVec[i]);
|
||||
glBindTexture(GL_TEXTURE_2D, texNameVec[i]);
|
||||
int miplevels = std::log2((float) img.width());
|
||||
int width = img.width();
|
||||
int height = img.height();
|
||||
for (int m = 0; m < miplevels; m++) {
|
||||
glFuncs->glTexImage2D(GL_TEXTURE_2D, m, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
|
||||
glTexImage2D(GL_TEXTURE_2D, m, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
|
||||
width = std::max(1, (width / 2));
|
||||
height = std::max(1, (height / 2));
|
||||
}
|
||||
//glFuncs->glTexStorage2D(GL_TEXTURE_2D, miplevels, GL_RGBA8, img.width(), img.height());
|
||||
glFuncs->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, img.width(), img.height(), GL_BGRA, GL_UNSIGNED_BYTE, img.constBits());
|
||||
glFuncs->glGenerateMipmap(GL_TEXTURE_2D);
|
||||
//glTexStorage2D(GL_TEXTURE_2D, miplevels, GL_RGBA8, img.width(), img.height());
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, img.width(), img.height(), GL_BGRA, GL_UNSIGNED_BYTE, img.constBits());
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
CheckGLError();
|
||||
}
|
||||
else {
|
||||
glFuncs->glBindTexture(GL_TEXTURE_2D, texNameVec[i]);
|
||||
glBindTexture(GL_TEXTURE_2D, texNameVec[i]);
|
||||
CheckGLError();
|
||||
}
|
||||
}
|
||||
@ -92,8 +91,7 @@ void TextureObject::Release(int i)
|
||||
{
|
||||
ensure(i >= 0 && i < (int) texInfoVec.size());
|
||||
if (texNameVec[i]) {
|
||||
OpenGLFunctionsHandle glFuncs = GetOpenGLFunctionsHandle();
|
||||
glFuncs->glDeleteTextures(1, &texNameVec[i]);
|
||||
glDeleteTextures(1, &texNameVec[i]);
|
||||
texNameVec[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,20 +37,14 @@
|
||||
|
||||
#include <QImage>
|
||||
|
||||
#include <QOpenGLContext>
|
||||
#include <QSurfaceFormat>
|
||||
#include <QOffscreenSurface>
|
||||
|
||||
|
||||
|
||||
static const char *vs_text[] = {
|
||||
"#version 410 core \n"
|
||||
" \n"
|
||||
"in vec2 position; \n"
|
||||
"in vec2 texcoord; \n"
|
||||
"in vec4 color; \n"
|
||||
"out vec2 uv; \n"
|
||||
"out vec4 fcolor; \n"
|
||||
"attribute vec2 position; \n"
|
||||
"attribute vec2 texcoord; \n"
|
||||
"attribute vec4 color; \n"
|
||||
"varying vec2 uv; \n"
|
||||
"varying vec4 fcolor; \n"
|
||||
" \n"
|
||||
"void main(void) \n"
|
||||
"{ \n"
|
||||
@ -63,25 +57,21 @@ static const char *vs_text[] = {
|
||||
};
|
||||
|
||||
static const char *fs_text[] = {
|
||||
"#version 410 core \n"
|
||||
" \n"
|
||||
"uniform sampler2D img0; \n"
|
||||
" \n"
|
||||
"uniform vec2 texture_size; \n"
|
||||
"uniform int render_mode; \n"
|
||||
" \n"
|
||||
"in vec2 uv; \n"
|
||||
"in vec4 fcolor; \n"
|
||||
" \n"
|
||||
"out vec4 texelColor; \n"
|
||||
"varying vec2 uv; \n"
|
||||
"varying vec4 fcolor; \n"
|
||||
" \n"
|
||||
"void main(void) \n"
|
||||
"{ \n"
|
||||
" if (render_mode == 0) { \n"
|
||||
" if (uv.s < 0) \n"
|
||||
" texelColor = vec4(0, 1, 0, 1); \n"
|
||||
" if (uv.s < float(0)) \n"
|
||||
" gl_FragColor = vec4(0, 1, 0, 1); \n"
|
||||
" else \n"
|
||||
" texelColor = vec4(texture2D(img0, uv).rgb, 1); \n"
|
||||
" gl_FragColor = vec4(texture2D(img0, uv).rgb, 1); \n"
|
||||
" } else if (render_mode == 1) { \n"
|
||||
" vec2 coord = uv * texture_size - vec2(0.5, 0.5); \n"
|
||||
" vec2 idx = floor(coord); \n"
|
||||
@ -103,9 +93,9 @@ static const char *fs_text[] = {
|
||||
" vec4 tex11 = texture2D(img0, vec2(h1.x, h1.y) / texture_size);\n"
|
||||
" tex00 = mix(tex00, tex01, g1.y); \n"
|
||||
" tex10 = mix(tex10, tex11, g1.y); \n"
|
||||
" texelColor = mix(tex00, tex10, g1.x); \n"
|
||||
" gl_FragColor = mix(tex00, tex10, g1.x); \n"
|
||||
" } else { \n"
|
||||
" texelColor = fcolor; \n"
|
||||
" gl_FragColor = fcolor; \n"
|
||||
" } \n"
|
||||
"} \n"
|
||||
};
|
||||
@ -170,50 +160,21 @@ static std::shared_ptr<QImage> RenderTexture(std::vector<Mesh::FacePointer>& fve
|
||||
|
||||
std::sort(fvec.begin(), fvec.end(), FaceComparatorByInputTexIndex);
|
||||
|
||||
bool contextAvailable = (QOpenGLContext::currentContext() != nullptr);
|
||||
|
||||
QOpenGLContext context;
|
||||
QOffscreenSurface surface;
|
||||
|
||||
if (!contextAvailable) {
|
||||
LOG_DEBUG << "Creating context";
|
||||
QSurfaceFormat format;
|
||||
format.setVersion(4, 1);
|
||||
format.setProfile(QSurfaceFormat::OpenGLContextProfile::CoreProfile);
|
||||
|
||||
context.setFormat(format);
|
||||
|
||||
if (!context.create()) {
|
||||
LOG_ERR << "Failed to create opengl context";
|
||||
std::exit(-1);
|
||||
}
|
||||
|
||||
surface.setFormat(context.format());
|
||||
surface.create();
|
||||
|
||||
if (!context.makeCurrent(&surface)) {
|
||||
LOG_ERR << "Failed to make OpenGL context current";
|
||||
std::exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
OpenGLFunctionsHandle glFuncs = GetOpenGLFunctionsHandle();
|
||||
|
||||
// OpenGL setup
|
||||
|
||||
GLuint vao;
|
||||
glFuncs->glGenVertexArrays(1, &vao);
|
||||
glFuncs->glBindVertexArray(vao);
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
GLint program = CompileShaders(vs_text, fs_text);
|
||||
glFuncs->glUseProgram(program);
|
||||
glUseProgram(program);
|
||||
|
||||
CheckGLError();
|
||||
|
||||
// Allocate vertex data
|
||||
|
||||
GLuint vertexbuf;
|
||||
glFuncs->glGenBuffers(1, &vertexbuf);
|
||||
glGenBuffers(1, &vertexbuf);
|
||||
|
||||
std::vector<TextureSize> inTexSizes;
|
||||
for (std::size_t i = 0; i < textureObject->ArraySize(); ++i) {
|
||||
@ -222,9 +183,9 @@ static std::shared_ptr<QImage> RenderTexture(std::vector<Mesh::FacePointer>& fve
|
||||
inTexSizes.push_back({iw, ih});
|
||||
}
|
||||
|
||||
glFuncs->glBindBuffer(GL_ARRAY_BUFFER, vertexbuf);
|
||||
glFuncs->glBufferData(GL_ARRAY_BUFFER, m.FN()*15*sizeof(float), NULL, GL_STATIC_DRAW);
|
||||
float *p = (float *) glFuncs->glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuf);
|
||||
glBufferData(GL_ARRAY_BUFFER, m.FN()*15*sizeof(float), NULL, GL_STATIC_DRAW);
|
||||
float *p = (float *) glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
||||
for (auto fptr : fvec) {
|
||||
int ti = WTCSh[fptr].tc[0].N();
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
@ -242,60 +203,60 @@ static std::shared_ptr<QImage> RenderTexture(std::vector<Mesh::FacePointer>& fve
|
||||
|
||||
}
|
||||
}
|
||||
glFuncs->glUnmapBuffer(GL_ARRAY_BUFFER);
|
||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
||||
|
||||
GLint pos_location = glFuncs->glGetAttribLocation(program, "position");
|
||||
glFuncs->glVertexAttribPointer(pos_location, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), 0);
|
||||
glFuncs->glEnableVertexAttribArray(pos_location);
|
||||
GLint pos_location = glGetAttribLocation(program, "position");
|
||||
glVertexAttribPointer(pos_location, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), 0);
|
||||
glEnableVertexAttribArray(pos_location);
|
||||
|
||||
GLint tc_location = glFuncs->glGetAttribLocation(program, "texcoord");
|
||||
glFuncs->glVertexAttribPointer(tc_location, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void *) (2*sizeof(float)));
|
||||
glFuncs->glEnableVertexAttribArray(tc_location);
|
||||
GLint tc_location = glGetAttribLocation(program, "texcoord");
|
||||
glVertexAttribPointer(tc_location, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void *) (2*sizeof(float)));
|
||||
glEnableVertexAttribArray(tc_location);
|
||||
|
||||
GLint color_location = glFuncs->glGetAttribLocation(program, "color");
|
||||
glFuncs->glVertexAttribPointer(color_location, 4, GL_UNSIGNED_BYTE, GL_TRUE, 5*sizeof(float), (void *) (4*sizeof(float)));
|
||||
glFuncs->glEnableVertexAttribArray(color_location);
|
||||
GLint color_location = glGetAttribLocation(program, "color");
|
||||
glVertexAttribPointer(color_location, 4, GL_UNSIGNED_BYTE, GL_TRUE, 5*sizeof(float), (void *) (4*sizeof(float)));
|
||||
glEnableVertexAttribArray(color_location);
|
||||
|
||||
p = nullptr;
|
||||
glFuncs->glBindBuffer(GL_ARRAY_BUFFER, 0); // done, unbind
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0); // done, unbind
|
||||
|
||||
int renderedTexWidth = textureWidth;
|
||||
int renderedTexHeight = textureHeight;
|
||||
|
||||
GLuint fbo;
|
||||
glFuncs->glGenFramebuffers(1, &fbo);
|
||||
glFuncs->glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
GLint drawBuffer;
|
||||
glGetIntegerv(GL_DRAW_BUFFER, &drawBuffer);
|
||||
|
||||
glFuncs->glViewport(0, 0, renderedTexWidth, renderedTexHeight);
|
||||
GLuint fbo;
|
||||
glGenFramebuffers(1, &fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
|
||||
glViewport(0, 0, renderedTexWidth, renderedTexHeight);
|
||||
|
||||
GLuint renderTarget;
|
||||
glFuncs->glGenTextures(1, &renderTarget);
|
||||
glFuncs->glBindTexture(GL_TEXTURE_2D, renderTarget);
|
||||
glFuncs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, renderedTexWidth, renderedTexHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
|
||||
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glFuncs->glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderTarget, 0);
|
||||
glFuncs->glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glGenTextures(1, &renderTarget);
|
||||
glBindTexture(GL_TEXTURE_2D, renderTarget);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, renderedTexWidth, renderedTexHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderTarget, 0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
if (glFuncs->glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||
LOG_ERR << "Framebuffer is not complete " << glFuncs->glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||
LOG_ERR << "Framebuffer is not complete " << glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||
std::exit(-1);
|
||||
}
|
||||
|
||||
std::shared_ptr<QImage> textureImage = std::make_shared<QImage>(renderedTexWidth, renderedTexHeight, QImage::Format_ARGB32);
|
||||
|
||||
// disable depth and stencil test (if they were enabled) as the render target does not have the buffers attached
|
||||
glFuncs->glDisable(GL_DEPTH_TEST);
|
||||
glFuncs->glDisable(GL_STENCIL_TEST);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_STENCIL_TEST);
|
||||
|
||||
GLint drawBuffer;
|
||||
glFuncs->glGetIntegerv(GL_DRAW_BUFFER, &drawBuffer);
|
||||
glFuncs->glDrawBuffer(GL_COLOR_ATTACHMENT0);
|
||||
glDrawBuffer(GL_COLOR_ATTACHMENT0);
|
||||
|
||||
glFuncs->glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
||||
glFuncs->glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
|
||||
glClearColor(0.0f, 1.0f, 0.0f, 128 / float(255));
|
||||
|
||||
glFuncs->glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
auto f0 = fvec.begin();
|
||||
auto fbase = f0;
|
||||
@ -308,44 +269,44 @@ static std::shared_ptr<QImage> RenderTexture(std::vector<Mesh::FacePointer>& fve
|
||||
int count = std::distance(fbase, fcurr) * 3;
|
||||
|
||||
// Load texture image
|
||||
glFuncs->glActiveTexture(GL_TEXTURE0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
LOG_DEBUG << "Binding texture unit " << currTexIndex;
|
||||
textureObject->Bind(currTexIndex);
|
||||
|
||||
GLint loc_img0 = glFuncs->glGetUniformLocation(program, "img0");
|
||||
glFuncs->glUniform1i(loc_img0, 0);
|
||||
GLint loc_texture_size = glFuncs->glGetUniformLocation(program, "texture_size");
|
||||
glFuncs->glUniform2f(loc_texture_size, float(textureObject->TextureWidth(currTexIndex)), float(textureObject->TextureHeight(currTexIndex)));
|
||||
GLint loc_img0 = glGetUniformLocation(program, "img0");
|
||||
glUniform1i(loc_img0, 0);
|
||||
GLint loc_texture_size = glGetUniformLocation(program, "texture_size");
|
||||
glUniform2f(loc_texture_size, float(textureObject->TextureWidth(currTexIndex)), float(textureObject->TextureHeight(currTexIndex)));
|
||||
|
||||
|
||||
GLint loc_render_mode = glFuncs->glGetUniformLocation(program, "render_mode");
|
||||
glFuncs->glUniform1i(loc_render_mode, 0);
|
||||
GLint loc_render_mode = glGetUniformLocation(program, "render_mode");
|
||||
glUniform1i(loc_render_mode, 0);
|
||||
switch (imode) {
|
||||
case Cubic:
|
||||
glFuncs->glUniform1i(loc_render_mode, 1);
|
||||
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glFuncs->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
|
||||
glUniform1i(loc_render_mode, 1);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
|
||||
break;
|
||||
case Linear:
|
||||
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glFuncs->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
|
||||
//glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
//glFuncs->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
|
||||
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
|
||||
break;
|
||||
case Nearest:
|
||||
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glFuncs->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_NEAREST);
|
||||
break;
|
||||
case FaceColor:
|
||||
glFuncs->glUniform1i(loc_render_mode, 2);
|
||||
glUniform1i(loc_render_mode, 2);
|
||||
break;
|
||||
default:
|
||||
ensure(0 && "Should never happen");
|
||||
}
|
||||
|
||||
glFuncs->glDrawArrays(GL_TRIANGLES, baseIndex, count);
|
||||
glDrawArrays(GL_TRIANGLES, baseIndex, count);
|
||||
CheckGLError();
|
||||
|
||||
textureObject->Release(currTexIndex);
|
||||
@ -353,25 +314,24 @@ static std::shared_ptr<QImage> RenderTexture(std::vector<Mesh::FacePointer>& fve
|
||||
fbase = fcurr;
|
||||
}
|
||||
|
||||
glFuncs->glReadBuffer(GL_COLOR_ATTACHMENT0);
|
||||
glFuncs->glReadPixels(0, 0, renderedTexWidth, renderedTexHeight, GL_BGRA, GL_UNSIGNED_BYTE, textureImage->bits());
|
||||
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
||||
glReadPixels(0, 0, renderedTexWidth, renderedTexHeight, GL_BGRA, GL_UNSIGNED_BYTE, textureImage->bits());
|
||||
|
||||
// clean up
|
||||
glFuncs->glUseProgram(0);
|
||||
glFuncs->glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glFuncs->glBindVertexArray(0);
|
||||
glUseProgram(0);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glFuncs->glDeleteTextures(1, &renderTarget);
|
||||
glFuncs->glDeleteFramebuffers(1, &fbo);
|
||||
glFuncs->glDeleteBuffers(1, &vertexbuf);
|
||||
glFuncs->glDeleteProgram(program);
|
||||
glFuncs->glDeleteVertexArrays(1, &vao);
|
||||
glDeleteTextures(1, &renderTarget);
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
glDeleteBuffers(1, &vertexbuf);
|
||||
glDeleteProgram(program);
|
||||
glDeleteVertexArrays(1, &vao);
|
||||
|
||||
//if (contextAvailable)
|
||||
// glFuncs->glDrawBuffer(drawBuffer);
|
||||
glDrawBuffer(drawBuffer);
|
||||
|
||||
if (filter)
|
||||
vcg::PullPush(*textureImage, qRgba(0, 255, 0, 255));
|
||||
vcg::PullPush(*textureImage, qRgba(0, 255, 0, 128));
|
||||
|
||||
Mirror(*textureImage);
|
||||
|
||||
|
||||
@ -32,6 +32,8 @@
|
||||
#include <vcg/complex/algorithms/update/topology.h>
|
||||
#include <vcg/complex/algorithms/update/normal.h>
|
||||
|
||||
#include <common/GLExtensionsManager.h>
|
||||
|
||||
#include "TextureDefragmentation/src/mesh.h"
|
||||
#include "TextureDefragmentation/src/texture_object.h"
|
||||
#include "TextureDefragmentation/src/mesh_attribute.h"
|
||||
@ -114,7 +116,7 @@ int FilterTextureDefragPlugin::postCondition(const QAction *a) const
|
||||
{
|
||||
switch (ID(a)) {
|
||||
case FP_TEXTURE_DEFRAG:
|
||||
return MeshModel::MM_WEDGTEXCOORD;
|
||||
return MeshModel::MM_WEDGTEXCOORD | MeshModel::MM_GEOMETRY_AND_TOPOLOGY_CHANGE; // just to disable preview...
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
@ -182,12 +184,19 @@ std::map<std::string, QVariant> FilterTextureDefragPlugin::applyFilter(
|
||||
unsigned int& /*postConditionMask*/,
|
||||
CallBackPos *cb)
|
||||
{
|
||||
MeshModel &mm = *(md.mm());
|
||||
MeshModel ¤tModel = *(md.mm());
|
||||
switch(ID(filter)) {
|
||||
case FP_TEXTURE_DEFRAG:
|
||||
{
|
||||
if (mm.fullName().isEmpty())
|
||||
throw MLException("Mesh must be saved to disk before proceeding.");
|
||||
MeshModel& mm = *(md.addNewMesh("", "texdefrag_" + currentModel.label(), false));
|
||||
mm.updateDataMask(¤tModel);
|
||||
tri::Append<CMeshO, CMeshO>::Mesh(mm.cm, currentModel.cm);
|
||||
mm.UpdateBoxAndNormals();
|
||||
mm.cm.Tr = currentModel.cm.Tr;
|
||||
|
||||
GLExtensionsManager::initializeGLextensions();
|
||||
|
||||
QString path = currentModel.pathName();
|
||||
|
||||
tri::UpdateTopology<CMeshO>::FaceFace(mm.cm);
|
||||
if (tri::Clean<CMeshO>::CountNonManifoldEdgeFF(mm.cm) > 0)
|
||||
@ -195,7 +204,7 @@ std::map<std::string, QVariant> FilterTextureDefragPlugin::applyFilter(
|
||||
|
||||
// switch working directory
|
||||
QDir wd = QDir::current();
|
||||
QDir::setCurrent(mm.pathName());
|
||||
QDir::setCurrent(path);
|
||||
|
||||
tri::Allocator<CMeshO>::CompactEveryVector(mm.cm);
|
||||
|
||||
@ -352,14 +361,15 @@ std::map<std::string, QVariant> FilterTextureDefragPlugin::applyFilter(
|
||||
|
||||
const char *imageFormat = "png";
|
||||
const int quality = 66;
|
||||
std::string textureBase = QFileInfo(mm.fullName()).baseName().toStdString() + "_optimized_texture_";
|
||||
QString textureBase = QFileInfo(currentModel.fullName()).baseName() + "_optimized_texture_";
|
||||
for (unsigned i = 0; i < newTextures.size(); ++i) {
|
||||
std::string tname = textureBase + std::to_string(i) + "." + imageFormat;
|
||||
if (!newTextures[i]->save(tname.c_str(), imageFormat, quality)) {
|
||||
std::string err = "Texture file " + tname + " cannot be saved on disk";
|
||||
QString tname = textureBase + QString(std::to_string(i).c_str()) + "." + imageFormat;
|
||||
std::string fullTextureName = QDir(currentModel.pathName()).filePath(tname).toStdString();
|
||||
if (!newTextures[i]->save(fullTextureName.c_str(), imageFormat, quality)) {
|
||||
std::string err = "Texture file " + fullTextureName + " cannot be saved on disk";
|
||||
throw MLException(err.c_str());
|
||||
}
|
||||
mm.cm.textures.push_back(tname);
|
||||
mm.cm.textures.push_back(fullTextureName);
|
||||
}
|
||||
|
||||
cb(100, "Done!");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user