• 12
• 12
• 9
• 10
• 13
• ### Similar Content

• By elect
Hi,
ok, so, we are having problems with our current mirror reflection implementation.
At the moment we are doing it very simple, so for the i-th frame, we calculate the reflection vectors given the viewPoint and some predefined points on the mirror surface (position and normal).
Then, using the least squared algorithm, we find the point that has the minimum distance from all these reflections vectors. This is going to be our virtual viewPoint (with the right orientation).
After that, we render offscreen to a texture by setting the OpenGL camera on the virtual viewPoint.
And finally we use the rendered texture on the mirror surface.
So far this has always been fine, but now we are having some more strong constraints on accuracy.
What are our best options given that:
- we have a dynamic scene, the mirror and parts of the scene can change continuously from frame to frame
- we have about 3k points (with normals) per mirror, calculated offline using some cad program (such as Catia)
- all the mirror are always perfectly spherical (with different radius vertically and horizontally) and they are always convex
- a scene can have up to 10 mirror
- it should be fast enough also for vr (Htc Vive) on fastest gpus (only desktops)

Looking around, some papers talk about calculating some caustic surface derivation offline, but I don't know if this suits my case
Also, another paper, used some acceleration structures to detect the intersection between the reflection vectors and the scene, and then adjust the corresponding texture coordinate. This looks the most accurate but also very heavy from a computational point of view.

Other than that, I couldn't find anything updated/exhaustive around, can you help me?

• Hello all,
I am currently working on a game engine for use with my game development that I would like to be as flexible as possible.  As such the exact requirements for how things should work can't be nailed down to a specific implementation and I am looking for, at least now, a default good average case scenario design.
Here is what I have implemented:
Deferred rendering using OpenGL Arbitrary number of lights and shadow mapping Each rendered object, as defined by a set of geometry, textures, animation data, and a model matrix is rendered with its own draw call Skeletal animations implemented on the GPU.   Model matrix transformation implemented on the GPU Frustum and octree culling for optimization Here are my questions and concerns:
Doing the skeletal animation on the GPU, currently, requires doing the skinning for each object multiple times per frame: once for the initial geometry rendering and once for the shadow map rendering for each light for which it is not culled.  This seems very inefficient.  Is there a way to do skeletal animation on the GPU only once across these render calls? Without doing the model matrix transformation on the CPU, I fail to see how I can easily batch objects with the same textures and shaders in a single draw call without passing a ton of matrix data to the GPU (an array of model matrices then an index for each vertex into that array for transformation purposes?) If I do the matrix transformations on the CPU, It seems I can't really do the skinning on the GPU as the pre-transformed vertexes will wreck havoc with the calculations, so this seems not viable unless I am missing something Overall it seems like simplest solution is to just do all of the vertex manipulation on the CPU and pass the pre-transformed data to the GPU, using vertex shaders that do basically nothing.  This doesn't seem the most efficient use of the graphics hardware, but could potentially reduce the number of draw calls needed.

Really, I am looking for some advice on how to proceed with this, how something like this is typically handled.  Are the multiple draw calls and skinning calculations not a huge deal?  I would LIKE to save as much of the CPU's time per frame so it can be tasked with other things, as to keep CPU resources open to the implementation of the engine.  However, that becomes a moot point if the GPU becomes a bottleneck.

• Hello!
I would like to introduce Diligent Engine, a project that I've been recently working on. Diligent Engine is a light-weight cross-platform abstraction layer between the application and the platform-specific graphics API. Its main goal is to take advantages of the next-generation APIs such as Direct3D12 and Vulkan, but at the same time provide support for older platforms via Direct3D11, OpenGL and OpenGLES. Diligent Engine exposes common front-end for all supported platforms and provides interoperability with underlying native API. Shader source code converter allows shaders authored in HLSL to be translated to GLSL and used on all platforms. Diligent Engine supports integration with Unity and is designed to be used as a graphics subsystem in a standalone game engine, Unity native plugin or any other 3D application. It is distributed under Apache 2.0 license and is free to use. Full source code is available for download on GitHub.
Features:
True cross-platform Exact same client code for all supported platforms and rendering backends No #if defined(_WIN32) ... #elif defined(LINUX) ... #elif defined(ANDROID) ... No #if defined(D3D11) ... #elif defined(D3D12) ... #elif defined(OPENGL) ... Exact same HLSL shaders run on all platforms and all backends Modular design Components are clearly separated logically and physically and can be used as needed Only take what you need for your project (do not want to keep samples and tutorials in your codebase? Simply remove Samples submodule. Only need core functionality? Use only Core submodule) No 15000 lines-of-code files Clear object-based interface No global states Key graphics features: Automatic shader resource binding designed to leverage the next-generation rendering APIs Multithreaded command buffer generation 50,000 draw calls at 300 fps with D3D12 backend Descriptor, memory and resource state management Modern c++ features to make code fast and reliable The following platforms and low-level APIs are currently supported:
Windows Desktop: Direct3D11, Direct3D12, OpenGL Universal Windows: Direct3D11, Direct3D12 Linux: OpenGL Android: OpenGLES MacOS: OpenGL iOS: OpenGLES API Basics
Initialization
The engine can perform initialization of the API or attach to already existing D3D11/D3D12 device or OpenGL/GLES context. For instance, the following code shows how the engine can be initialized in D3D12 mode:
#include "RenderDeviceFactoryD3D12.h" using namespace Diligent; // ...  GetEngineFactoryD3D12Type GetEngineFactoryD3D12 = nullptr; // Load the dll and import GetEngineFactoryD3D12() function LoadGraphicsEngineD3D12(GetEngineFactoryD3D12); auto *pFactoryD3D11 = GetEngineFactoryD3D12(); EngineD3D12Attribs EngD3D12Attribs; EngD3D12Attribs.CPUDescriptorHeapAllocationSize[0] = 1024; EngD3D12Attribs.CPUDescriptorHeapAllocationSize[1] = 32; EngD3D12Attribs.CPUDescriptorHeapAllocationSize[2] = 16; EngD3D12Attribs.CPUDescriptorHeapAllocationSize[3] = 16; EngD3D12Attribs.NumCommandsToFlushCmdList = 64; RefCntAutoPtr<IRenderDevice> pRenderDevice; RefCntAutoPtr<IDeviceContext> pImmediateContext; SwapChainDesc SwapChainDesc; RefCntAutoPtr<ISwapChain> pSwapChain; pFactoryD3D11->CreateDeviceAndContextsD3D12( EngD3D12Attribs, &pRenderDevice, &pImmediateContext, 0 ); pFactoryD3D11->CreateSwapChainD3D12( pRenderDevice, pImmediateContext, SwapChainDesc, hWnd, &pSwapChain ); Creating Resources
Device resources are created by the render device. The two main resource types are buffers, which represent linear memory, and textures, which use memory layouts optimized for fast filtering. To create a buffer, you need to populate BufferDesc structure and call IRenderDevice::CreateBuffer(). The following code creates a uniform (constant) buffer:
BufferDesc BuffDesc; BufferDesc.Name = "Uniform buffer"; BuffDesc.BindFlags = BIND_UNIFORM_BUFFER; BuffDesc.Usage = USAGE_DYNAMIC; BuffDesc.uiSizeInBytes = sizeof(ShaderConstants); BuffDesc.CPUAccessFlags = CPU_ACCESS_WRITE; m_pDevice->CreateBuffer( BuffDesc, BufferData(), &m_pConstantBuffer ); Similar, to create a texture, populate TextureDesc structure and call IRenderDevice::CreateTexture() as in the following example:
TextureDesc TexDesc; TexDesc.Name = "My texture 2D"; TexDesc.Type = TEXTURE_TYPE_2D; TexDesc.Width = 1024; TexDesc.Height = 1024; TexDesc.Format = TEX_FORMAT_RGBA8_UNORM; TexDesc.Usage = USAGE_DEFAULT; TexDesc.BindFlags = BIND_SHADER_RESOURCE | BIND_RENDER_TARGET | BIND_UNORDERED_ACCESS; TexDesc.Name = "Sample 2D Texture"; m_pRenderDevice->CreateTexture( TexDesc, TextureData(), &m_pTestTex ); Initializing Pipeline State
Diligent Engine follows Direct3D12 style to configure the graphics/compute pipeline. One big Pipelines State Object (PSO) encompasses all required states (all shader stages, input layout description, depth stencil, rasterizer and blend state descriptions etc.)
To create a shader, populate ShaderCreationAttribs structure. An important member is ShaderCreationAttribs::SourceLanguage. The following are valid values for this member:
SHADER_SOURCE_LANGUAGE_DEFAULT  - The shader source format matches the underlying graphics API: HLSL for D3D11 or D3D12 mode, and GLSL for OpenGL and OpenGLES modes. SHADER_SOURCE_LANGUAGE_HLSL  - The shader source is in HLSL. For OpenGL and OpenGLES modes, the source code will be converted to GLSL. See shader converter for details. SHADER_SOURCE_LANGUAGE_GLSL  - The shader source is in GLSL. There is currently no GLSL to HLSL converter. To allow grouping of resources based on the frequency of expected change, Diligent Engine introduces classification of shader variables:
Static variables (SHADER_VARIABLE_TYPE_STATIC) are variables that are expected to be set only once. They may not be changed once a resource is bound to the variable. Such variables are intended to hold global constants such as camera attributes or global light attributes constant buffers. Mutable variables (SHADER_VARIABLE_TYPE_MUTABLE) define resources that are expected to change on a per-material frequency. Examples may include diffuse textures, normal maps etc. Dynamic variables (SHADER_VARIABLE_TYPE_DYNAMIC) are expected to change frequently and randomly. This post describes the resource binding model in Diligent Engine.
The following is an example of shader initialization:
To create a pipeline state object, define instance of PipelineStateDesc structure. The structure defines the pipeline specifics such as if the pipeline is a compute pipeline, number and format of render targets as well as depth-stencil format:
// This is a graphics pipeline PSODesc.IsComputePipeline = false; PSODesc.GraphicsPipeline.NumRenderTargets = 1; PSODesc.GraphicsPipeline.RTVFormats[0] = TEX_FORMAT_RGBA8_UNORM_SRGB; PSODesc.GraphicsPipeline.DSVFormat = TEX_FORMAT_D32_FLOAT; The structure also defines depth-stencil, rasterizer, blend state, input layout and other parameters. For instance, rasterizer state can be defined as in the code snippet below:
// Init rasterizer state RasterizerStateDesc &RasterizerDesc = PSODesc.GraphicsPipeline.RasterizerDesc; RasterizerDesc.FillMode = FILL_MODE_SOLID; RasterizerDesc.CullMode = CULL_MODE_NONE; RasterizerDesc.FrontCounterClockwise = True; RasterizerDesc.ScissorEnable = True; //RSDesc.MultisampleEnable = false; // do not allow msaa (fonts would be degraded) RasterizerDesc.AntialiasedLineEnable = False; When all fields are populated, call IRenderDevice::CreatePipelineState() to create the PSO:
Shader resource binding in Diligent Engine is based on grouping variables in 3 different groups (static, mutable and dynamic). Static variables are variables that are expected to be set only once. They may not be changed once a resource is bound to the variable. Such variables are intended to hold global constants such as camera attributes or global light attributes constant buffers. They are bound directly to the shader object:

