Jump to content
  • Advertisement

Tonyx97

Member
  • Content Count

    15
  • Joined

  • Last visited

Community Reputation

218 Neutral

About Tonyx97

  • Rank
    Member

Personal Information

  • Interests
    Programming
  1. Hey guys, I'm using OpenAL for long time ago and I realized if you apply an effect to a audio source and then you apply another one to another audio source then the effect of both will change to the latest one, is this normal? I think it's a pretty stupid question to be honest but after some search through internet I couldn't get a clear answer. As far as I know, audio effects are handled by current listener zone (I'm not sure about this). Can anyone confirm that statement is right? Thanks in advance.
  2. Okay I'm understanding this, I just want to develop a good way to avoid this problem. What do you think about this. It's a class example to use tasks etc. I've been testing some stuff in my project and I realized something. No matter what, the character will move lagged if I put the function to change the velocity in several parts of each threads. I tried putting setvelocity in those 3 threads (before and after everything) and still is lagged when there are a lot of entities. Even it flicks when there are no entities but with less rate. Maybe the friction idk.
  3. Thanks both for answering. I'll investigate further about condvars etc. Adding a C in front of a class is pure esthetic I like it and I won't have any problems of naming in other places and it doesn't conflict with other libraries so it's fine, I don't know why would it be harder to read a class name since it has the class keyword in front of it and it's just a simple C, if you tell me that underscore bar it's bad naming I can agree since it can conflict with other headers and libs. Anyways, The renderer thread needs to wait to the GPU job including scripting and physics afaik. I was curious and opened Unity and I checked that the FPS time was depending on the CPU time, for example it shows: CPU: main 15 ms | renderer thread: 0.2 ms and then the FPS was 1000 / 15 so I guess the CPU time goes first and then the renderer. You mentioned tasks, I guess this is a kind of queue that is called with priorities each frame before rendering, isn't it? Also I'm not really locking threads now, I stopped using that class (the class it's not mine, I just pasted it to test it). I used an atomic bool between renderer, physics and scripting thread but it's not enough. I think I have to implement some kind of task manager...
  4. Hello guys, I reached the point where I care about the sync between threads because I encountered a big problem long time ago but now it turned to be really annoying. My project has 4 thread which 1 of them doesn't need sync (audio one). The other threads are: Scripting (LUA), Physics (Bullet 2.83.7) and Rendering thread. I tried to add some ghetto bools to test if they sync better, it works but I don't want to do this since it's not good and also it doesn't work properly when the physics thread has a lot of entities to process. You can see the main problem here. As you can see when there are no entities the character goes decently smooth but when there are tons of entities it's so lagged and annoying. There are examples for multithreading in Bullet but I don't know if that's enough to cover my problem since scripting thread also change entities position, velocity etc. I tried using the following simple code to sync threads but I think it's not enough nor good practice of mutex concept.  class CBarrier { public: CBarrier(const CBarrier&) = delete; CBarrier& operator=(const CBarrier&) = delete; explicit CBarrier() : m_continue(true), m_gen(0) {} void wait_continue() { unsigned int gen = m_gen.load(); m_continue = false; while (m_gen == gen && !m_continue) { std::this_thread::yield(); } } void set_continue() { unsigned int gen = m_gen.load(); if (m_gen.compare_exchange_weak(gen, gen + 1)) { m_continue = true; } } private: std::atomic<bool> m_continue; std::atomic<unsigned int> m_gen; }; This class has a very simple use so you can guess how it works. I heard rendering thread should wait until physics thread is done (like Unity does). I need some tips about how to sync everything properly, like what I should use, winapi critical sections, mutexes, atomic, a mix of some of them...? I'm a bit confused now. Someone maybe knows about this and he/she can recommend me what I should use to achieve my goal. Maybe some kind of organization for threads, the correct flow of the game etc. Thanks for reading :)   Edit: I've been reading about bullet physics timestep and fixed timestep, maybe could it be the problem? Also entities that are processed from the physics thread directly without changing anything from them (only gravity and collisions) are smooth but since I move my character from the scripting thread the problem appears. Should I really wait scripting thread before rendering...?
  5. Tonyx97

    Shadows problems

    You're right I added some bias to remove the acne and now it works, do you know any article or website to fix the peter-panning without having to render the front faces? I'm modelling something and it doesn't have front faces due to remove useless geometry, the model is this and from this is from other perspective. At the moment I have no idea if this is possible to achieve but many games do that, for example GTA5 has a terrain with no front faces and the shadows of the hills are perfect, maybe this is more complex but I want to at least try or read how it's achieved.
  6. Tonyx97

    Shadows problems

    Staying in topic, after I finished directional light I started with spot light since they are easy because they don't need a cube map like point lights. I've been programming the shadows and FBO's for it but I encounter a curious problem projection the shadow, In this pic you can see that there are some black lines but the real shadow of the cube it's properly projected, I need a way to remove those lines somehow, maybe you WiredCat know about this issue. The shadow calculation function: float ShadowCalculation() { vec3 projCoords = ShadowCoord.xyz / ShadowCoord.w; vec2 texelSize = 1.0 / textureSize(shadowMap, 0); projCoords = projCoords * 0.5 + 0.5; float closestDepth = texture(shadowMap, projCoords.xy).r; float currentDepth = projCoords.z; float shadow = 1.0; for (int x = -1; x <= 4; x++) for (int y = -1; y <= 4; y++) shadow += currentDepth > (texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r) ? 1.0 : 0.0; shadow *= 0.01; shadow = currentDepth > closestDepth ? shadow : 0.0; if (dot(normalize(vWorldPos - LightPos), normalize(vNormal)) >= 0.0) { BackFaceShadow = true; return 0.0; } if (projCoords.z <= 0.0 || projCoords.z >= 1.0 || projCoords.x >= 1.0 || projCoords.y >= 1.0 || projCoords.x <= 0.0 || projCoords.y <= 0.0) return 0.0; BackFaceShadow = false; return shadow * 0.8; } I added the  if (projCoords.z <= 0.0 || projCoords.z >= 1.0 || projCoords.x >= 1.0 || projCoords.y >= 1.0 || projCoords.x <= 0.0 || projCoords.y <= 0.0) to remove the shadow outside of the perpective projection by the spot light. And where I call it: if (bShadowsOn == 1) { ShadowFactor = 1.0 - ShadowCalculation(); if (BackFaceShadow) FinalColor *= vec4(vec3(length(cross(normalize(vWorldPos - LightPos), vNormal))), 1.0); else FinalColor *= vec4(vec3(ShadowFactor), 1.0); } I also added the line when BackFaceShadow it's true to make progressive color changing to avoid faces change directly from shadow to light.
  7. Tonyx97

    Shadows problems

    Thanks WiredCat! I've been the whole day working on this and struggling with the shaders and finally got it working taking into account your notes. My fear now is that maybe in the future when I use more complex models the bias I use will fuck up everything in shadows, I set the bias as 0.00005 which is pretty low but it's the one working for all models at the moment. Here's the result:
  8. Tonyx97

    Shadows problems

    Thanks for the reply WiredCat! It's directional light as you can see in the tutorial and also in my code where I transform the matrices using the projection matrix and the light direction matrix too. I've fixed the clamping and I know why it's repeated along the terrain because the terrain it's supposed to be a cube originally modelled with (1, 1, 1) scale and I scale it manually runtime to (100, 1, 100), I've tested with others big models and it doesn't repeat. The last problem is that I can't achieve properly the shadow for the entities that are casting the shadows. Take a look at this and this. You can see that it's not properly projected into the entities and I don't know what's happening. There is the shaders I to project: MainFrag.frag: #version 430 #extension GL_ARB_shader_storage_buffer_object : enable smooth in vec2 texCoord; smooth in vec3 vNormal; smooth in vec3 vWorldPos; smooth in vec4 vEyeSpacePos; smooth in vec4 ShadowCoord; uniform bool ison = false; uniform int bShadowsOn; uniform vec4 vColor; uniform vec4 vAmbientColor = vec4(1.0); uniform vec3 vEyePosition; uniform sampler2D gSampler; uniform sampler2D gNormalMap; uniform sampler2D shadowMap; uniform bool bUseOnlyColor = false; uniform bool bSkybox = false; uniform sampler2DShadow testt; uniform int bEnableBumpMap; in mat3 vLightTangent; #include "Directional_light.frag" #include "Point_light.frag" #include "Spot_light.frag" #include "Fog.frag" #include "Plasma.frag" #include "Text_1.frag" #include "Water.frag" #include "RoomEditor.frag" /*vec2 poissonDisk[16] = vec2[]( vec2( -0.94201624, -0.39906216 ), vec2( 0.94558609, -0.76890725 ), vec2( -0.094184101, -0.92938870 ), vec2( 0.34495938, 0.29387760 ), vec2( -0.91588581, 0.45771432 ), vec2( -0.81544232, -0.87912464 ), vec2( -0.38277543, 0.27676845 ), vec2( 0.97484398, 0.75648379 ), vec2( 0.44323325, -0.97511554 ), vec2( 0.53742981, -0.47373420 ), vec2( -0.26496911, -0.41893023 ), vec2( 0.79197514, 0.19090188 ), vec2( -0.24188840, 0.99706507 ), vec2( -0.81409955, 0.91437590 ), vec2( 0.19984126, 0.78641367 ), vec2( 0.14383161, -0.14100790 ) ); float GetVisibility() { if (bShadowsOn == 0) return 1.0; float visibility = 1.0; float bias = 0.000; for (int i = 0; i < 4; i++) { int index = i; vec4 vShadowSmooth = vec4(ShadowCoord.x + poissonDisk[index].x/700.0, ShadowCoord.y + poissonDisk[index].y/700.0, (ShadowCoord.z-bias)/ShadowCoord.w, 1.0); vShadowSmooth = vShadowSmooth * 0.5 + 0.5; vShadowSmooth.w = 1.0; float fSub = texture(shadowMap, vShadowSmooth.xy).r; visibility -= 0.25 * (1.0 - fSub); } return visibility; }*/ float ShadowCalculation(vec4 fragPosLightSpace) { vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; projCoords = projCoords * 0.5 + 0.5; float closestDepth = texture(shadowMap, projCoords.xy).r; float currentDepth = projCoords.z; float shadow = currentDepth > closestDepth ? 1.0 : 0.0; if (projCoords.z > 1.0) return 0.0; return shadow * 0.6; } void main() { vec4 vTexColor = texture2D(gSampler, texCoord); vec3 vFragColor; vec3 vNormalized = normalize(vNormal); if (!bUseOnlyColor) gl_FragColor = vTexColor * vColor * vAmbientColor; //texture without any kind of effect applied (lights etc.) else gl_FragColor = vColor * vAmbientColor; //texture without any kind of effect applied (lights etc.) vFragColor = gl_FragColor.xyz; //ENGINE SHADERS, will change gl_FragColor if the entity has the shader enabled PlasmaShader(texCoord); //Plasma.frag Text_1Shader(texCoord); //Text_1.frag Water_Shader(texCoord, vColor.w); //Water.frag Arrow_Shader(texCoord); //RoomEditor.frag //LIGHTING if (iDirectionalLights > 0) { vec4 Diffuse_DirectionalLightsFinalColor = vec4(1.0); vec4 Specular_DirectionalLightsFinalColor = vec4(1.0); vec4 Normal_DirectionalLightsFinalColor = vec4(1.0); for (int i = 0; i < iDirectionalLights; i++) { if (DirectionalLights.Lights[i].bEnabled == 1 && !bSkybox) { Diffuse_DirectionalLightsFinalColor += GetDirectionalLightColor(DirectionalLights.Lights[i], vNormalized); Specular_DirectionalLightsFinalColor += GetDirectionalLightSpecularColor(vWorldPos, vEyePosition, DirectionalLights.Lights[i], vNormalized); if (bEnableBumpMap == 1) Normal_DirectionalLightsFinalColor += GetDirectionalLightNormalColor(DirectionalLights.Lights[i], vLightTangent, texCoord, gNormalMap); } } gl_FragColor *= Diffuse_DirectionalLightsFinalColor * Specular_DirectionalLightsFinalColor * Normal_DirectionalLightsFinalColor; } if (iPointLights > 0) { vec4 Diffuse_PointLightsFinalColor = vec4(1.0); vec4 Specular_PointLightsFinalColor = vec4(1.0); for (int i = 0; i < iPointLights; i++) { if (PointLights.Lights[i].bEnabled == 1 && !bSkybox) { Diffuse_PointLightsFinalColor += GetPointLightColor(PointLights.Lights[i], vWorldPos, vNormalized); Specular_PointLightsFinalColor += GetPointLightSpecularColor(vWorldPos, vEyePosition, PointLights.Lights[i], vNormalized); } } gl_FragColor *= Diffuse_PointLightsFinalColor * Specular_PointLightsFinalColor; // vec4(pow(vFragColor, vec3(1.0/2.2)), 1.0) } if (iSpotLights > 0) { vec4 SpotLightsFinalColor = vec4(1.0); for (int i = 0; i < iSpotLights; i++) { if (SpotLights.Lights[i].bEnabled == 1) { SpotLightsFinalColor += GetSpotLightColor(SpotLights.Lights[i], vWorldPos); } } gl_FragColor *= SpotLightsFinalColor; } if (bShadowsOn == 1) { //gl_FragColor *= GetVisibility(); gl_FragColor *= 1.0 - ShadowCalculation(ShadowCoord); gl_FragColor.w = 1.0; } if (FogData.Enabled) gl_FragColor = mix(gl_FragColor, FogData.FogColor, GetFogFactor(abs(vEyeSpacePos.z / vEyeSpacePos.w))); } ShadowMap.vert: #version 330 core layout(location = 0) in vec3 inPous; uniform mat4 depthMVP; uniform mat4 model; void main() { gl_Position = depthMVP * model * vec4(inPous, 1.0); } ShadowFrag.frag: #version 330 core // Ouput data layout(location = 0) out float fragmentdepth; void main() { //fragmentdepth = gl_FragCoord.z; } These are all the shaders I use and everything related to shadows it's in the first code I sent in the main post so you can compare. I think I'm missing something in my C++ main code because as you can see in the picture the shadow is applied triangle by triangle and that should be something I'm missing not sure. Also in the commented code of the MainFrag you can see GetVisibility function which I tested and it works like the one I'm using but with smooth only, the shadow it's applied to the whole entity that casts the shadow and also happens the same error that there are parts in the entity that have no shadow in the borders...
  9. Hey guys, I worked long time ago in shadows but I left it because I couldn't find the problem I still have. I've been following this tutorial and I adapted it on my project but when I render everything I'm having weird issues I don't know why. As you can see in this pic the shadow projection is well calculated from the shader etc but you also can see that the back and front faces of both cubes are getting also weird shadow and also the shadow is repeated among the "terrain". This last problem is because of GL_REPEAT but if I use others parameters to create the texture it's worst... I tried using GL_CULLFACE to remove the weird shadows in the cubes but doesn't work. I'll leave the code here: #include "../stdafx.h" #include "Main.h" #include "../Renderer/Renderer.h" #include "../GLUtils/GLUtils.h" #include "../Camera/Camera.h" #include "../Audio/Audio.h" #include "../Physics/Physics.h" #include "../Skybox/Skybox.h" #include "../Lighting/LightManager.h" #include "../Entity/Entity.h" #include "../Editor/Editor.h" #include "../NativeGeometry/NativeGeometry.h" #include "../Textures/TextureManager.h" #include "../RenderTargets/RenderTargets.h" #include "../ParticleSystem/ParticleSystem.h" #include "../CEV/CEV.h" #include "../Timers/Timers.h" #include "../LUA/LuaManager.h" #include "../LUA/EventManager.h" #include "../LUA/EventArguments.h" #include "../ColShapes/ColShapes.h" #include "../KeyCode/Keycode.h" int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //CoInitializeEx(nullptr, COINIT_MULTITHREADED); //wtf is this xd Console.reset(new CConsole()); Console->AllocateConsole(); Renderer = std::make_unique<CRenderer>(); ColShapeManager = std::make_unique<CColShapeManager>(); Physics = std::make_unique<CPhysics>(); EventArguments = std::make_unique<CEventsArguments>(); LuaManager = std::make_unique<CLuaManager>(); EventManager = std::make_unique<CEventManager>(); KeyCode = std::make_unique<CKeyCode>(); RenderTargetManager = std::make_unique<CRenderTargetManager>(); TextureManager = std::make_unique<CTextureManager>(); EntityManager = std::make_unique<CEntityManager>(); FogManager = std::make_unique<CFogManager>(); LightManager = std::make_unique<CLightManager>(); Skybox = std::make_unique<CSkybox>(); Camera = std::make_unique<CCamera>(glm::vec3(10, 10, 10)); CEAudio = std::make_unique<CAEngine>(); GLUtils = std::make_unique<CGLUtils>(); if (!Renderer->CreateGLWindow(1920 / 2 - (1280 / 3) + 200, 1080 / 2 - (800 / 2), 1600, 1200, 16, false)) return 0; Renderer.release(); ColShapeManager.release(); Physics.release(); EventArguments.release(); LuaManager.release(); EventManager.release(); KeyCode.release(); RenderTargetManager.release(); TextureManager.release(); EntityManager.release(); FogManager.release(); LightManager.release(); Skybox.release(); Camera.release(); CEAudio.release(); GLUtils.release(); return 0; } bool first = true; BOOL DrawGLScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); static GLuint depthMapFBO; static GLuint SHADOW_WIDTH = 2048, SHADOW_HEIGHT = 2048; static GLuint depthMap; if (first) { glGenFramebuffers(1, &depthMapFBO); glGenTextures(1, &depthMap); glBindTexture(GL_TEXTURE_2D, depthMap); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0); glDrawBuffer(GL_NONE); glReadBuffer(GL_NONE); glBindFramebuffer(GL_FRAMEBUFFER, 0); first = false; } //SKYBOX RENDERING, LIGHT, FOG, CAMERA UPDATE & VIEWMATRIX UPDATE Renderer->MainProgram.Start(); Camera->Update(); Renderer->SetViewMatrices(); Skybox->RenderSkybox(); LightManager->SetUpAmbientColor(); LightManager->UpdateLightsInShader(Renderer->MainProgram); FogManager->RenderFog(); for (int i = 0; i < RenderTargetManager->GetRenderTargets(); i++) { CRenderTarget* RT = RenderTargetManager->GetRenderTarget(i); if (RT) { RT->Bind(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); RT->UpdateTrans(); Skybox->RenderSkybox(); RenderTargetManager->BackToNormalRenderer(); } } //SHADOWS static float xx = 0, yy = 3.f, zz = 0; if (GetAsyncKeyState(VK_NUMPAD8)) { xx += 0.1f; } if (GetAsyncKeyState(VK_NUMPAD2)) { xx -= 0.1f; } if (GetAsyncKeyState(VK_NUMPAD7)) { yy += 0.1f; } if (GetAsyncKeyState(VK_NUMPAD1)) { yy -= 0.1f; } if (GetAsyncKeyState(VK_NUMPAD9)) { zz += 0.1f; } if (GetAsyncKeyState(VK_NUMPAD3)) { zz -= 0.1f; } glm::mat4 mPROJ = glm::ortho<float>(-10.0f, 10.0f, -10.0f, 10.0f, 0.05f, 50.f); glm::mat4 lightView = glm::lookAt(glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 biasMatrix( 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0 ); glm::mat4 lightSpaceMatrix = biasMatrix * mPROJ * lightView; Renderer->ShadowMapping.Start(); Renderer->ShadowMapping.setUniform("depthMVP", lightSpaceMatrix); glm::mat4 mModelToCamera; glm::vec3 vPos = glm::vec3(xx, yy, zz); mModelToCamera = glm::translate(glm::mat4(1.0), vPos); mModelToCamera = glm::scale(mModelToCamera, glm::vec3(2.f)); Renderer->ShadowMapping.setUniform("model", mModelToCamera); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); /*glEnable(GL_CULL_FACE); glCullFace(GL_FRONT);*/ glClear(GL_DEPTH_BUFFER_BIT); Environment::DrawCube(glm::vec3(xx, yy, zz), glm::vec3(2.f), glm::vec4(1.f), 7, true); vPos = glm::vec3(xx + 3.f, yy, zz); mModelToCamera = glm::translate(glm::mat4(1.0), vPos); mModelToCamera = glm::scale(mModelToCamera, glm::vec3(2.f)); Renderer->ShadowMapping.setUniform("model", mModelToCamera); Environment::DrawCube(glm::vec3(xx + 3, yy, zz), glm::vec3(2.f), glm::vec4(1.f), 7, true); RenderTargetManager->BackToNormalRenderer(); //glDisable(GL_CULL_FACE); Renderer->ShadowMapping.Stop(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Renderer->MainProgram.Start(); Environment::DrawPlane(glm::vec3(0, 20.f, -20.f), glm::vec3(0.f, 90.f, 90.f), glm::vec3(10.f, 1.f, 10.f), depthMap); Renderer->MainProgram.setUniform("EngineMatrices.DepthBiasMVP", lightSpaceMatrix); Renderer->MainProgram.setUniform("shadowMap", 1); Renderer->MainProgram.setUniform("bShadowsOn", 1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, depthMap); glActiveTexture(GL_TEXTURE0); Environment::DrawCube(glm::vec3(xx, yy, zz), glm::vec3(2.f), glm::vec4(1.f), 7, false); Environment::DrawCube(glm::vec3(xx + 3.f, yy, zz), glm::vec3(2.f), glm::vec4(1.f), 7, false); //SHADOWS //SHADOWS #ifdef ENGINE_EDITOR //CEditor::UpdateSelectedEntity(); #endif //MODEL TYPE RENDERING if (Renderer->IsCullFaceEnabled()) { /*glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glFrontFace(GL_CW);*/ } auto RenderEntity = [](Entity* _Entity) { if (_Entity->UpdateTransform()) { _Entity->StartShader(); _Entity->Mesh->Render(); _Entity->StopShader(); } if (_Entity->ShowCollider) { btVector3 one, two; _Entity->Phys->RigidBody->getAabb(one, two); Environment::DrawPoint(glm::vec3(one.x(), one.y(), one.z()), glm::vec3(2.f), glm::vec3(0.f, 1.f, 0.f)); Environment::DrawPoint(glm::vec3(one.x() + (two.x() - one.x()), one.y(), one.z()), glm::vec3(2.f), glm::vec3(0.f, 1.f, 0.f)); Environment::DrawPoint(glm::vec3(one.x(), one.y(), one.z() + (two.z() - one.z())), glm::vec3(2.f), glm::vec3(0.f, 1.f, 0.f)); Environment::DrawPoint(glm::vec3(one.x(), one.y() + (two.y() - one.y()), one.z()), glm::vec3(2.f), glm::vec3(0.f, 1.f, 0.f)); Environment::DrawPoint(glm::vec3(two.x() - (two.x() - one.x()), two.y(), two.z()), glm::vec3(2.f), glm::vec3(0.f, 1.f, 0.f)); Environment::DrawPoint(glm::vec3(two.x(), two.y() - (two.y() - one.y()), two.z()), glm::vec3(2.f), glm::vec3(0.f, 1.f, 0.f)); Environment::DrawPoint(glm::vec3(two.x(), two.y(), two.z() - (two.z() - one.z())), glm::vec3(2.f), glm::vec3(0.f, 1.f, 0.f)); Environment::DrawPoint(glm::vec3(two.x(), two.y(), two.z()), glm::vec3(2.f), glm::vec3(0.f, 1.f, 0.f)); } }; //COL SHAPES #ifdef ENGINE_EDITOR for (uint i = 0; i < ColShapeManager->GetShapes(SHAPE_RECTANGLE); i++) { RectangleCol* currentShape = ColShapeManager->GetShape<RectangleCol>(SHAPE_RECTANGLE, i); if (currentShape && currentShape->IsDebugEnabled()) currentShape->DebugShowCol(); } for (uint i = 0; i < ColShapeManager->GetShapes(SHAPE_SPHERE); i++) { SphereCol* currentShape = ColShapeManager->GetShape<SphereCol>(SHAPE_SPHERE, i); if (currentShape && currentShape->IsDebugEnabled()) currentShape->DebugShowCol(); } #endif //ENTITIES std::map<float, Entity*> TransparentEntities; std::map<float, Entity*> FarestEntities; EntityManager->LoadQueueEntities(); #ifdef ENGINE_EDITOR EntityManager->ResetEntitiesRendered(); #endif for (uint i = 0; i < EntityManager->GetEntities(); i++) { Entity* currentEntity = EntityManager->GetEntity(i); float EntityToCamera = glm::distance(Camera->Position, currentEntity->Position); if (currentEntity && currentEntity->CanRender() && EntityToCamera <= Renderer->GetRenderMaxDistance() && currentEntity->IsVisible()) { if (currentEntity->IsDestroying()) { float _alpha = currentEntity->GetAlpha(); if (_alpha <= 0.f) { EntityManager->DestroyEntity(currentEntity); continue; } currentEntity->SetAlpha(_alpha - 0.01f); } #ifdef ENGINE_EDITOR EntityManager->IncreaseEntitiesRendered(); #endif if (currentEntity->GetAlpha() < 1.f) TransparentEntities[EntityToCamera] = currentEntity; else FarestEntities[EntityToCamera] = currentEntity; } } //RENDER TRANSPARENT ENTITIES & FAREST ENTITIES FIRST (lol?) for (auto it = FarestEntities.begin(); it != FarestEntities.end(); ++it) RenderEntity(it->second); for (auto it = TransparentEntities.rbegin(); it != TransparentEntities.rend(); ++it) RenderEntity(it->second); Renderer->MainProgram.setUniform("bShadowsOn", 0); TransparentEntities.empty(); TransparentEntities.clear(); //PARTICLE SYSTEM for (uint i = 0; i < ParticleSystemManager::GetParticleSystems(); i++) { if (ParticleSystems[i] && ParticleSystems[i]->ID != INT_MAX) { ParticleSystems[i]->SetMatrices(&Renderer->GetProjectionMatrix(), Camera->Position, Camera->vView, Camera->vUp); ParticleSystems[i]->UpdateParticles(0.025f); ParticleSystems[i]->RenderParticles(); } } Renderer->ResetColor(); Renderer->MainProgram.Stop(); if (Renderer->IsCullFaceEnabled()) glDisable(GL_CULL_FACE); OGLImGui::DrawGUI(); EventManager->CallEvent(ON_RENDER, EventArguments->GetEventArguments(ON_RENDER)); return true; } Sorry for the messed code but I use to do that when I'm testing something instead of organizing it first. Maybe I'm missing something stupid but I can't figure out lol. I appreciate any help. Thanks in advance.
  10. Tonyx97

    OGL FBOs recommendation

    Thanks you guys! I'll try your suggestions let's see how they work :)
  11. Hello guys, yesterday I started to work on FBOs to make some render targets using cameras like Unity does, today I finished a stable code to test it but I realized it's a bit laggy depending on the entities or times I call glDrawArrays. I don't really know if it's normal or I should do it better but I'll post some code to explain myself, I need some recommendations about how to properly use this because I'm pretty sure I don't need to call glDrawArrays 2 times (1 for the FBO and another one for the real rendering).   This is exactly where I do the rendering:   glBindFramebuffer(GL_FRAMEBUFFER, 1); glViewport(0, 0, 256, 256); static glm::mat4 mView2 = glm::lookAt(glm::vec3(0, 70.f, 70.f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); Renderer->MainProgram.setUniform("EngineMatrices.ProjectionMatrix", Television.CalculateProjectionMatrix(60.f, 0.5f, 201.f)); Renderer->MainProgram.setUniform("EngineMatrices.ViewMatrix", mView2); glDrawArrays(GL_TRIANGLES, 0, VertexSize); glBindFramebuffer(GL_FRAMEBUFFER, 0); //cleaning buffers to render 3D models glViewport(0, 0, Renderer->ScreenSX, Renderer->ScreenSY); Renderer->MainProgram.setUniform("EngineMatrices.ProjectionMatrix", Renderer->GetProjectionMatrix()); Renderer->MainProgram.setUniform("EngineMatrices.ViewMatrix", Renderer->GetViewMatrix()); glDrawArrays(GL_TRIANGLES, 0, VertexSize); After this code I update the viewport to come back to the normal rendering and I render the texture of the FBO in a plane (a plane, not a cube so I only render 1 face). Here's a picture of the frame buffer:    in this case I have 1000 cubes with a total of 12 faces each one (triangulated obviously) causing a FPS drop of ~7 FPS.   In the next image I'll show the same but with only 600 cubes:   In this case the FPS are constant and it's stable at 59-60 FPS. I hope someone could tell me some tricks or tips about how I should render a model in both buffers with the less FPS drop possible. I also thought about occlusion but first I want to make sure this is well done. Thanks in advance.   Edit: Using a 8192x8192 buffer I get like 27 FPS drop.
  12. Tonyx97

    Shader array limit?

    Thanks for your answer! I'm using what you said, the shader is still working but I think my GL program can't write the uniforms because the program never pass the condition if any light is enabled or disabled so it's not drawing the light source. I'm also doing the same method to access the data as the shader. It should work with all information you provided but I don't know why I can't write the data of each light... Should I use glUniformBlockIndex etc? Without that method the lights render. for (uint i = 0; i < iPointLights; i++) { CPointLight* _light = PointLights[i]; if (!IsBadReadPtr(_light, 4) && _light->ID != INT_MAX) { _shader.setUniform("PointLightBuffer.PointLights[" + std::to_string(i) + "].bEnabled", true); _shader.setUniform("PointLightBuffer.PointLights[" + std::to_string(i) + "].vPosition", glm::vec3(_light->Position.x, _light->Position.y, _light->Position.z)); _shader.setUniform("PointLightBuffer.PointLights[" + std::to_string(i) + "].vLightColor", glm::vec3(_light->LightColor.x, _light->LightColor.y, _light->LightColor.z)); _shader.setUniform("PointLightBuffer.PointLights[" + std::to_string(i) + "].fIntensity", _light->Intensity); _shader.setUniform("PointLightBuffer.PointLights[" + std::to_string(i) + "].fConstantAtt", _light->ConstantAtt); _shader.setUniform("PointLightBuffer.PointLights[" + std::to_string(i) + "].fLinearAtt", _light->LinearAtt); _shader.setUniform("PointLightBuffer.PointLights[" + std::to_string(i) + "].fExpAtt", _light->ExpAtt); _shader.setUniform("iPointLights", (int)iPointLights); } else _shader.setUniform("PointLightBuffer.PointLights[" + std::to_string(i) + "].bEnabled", false); } struct CPointLight { vec3 vLightColor; vec3 vPosition; float fIntensity; float fConstantAtt; float fLinearAtt; float fExpAtt; bool bEnabled; }; uniform CPointLightBuffer { CPointLight PointLights[200]; } PointLightBuffer;
  13. //Main Fragment shader (the one that manages all fragment shaders): #version 430 smooth in vec2 texCoord; smooth in vec3 NormalVec; smooth in vec3 CamPosition; uniform sampler2D gSampler; uniform vec4 vColor; uniform bool bUseOnlyColor = false; uniform vec4 vAmbientColor = vec4(1.0); #include "Directional_light.frag" //all this includes are properly done cause all them works. #include "Spot_light.frag" #include "Point_light.frag" #include "Fog.frag" #include "Plasma.frag" #include "Text_1.frag" #include "Water.frag" void main() { vec4 vTexColor = texture2D(gSampler, texCoord); vec3 vNormalized = normalize(NormalVec); if (!bUseOnlyColor) gl_FragColor = vTexColor * vColor * vAmbientColor; else gl_FragColor = vColor * vAmbientColor; PlasmaShader(texCoord); //Plasma.frag Text_1Shader(texCoord); //Text_1.frag Water_Shader(texCoord, vColor.w); //Water.frag //LIGHTING if (iDirectionalLights > 0) { vec4 DirectionalLightsFinalColor = vec4(1.0); for (int i = 0; i < iDirectionalLights; i++) { if (DirectionalLight[i].bEnabled) { DirectionalLightsFinalColor += getDirectionalLightColor(DirectionalLight[i], NormalVec); } } gl_FragColor *= DirectionalLightsFinalColor; } if (iPointLights > 0) { vec4 PointLightsFinalColor = vec4(1.0); for (int i = 0; i < iPointLights; i++) { if (PointLights[i].bEnabled) { PointLightsFinalColor += getPointLightColor(PointLights[i], CamPosition, vNormalized); } } gl_FragColor *= PointLightsFinalColor; } if (iSpotLights > 0) { vec4 SpotLightsFinalColor = vec4(1.0); for (int i = 0; i < iSpotLights; i++) { if (SpotLights[i].Enabled) //IF I COMMENT THIS LINE THE SHADER STOP WORKING { SpotLightsFinalColor += GetSpotLightColor(SpotLights[i], CamPosition); } } gl_FragColor *= SpotLightsFinalColor; } }   #version 430 //SPOT LIGHT SHADER #include_part uniform struct CSpotLight { vec3 vColor; vec3 vPosition; vec3 vDirection; float fConeAngle; float fConeCosine; float fLinearAtt; bool Enabled; } SpotLights[90]; //putting here more than 90 will destroy all the shaders uniform int iSpotLights = 0; vec4 GetSpotLightColor(const CSpotLight spotLight, vec3 vWorldPos); #definition_part vec4 GetSpotLightColor(const CSpotLight spotLight, vec3 vWorldPos) { float fDistance = distance(vWorldPos, spotLight.vPosition); vec3 vDir = vWorldPos-spotLight.vPosition; vDir = normalize(vDir); float fCosine = dot(spotLight.vDirection, vDir); float fDif = 1.0-spotLight.fConeCosine; float fFactor = clamp((fCosine-spotLight.fConeCosine)/fDif, 0.0, 1.0); if(fCosine > spotLight.fConeCosine) return vec4(spotLight.vColor, 1.0)*fFactor/(fDistance*spotLight.fLinearAtt); } Hello everyone, since yesterday I've been trying to figure out what was the problem with my shaders. Basically I realized something was wrong when I started to implement the spot light shader in my project. I have a loop to render each different light (directional, point and spot) and I have 3 arrays that contains the data of each type of light. Directional light array has 75 lights, Point light 75 and then when I put the spot light I couldn't put more than 30 because the whole shader program stopped working. I firstly thought it was an array limit but that's weird... 3 arrays, in total 165 lights more or less... Then I started to figure out where was the problem. I realized something, if I don't read any member of the spot light (for example) in the shader it works. Also I want to say that I'm storing all shaders in 1 program because I've heard that it's better to use 1 shader for all entities than putting each shader to each entity. The Shaders code are upper. I've been researching in internet for solutions but I didn't find anything that can answer me question/issue. If this problem is because I reached the max shader size or something like that I don't know how to do this with the best performance because I don't want to have 2 same vertex shaders sending data to their fragments shaders because I need to set the data to all vertex shaders and I only want to do it 1 time for all entities etc..   In this screenshot I add all shaders to my main program, there are 10 shaders in total.   Does anyone know what could be the problem or how I should manage this?  Thanks in advance.  
  14. Thanks you! At least now I know that was the problem and it's almost fixed. Now I've to figure out how to apply textures without shader (or with it) because I'm trying to glBindTexture but that's not even working. I've edited my rendering function like this: BOOL DrawGLScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //MESHES & SKYBOX RENDERING CSkybox::RenderSkybox(0, 0, 0, 1, 1, 1); Camera->UpdateCamera(); RenderSpaceLines(); Light.useProgram(); glm::mat4 mModelView = Renderer->GetViewMatrix(); glm::mat4 mModelToCamera; Light.setUniform("sunLight.vColor", glm::vec3(0.0f, 1.0f, 1.0f)); Light.setUniform("sunLight.fAmbientIntensity", 0.95f); Light.setUniform("sunLight.vDirection", -glm::normalize(glm::vec3(5.f, 1.f, 0))); Light.setUniform("projectionMatrix", Renderer->GetProjectionMatrix()); Light.setUniform("gSampler", 0); Light.setUniform("modelViewMatrix", &mModelView); Light.setUniform("vColor", glm::vec4(0.0f, 1.0f, 1.0f, 1.0f)); for (uint i = 0; i < EntityManager::GetEntities(); i++) { if (Entities[i] && Entities[i]->ID != INT_MAX && Vector3::Distance(Camera->position, Entities[i]->Position) <= Renderer->GetRenderMaxDistance()) //AVOIDING RENDER ENTITIES THAT ARE OUTSIDE OF MAX DISTANCE { glPushMatrix(); if (Entities[i]->Parent == -1) { glTranslatef(Entities[i]->Position.x, Entities[i]->Position.y, Entities[i]->Position.z); glRotatef(Entities[i]->Rotation.x, 1.f, 0.f, 0.f); glRotatef(Entities[i]->Rotation.y, 0.f, 1.f, 0.f); glRotatef(Entities[i]->Rotation.z, 0.f, 0.f, 1.f); } else { glTranslatef(Entities[Entities[i]->Parent]->Position.x, Entities[Entities[i]->Parent]->Position.y, Entities[Entities[i]->Parent]->Position.z); glRotatef(Entities[Entities[i]->Parent]->Rotation.x, 1.f, 0.f, 0.f); glRotatef(Entities[Entities[i]->Parent]->Rotation.y, 0.f, 1.f, 0.f); glRotatef(Entities[Entities[i]->Parent]->Rotation.z, 0.f, 0.f, 1.f); glTranslatef(Entities[i]->Position.x, Entities[i]->Position.y, Entities[i]->Position.z); } glScalef(Entities[i]->Scale.x, Entities[i]->Scale.y, Entities[i]->Scale.z); if (Entities[i]->HasCollider && Entities[i]->ShowCollider) { glUseProgram(0); glBindTexture(GL_TEXTURE_2D, 0); glEnableClientState(GL_VERTEX_ARRAY); glPushMatrix(); if (Entities[i]->IsCollidingWithAnyEntity()) Entities[i]->ColliderColor = Color(1.f, 0.f, 0.f); else Entities[i]->ColliderColor = Color(0.f, 1.f, 0.f); glColor3f(Entities[i]->ColliderColor.R(), Entities[i]->ColliderColor.G(), Entities[i]->ColliderColor.B()); if (Entities[i]->CollisionType == eCollisionType::ORIENTED_BOX || Entities[i]->CollisionType == eCollisionType::BOX) { glVertexPointer(3, GL_FLOAT, 0, NTV_LineCube); glDrawArrays(GL_LINES, 0, 24); } else if (Entities[i]->CollisionType == eCollisionType::SPHERE) { glScalef(Entities[i]->Scale.x, Entities[i]->Scale.y, Entities[i]->Scale.z); glVertexPointer(3, GL_FLOAT, 0, NTV_Sphere); glDrawArrays(GL_POINTS, 0, 5952); } glColor3f(1.0f, 1.0f, 1.0f); glPopMatrix(); glDisableClientState(GL_VERTEX_ARRAY); Light.useProgram(); } glm::vec3 vPos = glm::vec3(Entities[i]->Position.x, Entities[i]->Position.y, Entities[i]->Position.z); mModelToCamera = glm::translate(glm::mat4(1.0), vPos); mModelToCamera = glm::rotate(mModelToCamera, 0.f, glm::vec3(0.0f, 1.0f, 0.0f)); Light.setUniform("normalMatrix", glm::transpose(glm::inverse(mModelToCamera))); Light.setUniform("modelViewMatrix", mModelView*mModelToCamera); Entities[i]->Mesh->render(); glPopMatrix(); } } glUseProgram(0); OGLImGui::DrawGUI(); return TRUE; } and the "render" function of a mesh is (Entities->Mesh->render()): void CMesh::MeshEntry::render() { glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(3, GL_FLOAT, 0, (GLfloat*)VertexCoords.data()); glNormalPointer(GL_FLOAT, 0, (GLfloat*)NormalCoords.data()); glTexCoordPointer(2, GL_FLOAT, 0, (GLfloat*)TextureCoords.data()); glBindTexture(GL_TEXTURE_2D, TextureManager::Texture[modelName]); glDrawArrays(GL_TRIANGLES, 0, VertexSize); //ALWAYS GL_TRIANGLES (PROBABLY the ENGINE WILL SUPPORT QUAD IN THE FUTURE) glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); } Also when I move the camera the entities are like "lagged", I don't know if that's a shader problem or I should edit the order of the code.. Now it's like this:
  15. Hello guys, this is my first thread in the community (well I'm not sure haha, long time ago I didn't enter here), to be honest I never came to internet to ask something related with OpenGL since there are a lot of good sources to learn and I'm C++ programmer, but these shaders are a bit different or I'm doing something stupid... they are turning me crazy. I took the source from a good site where the everything works perfectly, I didn't modify the source of the shaders so the link of the example source is this. Basically the problem is when I load the shader, the texture disappears and there are some graphics problems. In the first case, the normal look of my project is this   And when I load the shaders of lighting (or any simple shader) happens this:   The shader is really working because the shadow of the entities are properly situated according to the position of the directional light.  I'm also using ImGui but that's not the problem because I commented the initializing function and the problem persists. I'm posting the source of the rendering function here: BOOL DrawGLScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //MESHES & SKYBOX RENDERING CSkybox::RenderSkybox(0, 0, 0, 1, 1, 1); Camera->UpdateCamera(); RenderSpaceLines(); for (uint i = 0; i < EntityManager::GetEntities(); i++) { if (Entities[i] && Entities[i]->ID != INT_MAX && Vector3::Distance(Camera->position, Entities[i]->Position) <= Renderer->GetRenderMaxDistance()) //AVOIDING RENDER ENTITIES THAT ARE OUTSIDE OF MAX DISTANCE { glPushMatrix(); if (Entities[i]->Parent == -1) { glTranslatef(Entities[i]->Position.x, Entities[i]->Position.y, Entities[i]->Position.z); glRotatef(Entities[i]->Rotation.x, 1.f, 0.f, 0.f); glRotatef(Entities[i]->Rotation.y, 0.f, 1.f, 0.f); glRotatef(Entities[i]->Rotation.z, 0.f, 0.f, 1.f); } else { glTranslatef(Entities[Entities[i]->Parent]->Position.x, Entities[Entities[i]->Parent]->Position.y, Entities[Entities[i]->Parent]->Position.z); glRotatef(Entities[Entities[i]->Parent]->Rotation.x, 1.f, 0.f, 0.f); glRotatef(Entities[Entities[i]->Parent]->Rotation.y, 0.f, 1.f, 0.f); glRotatef(Entities[Entities[i]->Parent]->Rotation.z, 0.f, 0.f, 1.f); glTranslatef(Entities[i]->Position.x, Entities[i]->Position.y, Entities[i]->Position.z); } glScalef(Entities[i]->Scale.x, Entities[i]->Scale.y, Entities[i]->Scale.z); if (Entities[i]->HasCollider && Entities[i]->ShowCollider) { glBindTexture(GL_TEXTURE_2D, 0); glEnableClientState(GL_VERTEX_ARRAY); glPushMatrix(); if (Entities[i]->IsCollidingWithAnyEntity()) Entities[i]->ColliderColor = Color(1.f, 0.f, 0.f); else Entities[i]->ColliderColor = Color(0.f, 1.f, 0.f); glColor3f(Entities[i]->ColliderColor.R(), Entities[i]->ColliderColor.G(), Entities[i]->ColliderColor.B()); if (Entities[i]->CollisionType == eCollisionType::ORIENTED_BOX || Entities[i]->CollisionType == eCollisionType::BOX) { glVertexPointer(3, GL_FLOAT, 0, NTV_LineCube); glDrawArrays(GL_LINES, 0, 24); } else if (Entities[i]->CollisionType == eCollisionType::SPHERE) { glScalef(Entities[i]->Scale.x, Entities[i]->Scale.y, Entities[i]->Scale.z); glVertexPointer(3, GL_FLOAT, 0, NTV_Sphere); glDrawArrays(GL_POINTS, 0, 5952); } glColor3f(1.0f, 1.0f, 1.0f); glPopMatrix(); glDisableClientState(GL_VERTEX_ARRAY); } //Light.useProgram(); //problem starts here glm::mat4 mModelView = Renderer->GetViewMatrix(); glm::mat4 mModelToCamera; Light.setUniform("sunLight.vColor", glm::vec3(1.0f, 1.0f, 1.0f)); Light.setUniform("sunLight.fAmbientIntensity", 0.95f); Light.setUniform("sunLight.vDirection", -glm::normalize(glm::vec3(5.f, 1.f, 0))); Light.setUniform("projectionMatrix", Renderer->GetProjectionMatrix()); Light.setUniform("gSampler", 0); Light.setUniform("modelViewMatrix", &mModelView); Light.setUniform("vColor", glm::vec4(0.0f, 1.0f, 1.0f, 1.0f)); glm::vec3 vPos = glm::vec3(Entities[i]->Position.x, Entities[i]->Position.y, Entities[i]->Position.z); mModelToCamera = glm::translate(glm::mat4(1.0), vPos); mModelToCamera = glm::rotate(mModelToCamera, 0.f, glm::vec3(0.0f, 1.0f, 0.0f)); Light.setUniform("normalMatrix", glm::transpose(glm::inverse(mModelToCamera))); Light.setUniform("modelViewMatrix", mModelView*mModelToCamera); //problem "finish" here (lol) Entities[i]->Mesh->render(); glPopMatrix(); } } OGLImGui::DrawGUI(); return TRUE; } When this code is reach the shaders are loaded properly and I use glUseProgram to enable them before any rendering. I tried to change the order of the lines since OpenGL is very sensitive with the order but still nothing. Maybe I am missing something, I'm not sure. Does anyone know what could be the problem? if any other code is needed ask please. Thanks in advance.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!