Update
So by printing the compiled shader code from the QShaderProgramBuilder
I found out that the shader nearly works and the only issue is the call to renderClicks
. This line
fragColor = (((renderClicks(clicks[10], colors[10], (((((((phongFunction(ka, kd, ks, shininess, worldPosition, normalize(((eyePosition - worldPosition))), normalize(worldNormal)))))))))))));
should look like this
fragColor = (((renderClicks(clicks, colors, (((((((phongFunction(ka, kd, ks, shininess, worldPosition, normalize(((eyePosition - worldPosition))), normalize(worldNormal)))))))))))));
but since the names of the uniforms are clicks[10]
and colors[10]
I'm not sure if that is fixable.
The Problem
I'm using QSceneLoader
in my project to load objects automatically. Now I want to visualize clicks on the 3D surface of the objects. To this end, I'm traversing the entity hierarchy created by the scene loader until I reach a QShaderProgramBuilder
. Next, I'm replacing the fragment shader graph in the shader program builder using setFragmentShaderGraph
- you cannot supply a textfile containing normal shader code unfortunately.
I used the default phong graph from GitHub as a starting point and added my own uniforms - the clicks and the colors of the clicks, both vec3
arrays. Unfortunately, the shader doesn't work until I remove the array definition from both.
When I remove the array definition everwhere (i.e. vec3 clicks
instead of vec3 clicks[10]
) and pass a QVector3D
instead of a QVector<QVector3D>
I get the normal scene rendered.
I also can't find the Qt3D node editor mentioned here but I doubt that it would solve my problem because in the videos that are on YouTube there don't seem to be any options to declare an array.
So, any ideas how I can properly use uniform arrays in the shader graph?
Note: I already checked the OpenGL version which is 4.6 so that shouldn't be the issue.
Note 2: If someone needs a sample project let me know. I started to create one but it became rather large even though I tried to keep it small. It's not finished yet but I could complete it and upload it to GitHub.
The Code
This is the way I'm supplying the data from C++ as a parameter:
Qt3DRender::QParameter *clicksParameter = new Qt3DRender::QParameter(QStringLiteral("clicks"), m_clicks.constData());
I altered the shader graph to include the following new prototype
:
"prototypes": {
"renderClicks": {
"inputs": ["clicks[10]", "colors[10]", "currentColor"],
"outputs": ["result"],
"parameters": {
"type": {
"type": "QShaderLanguage::VariableType",
"value": "QShaderLanguage::Vec4"
}
},
"rules": [{
"format": {
"api": "OpenGLCoreProfile",
"major": 3,
"minor": 0
},
"headerSnippets": [
"#pragma include :shaders/poseeditor/clicksRendering.inc.frag"
],
"substitution": "vec4 $result = renderClicks($clicks, $colors, $currentColor);"
}]
}
},
"nodes": ...
which references the following shader code in clicksRendering.inc.frag
:
vec4 renderClicks(vec3 clicks[10], vec3 colors[10], vec4 currentColor) {
return currentColor;
}
Then I added the following new nodes
:
...
{
"uuid": "{00000000-0000-0000-0000-000000000026}",
"type": "input",
"parameters": {
"name": "clicks[10]",
"qualifier": {
"type": "QShaderLanguage::StorageQualifier",
"value": "QShaderLanguage::Uniform"
},
"type": {
"type": "QShaderLanguage::VariableType",
"value": "QShaderLanguage::Vec3"
}
}
},
{
"uuid": "{00000000-0000-0000-0000-000000000027}",
"type": "input",
"parameters": {
"name": "colors[10]",
"qualifier": {
"type": "QShaderLanguage::StorageQualifier",
"value": "QShaderLanguage::Uniform"
},
"type": {
"type": "QShaderLanguage::VariableType",
"value": "QShaderLanguage::Vec3"
}
}
},
{
"uuid": "{00000000-0000-0000-0000-000000000028}",
"type": "renderClicks"
},
...
I also altered the last edges such that the final phong color is passed through my custom function:
...
{
"sourceUuid": "{00000000-0000-0000-0000-000000000026}",
"sourcePort": "value",
"targetUuid": "{00000000-0000-0000-0000-000000000028}",
"targetPort": "clicks"
},
{
"sourceUuid": "{00000000-0000-0000-0000-000000000027}",
"sourcePort": "value",
"targetUuid": "{00000000-0000-0000-0000-000000000028}",
"targetPort": "colors"
},
{
"sourceUuid": "{00000000-0000-0000-0000-000000000024}",
"sourcePort": "outputColor",
"targetUuid": "{00000000-0000-0000-0000-000000000028}",
"targetPort": "currentColor"
},
{
"sourceUuid": "{00000000-0000-0000-0000-000000000028}",
"sourcePort": "result",
"targetUuid": "{00000000-0000-0000-0000-000000000025}",
"targetPort": "fragColor"
}
...
Additional Info
I tried to remove the [10]
part from the inputs
of the new prototype
. This output an error and the faulty shader code:
const int MAX_LIGHTS = 8;
const int TYPE_POINT = 0;
const int TYPE_DIRECTIONAL = 1;
const int TYPE_SPOT = 2;
struct Light {
int type;
vec3 position;
vec3 color;
float intensity;
vec3 direction;
float constantAttenuation;
float linearAttenuation;
float quadraticAttenuation;
float cutOffAngle;
};
uniform Light lights[MAX_LIGHTS];
uniform int lightCount;
// Pre-convolved environment maps
struct EnvironmentLight {
samplerCube irradiance; // For diffuse contribution
samplerCube specular; // For specular contribution
};
uniform EnvironmentLight envLight;
uniform int envLightCount = 0;
#line 52
void adsModel(const in vec3 worldPos,
const in vec3 worldNormal,
const in vec3 worldView,
const in float shininess,
out vec3 diffuseColor,
out vec3 specularColor)
{
diffuseColor = vec3(0.0);
specularColor = vec3(0.0);
// We perform all work in world space
vec3 n = normalize(worldNormal);
vec3 s = vec3(0.0);
for (int i = 0; i < lightCount; ++i) {
float att = 1.0;
float sDotN = 0.0;
if (lights[i].type != TYPE_DIRECTIONAL) {
// Point and Spot lights
// Light position is already in world space
vec3 sUnnormalized = lights[i].position - worldPos;
s = normalize(sUnnormalized); // Light direction
// Calculate the attenuation factor
sDotN = dot(s, n);
if (sDotN > 0.0) {
if (lights[i].constantAttenuation != 0.0
|| lights[i].linearAttenuation != 0.0
|| lights[i].quadraticAttenuation != 0.0) {
float dist = length(sUnnormalized);
att = 1.0 / (lights[i].constantAttenuation +
lights[i].linearAttenuation * dist +
lights[i].quadraticAttenuation * dist * dist);
}
// The light direction is in world space already
if (lights[i].type == TYPE_SPOT) {
// Check if fragment is inside or outside of the spot light cone
if (degrees(acos(dot(-s, lights[i].direction))) > lights[i].cutOffAngle)
sDotN = 0.0;
}
}
} else {
// Directional lights
// The light direction is in world space already
s = normalize(-lights[i].direction);
sDotN = dot(s, n);
}
// Calculate the diffuse factor
float diffuse = max(sDotN, 0.0);
// Calculate the specular factor
float specular = 0.0;
if (diffuse > 0.0 && shininess > 0.0) {
float normFactor = (shininess + 2.0) / 2.0;
vec3 r = reflect(-s, n); // Reflection direction in world space
specular = normFactor * pow(max(dot(r, worldView), 0.0), shininess);
}
// Accumulate the diffuse and specular contributions
diffuseColor += att * lights[i].intensity * diffuse * lights[i].color;
specularColor += att * lights[i].intensity * specular * lights[i].color;
}
}
vec4 phongFunction(const in vec4 ambient,
const in vec4 diffuse,
const in vec4 specular,
const in float shininess,
const in vec3 worldPosition,
const in vec3 worldView,
const in vec3 worldNormal)
{
// Calculate the lighting model, keeping the specular component separate
vec3 diffuseColor, specularColor;
adsModel(worldPosition, worldNormal, worldView, shininess, diffuseColor, specularColor);
// Combine spec with ambient+diffuse for final fragment color
vec3 color = (ambient.rgb + diffuseColor) * diffuse.rgb
+ specularColor * specular.rgb;
return vec4(color, diffuse.a);
}
#line 11
uniform vec3 clicks[10];
uniform vec3 colors[10];
vec4 renderClicks(vec3 clicks[10], vec3 colors[10], vec4 currentColor) {
return currentColor;
}
#line 15
out vec4 fragColor;
void main()
{
fragColor = (((renderClicks(clicks[10], colors[10], (((((((phongFunction(ka, kd, ks, shininess, worldPosition, normalize(((eyePosition - worldPosition))), normalize(worldNormal)))))))))))));
}
So the shader looks good except for the call to renderClicks(clicks[10], colors[10], ...)
. But I don't know if I can get rid of the [10]
parts because that's their name.