int getMaxTextureSize() { int maxSize = 0; // Create a temp context - required if this is called from another thread QOpenGLContext ctx; if ( !ctx.create() ) { // TODO handle the error qDebug() << "No OpenGL context could be created, this is clearly bad..."; exit(-1); } // rather than using a QWindow - which actually dosen't seem to work in this case either! QOffscreenSurface surface; surface.setFormat( ctx.format() ); surface.create(); ctx.makeCurrent(&surface); // Now the call works QOpenGLFunctions glFuncs(QOpenGLContext::currentContext()); glFuncs.glEnable(GL_TEXTURE_2D); glFuncs.glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize); return maxSize; }
void withCurrentContext(ContextFunctor f) { auto _currentContext = QOpenGLContext::currentContext(); if (!_currentContext || !_currentContext->isValid()) return; QOpenGLFunctions glFuncs(_currentContext); f(glFuncs); }
void RasterImageLayer::draw() { if (newFile) makeGL(); prog->bind(); prog->setUniformValue("mvp", projection().viewMatrix()); prog->setUniformValue("tex", 0); glEnable(GL_TEXTURE_2D); tex.bind(0); // Separate block for handling binding/release of VAO { QOpenGLVertexArrayObject::Binder bind(&vao); glFuncs()->glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } glDisable(GL_TEXTURE_2D); prog->release(); }
// render model void COglRenderer::render() { // QCoreApplication::processEvents(); QOpenGLFunctions glFuncs(QOpenGLContext::currentContext()); glFuncs.glUseProgram(0); // 设置清屏色 glClearColor(1.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); double ratio = (double)m_ViewportWidth/(double)m_ViewportHeight; // glFrustum(-1.0, 1.0, -ratio, ratio, 3, 7); gluPerspective(45.0, ratio, 0.1,100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); // 画三角形 // 绘制数组 // 三角形坐标 float coords[9] = { 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }; // 启用顶点缓冲区 glEnableClientState(GL_VERTEX_ARRAY); // 指定顶点缓冲区指针 glVertexPointer(3, GL_FLOAT, 0, coords); // 绘制三角形 glDrawArrays(GL_TRIANGLES, 0, 3); // 关闭顶点缓冲区 glDisableClientState(GL_VERTEX_ARRAY); update(); // SYSTEMTIME sys_time; // GetLocalTime( &sys_time ); // static float time1 = 0; // static float time2 = 0; // static int fps = 0; // qsrand(uint(sys_time.wMilliseconds)); // if (time1 == 0) // { // time1 = sys_time.wMinute * 60 + sys_time.wSecond + sys_time.wMilliseconds * 0.001f; // fps = 0; // } // fps ++; // if (fps >= 60) // { // time2 = sys_time.wMinute * 60 + sys_time.wSecond + sys_time.wMilliseconds * 0.001f; // while (time2 < time1) // { // time2 += 3600; // } // float t = time2 - time1; // time1 = 0; // if (t < 0.00001f){t = 0.0001f;} // m_fps = float(int(60.0 / t)); // // qDebug("%f",m_fps); // } }
void OcioDisplayFilter::updateProcessor() { if (!config) { return; } if (!displayDevice) { displayDevice = config->getDefaultDisplay(); } if (!view) { view = config->getDefaultView(displayDevice); } if (!inputColorSpaceName) { inputColorSpaceName = config->getColorSpaceNameByIndex(0); } OCIO::DisplayTransformRcPtr transform = OCIO::DisplayTransform::Create(); transform->setInputColorSpaceName(inputColorSpaceName); transform->setDisplay(displayDevice); transform->setView(view); OCIO::GroupTransformRcPtr approximateTransform = OCIO::GroupTransform::Create(); // fstop exposure control -- not sure how that translates to our exposure { float exposureGain = powf(2.0f, exposure); const qreal minRange = 0.001; if (qAbs(blackPoint - whitePoint) < minRange) { whitePoint = blackPoint + minRange; } const float oldMin[] = { blackPoint, blackPoint, blackPoint, 0.0f }; const float oldMax[] = { whitePoint, whitePoint, whitePoint, 1.0f }; const float newMin[] = { 0.0f, 0.0f, 0.0f, 0.0f }; const float newMax[] = { exposureGain, exposureGain, exposureGain, 1.0f }; float m44[16]; float offset4[4]; OCIO::MatrixTransform::Fit(m44, offset4, oldMin, oldMax, newMin, newMax); OCIO::MatrixTransformRcPtr mtx = OCIO::MatrixTransform::Create(); mtx->setValue(m44, offset4); transform->setLinearCC(mtx); // approximation (no color correction); approximateTransform->push_back(mtx); } // channel swizzle { int channelHot[4]; switch (swizzle) { case LUMINANCE: channelHot[0] = 1; channelHot[1] = 1; channelHot[2] = 1; channelHot[3] = 0; break; case RGBA: channelHot[0] = 1; channelHot[1] = 1; channelHot[2] = 1; channelHot[3] = 1; break; case R: channelHot[0] = 1; channelHot[1] = 0; channelHot[2] = 0; channelHot[3] = 0; break; case G: channelHot[0] = 0; channelHot[1] = 1; channelHot[2] = 0; channelHot[3] = 0; break; case B: channelHot[0] = 0; channelHot[1] = 0; channelHot[2] = 1; channelHot[3] = 0; break; case A: channelHot[0] = 0; channelHot[1] = 0; channelHot[2] = 0; channelHot[3] = 1; default: ; } float lumacoef[3]; config->getDefaultLumaCoefs(lumacoef); float m44[16]; float offset[4]; OCIO::MatrixTransform::View(m44, offset, channelHot, lumacoef); OCIO::MatrixTransformRcPtr swizzle = OCIO::MatrixTransform::Create(); swizzle->setValue(m44, offset); transform->setChannelView(swizzle); } // Post-display transform gamma { float exponent = 1.0f/std::max(1e-6f, static_cast<float>(gamma)); const float exponent4f[] = { exponent, exponent, exponent, exponent }; OCIO::ExponentTransformRcPtr expTransform = OCIO::ExponentTransform::Create(); expTransform->setValue(exponent4f); transform->setDisplayCC(expTransform); // approximation (no color correction); approximateTransform->push_back(expTransform); } m_processor = config->getProcessor(transform); m_forwardApproximationProcessor = config->getProcessor(approximateTransform, OCIO::TRANSFORM_DIR_FORWARD); try { m_revereseApproximationProcessor = config->getProcessor(approximateTransform, OCIO::TRANSFORM_DIR_INVERSE); } catch (...) { warnKrita << "OCIO inverted matrix does not exist!"; //m_revereseApproximationProcessor; } #ifdef HAVE_OPENGL // check whether we are allowed to use shaders -- though that should // work for everyone these days KisConfig cfg; if (!cfg.useOpenGL()) return; QOpenGLFunctions glFuncs(QOpenGLContext::currentContext()); QOpenGLFunctions_3_2_Core *glFuncs3 = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_2_Core>(); const int lut3DEdgeSize = cfg.ocioLutEdgeSize(); if (m_lut3d.size() == 0) { //dbgKrita << "generating lut"; glFuncs.glGenTextures(1, &m_lut3dTexID); int num3Dentries = 3 * lut3DEdgeSize * lut3DEdgeSize * lut3DEdgeSize; m_lut3d.fill(0.0, num3Dentries); glFuncs.glActiveTexture(GL_TEXTURE1); glFuncs.glBindTexture(GL_TEXTURE_3D, m_lut3dTexID); glFuncs.glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glFuncs.glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glFuncs.glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glFuncs.glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFuncs.glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glFuncs3->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, lut3DEdgeSize, lut3DEdgeSize, lut3DEdgeSize, 0, GL_RGB, GL_FLOAT, &m_lut3d.constData()[0]); } // Step 1: Create a GPU Shader Description OCIO::GpuShaderDesc shaderDesc; shaderDesc.setLanguage(OCIO::GPU_LANGUAGE_GLSL_1_3); shaderDesc.setFunctionName("OCIODisplay"); shaderDesc.setLut3DEdgeLen(lut3DEdgeSize); // Step 2: Compute the 3D LUT QString lut3dCacheID = QString::fromLatin1(m_processor->getGpuLut3DCacheID(shaderDesc)); if(lut3dCacheID != m_lut3dcacheid) { //dbgKrita << "Computing 3DLut " << m_lut3dcacheid; m_lut3dcacheid = lut3dCacheID; m_processor->getGpuLut3D(&m_lut3d[0], shaderDesc); glFuncs.glBindTexture(GL_TEXTURE_3D, m_lut3dTexID); glFuncs3->glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, lut3DEdgeSize, lut3DEdgeSize, lut3DEdgeSize, GL_RGB, GL_FLOAT, &m_lut3d[0]); } // Step 3: Generate the shader text QString shaderCacheID = QString::fromLatin1(m_processor->getGpuShaderTextCacheID(shaderDesc)); if (m_program.isEmpty() || shaderCacheID != m_shadercacheid) { //dbgKrita << "Computing Shader " << m_shadercacheid; m_shadercacheid = shaderCacheID; std::ostringstream os; os << m_processor->getGpuShaderText(shaderDesc) << "\n"; m_program = QString::fromLatin1(os.str().c_str()); } #endif }