• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By fleissi
      Hey guys!

      I'm new here and I recently started developing my own rendering engine. It's open source, based on OpenGL/DirectX and C++.
      The full source code is hosted on github:
      https://github.com/fleissna/flyEngine

      I would appreciate if people with experience in game development / engine desgin could take a look at my source code. I'm looking for honest, constructive criticism on how to improve the engine.
      I'm currently writing my master's thesis in computer science and in the recent year I've gone through all the basics about graphics programming, learned DirectX and OpenGL, read some articles on Nvidia GPU Gems, read books and integrated some of this stuff step by step into the engine.

      I know about the basics, but I feel like there is some missing link that I didn't get yet to merge all those little pieces together.

      Features I have so far:
      - Dynamic shader generation based on material properties
      - Dynamic sorting of meshes to be renderd based on shader and material
      - Rendering large amounts of static meshes
      - Hierarchical culling (detail + view frustum)
      - Limited support for dynamic (i.e. moving) meshes
      - Normal, Parallax and Relief Mapping implementations
      - Wind animations based on vertex displacement
      - A very basic integration of the Bullet physics engine
      - Procedural Grass generation
      - Some post processing effects (Depth of Field, Light Volumes, Screen Space Reflections, God Rays)
      - Caching mechanisms for textures, shaders, materials and meshes

      Features I would like to have:
      - Global illumination methods
      - Scalable physics
      - Occlusion culling
      - A nice procedural terrain generator
      - Scripting
      - Level Editing
      - Sound system
      - Optimization techniques

      Books I have so far:
      - Real-Time Rendering Third Edition
      - 3D Game Programming with DirectX 11
      - Vulkan Cookbook (not started yet)

      I hope you guys can take a look at my source code and if you're really motivated, feel free to contribute :-)
      There are some videos on youtube that demonstrate some of the features:
      Procedural grass on the GPU
      Procedural Terrain Engine
      Quadtree detail and view frustum culling

      The long term goal is to turn this into a commercial game engine. I'm aware that this is a very ambitious goal, but I'm sure it's possible if you work hard for it.

      Bye,

      Phil
    • 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 !
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL How to draw visible shadow volumes, but through transparent polygons too

This topic is 1387 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

Disclaimer: this is older OpenGL, and while I would love to switch to all shaders (not sure if that would be appropriate here even), I'd rather complete what I have instead of switching the whole engine over at this point. It is an older project, but I'd still like to see it wrapped up in its current form.

 

oCF6mDB.png

 

My project is a 3d platformer, you have cubes jumping around on other cubes and they use shadow volumes to show where they are in relation to the ground. Recently I added crumbling blocks, the kind that disappear a few seconds after you touch them. That's great! I have them set so their alpha is reduced based on how long it's been since you've touched them so they fade away after a few seconds. The problem is: even at alpha zero, they're affecting the depth test for my stencil buffer.

 

The white/grey blocks are the crumbling ones:

 

ZEujodg.png

 