m_pPSO->CreateShaderResourceBinding(&m_pSRB); Dynamic and mutable resources are then bound through SRB object:
m_pSRB->GetVariable(SHADER_TYPE_VERTEX, "tex2DDiffuse")->Set(pDiffuseTexSRV); m_pSRB->GetVariable(SHADER_TYPE_VERTEX, "cbRandomAttribs")->Set(pRandomAttrsCB); The difference between mutable and dynamic resources is that mutable ones can only be set once for every instance of a shader resource binding. Dynamic resources can be set multiple times. It is important to properly set the variable type as this may affect performance. Static variables are generally most efficient, followed by mutable. Dynamic variables are most expensive from performance point of view. This post explains shader resource binding in more details.
Setting the Pipeline State and Invoking Draw Command
Before any draw command can be invoked, all required vertex and index buffers as well as the pipeline state should be bound to the device context:
// Clear render target const float zero[4] = {0, 0, 0, 0}; m_pContext->ClearRenderTarget(nullptr, zero); // Set vertex and index buffers IBuffer *buffer[] = {m_pVertexBuffer}; Uint32 offsets[] = {0}; Uint32 strides[] = {sizeof(MyVertex)}; m_pContext->SetVertexBuffers(0, 1, buffer, strides, offsets, SET_VERTEX_BUFFERS_FLAG_RESET); m_pContext->SetIndexBuffer(m_pIndexBuffer, 0); m_pContext->SetPipelineState(m_pPSO); Also, all shader resources must be committed to the device context:
m_pContext->CommitShaderResources(m_pSRB, COMMIT_SHADER_RESOURCES_FLAG_TRANSITION_RESOURCES); When all required states and resources are bound, IDeviceContext::Draw() can be used to execute draw command or IDeviceContext::DispatchCompute() can be used to execute compute command. Note that for a draw command, graphics pipeline must be bound, and for dispatch command, compute pipeline must be bound. Draw() takes DrawAttribs structure as an argument. The structure members define all attributes required to perform the command (primitive topology, number of vertices or indices, if draw call is indexed or not, if draw call is instanced or not, if draw call is indirect or not, etc.). For example:
DrawAttribs attrs; attrs.IsIndexed = true; attrs.IndexType = VT_UINT16; attrs.NumIndices = 36; attrs.Topology = PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; pContext->Draw(attrs); Tutorials and Samples
The GitHub repository contains a number of tutorials and sample applications that demonstrate the API usage.

AntTweakBar sample demonstrates how to use AntTweakBar library to create simple user interface.

Atmospheric scattering sample is a more advanced example. It demonstrates how Diligent Engine can be used to implement various rendering tasks: loading textures from files, using complex shaders, rendering to textures, using compute shaders and unordered access views, etc.

The repository includes Asteroids performance benchmark based on this demo developed by Intel. It renders 50,000 unique textured asteroids and lets compare performance of D3D11 and D3D12 implementations. Every asteroid is a combination of one of 1000 unique meshes and one of 10 unique textures.

Integration with Unity
Diligent Engine supports integration with Unity through Unity low-level native plugin interface. The engine relies on Native API Interoperability to attach to the graphics API initialized by Unity. After Diligent Engine device and context are created, they can be used us usual to create resources and issue rendering commands. GhostCubePlugin shows an example how Diligent Engine can be used to render a ghost cube only visible as a reflection in a mirror.

• By Yxjmir
I'm trying to load data from a .gltf file into a struct to use to load a .bin file. I don't think there is a problem with how the vertex positions are loaded, but with the indices. This is what I get when drawing with glDrawArrays(GL_LINES, ...):

Also, using glDrawElements gives a similar result. Since it looks like its drawing triangles using the wrong vertices for each face, I'm assuming it needs an index buffer/element buffer. (I'm not sure why there is a line going through part of it, it doesn't look like it belongs to a side, re-exported it without texture coordinates checked, and its not there)
I'm using jsoncpp to load the GLTF file, its format is based on JSON. Here is the gltf struct I'm using, and how I parse the file:
glBindVertexArray(g_pGame->m_VAO);
glDrawElements(GL_LINES, g_pGame->m_indices.size(), GL_UNSIGNED_BYTE, (void*)0); // Only shows with GL_UNSIGNED_BYTE
glDrawArrays(GL_LINES, 0, g_pGame->m_vertexCount);
So, I'm asking what type should I use for the indices? it doesn't seem to be unsigned short, which is what I selected with the Khronos Group Exporter for blender. Also, am I reading part or all of the .bin file wrong?
Test.gltf
Test.bin

• That means how do I use base DirectX or OpenGL api's to make a physics based destruction simulation?
Will it be just smart rendering or something else is required?

# OpenGL question about deferred rendering

