Merge pull request #1560 from YunfeiBai/master

Add shader and example for projective texture.
This commit is contained in:
erwincoumans
2018-02-21 10:23:18 -08:00
committed by GitHub
16 changed files with 516 additions and 5 deletions

View File

@@ -17,6 +17,7 @@ subject to the following restrictions:
///todo: make this configurable in the gui
bool useShadowMap = true;// true;//false;//true;
bool useProjectiveTexture = false;
int shadowMapWidth= 4096;
int shadowMapHeight= 4096;
float shadowMapWorldSize=10;
@@ -74,6 +75,8 @@ float shadowMapWorldSize=10;
#include "Shaders/createShadowMapInstancingPS.h"
#include "Shaders/useShadowMapInstancingVS.h"
#include "Shaders/useShadowMapInstancingPS.h"
#include "Shaders/projectiveTextureInstancingVS.h"
#include "Shaders/projectiveTextureInstancingPS.h"
#include "Shaders/linesPS.h"
#include "Shaders/linesVS.h"
@@ -281,6 +284,7 @@ static GLuint triangleIndexVbo=0;
static GLuint linesShader; // The line renderer
static GLuint useShadowMapInstancingShader; // The shadow instancing renderer
static GLuint createShadowMapInstancingShader; // The shadow instancing renderer
static GLuint projectiveTextureInstancingShader; // The projective texture instancing renderer
static GLuint instancingShader; // The instancing renderer
static GLuint instancingShaderPointSprite; // The point sprite instancing renderer
@@ -318,6 +322,20 @@ static GLint useShadow_shadowMap = 0;
static GLint createShadow_depthMVP=0;
static GLint projectiveTexture_ViewMatrixInverse=0;
static GLint projectiveTexture_ModelViewMatrix=0;
static GLint projectiveTexture_lightSpecularIntensity = 0;
static GLint projectiveTexture_materialSpecularColor = 0;
static GLint projectiveTexture_MVP=0;
static GLint projectiveTexture_lightPosIn=0;
static GLint projectiveTexture_cameraPositionIn = 0;
static GLint projectiveTexture_materialShininessIn = 0;
static GLint projectiveTexture_ProjectionMatrix=0;
static GLint projectiveTexture_TextureMVP=0;
static GLint projectiveTexture_uniform_texture_diffuse = 0;
static GLint projectiveTexture_shadowMap = 0;
static GLint ModelViewMatrix=0;
static GLint ProjectionMatrix=0;
static GLint regularLightDirIn=0;
@@ -1215,7 +1233,24 @@ void GLInstancingRenderer::InitShaders()
glGetIntegerv(GL_SMOOTH_LINE_WIDTH_RANGE, lineWidthRange);
projectiveTextureInstancingShader = gltLoadShaderPair(projectiveTextureInstancingVertexShader,projectiveTextureInstancingFragmentShader);
glLinkProgram(projectiveTextureInstancingShader);
glUseProgram(projectiveTextureInstancingShader);
projectiveTexture_ViewMatrixInverse = glGetUniformLocation(projectiveTextureInstancingShader, "ViewMatrixInverse");
projectiveTexture_ModelViewMatrix = glGetUniformLocation(projectiveTextureInstancingShader, "ModelViewMatrix");
projectiveTexture_lightSpecularIntensity = glGetUniformLocation(projectiveTextureInstancingShader, "lightSpecularIntensityIn");
projectiveTexture_materialSpecularColor = glGetUniformLocation(projectiveTextureInstancingShader, "materialSpecularColorIn");
projectiveTexture_MVP = glGetUniformLocation(projectiveTextureInstancingShader, "MVP");
projectiveTexture_ProjectionMatrix = glGetUniformLocation(projectiveTextureInstancingShader, "ProjectionMatrix");
projectiveTexture_TextureMVP = glGetUniformLocation(projectiveTextureInstancingShader, "TextureMVP");
projectiveTexture_uniform_texture_diffuse = glGetUniformLocation(projectiveTextureInstancingShader, "Diffuse");
projectiveTexture_shadowMap = glGetUniformLocation(projectiveTextureInstancingShader,"shadowMap");
projectiveTexture_lightPosIn = glGetUniformLocation(projectiveTextureInstancingShader,"lightPosIn");
projectiveTexture_cameraPositionIn = glGetUniformLocation(projectiveTextureInstancingShader,"cameraPositionIn");
projectiveTexture_materialShininessIn = glGetUniformLocation(projectiveTextureInstancingShader,"materialShininessIn");
glUseProgram(0);
useShadowMapInstancingShader = gltLoadShaderPair(useShadowMapInstancingVertexShader,useShadowMapInstancingFragmentShader);
@@ -1538,7 +1573,13 @@ void GLInstancingRenderer::renderScene()
//renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE_REFLECTION);
renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE);
} else
}
else if (useProjectiveTexture)
{
//renderSceneInternal(B3_CREATE_SHADOWMAP_RENDERMODE);
renderSceneInternal(B3_USE_PROJECTIVE_TEXTURE_RENDERMODE);
}
else
{
renderSceneInternal();
}
@@ -1977,11 +2018,16 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode)
reflectionPass = true;
renderMode = B3_USE_SHADOWMAP_RENDERMODE;
}
if (!useShadowMap)
{
renderMode = B3_DEFAULT_RENDERMODE;
}
if (orgRenderMode==B3_USE_PROJECTIVE_TEXTURE_RENDERMODE)
{
renderMode = B3_USE_PROJECTIVE_TEXTURE_RENDERMODE;
}
// glEnable(GL_DEPTH_TEST);
@@ -2010,6 +2056,10 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode)
float depthProjectionMatrix[4][4];
GLfloat depthModelViewMatrix[4][4];
//GLfloat depthModelViewMatrix2[4][4];
// For projective texture mapping
float textureProjectionMatrix[4][4];
GLfloat textureModelViewMatrix[4][4];
// Compute the MVP matrix from the light's point of view
if (renderMode==B3_CREATE_SHADOWMAP_RENDERMODE)
@@ -2120,6 +2170,9 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode)
GLfloat depthBiasMVP[4][4];
b3Matrix4x4Mul(biasMatrix,depthMVP,depthBiasMVP);
// TODO: Expose the projective texture matrix setup. Temporarily set it to be the same as camera view projection matrix.
GLfloat textureMVP[16];
b3Matrix4x4Mul16(m_data->m_projectionMatrix,m_data->m_viewMatrix,textureMVP);
//float m_frustumZNear=0.1;
//float m_frustumZFar=100.f;
@@ -2475,6 +2528,70 @@ b3Assert(glGetError() ==GL_NO_ERROR);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,0);
break;
}
case B3_USE_PROJECTIVE_TEXTURE_RENDERMODE:
{
if ( gfxObj->m_flags&eGfxTransparency)
{
glDepthMask(false);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glUseProgram(projectiveTextureInstancingShader);
glUniformMatrix4fv(projectiveTexture_ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]);
glUniform3f(projectiveTexture_lightSpecularIntensity, m_data->m_lightSpecularIntensity[0],m_data->m_lightSpecularIntensity[1],m_data->m_lightSpecularIntensity[2]);
glUniform3f(projectiveTexture_materialSpecularColor, gfxObj->m_materialSpecularColor[0],gfxObj->m_materialSpecularColor[1],gfxObj->m_materialSpecularColor[2]);
float MVP[16];
if (reflectionPass)
{
float tmp[16];
float reflectionMatrix[16] = {1,0,0,0,
0,1,0,0,
0,0,-1,0,
0,0,0,1};
glCullFace(GL_FRONT);
b3Matrix4x4Mul16(m_data->m_viewMatrix,reflectionMatrix,tmp);
b3Matrix4x4Mul16(m_data->m_projectionMatrix,tmp,MVP);
} else
{
b3Matrix4x4Mul16(m_data->m_projectionMatrix,m_data->m_viewMatrix,MVP);
glCullFace(GL_BACK);
}
glUniformMatrix4fv(projectiveTexture_MVP, 1, false, &MVP[0]);
glUniform3f(projectiveTexture_lightPosIn,m_data->m_lightPos[0],m_data->m_lightPos[1],m_data->m_lightPos[2]);
float camPos[3];
m_data->m_activeCamera->getCameraPosition(camPos);
glUniform3f(projectiveTexture_cameraPositionIn,camPos[0],camPos[1],camPos[2]);
glUniform1f(projectiveTexture_materialShininessIn,gfxObj->m_materialShinyNess);
glUniformMatrix4fv(projectiveTexture_TextureMVP, 1, false, &textureMVP[0]);
//sort transparent objects
if ( gfxObj->m_flags&eGfxTransparency)
{
int instanceId = transparentInstances[i].m_instanceId;
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE));
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE));
glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*3*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE+COLOR_BUFFER_SIZE));
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
} else
{
glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances);
}
if ( gfxObj->m_flags&eGfxTransparency)
{
glDisable (GL_BLEND);
glDepthMask(true);
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,0);
break;