1 /* 2 Copyright (c) 2019 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 Permission is hereby granted, free of charge, to any person or organization 6 obtaining a copy of the software and accompanying documentation covered by 7 this license (the "Software") to use, reproduce, display, distribute, 8 execute, and transmit the Software, and to prepare derivative works of the 9 Software, and to permit third-parties to whom the Software is furnished to 10 do so, all subject to the following: 11 12 The copyright notices in the Software and this entire statement, including 13 the above license grant, this restriction and the following disclaimer, 14 must be included in all copies of the Software, in whole or in part, and 15 all derivative works of the Software, unless such copies or derivative 16 works are solely in the form of machine-executable object code generated by 17 a source language processor. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 DEALINGS IN THE SOFTWARE. 26 */ 27 28 module dagon.graphics.cubemaprendertarget; 29 30 import dlib.core.memory; 31 import dlib.math.vector; 32 import dlib.math.matrix; 33 import dlib.image.color; 34 35 import dagon.core.libs; 36 import dagon.core.ownership; 37 import dagon.graphics.framebuffer; 38 import dagon.graphics.gbuffer; 39 import dagon.graphics.cubemap; 40 import dagon.graphics.rc; 41 42 class CubemapRenderTarget: RenderTarget 43 { 44 GBuffer gbuffer; 45 GLuint fbo; 46 GLuint depthTexture = 0; 47 48 this(uint res, Owner o) 49 { 50 super(res, res, o); 51 52 gbuffer = New!GBuffer(res, res, this); 53 54 glActiveTexture(GL_TEXTURE0); 55 56 glGenTextures(1, &depthTexture); 57 glBindTexture(GL_TEXTURE_2D, depthTexture); 58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 62 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, res, res, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, null); 63 glBindTexture(GL_TEXTURE_2D, 0); 64 65 glGenFramebuffers(1, &fbo); 66 glBindFramebuffer(GL_FRAMEBUFFER, fbo); 67 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0); 68 GLenum[1] bufs = [GL_COLOR_ATTACHMENT0]; 69 glDrawBuffers(1, bufs.ptr); 70 71 glBindFramebuffer(GL_FRAMEBUFFER, 0); 72 } 73 74 ~this() 75 { 76 if (glIsTexture(depthTexture)) 77 glDeleteTextures(1, &depthTexture); 78 79 glBindFramebuffer(GL_FRAMEBUFFER, 0); 80 glDeleteFramebuffers(1, &fbo); 81 } 82 83 override void bind() 84 { 85 glBindFramebuffer(GL_FRAMEBUFFER, fbo); 86 } 87 88 override void unbind() 89 { 90 glBindFramebuffer(GL_FRAMEBUFFER, 0); 91 } 92 93 override void clear(Color4f clearColor) 94 { 95 glClearColor(clearColor.r, clearColor.g, clearColor.b, 0.0f); 96 glClear(GL_DEPTH_BUFFER_BIT); 97 Color4f zero = Color4f(0, 0, 0, 0); 98 glClearBufferfv(GL_COLOR, 0, zero.arrayof.ptr); 99 } 100 101 void setCubemapFace(Cubemap cubemap, CubeFace face) 102 { 103 glBindFramebuffer(GL_FRAMEBUFFER, fbo); 104 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, face, cubemap.tex, 0); 105 glBindFramebuffer(GL_FRAMEBUFFER, 0); 106 } 107 108 void prepareRC(CubeFace face, Vector3f position, RenderingContext* rc) 109 { 110 rc.prevViewMatrix = rc.viewMatrix; 111 112 rc.invViewMatrix = cubeFaceCameraMatrix(face, position); 113 rc.viewMatrix = rc.invViewMatrix.inverse; 114 115 rc.modelViewMatrix = rc.viewMatrix; 116 rc.normalMatrix = rc.invViewMatrix.transposed; 117 rc.cameraPosition = position; 118 Matrix4x4f mvp = rc.projectionMatrix * rc.viewMatrix; 119 rc.frustum.fromMVP(mvp); 120 121 rc.prevCameraPosition = position; 122 123 rc.viewRotationMatrix = matrix3x3to4x4(matrix4x4to3x3(rc.viewMatrix)); 124 rc.invViewRotationMatrix = matrix3x3to4x4(matrix4x4to3x3(rc.invViewMatrix)); 125 } 126 }