Commit 9442788d authored by Seth Carter's avatar Seth Carter

Merge branch '34-use-map_d-to-determine-whether-fragment-should-be-discarded' into 'master'

Resolve "Use map_d to determine whether fragment should be discarded"

Closes #34

See merge request rsethc/slice!19
parents 0cee675f b19b6577
Pipeline #244115078 passed with stage
in 1 minute and 11 seconds
......@@ -274,6 +274,7 @@ struct s3dMesh
slTexture* ambient_texref;
slTexture* diffuse_texref;
slTexture* specular_texref;
slTexture* opacity_texref;
//float* vertices;
GLuint vertex_buffer,vertex_array;
uint32_t triangle_count;
......
......@@ -33,13 +33,17 @@ struct objColor
struct objMaterial
{
char* name; // freed after loading is complete, but during loading is guaranteed to exist.
objColor ambient_color, diffuse_color, specular_color;
objColor
ambient_color,
diffuse_color,
specular_color;
float specular_exponent;
char mag_filter_nearest;
char* ambient_texpath; // will be NULL if not specified by the file
char* diffuse_texpath; // will be NULL if not specified by the file
char* specular_texpath; // will be NULL if not specified by the file
char* opacity_texpath; // will be NULL if not specified by the file
char* bump_texpath; // will be NULL if not specified by the file
char mag_filter_nearest;
};
extern objMaterial objFallbackMaterial;
struct objGroup
......
#include <slice3d/load-obj.h>
objMaterial objFallbackMaterial =
{
NULL,
{1,1,1},{1,1,1},{1,1,1},
250,
NULL,
NULL,
0
NULL, // name
{1,1,1}, // ambient_color
{1,1,1}, // diffuse_color
{1,1,1}, // specular_color
250, // specular_exponent
0, // mag_filter_nearest
NULL, // ambient_texpath
NULL, // diffuse_texpath
NULL, // specular_texpath
NULL, // opacity_texpath
NULL, // bump_texpath
};
void objDiscardRemainingLine (FILE* file)
{
......@@ -203,18 +208,18 @@ int objReadMaterials (FILE* file, objModel* model, int& material_capacity, char*
*current_material = objFallbackMaterial; // Sets default colors, and initializes char*'s to NULL.
objLexString(file,&current_material->name);
}
else if (!strcmp(keyword,"Na"))
else if (!strcmp(keyword,"Ns"))
{
free(keyword);
if (!current_material)
{
printf("'Na' without prior 'newmtl'\n");
printf("'Ns' without prior 'newmtl'\n");
return 1;
}
if (objLexFloat(file,&current_material->specular_exponent))
{
printf("value missing after 'Na'");
printf("value missing after 'Ns'");
return 1;
}
}
......@@ -314,6 +319,27 @@ int objReadMaterials (FILE* file, objModel* model, int& material_capacity, char*
current_material->specular_texpath = objFilePath(basepath,filename);
if (basepath) free(filename); // only free if texpath was separately allocated
}
else if (!strcmp(keyword,"map_d"))
{
free(keyword);
if (!current_material)
{
printf("'map_d' without prior 'newmtl'\n");
return 1;
}
if (current_material->opacity_texpath)
{
printf("multiple map_d lines for the same material\n");
return 1;
}
char* filename;
objLexString(file,&filename);
objForwardSlashes(filename);
current_material->opacity_texpath = objFilePath(basepath,filename);
if (basepath) free(filename); // only free if texpath was separately allocated
}
else if (!strcmp(keyword,"bump") || !strcmp(keyword,"map_bump"))
{
free(keyword);
......
......@@ -258,6 +258,39 @@ s3dVec3 cross (s3dVec3 a, s3dVec3 b)
a.x * b.y - a.y * b.x
);
}
void s3dMeshMaterialInfo (s3dMesh* outmesh, objMaterial* mat)
{
outmesh->ambient_color = mat->ambient_color;
outmesh->diffuse_color = mat->diffuse_color;
outmesh->specular_color = mat->specular_color;
outmesh->specular_exponent = mat->specular_exponent;
outmesh->mag_filter_nearest = mat->mag_filter_nearest;
if (mat->ambient_texpath)
{
outmesh->ambient_texref = slLoadTexture(mat->ambient_texpath);
outmesh->ambient_texref->Reserve();
}
else outmesh->ambient_texref = NULL;
if (mat->diffuse_texpath)
{
outmesh->diffuse_texref = slLoadTexture(mat->diffuse_texpath);
outmesh->diffuse_texref->Reserve();
}
else outmesh->diffuse_texref = NULL;
if (mat->specular_texpath)
{
outmesh->specular_texref = slLoadTexture(mat->specular_texpath);
outmesh->specular_texref->Reserve();
}
else outmesh->specular_texref = NULL;
if (mat->opacity_texpath)
{
outmesh->opacity_texref = slLoadTexture(mat->opacity_texpath);
outmesh->opacity_texref->Reserve();
}
else outmesh->opacity_texref = NULL;
}
s3dModel* s3dSpriteModel (objMaterial material)
{
s3dModel* model = s3dModelAllocator.Allocate();
......@@ -282,17 +315,7 @@ s3dModel* s3dSpriteModel (objMaterial material)
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
mesh->ambient_color = material.ambient_color;
mesh->diffuse_color = material.diffuse_color;
mesh->specular_color = material.specular_color;
mesh->ambient_texref = slLoadTexture(material.ambient_texpath);
mesh->ambient_texref->Reserve();
mesh->diffuse_texref = slLoadTexture(material.diffuse_texpath);
mesh->diffuse_texref->Reserve();
mesh->specular_texref = slLoadTexture(material.specular_texpath);
mesh->specular_texref->Reserve();
mesh->specular_exponent = material.specular_exponent;
mesh->mag_filter_nearest = material.mag_filter_nearest;
s3dMeshMaterialInfo(mesh,&material);
return model;
}
......@@ -420,31 +443,7 @@ s3dModel* s3dLoadObj (char* basepath, char* filename)
s3dMesh* outmesh = output->meshes + obj_id;
objMaterial* mat = group->material;
outmesh->ambient_color = mat->ambient_color;
outmesh->diffuse_color = mat->diffuse_color;
outmesh->specular_color = mat->specular_color;
outmesh->specular_exponent = mat->specular_exponent;
outmesh->mag_filter_nearest = mat->mag_filter_nearest;
if (mat->ambient_texpath)
{
outmesh->ambient_texref = slLoadTexture(mat->ambient_texpath);
outmesh->ambient_texref->Reserve();
}
else outmesh->ambient_texref = NULL;
if (mat->diffuse_texpath)
{
outmesh->diffuse_texref = slLoadTexture(mat->diffuse_texpath);
outmesh->diffuse_texref->Reserve();
}
else outmesh->diffuse_texref = NULL;
if (mat->specular_texpath)
{
outmesh->specular_texref = slLoadTexture(mat->specular_texpath);
outmesh->specular_texref->Reserve();
}
else outmesh->specular_texref = NULL;
s3dMeshMaterialInfo(outmesh,group->material);
outmesh->triangle_count = tricount;
s3dInitMeshBuffer(outmesh);
......@@ -658,13 +657,22 @@ GLuint
s3dProgram_FogVoxelingSize;
slTexture* s3dWhiteTexture;
inline slTexture* s3dTexOrWhite (slTexture* texref)
inline slForceInline slTexture* s3dTexOrWhite (slTexture* texref)
{
if (!texref) return s3dWhiteTexture;
if (!texref->ready) return s3dWhiteTexture;
return texref;
}
GLfloat s3dTextureAnisotropicValue;
inline slForceInline void s3dUseTex (int texid, slTexture* texref, int mag_filter)
{
glActiveTexture(GL_TEXTURE0 + texid);
glBindTexture(GL_TEXTURE_2D,texref->tex);
slSetTextureClamping(GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,mag_filter);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAX_ANISOTROPY_EXT,s3dTextureAnisotropicValue);
}
void s3dRender ()
{
//printf("Drawing 3D\n");
......@@ -693,32 +701,18 @@ void s3dRender ()
slTexture* ambient_texref = s3dTexOrWhite(mesh->ambient_texref);
slTexture* diffuse_texref = s3dTexOrWhite(mesh->diffuse_texref);
slTexture* specular_texref = s3dTexOrWhite(mesh->specular_texref);
slTexture* opacity_texref = s3dTexOrWhite(mesh->opacity_texref);
int mag_filter = mesh->mag_filter_nearest ? GL_NEAREST : GL_LINEAR;
glUniform3fv(s3dProgram_MaterialAmbientColor,1,(GLfloat*)&mesh->ambient_color);
glUniform3fv(s3dProgram_MaterialDiffuseColor,1,(GLfloat*)&mesh->diffuse_color);
glUniform3fv(s3dProgram_MaterialSpecularColor,1,(GLfloat*)&mesh->specular_color);
glUniform1f(s3dProgram_MaterialSpecularExponent,mesh->specular_exponent);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D,ambient_texref->tex);
slSetTextureClamping(GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,mesh->mag_filter_nearest ? GL_NEAREST : GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAX_ANISOTROPY_EXT,s3dTextureAnisotropicValue);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D,diffuse_texref->tex);
slSetTextureClamping(GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,mesh->mag_filter_nearest ? GL_NEAREST : GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAX_ANISOTROPY_EXT,s3dTextureAnisotropicValue);
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D,specular_texref->tex);
slSetTextureClamping(GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,mesh->mag_filter_nearest ? GL_NEAREST : GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAX_ANISOTROPY_EXT,s3dTextureAnisotropicValue);
s3dUseTex(0,ambient_texref,mag_filter);
s3dUseTex(1,diffuse_texref,mag_filter);
s3dUseTex(2,specular_texref,mag_filter);
s3dUseTex(3,opacity_texref,mag_filter);
//printf("\t\tDrawing Mesh\n");
glBindVertexArray(mesh->vertex_array);
......@@ -768,6 +762,7 @@ in vec3 FragNormal;
uniform sampler2D AmbientTex;
uniform sampler2D DiffuseTex;
uniform sampler2D SpecularTex;
uniform sampler2D OpacityTex;
uniform vec3 CameraPos;
uniform bool PhongEnabled;
......@@ -797,9 +792,11 @@ uniform float SpecularVoxelingSize;
void main ()
{
vec4 ambient_tex_color = texture(AmbientTex,FragTexCoords);
if (ambient_tex_color.a == 0) discard;
float opacity = texture(OpacityTex,FragTexCoords).r;
if (opacity == 0) discard;
gl_FragColor.a = 1;
vec3 ambient_tex_color = texture(AmbientTex,FragTexCoords).rgb;
vec3 adjusted_world_pos;
vec3 before_flooring = FragWorldPos - normalize(FragNormal)*.01; // trying to prevent weird chattering when exactly at a voxel boundary
......@@ -809,7 +806,7 @@ void main ()
vec3 diffuse_tex_color = texture(DiffuseTex,FragTexCoords).rgb;
vec3 specular_tex_color = texture(SpecularTex,FragTexCoords).rgb;
gl_FragColor.rgb = EnvironmentAmbientColor * MaterialAmbientColor * ambient_tex_color.rgb;
gl_FragColor.rgb = EnvironmentAmbientColor * MaterialAmbientColor * ambient_tex_color;
if (DiffuseVoxelingEnabled) adjusted_world_pos = (floor(before_flooring / DiffuseVoxelingSize)+.5) * DiffuseVoxelingSize;
else adjusted_world_pos = FragWorldPos;
......@@ -843,7 +840,7 @@ void main ()
gl_FragColor.rgb += (diffuse + specular) * directional_light_diff * LightColor * light_intensity;
}
else gl_FragColor.rgb = ambient_tex_color.rgb;
else gl_FragColor.rgb = ambient_tex_color;
if (DistanceFogEnabled)
{
......@@ -880,6 +877,7 @@ void s3dInitShaders ()
glUniform1i(slLocateUniform(s3dProgram,"AmbientTex"),0);
glUniform1i(slLocateUniform(s3dProgram,"DiffuseTex"),1);
glUniform1i(slLocateUniform(s3dProgram,"SpecularTex"),2);
glUniform1i(slLocateUniform(s3dProgram,"OpacityTex"),3);
s3dProgram_CameraPos = slLocateUniform(s3dProgram,"CameraPos");
s3dProgram_PhongEnabled = slLocateUniform(s3dProgram,"PhongEnabled");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment