Commit 75e0b335 authored by Jan Möbius's avatar Jan Möbius
Browse files

Basic patches for better geometry shader support.

Additional Shaders to support Line width via geometry shader

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@17513 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 386f492f
......@@ -747,13 +747,10 @@ int ShaderProgGenerator::checkForIncludes(QString _str, ShaderGenerator* _gen, Q
void ShaderProgGenerator::buildGeometryShader()
{
// Only build a geometry shader if enabled
if ( !desc_.geometryShader )
return;
std::cerr << "TODO : buildGeometryShader for object\n " << std::endl;
desc_.toString();
delete geometry_;
......@@ -761,7 +758,6 @@ void ShaderProgGenerator::buildGeometryShader()
geometry_->initGeometryShaderIO(&desc_);
geometry_->initDefaultUniforms();
......@@ -773,34 +769,72 @@ void ShaderProgGenerator::buildGeometryShader()
}
// initGenDefines(fragment_);
//
//
//
// // io
//
// // when to use fragment lights
// if (desc_.shadeMode == SG_SHADE_PHONG)
// {
// for (int i = 0; i < desc_.numLights; ++i)
// fragment_->addLight(i, desc_.lightTypes[i]);
// }
//
//
// assemble main function
QStringList mainCode;
QStringList mainCode;
// add simple io passthrough mapper
{
mainCode.push_back("void sg_MapIO(const int inIdx)");
mainCode.push_back("{");
mainCode.push_back("gl_Position = gl_in[inIdx].gl_Position;");
mainCode.push_back("outGeometryPosCS = outVertexPosCS[inIdx];");
if (desc_.textured())
mainCode.push_back("outGeometryTexCoord = outVertexTexCoord[inIdx];");
if (desc_.shadeMode == SG_SHADE_GOURAUD ||
desc_.shadeMode == SG_SHADE_FLAT ||
desc_.vertexColors)
mainCode.push_back("outGeometryColor = outVertexColor[inIdx];");
if (desc_.shadeMode == SG_SHADE_PHONG) {
mainCode.push_back("outGeometryNormal = outVertexNormal[inIdx];");
mainCode.push_back("outGeometryPosVS = outVertexPosVS[inIdx];");
}
mainCode.push_back("}");
}
// addLightingFunctions(&mainCode);
//
//
if (!geometryTemplate_.size())
{
mainCode.push_back("void main()");
mainCode.push_back("{");
addGeometryBeginCode(&mainCode);
addGeometryEndCode(&mainCode);
// num input vertices
int numInputVerts = 0;
switch (desc_.geometryShaderInput) {
case SG_GEOMETRY_IN_TRIANGLES:
numInputVerts = 3;
break;
case SG_GEOMETRY_IN_LINES:
numInputVerts = 2;
break;
case SG_GEOMETRY_IN_POINTS:
numInputVerts = 1;
break;
default:
numInputVerts = 1;
break;
}
QString strValNum;
strValNum.append(QString("%1").arg(numInputVerts));
mainCode.push_back(QString("for ( int inIdx = 0; inIdx < ") + strValNum + QString("; ++inIdx ){"));
mainCode.push_back("sg_MapIO( inIdx );");
mainCode.push_back("EmitVertex();");
mainCode.push_back("}");
mainCode.push_back("EndPrimitive();");
mainCode.push_back("}");
}
......@@ -817,7 +851,7 @@ void ShaderProgGenerator::buildGeometryShader()
// check for SG_ markers
if (it.contains("SG_GEOMETRY_BEGIN"))
addFragmentBeginCode(&mainCode);
addGeometryBeginCode(&mainCode);
else
{
if (it.contains("SG_GEOMETRY_END"))
......@@ -839,37 +873,11 @@ void ShaderProgGenerator::buildGeometryShader()
geometry_->buildShaderCode(&mainCode);
}
void ShaderProgGenerator::addGeometryBeginCode(QStringList* _code)
{
std::cerr << "TODO : addGeometryBeginCode" << std::endl;
// // support for projective texture mapping
// _code->push_back("vec2 sg_vScreenPos = outPosCS.xy / outPosCS.w * 0.5 + vec2(0.5, 0.5);");
//
// _code->push_back("vec4 sg_cColor = vec4(g_cEmissive, ALPHA);");
//
// if (desc_.shadeMode == SG_SHADE_GOURAUD ||
// desc_.shadeMode == SG_SHADE_FLAT ||
// desc_.vertexColors)
// _code->push_back("sg_cColor = outColor;");
//
//
// if (desc_.shadeMode == SG_SHADE_PHONG)
// {
// _code->push_back("vec4 sg_vPosVS = outPosVS;");
// _code->push_back("vec3 sg_vNormalVS = outNormal;");
//
// addLightingCode(_code);
// }
//
// if (desc_.textured())
// {
// _code->push_back("vec4 sg_cTex = texture(g_Texture0, outTexCoord);");
// _code->push_back("sg_cColor *= sg_cTex;");
// }
//
//
// std::cerr << "TODO : addGeometryBeginCode" << std::endl;
// apply modifiers
for (int i = 0; i < numModifiers_; ++i)
{
......@@ -880,10 +888,6 @@ void ShaderProgGenerator::addGeometryBeginCode(QStringList* _code)
void ShaderProgGenerator::addGeometryEndCode(QStringList* _code)
{
std::cerr << "TODO : addGeometryEndCode" << std::endl;
// _code->push_back("outFragment = sg_cColor;");
//
// apply modifiers
for (int i = 0; i < numModifiers_; ++i)
{
......@@ -1192,7 +1196,7 @@ void ShaderProgGenerator::setShaderDir( QString _dir )
QString ShaderProgGenerator::getShaderDir()
{
return shaderDir_;
return shaderDir_ + QString("/");
}
......
......@@ -544,9 +544,9 @@ getRenderObjects(ACG::IRenderer* _renderer, ACG::GLState& _state , const ACG::S
// The first point is mapped to an additional last point in buffer, so we can
// just Render one point more to get a closed line
if ( polyline_.is_closed() )
ro.glDrawArrays(GL_LINE_STRIP, 0, polyline_.n_vertices() + 1);
ro.glDrawArrays(GL_LINE_STRIP_ADJACENCY, 0, polyline_.n_vertices() + 1);
else
ro.glDrawArrays(GL_LINE_STRIP, 0, polyline_.n_vertices());
ro.glDrawArrays(GL_LINE_STRIP_ADJACENCY, 0, polyline_.n_vertices());
_renderer->addRenderObject(&ro);
......
// template file for shader generator
in vec3 outGeometryBary;
float edgeFactor()
{
// compute smoothed distance to edge, source:
// http://codeflow.org/entries/2012/aug/02/easy-wireframe-display-with-barycentric-coordinates/
vec3 d = fwidth(outGeometryBary);
vec3 a3 = smoothstep(vec3(0.0), d*1.5, outGeometryBary);
return min(min(a3.x, a3.y), a3.z);
}
void main()
{
SG_FRAGMENT_BEGIN
float t = 1-edgeFactor();
// use alpha blending to fade out
sg_cColor.a = t;
// without alpha blending:
// sg_cColor.xyz *= t; //mix(vec3(0.0), sg_cColor.xyz, t);
// discard pixels inside face
if (t < 0.1)
discard;
SG_FRAGMENT_END
}
\ No newline at end of file
// template file for shader generator
in vec3 outGeometryBary;
float edgeFactor()
{
// compute smoothed distance to edge, source:
// http://codeflow.org/entries/2012/aug/02/easy-wireframe-display-with-barycentric-coordinates/
vec3 d = fwidth(outGeometryBary);
vec3 a3 = smoothstep(vec3(0.0), d*1.5, outGeometryBary);
return min(min(a3.x, a3.y), a3.z);
}
void main()
{
SG_FRAGMENT_BEGIN
sg_cColor.xyz = mix(vec3(0.0), sg_cColor.xyz, 1-edgeFactor());
SG_FRAGMENT_END
}
\ No newline at end of file
// template file for shader generator
void main()
{
const float lineWidth = 0.005;
const vec2 screenSize = vec2(1.0, 1.0);
// start/end points of line
vec4 p0 = gl_in[0].gl_Position;
vec4 p1 = gl_in[1].gl_Position;
// convert to screen space
p0.xy = p0.xy / p0.w * screenSize;
p1.xy = p1.xy / p1.w * screenSize;
// compute dir and normal
vec2 lineDir = p1.xy - p0.xy;
vec2 lineNormal = normalize(vec2(-lineDir.y, lineDir.x));
// create screen-aligned quad
vec2 offset = lineNormal * lineWidth;
sg_MapIO(0); // default IO mapping
gl_Position = vec4( (p0 + offset) * p0.w / screenSize, p0.z, p0.w);
EmitVertex();
sg_MapIO(0); // default IO mapping
gl_Position = vec4( (p0 - offset) * p0.w / screenSize, p0.z, p0.w);
EmitVertex();
sg_MapIO(1); // default IO mapping
gl_Position = vec4( (p1 + offset) * p1.w / screenSize, p1.z, p1.w);
EmitVertex();
sg_MapIO(1); // default IO mapping
gl_Position = vec4( (p1 - offset) * p1.w / screenSize, p1.z, p1.w);
EmitVertex();
EndPrimitive();
}
\ No newline at end of file
// template file for shader generator
// send barycentric coords to fragment
out vec3 outGeometryBary;
void main()
{
sg_MapIO(0); // IO mapping provided by ShaderGen, send vertex 0
outGeometryBary = vec3(1, 0, 0);
EmitVertex();
sg_MapIO(1); // IO mapping provided by ShaderGen, send vertex 1
outGeometryBary = vec3(0, 1, 0);
EmitVertex();
sg_MapIO(2); // IO mapping provided by ShaderGen, send vertex 2
outGeometryBary = vec3(0, 0, 1);
EmitVertex();
EndPrimitive();
}
\ No newline at end of file
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