add a textured sphere8.obj test with obj loader

fix shaders, so that shadowed and non-shadowed are matching
fix registerGraphicsUnitSphereShape API (level of detail), support textured sphere too
add support for textured cube
add start for a Tutorial.cpp
This commit is contained in:
erwin coumans
2015-08-05 19:03:27 -07:00
parent 250068d5eb
commit a5669d2ffd
29 changed files with 2138 additions and 105 deletions

View File

@@ -214,6 +214,8 @@ static GLint createShadow_depthMVP=0;
static GLint ModelViewMatrix=0;
static GLint ProjectionMatrix=0;
static GLint regularLightDirIn=0;
static GLint uniform_texture_diffuse = 0;
@@ -688,6 +690,8 @@ void GLInstancingRenderer::InitShaders()
ModelViewMatrix = glGetUniformLocation(instancingShader, "ModelViewMatrix");
ProjectionMatrix = glGetUniformLocation(instancingShader, "ProjectionMatrix");
uniform_texture_diffuse = glGetUniformLocation(instancingShader, "Diffuse");
regularLightDirIn = glGetUniformLocation(instancingShader,"lightDirIn");
glUseProgram(0);
instancingShaderPointSprite = gltLoadShaderPair(pointSpriteVertexShader,pointSpriteFragmentShader);
@@ -1559,6 +1563,11 @@ b3Assert(glGetError() ==GL_NO_ERROR);
glUseProgram(instancingShader);
glUniformMatrix4fv(ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]);
glUniformMatrix4fv(ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
b3Vector3 gLightDir = gLightPos;
gLightDir.normalize();
glUniform3f(regularLightDirIn,gLightDir[0],gLightDir[1],gLightDir[2]);
glUniform1i(uniform_texture_diffuse, 0);
glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances);
break;

View File

@@ -26,7 +26,7 @@ void main(void)
vec3 ct,cf;
float intensity,at,af;
intensity = max(dot(lightDir,normalize(normal)),0);
cf = intensity*vec3(1.0,1.0,1.0)+ambient;
cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient;
af = 1.0;
ct = texel.rgb;

View File

@@ -23,7 +23,7 @@ static const char* instancingFragmentShader= \
" vec3 ct,cf;\n"
" float intensity,at,af;\n"
" intensity = max(dot(lightDir,normalize(normal)),0);\n"
" cf = intensity*vec3(1.0,1.0,1.0)+ambient;\n"
" cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient;\n"
" af = 1.0;\n"
" \n"
" ct = texel.rgb;\n"

View File

@@ -13,6 +13,7 @@ layout (location = 6) in vec3 instance_scale;
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
uniform vec3 lightDirIn;
out Fragment
{
@@ -60,17 +61,13 @@ out vec3 lightDir,normal,ambient;
void main(void)
{
vec4 q = instance_quaternion;
ambient = vec3(0.3,.3,0.3);
vec4 local_normal = (quatRotate3( vertexnormal,q));
vec3 light_pos = vec3(-0.3,0.1,0.1);
normal = local_normal.xyz;//normalize(ModelViewMatrix * local_normal).xyz;
lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz));
// lightDir = normalize(vec3(gl_LightSource[0].position));
vec4 axis = vec4(1,1,1,0);
ambient = vec3(0.6,.6,0.6);
vec4 worldNormal = (quatRotate3( vertexnormal,q));
normal = normalize(worldNormal).xyz;
lightDir = lightDirIn;
vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);
vec4 vertexPos = ProjectionMatrix * ModelViewMatrix *(instance_position+localcoord);

View File

@@ -11,6 +11,7 @@ static const char* instancingVertexShader= \
"layout (location = 6) in vec3 instance_scale;\n"
"uniform mat4 ModelViewMatrix;\n"
"uniform mat4 ProjectionMatrix;\n"
"uniform vec3 lightDirIn;\n"
"out Fragment\n"
"{\n"
" vec4 color;\n"
@@ -51,16 +52,13 @@ static const char* instancingVertexShader= \
"void main(void)\n"
"{\n"
" vec4 q = instance_quaternion;\n"
" ambient = vec3(0.3,.3,0.3);\n"
" \n"
" \n"
" vec4 local_normal = (quatRotate3( vertexnormal,q));\n"
" vec3 light_pos = vec3(-0.3,0.1,0.1);\n"
" normal = local_normal.xyz;//normalize(ModelViewMatrix * local_normal).xyz;\n"
" lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz));\n"
"// lightDir = normalize(vec3(gl_LightSource[0].position));\n"
" \n"
" vec4 axis = vec4(1,1,1,0);\n"
" ambient = vec3(0.6,.6,0.6);\n"
" \n"
" vec4 worldNormal = (quatRotate3( vertexnormal,q));\n"
" normal = normalize(worldNormal).xyz;\n"
" \n"
" lightDir = lightDirIn;\n"
" \n"
" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n"
" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix *(instance_position+localcoord);\n"
" gl_Position = vertexPos;\n"

View File

@@ -23,13 +23,13 @@ out vec4 color;
void main(void)
{
vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color;
vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);
vec3 ct,cf;
float intensity,at,af;
intensity = clamp( dot( normalize(normal),lightDir ), 0,1 );
cf = ambient;
af = 1.0;
ct = texel.rgb;
@@ -43,14 +43,9 @@ void main(void)
float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z-bias)/ShadowCoord.w));
intensity*=2;
if (intensity>1)
intensity=1.f;
intensity = 0.7*intensity + 0.3*intensity*visibility;
visibility *= intensity;
if (visibility<0.6)
visibility=0.6f;
cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient;
color = vec4(ct * visibility, fragment.color.w);
color = vec4(ct * cf, fragment.color.w);
}

View File

@@ -17,13 +17,13 @@ static const char* useShadowMapInstancingFragmentShader= \
"out vec4 color;\n"
"void main(void)\n"
"{\n"
" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color;\n"
" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);\n"
" vec3 ct,cf;\n"
" float intensity,at,af;\n"
" \n"
" intensity = clamp( dot( normalize(normal),lightDir ), 0,1 );\n"
" \n"
" cf = ambient;\n"
" \n"
" af = 1.0;\n"
" \n"
" ct = texel.rgb;\n"
@@ -35,15 +35,10 @@ static const char* useShadowMapInstancingFragmentShader= \
" bias = clamp(bias, 0,0.01);\n"
" float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z-bias)/ShadowCoord.w));\n"
" \n"
" intensity*=2;\n"
" if (intensity>1)\n"
" intensity=1.f;\n"
" intensity = 0.7*intensity + 0.3*intensity*visibility;\n"
" \n"
" visibility *= intensity;\n"
" \n"
" if (visibility<0.6)\n"
" visibility=0.6f;\n"
" cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient;\n"
" \n"
" color = vec4(ct * visibility, fragment.color.w);\n"
" color = vec4(ct * cf, fragment.color.w);\n"
"}\n"
;

View File

@@ -65,7 +65,7 @@ out vec3 lightDir,normal,ambient;
void main(void)
{
vec4 q = instance_quaternion;
ambient = vec3(0.3,.3,0.3);
ambient = vec3(0.6,.6,0.6);
vec4 worldNormal = (quatRotate3( vertexnormal,q));
@@ -73,7 +73,7 @@ void main(void)
lightDir = lightDirIn;
vec4 axis = vec4(1,1,1,0);
vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);
vec4 vertexPos = MVP* vec4((instance_position+localcoord).xyz,1);

View File

@@ -55,14 +55,14 @@ static const char* useShadowMapInstancingVertexShader= \
"void main(void)\n"
"{\n"
" vec4 q = instance_quaternion;\n"
" ambient = vec3(0.3,.3,0.3);\n"
" ambient = vec3(0.6,.6,0.6);\n"
" \n"
" vec4 worldNormal = (quatRotate3( vertexnormal,q));\n"
" \n"
" normal = normalize(worldNormal).xyz;\n"
" lightDir = lightDirIn;\n"
" \n"
" vec4 axis = vec4(1,1,1,0);\n"
" \n"
" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n"
" vec4 vertexPos = MVP* vec4((instance_position+localcoord).xyz,1);\n"
" gl_Position = vertexPos;\n"

View File

@@ -19,11 +19,11 @@ public:
virtual void swapBuffer();
virtual void drawText( const char* txt, int posX, int posY);
virtual void setBackgroundColor(float red, float green, float blue);
virtual int registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ)
virtual int registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ, int textureIndex = -1)
{
return 0;
}
virtual int registerGraphicsSphereShape(float radius, bool usePointSprites, int largeSphereThreshold, int mediumSphereThreshold)
virtual int registerGraphicsUnitSphereShape(EnumSphereLevelOfDetail lod, int textureId=-1)
{
return 0;
}

View File

@@ -46,6 +46,10 @@ struct SimpleOpenGL2Renderer : public CommonRenderInterface
{
return m_height;
}
virtual int registerTexture(const unsigned char* texels, int width, int height)
{
return -1;
}
virtual int registerGraphicsInstance(int shapeIndex, const double* position, const double* quaternion, const double* color, const double* scaling);
virtual int registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling);

View File

@@ -419,7 +419,7 @@ struct GfxVertex
float u,v;
};
int SimpleOpenGL3App::registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ)
int SimpleOpenGL3App::registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ, int textureIndex)
{
@@ -442,7 +442,7 @@ int SimpleOpenGL3App::registerCubeShape(float halfExtentsX,float halfExtentsY, f
verts[i].v = cube_vertices[i*9+8];
}
int shapeId = m_instancingRenderer->registerShape(&verts[0].x,numVertices,cube_indices,numIndices);
int shapeId = m_instancingRenderer->registerShape(&verts[0].x,numVertices,cube_indices,numIndices,B3_GL_TRIANGLES,textureIndex);
return shapeId;
}
@@ -480,42 +480,46 @@ void SimpleOpenGL3App::registerGrid(int cells_x, int cells_z, float color0[4], f
}
int SimpleOpenGL3App::registerGraphicsSphereShape(float radius, bool usePointSprites, int largeSphereThreshold, int mediumSphereThreshold)
int SimpleOpenGL3App::registerGraphicsUnitSphereShape(EnumSphereLevelOfDetail lod, int textureId)
{
int strideInBytes = 9*sizeof(float);
int graphicsShapeIndex = -1;
if (radius>=largeSphereThreshold)
switch (lod)
{
int numVertices = sizeof(detailed_sphere_vertices)/strideInBytes;
int numIndices = sizeof(detailed_sphere_indices)/sizeof(int);
graphicsShapeIndex = m_instancingRenderer->registerShape(&detailed_sphere_vertices[0],numVertices,detailed_sphere_indices,numIndices);
} else
{
if (usePointSprites)
case SPHERE_LOD_POINT_SPRITE:
{
int numVertices = sizeof(point_sphere_vertices)/strideInBytes;
int numIndices = sizeof(point_sphere_indices)/sizeof(int);
graphicsShapeIndex = m_instancingRenderer->registerShape(&point_sphere_vertices[0],numVertices,point_sphere_indices,numIndices,B3_GL_POINTS);
} else
{
if (radius>=mediumSphereThreshold)
{
int numVertices = sizeof(medium_sphere_vertices)/strideInBytes;
int numIndices = sizeof(medium_sphere_indices)/sizeof(int);
graphicsShapeIndex = m_instancingRenderer->registerShape(&medium_sphere_vertices[0],numVertices,medium_sphere_indices,numIndices);
} else
{
int numVertices = sizeof(low_sphere_vertices)/strideInBytes;
int numIndices = sizeof(low_sphere_indices)/sizeof(int);
graphicsShapeIndex = m_instancingRenderer->registerShape(&low_sphere_vertices[0],numVertices,low_sphere_indices,numIndices);
}
graphicsShapeIndex = m_instancingRenderer->registerShape(&point_sphere_vertices[0],numVertices,point_sphere_indices,numIndices,B3_GL_POINTS,textureId);
break;
}
}
case SPHERE_LOD_LOW:
{
int numVertices = sizeof(low_sphere_vertices)/strideInBytes;
int numIndices = sizeof(low_sphere_indices)/sizeof(int);
graphicsShapeIndex = m_instancingRenderer->registerShape(&low_sphere_vertices[0],numVertices,low_sphere_indices,numIndices,B3_GL_TRIANGLES,textureId);
break;
}
case SPHERE_LOD_MEDIUM:
{
int numVertices = sizeof(medium_sphere_vertices)/strideInBytes;
int numIndices = sizeof(medium_sphere_indices)/sizeof(int);
graphicsShapeIndex = m_instancingRenderer->registerShape(&medium_sphere_vertices[0],numVertices,medium_sphere_indices,numIndices,B3_GL_TRIANGLES,textureId);
break;
}
case SPHERE_LOD_HIGH:
default:
{
int numVertices = sizeof(detailed_sphere_vertices)/strideInBytes;
int numIndices = sizeof(detailed_sphere_indices)/sizeof(int);
graphicsShapeIndex = m_instancingRenderer->registerShape(&detailed_sphere_vertices[0],numVertices,detailed_sphere_indices,numIndices,B3_GL_TRIANGLES,textureId);
break;
}
};
return graphicsShapeIndex;
}

View File

@@ -19,8 +19,8 @@ struct SimpleOpenGL3App : public CommonGraphicsApp
SimpleOpenGL3App(const char* title, int width,int height);
virtual ~SimpleOpenGL3App();
virtual int registerCubeShape(float halfExtentsX=1.f,float halfExtentsY=1.f, float halfExtentsZ = 1.f);
virtual int registerGraphicsSphereShape(float radius, bool usePointSprites=true, int largeSphereThreshold=100, int mediumSphereThreshold=10);
virtual int registerCubeShape(float halfExtentsX=1.f,float halfExtentsY=1.f, float halfExtentsZ = 1.f, int textureIndex = -1);
virtual int registerGraphicsUnitSphereShape(EnumSphereLevelOfDetail lod, int textureId=-1);
virtual void registerGrid(int xres, int yres, float color0[4], float color1[4]);
void dumpNextFrameToPng(const char* pngFilename);
void dumpFramesToVideo(const char* mp4Filename);