Added custom mtxProj() call to properly handle OGL NDC.

This commit is contained in:
Dario Manesku
2014-10-06 07:03:41 +02:00
parent ba9651cb57
commit 48ce6bb5ff
7 changed files with 61 additions and 20 deletions

View File

@@ -30,7 +30,12 @@ struct PosColorTexCoord0Vertex
bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
static bool s_flipV = false;
static bool s_oglNdc = false;
inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
{
bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_oglNdc);
}
void renderScreenSpaceQuad(uint32_t _view, bgfx::ProgramHandle _program, float _x, float _y, float _width, float _height)
{
@@ -128,7 +133,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
case bgfx::RendererType::OpenGL:
case bgfx::RendererType::OpenGLES:
s_flipV = true;
s_oglNdc = true;
break;
}
@@ -175,7 +180,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
float view[16];
float proj[16];
bx::mtxLookAt(view, eye, at);
bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
// Set view and projection matrix for view 1.
bgfx::setViewTransform(0, view, proj);

View File

@@ -8,6 +8,12 @@
#include "imgui/imgui.h"
static float s_texelHalf = 0.0f;
static bool s_originBottomLeft = false;
inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
{
bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_originBottomLeft);
}
struct PosColorTexCoord0Vertex
{
@@ -209,7 +215,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
s_texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
const bool originBottomLeft = bgfx::RendererType::OpenGL == renderer || bgfx::RendererType::OpenGLES == renderer;
s_originBottomLeft = bgfx::RendererType::OpenGL == renderer || bgfx::RendererType::OpenGLES == renderer;
uint32_t oldWidth = 0;
uint32_t oldHeight = 0;
@@ -372,7 +378,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::setTexture(0, u_texColor, fbtextures[0]);
bgfx::setProgram(lumProgram);
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
screenSpaceQuad(128.0f, 128.0f, originBottomLeft);
screenSpaceQuad(128.0f, 128.0f, s_originBottomLeft);
bgfx::submit(2);
// Downscale luminance 0.
@@ -380,7 +386,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::setTexture(0, u_texColor, lum[0]);
bgfx::setProgram(lumAvgProgram);
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
screenSpaceQuad(64.0f, 64.0f, originBottomLeft);
screenSpaceQuad(64.0f, 64.0f, s_originBottomLeft);
bgfx::submit(3);
// Downscale luminance 1.
@@ -388,7 +394,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::setTexture(0, u_texColor, lum[1]);
bgfx::setProgram(lumAvgProgram);
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
screenSpaceQuad(16.0f, 16.0f, originBottomLeft);
screenSpaceQuad(16.0f, 16.0f, s_originBottomLeft);
bgfx::submit(4);
// Downscale luminance 2.
@@ -396,7 +402,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::setTexture(0, u_texColor, lum[2]);
bgfx::setProgram(lumAvgProgram);
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
screenSpaceQuad(4.0f, 4.0f, originBottomLeft);
screenSpaceQuad(4.0f, 4.0f, s_originBottomLeft);
bgfx::submit(5);
// Downscale luminance 3.
@@ -404,7 +410,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::setTexture(0, u_texColor, lum[3]);
bgfx::setProgram(lumAvgProgram);
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
screenSpaceQuad(1.0f, 1.0f, originBottomLeft);
screenSpaceQuad(1.0f, 1.0f, s_originBottomLeft);
bgfx::submit(6);
float tonemap[4] = { middleGray, square(white), treshold, 0.0f };
@@ -416,14 +422,14 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::setTexture(1, u_texLum, lum[4]);
bgfx::setProgram(brightProgram);
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
screenSpaceQuad( (float)width/2.0f, (float)height/2.0f, originBottomLeft);
screenSpaceQuad( (float)width/2.0f, (float)height/2.0f, s_originBottomLeft);
bgfx::submit(7);
// Blur bright pass vertically.
bgfx::setTexture(0, u_texColor, bright);
bgfx::setProgram(blurProgram);
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
screenSpaceQuad( (float)width/8.0f, (float)height/8.0f, originBottomLeft);
screenSpaceQuad( (float)width/8.0f, (float)height/8.0f, s_originBottomLeft);
bgfx::submit(8);
// Blur bright pass horizontally, do tonemaping and combine.
@@ -432,7 +438,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::setTexture(2, u_texBlur, blur);
bgfx::setProgram(tonemapProgram);
bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
screenSpaceQuad( (float)width, (float)height, originBottomLeft);
screenSpaceQuad( (float)width, (float)height, s_originBottomLeft);
bgfx::submit(9);
// Advance to next frame. Rendering thread will be kicked to

View File

@@ -151,6 +151,11 @@ static uint32_t s_viewMask = 0;
static uint32_t s_clearMask = 0;
static bgfx::UniformHandle u_texColor;
inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
{
bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_flipV);
}
static void shaderFilePath(char* _out, const char* _name)
{
strcpy(_out, s_shaderPath);
@@ -1004,7 +1009,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
// Set view and projection matrices.
const float aspect = float(viewState.m_width)/float(viewState.m_height);
bx::mtxProj(viewState.m_proj, 60.0f, aspect, 0.1f, 100.0f);
mtxProj(viewState.m_proj, 60.0f, aspect, 0.1f, 100.0f);
float initialPos[3] = { 0.0f, 18.0f, -40.0f };
cameraCreate();

View File

@@ -111,6 +111,7 @@ static const uint16_t s_planeIndices[] =
};
static const char* s_shaderPath = NULL;
static bool s_oglNdc = false;
static float s_texelHalf = 0.0f;
static uint32_t s_viewMask = 0;
@@ -221,6 +222,11 @@ void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _wid
}
}
inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
{
bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_oglNdc);
}
void mtxBillboard(float* __restrict _result
, const float* __restrict _view
, const float* __restrict _pos
@@ -1951,10 +1957,12 @@ int _main_(int /*_argc*/, char** /*_argv*/)
case bgfx::RendererType::OpenGL:
s_shaderPath = "shaders/glsl/";
s_oglNdc = true;
break;
case bgfx::RendererType::OpenGLES:
s_shaderPath = "shaders/gles/";
s_oglNdc = true;
break;
}
@@ -2163,7 +2171,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
const float aspect = float(viewState.m_width)/float(viewState.m_height);
const float nearPlane = 1.0f;
const float farPlane = 1000.0f;
bx::mtxProj(viewState.m_proj, fov, aspect, nearPlane, farPlane);
mtxProj(viewState.m_proj, fov, aspect, nearPlane, farPlane);
float initialPos[3] = { 3.0f, 20.0f, -58.0f };
cameraCreate();

View File

@@ -71,6 +71,11 @@ static float s_texelHalf = 0.0f;
bgfx::FrameBufferHandle s_shadowMapFB;
static bgfx::UniformHandle u_shadowMap;
inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
{
bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_flipV);
}
static void shaderFilePath(char* _out, const char* _name)
{
strcpy(_out, s_shaderPath);
@@ -486,7 +491,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bx::mtxLookAt(view, eye, at);
const float aspect = float(int32_t(width) ) / float(int32_t(height) );
bx::mtxProj(proj, 60.0f, aspect, 0.1f, 1000.0f);
mtxProj(proj, 60.0f, aspect, 0.1f, 1000.0f);
// Time acumulators.
float timeAccumulatorLight = 0.0f;

View File

@@ -83,6 +83,11 @@ static const uint16_t s_cubeIndices[36] =
static float s_texelHalf = 0.0f;
static bool s_flipV = false;
inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
{
bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_flipV);
}
void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = false, float _width = 1.0f, float _height = 1.0f)
{
if (bgfx::checkAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) )
@@ -303,7 +308,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
// Set view and projection matrix for view 0.
bx::mtxLookAt(view, eye, at);
bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
bgfx::setViewTransform(0, view, proj);

View File

@@ -15,6 +15,13 @@
#define RENDER_PASS_DEBUG_LIGHTS_ID 3
#define RENDER_PASS_DEBUG_GBUFFER_ID 4
static bool s_originBottomLeft = false;
inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
{
bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_originBottomLeft);
}
struct PosNormalTangentTexcoordVertex
{
float m_x;
@@ -303,7 +310,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
const int64_t timeOffset = bx::getHPCounter();
const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
const float texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
const bool originBottomLeft = bgfx::RendererType::OpenGL == renderer || bgfx::RendererType::OpenGLES == renderer;
s_originBottomLeft = bgfx::RendererType::OpenGL == renderer || bgfx::RendererType::OpenGLES == renderer;
// Get renderer capabilities info.
const bgfx::Caps* caps = bgfx::getCaps();
@@ -448,7 +455,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::setViewFrameBuffer(RENDER_PASS_LIGHT_ID, lightBuffer);
float proj[16];
bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
bgfx::setViewFrameBuffer(RENDER_PASS_GEOMETRY_ID, gbuffer);
bgfx::setViewTransform(RENDER_PASS_GEOMETRY_ID, view, proj);
@@ -647,7 +654,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
| BGFX_STATE_ALPHA_WRITE
| BGFX_STATE_BLEND_ADD
);
screenSpaceQuad( (float)width, (float)height, texelHalf, originBottomLeft);
screenSpaceQuad( (float)width, (float)height, texelHalf, s_originBottomLeft);
bgfx::submit(RENDER_PASS_LIGHT_ID);
}
}
@@ -660,7 +667,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
| BGFX_STATE_RGB_WRITE
| BGFX_STATE_ALPHA_WRITE
);
screenSpaceQuad( (float)width, (float)height, texelHalf, originBottomLeft);
screenSpaceQuad( (float)width, (float)height, texelHalf, s_originBottomLeft);
bgfx::submit(RENDER_PASS_COMBINE_ID);
if (showGBuffer)