void GLWindow::changeTextureMagFilter(int _i) { switch(_i) { case 0 : m_textureMagFilter = GL_NEAREST; break; case 1 : m_textureMagFilter = GL_LINEAR; break; } glDeleteTextures(1,&m_textureID); glDeleteFramebuffers(1,&m_fboID); createFramebufferObject(); updateGL(); }
QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() { if (!_primaryFramebufferObject) { _primaryFramebufferObject = createFramebufferObject(); glGenTextures(1, &_primaryDepthTextureID); glBindTexture(GL_TEXTURE_2D, _primaryDepthTextureID); QSize size = Application::getInstance()->getGLWidget()->size(); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.width(), size.height(), 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); _primaryFramebufferObject->bind(); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _primaryDepthTextureID, 0); _primaryFramebufferObject->release(); } return _primaryFramebufferObject; }
void GLWindow::changeTextureSize(int _i) { switch(_i) { case 0 : m_textureSize=64; break; case 1 : m_textureSize=128; break; case 2 : m_textureSize=256; break; case 3 : m_textureSize=512; break; case 4 : m_textureSize=1024; break; case 5 : m_textureSize=2048; break; } glDeleteTextures(1,&m_textureID); glDeleteFramebuffers(1,&m_fboID); createFramebufferObject(); updateGL(); }
NGLDraw::NGLDraw() { m_animate=true; m_lightPosition.set(8,4,8); m_lightYPos=4.0; m_lightXoffset=8.0; m_lightZoffset=8.0; m_width=720; m_height=576; m_rotate=false; // mouse rotation values set to 0 m_spinXFace=0; m_spinYFace=0; glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glClearColor(0.4f, 0.4f, 0.4f, 1.0f); // Grey Background // now to load the shader and set the values // grab an instance of shader manager ngl::ShaderLib *shader=ngl::ShaderLib::instance(); // Now we will create a basic Camera from the graphics library // This is a static camera so it only needs to be set once // First create Values for the camera position ngl::Vec3 from(0,2,6); ngl::Vec3 to(0,0,0); ngl::Vec3 up(0,1,0); // now load to our new camera m_cam= new ngl::Camera(from,to,up); // set the shape using FOV 45 Aspect Ratio based on Width and Height // The final two are near and far clipping planes of 0.5 and 10 m_cam->setShape(45,(float)720.0/576.0,znear,zfar); // now load to our light POV camera m_lightCamera= new ngl::Camera(m_lightPosition,to,up); // here we set the light POV camera shape, the aspect is 1 as our // texture is square. // use the same clip plane as before but set the FOV a bit larger // to get slightly better shadows and the clip planes will help // to get rid of some of the artefacts m_lightCamera->setShape(45,float(720/576),znear,zfar); // in this case I'm only using the light to hold the position // it is not passed to the shader directly m_lightAngle=0.0; // we are creating a shader called Texture shader->createShaderProgram("Texture"); // now we are going to create empty shaders for Frag and Vert shader->attachShader("TextureVertex",ngl::VERTEX); shader->attachShader("TextureFragment",ngl::FRAGMENT); // attach the source shader->loadShaderSource("TextureVertex","/shaders/TextureVert.glsl"); shader->loadShaderSource("TextureFragment","/shaders/TextureFrag.glsl"); // compile the shaders shader->compileShader("TextureVertex"); shader->compileShader("TextureFragment"); // add them to the program shader->attachShaderToProgram("Texture","TextureVertex"); shader->attachShaderToProgram("Texture","TextureFragment"); // now bind the shader attributes for most NGL primitives we use the following // layout attribute 0 is the vertex data (x,y,z) shader->bindAttribute("Phong",0,"inVert"); // attribute 1 is the UV data u,v (if present) shader->bindAttribute("Phong",1,"inUV"); // attribute 2 are the normals x,y,z //shader->bindAttribute("Phong",2,"inNormal"); // now we have associated this data we can link the shader shader->linkProgramObject("Texture"); // we are creating a shader called Shadow shader->createShaderProgram("Shadow"); // now we are going to create empty shaders for Frag and Vert shader->attachShader("ShadowVertex",ngl::VERTEX); shader->attachShader("ShadowFragment",ngl::FRAGMENT); // attach the source shader->loadShaderSource("ShadowVertex","/shaders/ShadowVert.glsl"); shader->loadShaderSource("ShadowFragment","/shaders/ShadowFrag.glsl"); // compile the shaders shader->compileShader("ShadowVertex"); shader->compileShader("ShadowFragment"); // add them to the program shader->attachShaderToProgram("Shadow","ShadowVertex"); shader->attachShaderToProgram("Shadow","ShadowFragment"); // now bind the shader attributes for most NGL primitives we use the following // layout attribute 0 is the vertex data (x,y,z) shader->bindAttribute("Phong",0,"inVert"); // attribute 1 is the UV data u,v (if present) shader->bindAttribute("Phong",1,"inUV"); // attribute 2 are the normals x,y,z shader->bindAttribute("Phong",2,"inNormal"); // now we have associated this data we can link the shader shader->linkProgramObject("Shadow"); // create the primitives to draw ngl::VAOPrimitives *prim=ngl::VAOPrimitives::instance(); prim->createSphere("sphere",0.5,50); prim->createTorus("torus",0.15,0.4,40,40); prim->createTrianglePlane("plane",14,14,80,80,ngl::Vec3(0,1,0)); // now create our FBO and texture createFramebufferObject(); // we need to enable depth testing glEnable(GL_DEPTH_TEST); // set the depth comparison mode glDepthFunc(GL_LEQUAL); // set the bg to black glClearColor(0,0,0,1.0f); // enable face culling this will be switch to front and back when // rendering shadow or scene // glEnable(GL_CULL_FACE); // glPolygonOffset(1.1,4); // m_text = new ngl::Text(QFont("Ariel",14)); // m_text->setColour(1,1,1); }
void NGLScene::initializeGL() { // we must call this first before any other GL commands to load and link the // gl commands from the lib, if this is not done program will crash ngl::NGLInit::instance(); glClearColor(0.4f, 0.4f, 0.4f, 1.0f); // Grey Background // enable depth testing for drawing glEnable(GL_DEPTH_TEST); // enable multisampling for smoother drawing glEnable(GL_MULTISAMPLE); // This is a static camera so it only needs to be set once // First create Values for the camera position ngl::Vec3 from(2,2,2); ngl::Vec3 to(0,0,0); ngl::Vec3 up(0,1,0); // now load to our new camera m_view=ngl::lookAt(from,to,up); // set the shape using FOV 45 Aspect Ratio based on Width and Height // The final two are near and far clipping planes of 0.5 and 10 m_project=ngl::perspective(45.0f,720.0f/576.0f,0.05f,350.0f); // now to load the shader and set the values // grab an instance of shader manager ngl::ShaderLib *shader=ngl::ShaderLib::instance(); // we are creating a shader called Phong shader->createShaderProgram("Phong"); // now we are going to create empty shaders for Frag and Vert shader->attachShader("PhongVertex",ngl::ShaderType::VERTEX); shader->attachShader("PhongFragment",ngl::ShaderType::FRAGMENT); // attach the source shader->loadShaderSource("PhongVertex","shaders/PhongVert.glsl"); shader->loadShaderSource("PhongFragment","shaders/PhongFrag.glsl"); // compile the shaders shader->compileShader("PhongVertex"); shader->compileShader("PhongFragment"); // add them to the program shader->attachShaderToProgram("Phong","PhongVertex"); shader->attachShaderToProgram("Phong","PhongFragment"); // now we have associated this data we can link the shader shader->linkProgramObject("Phong"); // and make it active ready to load values (*shader)["Phong"]->use(); ngl::Vec4 lightPos(-2.0f,5.0f,2.0f,0.0f); shader->setUniform("light.position",lightPos); shader->setUniform("light.ambient",0.0f,0.0f,0.0f,1.0f); shader->setUniform("light.diffuse",1.0f,1.0f,1.0f,1.0f); shader->setUniform("light.specular",0.8f,0.8f,0.8f,1.0f); // gold like phong material shader->setUniform("material.ambient",0.274725f,0.1995f,0.0745f,0.0f); shader->setUniform("material.diffuse",0.75164f,0.60648f,0.22648f,0.0f); shader->setUniform("material.specular",0.628281f,0.555802f,0.3666065f,0.0f); shader->setUniform("material.shininess",51.2f); shader->setUniform("viewerPos",from); // now load our texture shader shader->createShaderProgram("TextureShader"); shader->attachShader("TextureVertex",ngl::ShaderType::VERTEX); shader->attachShader("TextureFragment",ngl::ShaderType::FRAGMENT); shader->loadShaderSource("TextureVertex","shaders/TextureVertex.glsl"); shader->loadShaderSource("TextureFragment","shaders/TextureFragment.glsl"); shader->compileShader("TextureVertex"); shader->compileShader("TextureFragment"); shader->attachShaderToProgram("TextureShader","TextureVertex"); shader->attachShaderToProgram("TextureShader","TextureFragment"); // link the shader no attributes are bound shader->linkProgramObject("TextureShader"); (*shader)["TextureShader"]->use(); // now create our texture object createTextureObject(); // now the fbo createFramebufferObject(); // now create the primitives to draw ngl::VAOPrimitives *prim=ngl::VAOPrimitives::instance(); prim->createTrianglePlane("plane",2,2,20,20,ngl::Vec3(0,1,0)); prim->createSphere("sphere",0.4f,80); startTimer(1); GLint maxAttach = 0; glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxAttach); }
QOpenGLFramebufferObject* TextureCache::getTertiaryFramebufferObject() { if (!_tertiaryFramebufferObject) { _tertiaryFramebufferObject = createFramebufferObject(); } return _tertiaryFramebufferObject; }
QOpenGLFramebufferObject* TextureCache::getSecondaryFramebufferObject() { if (!_secondaryFramebufferObject) { _secondaryFramebufferObject = createFramebufferObject(); } return _secondaryFramebufferObject; }
void NGLScene::initialize() { // we must call this first before any other GL commands to load and link the // gl commands from the lib, if this is not done program will crash ngl::NGLInit::instance(); glClearColor(0.4f, 0.4f, 0.4f, 1.0f); // Grey Background // enable depth testing for drawing glEnable(GL_DEPTH_TEST); // enable multisampling for smoother drawing glEnable(GL_MULTISAMPLE); // now to load the shader and set the values // grab an instance of shader manager ngl::ShaderLib *shader=ngl::ShaderLib::instance(); // we are creating a shader called Phong shader->createShaderProgram("Phong"); // now we are going to create empty shaders for Frag and Vert shader->attachShader("PhongVertex",ngl::VERTEX); shader->attachShader("PhongFragment",ngl::FRAGMENT); // attach the source shader->loadShaderSource("PhongVertex","shaders/PhongVert.glsl"); shader->loadShaderSource("PhongFragment","shaders/PhongFrag.glsl"); // compile the shaders shader->compileShader("PhongVertex"); shader->compileShader("PhongFragment"); // add them to the program shader->attachShaderToProgram("Phong","PhongVertex"); shader->attachShaderToProgram("Phong","PhongFragment"); // now we have associated this data we can link the shader shader->linkProgramObject("Phong"); // and make it active ready to load values (*shader)["Phong"]->use(); // the shader will use the currently active material and light0 so set them ngl::Material m(ngl::GOLD); // load our material values to the shader into the structure material (see Vertex shader) m.loadToShader("material"); // Now we will create a basic Camera from the graphics library // This is a static camera so it only needs to be set once // First create Values for the camera position ngl::Vec3 from(1,1,1); ngl::Vec3 to(0,0,0); ngl::Vec3 up(0,1,0); // now load to our new camera m_cam= new ngl::Camera(from,to,up); // set the shape using FOV 45 Aspect Ratio based on Width and Height // The final two are near and far clipping planes of 0.5 and 10 m_cam->setShape(45,(float)720.0/576.0,0.05,350); shader->setShaderParam3f("viewerPos",m_cam->getEye().m_x,m_cam->getEye().m_y,m_cam->getEye().m_z); // now create our light this is done after the camera so we can pass the // transpose of the projection matrix to the light to do correct eye space // transformations ngl::Mat4 iv=m_cam->getViewMatrix(); iv.transpose(); m_light = new ngl::Light(ngl::Vec3(-2,5,2),ngl::Colour(1,1,1,1),ngl::Colour(1,1,1,1),ngl::POINTLIGHT); m_light->setTransform(iv); // load these values to the shader as well m_light->loadToShader("light"); // now load our texture shader shader->createShaderProgram("TextureShader"); shader->attachShader("TextureVertex",ngl::VERTEX); shader->attachShader("TextureFragment",ngl::FRAGMENT); shader->loadShaderSource("TextureVertex","shaders/TextureVertex.glsl"); shader->loadShaderSource("TextureFragment","shaders/TextureFragment.glsl"); shader->compileShader("TextureVertex"); shader->compileShader("TextureFragment"); shader->attachShaderToProgram("TextureShader","TextureVertex"); shader->attachShaderToProgram("TextureShader","TextureFragment"); // link the shader no attributes are bound shader->linkProgramObject("TextureShader"); (*shader)["TextureShader"]->use(); // now create our texture object createTextureObject(); // now the fbo createFramebufferObject(); // now create the primitives to draw ngl::VAOPrimitives *prim=ngl::VAOPrimitives::instance(); prim->createTrianglePlane("plane",2,2,20,20,ngl::Vec3(0,1,0)); prim->createSphere("sphere",0.4,80); // as re-size is not explicitly called we need to do this. glViewport(0,0,width(),height()); }
//---------------------------------------------------------------------------------------------------------------------- // This virtual function is called once before the first call to paintGL() or resizeGL(), //and then once whenever the widget has been assigned a new QGLContext. // This function should set up any required OpenGL context rendering flags, defining VBOs etc. //---------------------------------------------------------------------------------------------------------------------- void GLWindow::initializeGL() { // we must call this first before any other GL commands to load and link the // gl commands from the lib, if this is not done program will crash ngl::NGLInit::instance(); glClearColor(0.4f, 0.4f, 0.4f, 1.0f); // Grey Background // enable depth testing for drawing glEnable(GL_DEPTH_TEST); // enable multisampling for smoother drawing glEnable(GL_MULTISAMPLE); // now to load the shader and set the values // grab an instance of shader manager ngl::ShaderLib *shader=ngl::ShaderLib::instance(); // Now we will create a basic Camera from the graphics library // This is a static camera so it only needs to be set once // First create Values for the camera position ngl::Vec3 from(0,2,6); ngl::Vec3 to(0,0,0); ngl::Vec3 up(0,1,0); // now load to our new camera m_cam= new ngl::Camera(from,to,up); // set the shape using FOV 45 Aspect Ratio based on Width and Height // The final two are near and far clipping planes of 0.5 and 10 m_cam->setShape(45,(float)720.0/576.0,m_zNear,m_zfar); // now load to our light POV camera m_lightCamera= new ngl::Camera(m_lightPosition,to,up); // here we set the light POV camera shape, the aspect is 1 as our // texture is square. // use the same clip plane as before but set the FOV a bit larger // to get slightly better shadows and the clip planes will help // to get rid of some of the artefacts m_lightCamera->setShape(m_fov,1.0,m_zNear,m_zfar); // in this case I'm only using the light to hold the position // it is not passed to the shader directly m_lightAngle=0.0; // we are creating a shader called Texture shader->createShaderProgram("Texture"); // now we are going to create empty shaders for Frag and Vert shader->attachShader("TextureVertex",ngl::VERTEX); shader->attachShader("TextureFragment",ngl::FRAGMENT); // attach the source shader->loadShaderSource("TextureVertex","shaders/TextureVert.glsl"); shader->loadShaderSource("TextureFragment","shaders/TextureFrag.glsl"); // compile the shaders shader->compileShader("TextureVertex"); shader->compileShader("TextureFragment"); // add them to the program shader->attachShaderToProgram("Texture","TextureVertex"); shader->attachShaderToProgram("Texture","TextureFragment"); // now bind the shader attributes for most NGL primitives we use the following // layout attribute 0 is the vertex data (x,y,z) shader->bindAttribute("Texture",0,"inVert"); // now we have associated this data we can link the shader shader->linkProgramObject("Texture"); shader->use("Texture"); shader->autoRegisterUniforms("Texture"); // we are creating a shader called Colour shader->createShaderProgram("Colour"); // now we are going to create empty shaders for Frag and Vert shader->attachShader("ColourVertex",ngl::VERTEX); shader->attachShader("ColourFragment",ngl::FRAGMENT); // attach the source shader->loadShaderSource("ColourVertex","shaders/ColourVert.glsl"); shader->loadShaderSource("ColourFragment","shaders/ColourFrag.glsl"); // compile the shaders shader->compileShader("ColourVertex"); shader->compileShader("ColourFragment"); // add them to the program shader->attachShaderToProgram("Colour","ColourVertex"); shader->attachShaderToProgram("Colour","ColourFragment"); // now we have associated this data we can link the shader shader->linkProgramObject("Colour"); shader->use("Colour"); shader->setShaderParam4f("Colour",1,0,0,1); shader->autoRegisterUniforms("Colour"); // we are creating a shader called Shadow shader->createShaderProgram("Shadow"); // now we are going to create empty shaders for Frag and Vert shader->attachShader("ShadowVertex",ngl::VERTEX); shader->attachShader("ShadowFragment",ngl::FRAGMENT); // attach the source shader->loadShaderSource("ShadowVertex","shaders/ShadowVert.glsl"); shader->loadShaderSource("ShadowFragment","shaders/ShadowFrag.glsl"); // compile the shaders shader->compileShader("ShadowVertex"); shader->compileShader("ShadowFragment"); // add them to the program shader->attachShaderToProgram("Shadow","ShadowVertex"); shader->attachShaderToProgram("Shadow","ShadowFragment"); // now we have associated this data we can link the shader shader->linkProgramObject("Shadow"); shader->use("Shadow"); shader->autoRegisterUniforms("Shadow"); // create the primitives to draw ngl::VAOPrimitives *prim=ngl::VAOPrimitives::instance(); prim->createSphere("sphere",0.5,50); prim->createTorus("torus",0.15,0.4,40,40); prim->createTrianglePlane("plane",14,14,80,80,ngl::Vec3(0,1,0)); // now create our FBO and texture createFramebufferObject(); // we need to enable depth testing glEnable(GL_DEPTH_TEST); // set the depth comparison mode glDepthFunc(GL_LEQUAL); // set the bg to black glClearColor(0,0,0,1.0f); // enable face culling this will be switch to front and back when // rendering shadow or scene glEnable(GL_CULL_FACE); m_text = new ngl::Text(QFont("Ariel",14)); m_text->setColour(1,1,1); }