~GLWidgetRendererPrivate() { releaseShaderProgram(); if (!textures.isEmpty()) { glDeleteTextures(textures.size(), textures.data()); textures.clear(); } if (shader_program) { delete shader_program; shader_program = 0; } }
bool GLWidgetRendererPrivate::prepareShaderProgram(const VideoFormat &fmt) { // isSupported(pixfmt) if (!fmt.isValid()) return false; releaseShaderProgram(); video_format.setPixelFormatFFmpeg(fmt.pixelFormatFFmpeg()); // TODO: only to kinds, packed.glsl, planar.glsl QString frag; if (fmt.isPlanar()) { frag = getShaderFromFile("shaders/yuv_rgb.f.glsl"); } else { frag = getShaderFromFile("shaders/rgb.f.glsl"); } if (frag.isEmpty()) return false; if (!fmt.isRGB() && fmt.isPlanar() && fmt.bytesPerPixel(0) == 2) { if (fmt.isBigEndian()) frag.prepend("#define YUV16BITS_BE_LUMINANCE_ALPHA\n"); else frag.prepend("#define YUV16BITS_LE_LUMINANCE_ALPHA\n"); frag.prepend(QString("#define YUV%1P\n").arg(fmt.bitsPerPixel(0))); } #if NO_QGL_SHADER program = createProgram(kVertexShader, frag.toUtf8().constData()); if (!program) { qWarning("Could not create shader program."); return false; } // vertex shader a_Position = glGetAttribLocation(program, "a_Position"); a_TexCoords = glGetAttribLocation(program, "a_TexCoords"); u_matrix = glGetUniformLocation(program, "u_MVP_matrix"); // fragment shader u_colorMatrix = glGetUniformLocation(program, "u_colorMatrix"); #else if (!shader_program->addShaderFromSourceCode(QGLShader::Vertex, kVertexShader)) { qWarning("Failed to add vertex shader: %s", shader_program->log().toUtf8().constData()); return false; } if (!shader_program->addShaderFromSourceCode(QGLShader::Fragment, frag)) { qWarning("Failed to add fragment shader: %s", shader_program->log().toUtf8().constData()); return false; } if (!shader_program->link()) { qWarning("Failed to link shader program...%s", shader_program->log().toUtf8().constData()); return false; } // vertex shader a_Position = shader_program->attributeLocation("a_Position"); a_TexCoords = shader_program->attributeLocation("a_TexCoords"); u_matrix = shader_program->uniformLocation("u_MVP_matrix"); // fragment shader u_colorMatrix = shader_program->uniformLocation("u_colorMatrix"); #endif //NO_QGL_SHADER qDebug("glGetAttribLocation(\"a_Position\") = %d\n", a_Position); qDebug("glGetAttribLocation(\"a_TexCoords\") = %d\n", a_TexCoords); qDebug("glGetUniformLocation(\"u_MVP_matrix\") = %d\n", u_matrix); qDebug("glGetUniformLocation(\"u_colorMatrix\") = %d\n", u_colorMatrix); if (fmt.isRGB()) u_Texture.resize(1); else u_Texture.resize(fmt.channels()); for (int i = 0; i < u_Texture.size(); ++i) { QString tex_var = QString("u_Texture%1").arg(i); #if NO_QGL_SHADER u_Texture[i] = glGetUniformLocation(program, tex_var.toUtf8().constData()); #else u_Texture[i] = shader_program->uniformLocation(tex_var); #endif qDebug("glGetUniformLocation(\"%s\") = %d\n", tex_var.toUtf8().constData(), u_Texture[i]); } return true; }
void Visualizer::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 mMatrix; QMatrix4x4 vMatrix; QMatrix4x4 cameraTransformation; cameraTransformation.rotate(alpha,0,1,0); cameraTransformation.rotate(beta,1,0,0); QVector3D cameraPosition = cameraTransformation * QVector3D(0,0,distance); QVector3D cameraUpDirection = cameraTransformation * QVector3D(0,1,0); vMatrix.lookAt(cameraPosition,QVector3D(0,0,0),cameraUpDirection); mMatrix.setToIdentity(); QMatrix4x4 mvMatrix; mvMatrix = vMatrix * mMatrix; QMatrix3x3 normalMatrix; normalMatrix = mvMatrix.normalMatrix(); QMatrix4x4 lightTransformation; lightTransformation.rotate(lightAngle,0 ,1, 0); //QVector3D lightPosition = lightTransformation * QVector3D(0,1,1); QVector3D lightPosition = QVector3D(0,1,1); QVector3D leftlightPosition = QVector3D(-1,0,0); int offset; // for(int k=0;k<myBlocks.size();k++) for(int k=0;k<27;k++) { mMatrix.setToIdentity(); // mMatrix *= stones[k].mMatrix; // mMatrix.translate(stones[k].position); if(isRotating) { mMatrix = myBlocks.value(k)->getModelMatrix() * mMatrix; mMatrix.translate(myBlocks.value(k)->getPosition()); } if(!isRotating) { mMatrix.translate(myBlocks.value(k)->getPosition()); mMatrix = myBlocks.value(k)->getOrientation() * mMatrix ; } //mMatrix = myBlocks.value(k)->getOrientation() * mMatrix; mMatrix.scale(1); mvMatrix = vMatrix * mMatrix; normalMatrix = mvMatrix.normalMatrix(); m_pBufferList[k]->shaderProgram->bind(); m_pBufferList[k]->shaderProgram->setUniformValue("mvpMatrix", pMatrix * mvMatrix); m_pBufferList[k]->shaderProgram->setUniformValue("mvMatrix", mvMatrix); m_pBufferList[k]->shaderProgram->setUniformValue("normalMatrix", normalMatrix); m_pBufferList[k]->shaderProgram->setUniformValue("lightPosition", vMatrix * lightPosition); m_pBufferList[k]->shaderProgram->setUniformValue("leftlightPosition", vMatrix * leftlightPosition); m_pBufferList[k]->shaderProgram->setUniformValue("ambientColor", QColor(162,162,162)); m_pBufferList[k]->shaderProgram->setUniformValue("diffuseColor", QColor(128,128,128)); m_pBufferList[k]->shaderProgram->setUniformValue("specularColor", QColor(255,255,255)); m_pBufferList[k]->shaderProgram->setUniformValue("ambientReflection", (GLfloat) 1.0); m_pBufferList[k]->shaderProgram->setUniformValue("diffuseReflection", (GLfloat) 1.0); m_pBufferList[k]->shaderProgram->setUniformValue("specularReflection", (GLfloat) 1.0); m_pBufferList[k]->shaderProgram->setUniformValue("shininess", (GLfloat) 100.0); //m_pBufferList[0]->shaderProgram->setUniformValue("texture",0); //glBindTexture(GL_TEXTURE_2D,cubeTexture); WriteBufferToGPU(m_pBufferList[k],0); glDrawArrays(GL_TRIANGLES, 0, m_pBufferList[k]->numVertices); releaseShaderProgram(m_pBufferList[k],0); } mMatrix.setToIdentity(); mMatrix.translate(lightPosition); mMatrix.rotate(lightAngle,0,1,0); mMatrix.rotate(45,1,0,0); mMatrix.scale(0.1); coloringShaderProgram.bind(); coloringShaderProgram.setUniformValue("mvpMatrix", pMatrix*vMatrix*mMatrix); offset = 0; spotlightBuffer.bind(); coloringShaderProgram.setAttributeBuffer("vertex", GL_FLOAT, offset, 3, 0); coloringShaderProgram.enableAttributeArray("vertex"); offset += numSpotlightVertices * 3 * sizeof (GLfloat); coloringShaderProgram.setAttributeBuffer("color", GL_FLOAT, offset, 3, 0); coloringShaderProgram.enableAttributeArray("color"); glDrawArrays(GL_TRIANGLES, 0, spotlightVertices.size()); coloringShaderProgram.disableAttributeArray("vertex"); coloringShaderProgram.disableAttributeArray("color"); coloringShaderProgram.release(); mMatrix.setToIdentity(); mMatrix.translate(-2,0,0); mMatrix.scale(0.1); leftlightShaderProgram.bind(); leftlightShaderProgram.setUniformValue("mvpMatrix", pMatrix*vMatrix*mMatrix); blenderObjectFileBuffer.bind(); offset = 0; leftlightShaderProgram.setAttributeBuffer("vertex", GL_FLOAT, offset, 3, 0); leftlightShaderProgram.enableAttributeArray("vertex"); offset += numSpotlightVertices * 3 * sizeof (GLfloat); leftlightShaderProgram.setAttributeBuffer("color", GL_FLOAT, offset, 3, 0); leftlightShaderProgram.enableAttributeArray("color"); glDrawArrays(GL_TRIANGLES, 0, 12); leftlightShaderProgram.disableAttributeArray("vertex"); leftlightShaderProgram.disableAttributeArray("color"); leftlightShaderProgram.release(); }