This topic is 2349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I'm trying to implement deferred rendering in a modern way using OGL 4.x core. For this goal I've read several articles pdfs, tutorials etc. about this topic, however I still have some questions.
First of all, when I create a G-buffer what do I really do?
I mean I surely want to create a FBO and an RBO and attach the RBO to the FBO so that I can render to a texture which I attach to the FBO.
But then how do I attach other stuff to the FBO?
As I've read I definitely will need at least an albedo a depth and a normal buffer. I suppose these will all be textures. However there are many formats to choose from, and it depends on the article (or rather game engine) which is used.
Which are the most common, or rather the most efficient, and nicest ones?
(to add I definitely want to go with the full HDR pipeline)
Another question is: how do I fill these buffers?
Especially when there are object in my scene which doesn't have textures or normals. And there is the cube mapping which should be completely untouched, since I only need the colors of it.
Finally, I ran into an article from Intel in which the whole shading part was solved within a compute shader. So how can I connect OpenCL and OpenGL so that I can get the same results with open source APIs?

Best regards,
Yours3!f

##### Share on other sites

Hi,

I'm trying to implement deferred rendering in a modern way using OGL 4.x core. For this goal I've read several articles pdfs, tutorials etc. about this topic, however I still have some questions.
First of all, when I create a G-buffer what do I really do?
I mean I surely want to create a FBO and an RBO and attach the RBO to the FBO so that I can render to a texture which I attach to the FBO.
But then how do I attach other stuff to the FBO?
As I've read I definitely will need at least an albedo a depth and a normal buffer. I suppose these will all be textures. However there are many formats to choose from, and it depends on the article (or rather game engine) which is used.
Which are the most common, or rather the most efficient, and nicest ones?
(to add I definitely want to go with the full HDR pipeline)
Another question is: how do I fill these buffers?
Especially when there are object in my scene which doesn't have textures or normals. And there is the cube mapping which should be completely untouched, since I only need the colors of it.
Finally, I ran into an article from Intel in which the whole shading part was solved within a compute shader. So how can I connect OpenCL and OpenGL so that I can get the same results with open source APIs?

Best regards,
Yours3!f

Well I am trying to work on the same thing and I can only attempt to try and answer one of your questions, "How do I fill these buffers?". And I believe to fill them you bind them before drawing and then your shader should output its final data to your buffer. I am not 100% sure if that is right, but I think it is.

##### Share on other sites
something like this?

 glActiveTexture(GL_TEXTURE0); texture0.bind(); glActiveTexture(GL_TEXTURE0 + 1); albedo_texture.bind(); glActiveTexture(GL_TEXTURE0 + 2); normal_texture.bind(); glActiveTexture(GL_TEXTURE0 + 3); depth_texture.bind(); glUniform1i(texture0_location, GL_TEXTURE0); glUniform1i(albedo_location, GL_TEXTURE0 + 1); glUniform1i(normal_location, GL_TEXTURE0 + 2); glUniform1i(depth_location, GL_TEXTURE0 + 3); //render stuff... (pixel shader) #version 410 uniform sampler2D texture0; uniform sampler2D albedo; uniform sampler2D normal; uniform sampler2D depth; in vec3 normals; in vec4 position; smooth in vec2 texture_coordinates; out vec4 fragment_color; void main() { depth = position.z; normal = vec4(normals, 1.0); fragment_color = albedo = texture(texture0, texture_coordinates); } 

##### Share on other sites

I've looked at the possible formats found here:
http://www.opengl.or...ki/Image_Format

I've found an nvidia HDR sample that has an FPS and a timer as well with it, which features some of these formats:

From best performance to worst:
 HDR format RT format FPS time (ms) RGB9_E5 R11F_G11F_B10F 750 1.31 R11F_G11F_B10F R11F_G11F_B10F 740 1.32 RGBA16F R11F_G11F_B10F 725 1.40 RGBA32F R11F_G11F_B10F 610 1.63 RGB9_E5 RGBA16F 523 1.91 R11F_G11F_B10F RGBA16F 523 1.91 RGBA16F RGBA16F 512 1.95 RGBA32F RGBA16F 470 2.12 RGB9_E5 RGBA32F 292 3.43 R11F_G11F_B10F RGBA32F 291 3.43 RGBA16F RGBA32F 288 3.46 RGBA32F RGBA32F 277 3.6 
These were achieved with a Core i3 540, a HD 5770 with 1GB GDDR5, and on 1440x900.

I didn't notice any visual difference only when using a RGBA32F HDR format. However this might be because of some bug.
To add when using high performance formats like RGB9_E5 some visual glitches might appear if we combine this with other effects.
Another thing to notice is that if we want to make the game go with 30 FPS then in the worst case HDR rendering took 10% of the rendering time, in the best it only took 4.36%. Using an easily implementable format like the RGBA16F + RGBA16F would be the best way in my opinion because it takes 6.5% of the rendering time which is nice, and we still have enough precision.

On the other hand I've read the deferred rendering tutorial on Catalina's XNA blog, in which several combinations are mentioned for the G-buffer, from which the best seemed to be using a RGBA16F for albedo, R16G16F (I don't know if there's such format in OGL) for normal data (or maybe RGB10_A2), and R32F for position data. This would use about 33 MB of memory and would leave 1 channel of 16 bit data free to use for other purposes such as storing material ID. I think there would be need for another G-buffer component for other stuff like specular intensity and exponent material ID etc. For this a RGBA, or RGB format could be used.

##### Share on other sites
Did you already understand how it works? Add multiple out variables to the shader, and use glBindFragDataLocation before linking shader. And when rendering, call glDrawBuffers to tell what buffers you are using.

 out vec4 depthFrag; out vec4 normalFrag; // ... whatever you want void main() { depthFrag = vec4(...); normalFrag = vec4(...); } 

 // Create shader program and attach shaders here // Bind frag data locations glBindFragDataLocation(shaderProgram, 0, "depthFrag"); glBindFragDataLocation(shaderProgram, 1, "normalFrag"); glLinkShader(shaderProgram); // And when rendering, tell what buffers we will be using GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}; glDrawBuffers(2, buffers); 

Hope this helps.

##### Share on other sites
No actually I didn't understand it until this point I just thought about it on a theoretical basis, but yeah that's what I was looking for, thank you.

##### Share on other sites
Ok, I tried to implement it, but in the final lighting stage I seem to be getting no normals or depth, and I also think that depth isn't calculated properly. I do get the albedo, which is great, but due to the lack of the other two components I can't do lighting.

So here's my initialization:
 void deferred::init() { //load the lighting shader objs::get()->shader_loader.load_shader_control_file ( "../shaders/deferred/fs_quad.sc", &fs_quad ); //load the full-screen quad objs::get()->obj.load_obj_file ( "../resources/fs_quad.obj", &quad, &fs_quad ); //set texture parameters float w = objs::get()->conf.SCREEN_WIDTH; float h = objs::get()->conf.SCREEN_HEIGHT; fbo.create(); //generate a fbo fbo.bind(); //bind it GLenum modes[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; //set which draw buffers will it use glDrawBuffers ( 3, modes ); //create render buffers rb_albedo.create(); rb_normal.create(); rb_position.create(); rb_depth.create(); //this is not a g-buffer component it is just a depth attachment //bind render buffers, set the storage format, and attach them to the fbo rb_albedo.bind(); rb_albedo.set_storage_format ( GL_RGBA16F, w, h ); rb_albedo.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, &fbo ); rb_normal.bind(); rb_normal.set_storage_format ( GL_RG16F, w, h ); rb_normal.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, &fbo ); rb_position.bind(); rb_position.set_storage_format ( GL_R32F, w, h ); rb_position.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, &fbo ); rb_depth.bind(); rb_depth.set_storage_format ( GL_DEPTH_COMPONENT32, w, h ); rb_depth.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, &fbo ); //create textures albedo.create(); normals.create(); position.create(); //set the albedo as the 5th (4) texture glActiveTexture ( GL_TEXTURE4 ); albedo.bind(); //bind it glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); //use rgba16f format glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA16F, w, h, 0, GL_RGBA, GL_FLOAT, 0 ); albedo.set_dimensions ( w, h ); //store the size of the texture for future use glActiveTexture ( GL_TEXTURE5 ); normals.bind(); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexImage2D ( GL_TEXTURE_2D, 0, GL_RG16F, w, h, 0, GL_RGBA, GL_FLOAT, 0 ); normals.set_dimensions ( w, h ); glActiveTexture ( GL_TEXTURE6 ); position.bind(); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexImage2D ( GL_TEXTURE_2D, 0, GL_R32F, w, h, 0, GL_RGBA, GL_FLOAT, 0 ); position.set_dimensions ( w, h ); //reset the active texture glActiveTexture ( GL_TEXTURE0 ); //attach textures to fbo albedo.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, &fbo ); normals.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, &fbo ); position.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, &fbo ); fbo.unbind(); } 

here's the rendering code
 void deferred::render() { //set orthographic view w, h, near, far event::get()->get_resize()->set_orthographic ( 1.0f, 1.0f, -1.0f, 1.0f ); //disable depth testing glDisable(GL_DEPTH_TEST); //bind the lighting shader fs_quad.bind(); //pass the matrices and scene information fs_quad.pass_m4x4 ( objs::get()->ppl.get_projection_matrix(), "m4_p" ); fs_quad.pass_m4x4 ( objs::get()->ppl.get_model_view_matrix(), "m4_mv" ); //reset perspective mode event::get()->get_resize()->set_perspective (); //inverse projection matrix for depth to position reconstruction fs_quad.pass_m4x4 ( objs::get()->ppl.get_projection_matrix().invert(), "inv_proj" ); //pass camera position fs_quad.pass_vec4 ( mymath::vec4f ( objs::get()->cam.pos[0], objs::get()->cam.pos[1], objs::get()->cam.pos[2], 1.0f ), "cam_pos" ); //pass the textures fs_quad.pass_int ( 4, "texture4" ); fs_quad.pass_int ( 5, "texture5" ); fs_quad.pass_int ( 6, "texture6" ); //draw full screen quad quad.render(); //unbind the lighting shader fs_quad.unbind(); //enable depth testing glEnable(GL_DEPTH_TEST); } 

the shader that fills the G-buffer
 //vertex shader #version 410 //projection, modelview matrices uniform mat4 m4_p, m4_mv; uniform mat3 m3_n; //the vertex position in vec4 v4_vertex; //the texture coordinates in vec2 v2_texture; in vec3 v3_normal; smooth out vec2 v2_texture_coords; out vec2 normal; out float depth; vec2 encode_normal_x_y_reconstruct_z(vec3 in_normal) { return vec2(in_normal.xy * 0.5 + 0.5); } void main() { normal = encode_normal_x_y_reconstruct_z(m3_n * v3_normal); v2_texture_coords = v2_texture; gl_Position = m4_p * m4_mv * v4_vertex; depth = gl_Position.z / gl_Position.w; } //////////////////////////////////////////------------------------------------------------------------- //pixel shader #version 410 uniform sampler2D texture0; smooth in vec2 v2_texture_coords; in vec2 normal; in float depth; out vec4 v4_color; //color attachment0 out vec4 v4_normal; //c. a. 1 out vec4 v4_depth; //c. a. 2 void main() { v4_normal.xy = normal; v4_depth.x = depth; v4_color = texture(texture0, v2_texture_coords); } 

Using this shader the normals here seem to be good when I draw them: v4_color = vec4(v2_normal, 0.0, 1.0);
however when I draw the depth the mesh get all black, instead of that grayscale depth look. v4_color = vec4(vec3(depth), 1.0);
I also use the glBindFragDataLocation before linking as you suggested, although I don't know if it was bound

 //vertex shader #version 410 //projection, modelview matrices uniform mat4 m4_p, m4_mv; //the vertex position in vec4 v4_vertex; //the texture coordinates in vec2 v2_texture; smooth out vec2 v2_texture_coords; void main() { v2_texture_coords = v2_texture; gl_Position = m4_p * m4_mv * v4_vertex; } ///////////////////////////////////--------------------------------------------------------------------------- //pixel shader #version 410 uniform sampler2D texture4; //albedo RGBA16F uniform sampler2D texture5; //normal RG16F uniform sampler2D texture6; //depth R32F smooth in vec2 v2_texture_coords; //texture coordinates for the G-buffer uniform mat4 inv_proj; //inverse projection matrix uniform vec4 cam_pos; //camera position out vec4 v4_color; //the outgoing color vec3 decode_normal_x_y_reconstruct_z(vec2 in_normal) { vec3 out_normal; out_normal.xy = in_normal * 2 - 1; //convert from range [0.0, 1.0] to [-1.0, 1.0] out_normal.z = sqrt(1 - dot(out_normal.xy, out_normal.xy)); //since it is perpendicular to x and y we can calculate it return out_normal; } void main() { vec4 albedo = texture(texture4, v2_texture_coords); vec3 normal = decode_normal_x_y_reconstruct_z(texture(texture5, v2_texture_coords).xy); float depth = texture(texture6, v2_texture_coords).x; //get texture coords from [0, 1] to [-1, 1], add them as x and y, add depth as z, and multiply by inverse projection matrix vec4 position = vec4(v2_texture_coords.x * 2.0 - 1.0, -(v2_texture_coords.y * 2.0 - 1.0), depth, 1.0) * inv_proj; position /= position.w; //blinn lighting with a light placed at [0, 5, -2] vec3 light = vec3( 0.0, 5.0, -2.0); vec3 light_dir = normalize(light - position.xyz); vec3 eye_dir = normalize(cam_pos.xyz - position.xyz); vec3 half_vec = normalize(light_dir + eye_dir); v4_color = max(dot(normal, light_dir), 0.0) * albedo + pow(max(dot(normal, half_vec), 0.0), 9.0) * 10.0; } 

When I try to draw the normals with v4_color = vec4(normal, 1.0); I get a black screen, this also happens with depth. Please help I'm struggling to get this working.

Best regards,
Yours3!f

##### Share on other sites
I think you have to call glDrawBuffers every time you want to render something on the buffers. It's probably using only the first buffer now (which is albedo), because you don't tell it what to use when rendering.

##### Share on other sites

I think you have to call glDrawBuffers every time you want to render something on the buffers. It's probably using only the first buffer now (which is albedo), because you don't tell it what to use when rendering.

Thanks, but you don't have to do this every frame. I know on Codinglabs in the tutorial it is done every frame, but you simply don't have to, because it only sets up the frame buffer object, so that it actually recieves the color input.

I updated the code (see the post above), and now I do get the color information, depth and normals as well.

Now I suppose the lighting equation or the decoding function is wrong, since I have all the data.

##### Share on other sites
ok so I tried to figure out why my ligthing doesn't work, so I tried some things.

first of all if in the lighting shader I gather the normal and depth and convert them the conversion works.
 void main() { vec3 normal = decode_normal_x_y_reconstruct_z(texture(texture5, v2_texture_coords).xy); float depth = texture(texture6, v2_texture_coords).x; //get texture coords from [0, 1] to [-1, 1], add them as x and y, add depth as z, and multiply by inverse projection matrix vec4 position = vec4(v2_texture_coords.x * 2.0 - 1.0, -(v2_texture_coords.y * 2.0 - 1.0), depth, 1.0) * inv_proj; position /= position.w; v4_color = vec4(normal, 1.0); //gives me a normal looking scene //v4_color = vec4(vec3(depth), 1.0); //gives me a depth looking scene //v4_color = position; //gives me a position scene (well it is red green and blue ) } 

However when I do this I get a black screen:
 void main() { v4_color = texture(texture4, v2_texture_coords); vec3 normal = decode_normal_x_y_reconstruct_z(texture(texture5, v2_texture_coords).xy); v4_color = vec4(normal, 1.0); //this should give me a normal looking scene } 

I have no idea why that happens, and I guess this is why my lighting doesn't work too...