(Don't mind the missing face, it's part of an efficiency measure to remove unused faces that doesn't consider disappearing blocks yet)

 

The problem you see here is that the shadows, while drawn fine on the left and right, are not appearing in the middle behind the player. That's because there are crumbling blocks there, though they have 0% opacity/alpha. You can even see the outline of the cube just above and to the right of the player because that's the shape cut out of the shadows.

 

Also: the player automatically gets a silhouette when behind another object, same stencil style test as the shadows, so that's why the player is mostly yellow and shadowed yellow but then turns green at the top. It's explained here.

 

I don't want to leave anything out I might be missing, so here's the whole code chunk (you can see my comments trying to remember what I wrote... I've gotten better at this since 2012... ):

// Give shadows to everything!
void drawAllShadows(int player) {
    // Blend function not causing any differences if enabled/disabled?
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  
    // GL_BLEND is the difference between black shadows and dark versions of the colors underneath
    glEnable( GL_BLEND );
    // Not sure GL_ALPHA is doing anything, nor ALPHA_TEST
    glEnable( GL_ALPHA );
    glEnable( GL_ALPHA_TEST );
 
    // Color Mask with everything disabled means it doesn't use this data to draw real shapes/objects
    // instead, we're going to use it for a stencil test later
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  
    // No Depth Test lets shadows overlap instead of their not-drawn sections deleting shadows behind them
    glDepthMask(GL_FALSE);
  
    // GL_STENCIL_TEST limits the shadows to being drawn in the stencils, which are the levelShadows
    glEnable(GL_STENCIL_TEST);
  
    // Just draw the level shadows as regular polygons, but with the stencil option
    if (levelShadows) {
    
      glEnableClientState(GL_COLOR_ARRAY);
      glEnableClientState(GL_VERTEX_ARRAY);
      
      // specify pointer to vertex array
      glColorPointer(3, GL_FLOAT, 0, shadowColors);
      glVertexPointer(3, GL_FLOAT, 0, shadowVertices);
      
      glPushMatrix();
      //glScalef(0.99,100.0,0.99);
      //glTranslatef(0.0,-50.505,0.0);
      
      glCullFace(GL_FRONT);
      glStencilFunc(GL_ALWAYS, 0x0, 0xff);
      // if depth fails (can't see only due to z) then increment pixel's value
      glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
      glDrawElements(GL_TRIANGLES, 36*shadowsVisible, GL_UNSIGNED_INT, shadowIndices);

      glCullFace(GL_BACK);
      glStencilFunc(GL_ALWAYS, 0x0, 0xff);
      // if depth also fails for back, decrement it so it stays at zero
      // but if one increments and the other does not...
      // (we'll come back to that later when we see where to place shadows)
      glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
      glDrawElements(GL_TRIANGLES, 36*shadowsVisible, GL_UNSIGNED_INT, shadowIndices);
      
      glPopMatrix();

      // deactivate vertex arrays after drawing
      glDisableClientState(GL_VERTEX_ARRAY);
      glDisableClientState(GL_COLOR_ARRAY);
    
    }

    /*
    for (int i=0; i<cubeNum; i++) {
      // cubeWithinPlayerRange demonstrates noticeable improvements,
      // castle level with 4player, sfx and music went from 7fps to 12.
      // now goes from 24 to 35fps! Wow!
      if (cubeWithinPlayerRange(i,player)) {
        drawCubeShadow(i); 
      }
    }*/

    for (int i=0; i<cubiorNum; i++) { drawPlayerShadow(i); }
    drawGoalShadow();
    drawItemShadows();
    
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glDepthMask(GL_TRUE);
  
    // Where the front and the back are not the same (so front face could be seen but back was obscured)
    // that is where a shadow is on the ground
    // or that is what I believe NOTEQUAL and REPLACE are being used for
    // all three replaces means no matter what (fail/zfail/succeed), we will the pixel with the new color
    glStencilFunc(GL_NOTEQUAL, 0x0, 0xff);
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
    fillScreenWithShadow();
    glDisable(GL_STENCIL_TEST);

    glDisable( GL_BLEND );
    // end of shadow stuff
}

// 100% copied from draw_shadow for testing purposes
// full permission to copy this though, according to Josh Beam:
// http://joshbeam.com/articles/stenciled_shadow_volumes_in_opengl/
void fillScreenWithShadow() {
    // Fills the stencilled area with a shadow of 50% opacity on top of everything else
    glPushMatrix();
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0, 1, 1, 0, 0, 1);
    glDisable(GL_DEPTH_TEST);

    glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
    glBegin(GL_QUADS);
        glVertex2i(0, 0);
        glVertex2i(0, 1);
        glVertex2i(1, 1);
        glVertex2i(1, 0);
    glEnd();

    glEnable(GL_DEPTH_TEST);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
}

The function drawItemShadows is the exact one for the crumbling block shadows, but it works the same way as what you see here already. It increments the stencil's pixel value for front faces and decrements it for back faces, so it should be zero if both faces are visible or both are obscured, but if only one is then that's where a shadow lands. But again, the problem is that the z test there isn't taking into account transparent polygons. Before this code is where I draw the items and world itself (shadows are drawn last) and those are drawn with 3 point polygon indexes and 4 point color indexes, 4th slot being alpha.

 

So how do I skip alpha in my depth tests, but then apply that alpha to the shadow I draw ultimately? Is that possible, or do I need to break this up into separate passes somehow?

Edited by BlinksTale

Share this post


Link to post
Share on other sites
Advertisement

Any chance you can sidestep the problem by having your disintegrating blocks flash on and off before disappearing instead of fading out?

 

Or maybe they can be broken up into lots of tiny voxels that disappear one at a time, or they could scale down to nothing?

 

Basically, shadow mapping with semi-transparent objects is a hard problem in general. I suppose you might have some luck drawing the shadows of the semi transparent objects separately, but it might be tricky to get them to appear on the player.

Share this post


Link to post
Share on other sites


The problem is: even at alpha zero, they're affecting the depth test for my stencil buffer.

Transparent objects shouldn't render depth. Won't the problem go away if you disable depth writes for the crumbling blocks?

Share this post


Link to post
Share on other sites

 


The problem is: even at alpha zero, they're affecting the depth test for my stencil buffer.

Transparent objects shouldn't render depth. Won't the problem go away if you disable depth writes for the crumbling blocks?

 

 

This is a lot better, but doesn't solve everything:

 

M9hKlnq.png

 

So, no depth test on crumbling blocks = awesome shadows passing through!

 

Heck, they go so far through it's like the crumbling blocks aren't even there!

 

...ah shoot, it's like the crumbling blocks aren't even there. Now there are no shadows on top. As you can see, I've added some crumbling blocks above the other crumbling blocks. With the old way, the shadow recognizes the blocks and lands on it.

 

54wPw55.png

 

But the new way makes that shadow invisible.

 

So what can I do to address that? Multiple passes won't work because if it goes...

 

noDepth crumblingBlocks

allShadows

depth crumblingBlocks

allShadows

 

...well, for one: it does this funky shadow thing:

 

SwZBSF5.png

 

That's because the shadow is applied twice to everything at the bottom (I could probably clean that up by finding the difference of the two stencils though and not drawing anything twice) but two:

 

HIDEJ6b.png

 

If I look through transparent crumbling blocks at the shadow of other blocks on TOP of crumbling blocks... nothing appears. I would have to do one of these passes for every single vertical layer of my game world. :|

 

So more dilemma! I want both to use the blocks with depth test, so I can draw shadows on top of them, but also without so I can draw shadows beneath them. Ah... any tips on this?

Share this post


Link to post
Share on other sites


So more dilemma! I want both to use the blocks with depth test, so I can draw shadows on top of them, but also without so I can draw shadows beneath them. Ah... any tips on this?

Yeah, that isn't going to work. You can't have transparent surfaces both casting and receiving shadows with less than one pass per layer.

Share this post


Link to post
Share on other sites

Alright, final product is a combination of the partial solution and a workaround. When you touch a block, it immediately switches to 50% opacity and disables itself as a shadow surface, but also doesn't render before shadows again until after it's back at 100%. The shadows disappear when the crumbling blocks are touched then, but appear as soon as they're whole again, and all transparencies look good.

 

So it's about 80% of what I hoped for, and it looks good. :) Thanks guys!

Share this post


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

  • Advertisement