• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By tj8146
      I have attached my project in a .zip file if you wish to run it for yourself.
      I am making a simple 2d top-down game and I am trying to run my code to see if my window creation is working and to see if my timer is also working with it. Every time I run it though I get errors. And when I fix those errors, more come, then the same errors keep appearing. I end up just going round in circles.  Is there anyone who could help with this? 
       
      Errors when I build my code:
      1>Renderer.cpp 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2039: 'string': is not a member of 'std' 1>c:\program files (x86)\windows kits\10\include\10.0.16299.0\ucrt\stddef.h(18): note: see declaration of 'std' 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2061: syntax error: identifier 'string' 1>c:\users\documents\opengl\game\game\renderer.cpp(28): error C2511: 'bool Game::Rendering::initialize(int,int,bool,std::string)': overloaded member function not found in 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.h(9): note: see declaration of 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.cpp(35): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(36): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(43): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>Done building project "Game.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========  
       
      Renderer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include "Renderer.h" #include "Timer.h" #include <iostream> namespace Game { GLFWwindow* window; /* Initialize the library */ Rendering::Rendering() { mClock = new Clock; } Rendering::~Rendering() { shutdown(); } bool Rendering::initialize(uint width, uint height, bool fullscreen, std::string window_title) { if (!glfwInit()) { return -1; } /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } /* Make the window's context current */ glfwMakeContextCurrent(window); glViewport(0, 0, (GLsizei)width, (GLsizei)height); glOrtho(0, (GLsizei)width, (GLsizei)height, 0, 1, -1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glfwSwapInterval(1); glEnable(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_TEXTURE_2D); glLoadIdentity(); return true; } bool Rendering::render() { /* Loop until the user closes the window */ if (!glfwWindowShouldClose(window)) return false; /* Render here */ mClock->reset(); glfwPollEvents(); if (mClock->step()) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwSwapBuffers(window); mClock->update(); } return true; } void Rendering::shutdown() { glfwDestroyWindow(window); glfwTerminate(); } GLFWwindow* Rendering::getCurrentWindow() { return window; } } Renderer.h
      #pragma once namespace Game { class Clock; class Rendering { public: Rendering(); ~Rendering(); bool initialize(uint width, uint height, bool fullscreen, std::string window_title = "Rendering window"); void shutdown(); bool render(); GLFWwindow* getCurrentWindow(); private: GLFWwindow * window; Clock* mClock; }; } Timer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include <time.h> #include "Timer.h" namespace Game { Clock::Clock() : mTicksPerSecond(50), mSkipTics(1000 / mTicksPerSecond), mMaxFrameSkip(10), mLoops(0) { mLastTick = tick(); } Clock::~Clock() { } bool Clock::step() { if (tick() > mLastTick && mLoops < mMaxFrameSkip) return true; return false; } void Clock::reset() { mLoops = 0; } void Clock::update() { mLastTick += mSkipTics; mLoops++; } clock_t Clock::tick() { return clock(); } } TImer.h
      #pragma once #include "Common.h" namespace Game { class Clock { public: Clock(); ~Clock(); void update(); bool step(); void reset(); clock_t tick(); private: uint mTicksPerSecond; ufloat mSkipTics; uint mMaxFrameSkip; uint mLoops; uint mLastTick; }; } Common.h
      #pragma once #include <cstdio> #include <cstdlib> #include <ctime> #include <cstring> #include <cmath> #include <iostream> namespace Game { typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; typedef unsigned long ulong; typedef float ufloat; }  
      Game.zip
    • By lxjk
      Hi guys,
      There are many ways to do light culling in tile-based shading. I've been playing with this idea for a while, and just want to throw it out there.
      Because tile frustums are general small compared to light radius, I tried using cone test to reduce false positives introduced by commonly used sphere-frustum test.
      On top of that, I use distance to camera rather than depth for near/far test (aka. sliced by spheres).
      This method can be naturally extended to clustered light culling as well.
      The following image shows the general ideas

       
      Performance-wise I get around 15% improvement over sphere-frustum test. You can also see how a single light performs as the following: from left to right (1) standard rendering of a point light; then tiles passed the test of (2) sphere-frustum test; (3) cone test; (4) spherical-sliced cone test
       

       
      I put the details in my blog post (https://lxjk.github.io/2018/03/25/Improve-Tile-based-Light-Culling-with-Spherical-sliced-Cone.html), GLSL source code included!
       
      Eric
    • By Fadey Duh
      Good evening everyone!

      I was wondering if there is something equivalent of  GL_NV_blend_equation_advanced for AMD?
      Basically I'm trying to find more compatible version of it.

      Thank you!
    • By Jens Eckervogt
      Hello guys, 
       
      Please tell me! 
      How do I know? Why does wavefront not show for me?
      I already checked I have non errors yet.
      using OpenTK; using System.Collections.Generic; using System.IO; using System.Text; namespace Tutorial_08.net.sourceskyboxer { public class WaveFrontLoader { private static List<Vector3> inPositions; private static List<Vector2> inTexcoords; private static List<Vector3> inNormals; private static List<float> positions; private static List<float> texcoords; private static List<int> indices; public static RawModel LoadObjModel(string filename, Loader loader) { inPositions = new List<Vector3>(); inTexcoords = new List<Vector2>(); inNormals = new List<Vector3>(); positions = new List<float>(); texcoords = new List<float>(); indices = new List<int>(); int nextIdx = 0; using (var reader = new StreamReader(File.Open("Contents/" + filename + ".obj", FileMode.Open), Encoding.UTF8)) { string line = reader.ReadLine(); int i = reader.Read(); while (true) { string[] currentLine = line.Split(); if (currentLine[0] == "v") { Vector3 pos = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inPositions.Add(pos); if (currentLine[1] == "t") { Vector2 tex = new Vector2(float.Parse(currentLine[1]), float.Parse(currentLine[2])); inTexcoords.Add(tex); } if (currentLine[1] == "n") { Vector3 nom = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inNormals.Add(nom); } } if (currentLine[0] == "f") { Vector3 pos = inPositions[0]; positions.Add(pos.X); positions.Add(pos.Y); positions.Add(pos.Z); Vector2 tc = inTexcoords[0]; texcoords.Add(tc.X); texcoords.Add(tc.Y); indices.Add(nextIdx); ++nextIdx; } reader.Close(); return loader.loadToVAO(positions.ToArray(), texcoords.ToArray(), indices.ToArray()); } } } } } And It have tried other method but it can't show for me.  I am mad now. Because any OpenTK developers won't help me.
      Please help me how do I fix.

      And my download (mega.nz) should it is original but I tried no success...
      - Add blend source and png file here I have tried tried,.....  
       
      PS: Why is our community not active? I wait very longer. Stop to lie me!
      Thanks !
    • By codelyoko373
      I wasn't sure if this would be the right place for a topic like this so sorry if it isn't.
      I'm currently working on a project for Uni using FreeGLUT to make a simple solar system simulation. I've got to the point where I've implemented all the planets and have used a Scene Graph to link them all together. The issue I'm having with now though is basically the planets and moons orbit correctly at their own orbit speeds.
      I'm not really experienced with using matrices for stuff like this so It's likely why I can't figure out how exactly to get it working. This is where I'm applying the transformation matrices, as well as pushing and popping them. This is within the Render function that every planet including the sun and moons will have and run.
      if (tag != "Sun") { glRotatef(orbitAngle, orbitRotation.X, orbitRotation.Y, orbitRotation.Z); } glPushMatrix(); glTranslatef(position.X, position.Y, position.Z); glRotatef(rotationAngle, rotation.X, rotation.Y, rotation.Z); glScalef(scale.X, scale.Y, scale.Z); glDrawElements(GL_TRIANGLES, mesh->indiceCount, GL_UNSIGNED_SHORT, mesh->indices); if (tag != "Sun") { glPopMatrix(); } The "If(tag != "Sun")" parts are my attempts are getting the planets to orbit correctly though it likely isn't the way I'm meant to be doing it. So I was wondering if someone would be able to help me? As I really don't have an idea on what I would do to get it working. Using the if statement is truthfully the closest I've got to it working but there are still weird effects like the planets orbiting faster then they should depending on the number of planets actually be updated/rendered.
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Material blending in a deferred renderer

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm about to start writing a scene manager and renderer for a game and I'm going to be using deferred shading. My scenes are going to be populated by lots of objects so I'll be using instancing to cut down on draw calls. I also want to try and defer material and texture sampling by having my objects output a material ID into a single-channel FB. Some kind of mega-shader would then convert the material ID into a diffuse and normal sample (probably by having a texture store all material attributes and using the material ID as a v coordinate, and by using texture arrays to get access to different textures).

 

This raises an issue with material blending. My scene will also feature a terrain at all times (other than when the player is looking up) and I want the textures on my terrain to blend from one to another across a tile. Of course, since the mapping between a pixel and material ID is 1-1, there's no way to represent blending between multiple materials using the setup described above. The way I see it, I have 2 options:

 

  1. Instead of using a single material ID per pixel, use multiple material IDs and have a second output which would be the weights of the materials to blend with in the mega-shader.
  2. Draw the terrain seperately straight into the GBuffer after the mega-shader has been run on the scene, using the depth-buffer from the material ID FB. Texture sampling would occur normally here. Lights would have to be rendered in a seperate pass from the mega-shader.

Both options have pros and cons. With option 1, the advantages of drawing into a relatively small FB are lost (now we have 2 textures to render into with weights) and you can only fit up to 4 materials to blend at once (though this might be enough for terrain blending?). Additionally, the uber-shader now has to make up to 4 samples into the material texture and corresponding diffuse/normal textures and blend everything together, potentially taking 4x as long to draw. I'm not sure if it's possible to optimise this somehow such that only 1 sample is made if only 1 material is used with full weight.

 

With option 2, the uber-shader remains relatively light-weight but there's an extra pass involved and there's probably some caveat of drawing everything in a strange order. Also benefits of the deferred rendering in this case are lost for the terrain (which will have a very large surface area and is also likely to be occluded by many polygons (trees, grass etc) and so would probably benefit from deferred rendering the most).

 

My question is are there any other options I haven't considered? There is surpsingly a lack of documentation and literature on this matter floating around. If there are no other better options, which option do you think is best for me to choose? Alternatively, is it worth me using deferred rendering at all and should I spend my time making a more intelligent batching system/render queue and try and squeeze more frames out of there?

 

If it makes any difference I'll be using OpenGL core profile 3.3. My host language is C# / OpenTK.

Share this post


Link to post
Share on other sites
Advertisement

I'm curious - what's the motivation for doing the material buffer and the second pass expansion of shading attributes? Deferred shading is already somewhat limited in what kind of material variation you can create - but having interesting materials that control what gets painted into the gbuffer can mitigate that. Your design is going to severely limit what you can do - and you're just hitting the first example of that. Are you worried about performance, or is there some other goal here?

Share this post


Link to post
Share on other sites

Writing out material IDs in a first pass and then creating a G-buffer from that in a second pass then doing lighting in a third pass sounds like an overly complicated solution to a problem you don't even know you have (the problem of doing too many texture samples). How do you know writing out an entire frame buffer of material IDs is going to be faster than doing a small number of unnecessary texture samples?

 

I would ditch that idea and just use a regular deferred renderer. Write out the G-buffer in the first pass, and do lighting in the second pass. Using whatever materials your objects have during the G-buffer pass. This will let you blend the diffuse color of your terrain in the G-buffer pass just by using the correct terrain shader. No hassle. If you're concerned about wasting time drawing terrain pixels that eventually get occluded, then simply draw the terrain last. The G-buffer pass should render everything front to back, anyway, to mitigate overdraw.

 

Also, typically if one were to write out a material ID at any point in the deferred pipeline, it would be written directly into the G-buffer, and it would be used to select a set of shading parameters (specular power, surface roughness, etc etc) for the lighting pass.

Share this post


Link to post
Share on other sites

Performance is my largest concern. Rendering an object with 5 materials would otherwise cost 5 draw calls (1 for each material). Using material IDs, that's only 1 draw call (a material ID could be assigned per-vertex).

 

I suppose I'm probably over-thinking things and inventing unnecessary solutions to problems which haven't surfaced yet, and may never surface. The biggest thing that will help me is instancing (since I'm likely to have many trees on-screen at any given point), so in theory each material is only going to cost 1 draw call anyway if my render queue batches things intelligently enough. I don't think I can control the order in which objects are drawn via instancing though.

Share this post


Link to post
Share on other sites

I feel like you would need very uniform materials and/or a unique virtual texturing system for your approach to work.

You may also want to keep in mind that it's very common in games to render dynamic "decals" into your G-Buffer to handle things like scorch marks and bullet holes, and this also wouldn't play nicely with your material ID system.

Share this post


Link to post
Share on other sites


Performance is my largest concern. Rendering an object with 5 materials would otherwise cost 5 draw calls (1 for each material). Using material IDs, that's only 1 draw call (a material ID could be assigned per-vertex).

 

If performance is your largest concern I would stay away from deferred rendering altogether, and instead use forward rendering. I've used both in our game and although deferred allows you to do more fancy stuff it comes with a cost.

Share this post


Link to post
Share on other sites

Performance is my largest concern. Rendering an object with 5 materials would otherwise cost 5 draw calls (1 for each material). Using material IDs, that's only 1 draw call (a material ID could be assigned per-vertex).

 

You may disable writing to z-buffer for the other passes than the first smile.png Also, you may find a way to combine the 5 materials into one.

 

Otherwise you were talking about a terrain rendering, in BF3 they used a terrain tile cache. Instead of blending 5 materials in the g-buffer, you may render the terrain materials into a tile texture and "flatten" the materials, then in the G-buffer pass you can draw pretty complex terrains with very simple shader since the materials have been blended already. So there is no need to overcomplicate your deferred renderer. 

 

Of course, there are limitations, such as tile texture detail, but this can be fixed to certain degree with detail texturing and volume decals etc. Also, the amount of tiles will eat up some GPU memory. 

 

Cheers!

Edited by kauna

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement