full support of ObjectBase:: isReceiveShadows, isCastShadows, isAcceptLight and isAcceptFog

This commit is contained in:
2023-02-12 22:49:38 +03:00
parent e3389bcc20
commit c8dcd5e9c0
10 changed files with 84 additions and 40 deletions

View File

@@ -3,6 +3,7 @@
out vec3 geom_normal;
out mat3 TBN;
out vec4 object_color;
out float object_flags;
void main(void) {
qgl_MaterialIndex = qgl_Material;
@@ -12,6 +13,7 @@ void main(void) {
geom_normal = normalize(qgl_Normal * qgl_getNormalMatrix());
TBN = qgl_getTangentMatrix() * mat3(qgl_Tangent, qgl_Bitangent, qgl_Normal);
object_color = qgl_ObjectColor;
object_flags = qgl_ObjectFlags;
}
@@ -20,6 +22,7 @@ void main(void) {
in vec3 geom_normal;
in mat3 TBN;
in vec4 object_color;
in float object_flags;
uniform vec2 dt;
uniform float z_near;
@@ -65,7 +68,7 @@ void main(void) {
qgl_FragData[0] = vec4(diffuse .rgba);
qgl_FragData[1] = vec4(normal .xyz, z);
qgl_FragData[2] = vec4(metalness, roughness, reflectivity, 0);
qgl_FragData[2] = vec4(metalness, roughness, reflectivity, object_flags);
qgl_FragData[3] = vec4(emission.rgb, 0/*bn.x*/);
//qgl_FragData[4] = vec4(speed.xy, bn.yz);

View File

@@ -37,6 +37,7 @@ vec4 pos, lpos, shp;
vec3 li, si, ldir, halfV, bn, bn2, lwdir;
vec3 vds, vds2;
float rough_diff, rough_spec, dist, NdotL, NdotH, spot, ldist, diff, spec, sdist, shadow;
uint flags;
vec4 mapScreenToShadow(in int light_index, in vec3 offset) {
@@ -74,12 +75,12 @@ void calcLight(in int index, in vec3 n, in vec3 v) {
spot *= scos * step(qgl_light_parameter[index].angles.w, scos);
spot *= smoothstep(qgl_light_parameter[index].angles.w, qgl_light_parameter[index].angles.y, scos);
if (qgl_light_parameter[index].flags == 1) {
if (qgl_light_parameter[index].flags == 1 && bitfieldExtract(flags, 3, 1) == 1) {
float ds = ldist/300.;
//float bias = ldist * 0.05;
vds = ds * bn.xyz;
vds2 = ds * bn2.xyz;
//vds = ds * bn.xyz;
//vds2 = ds * bn2.xyz;
int layer = index - lights_start;
float shadow = 0.;
@@ -192,16 +193,21 @@ void main(void) {
float reflectivity = v2.b;
float NdotV = dot(normal, v);
float roughness3 = roughness*roughness*roughness;
bn = normalize(cross(normal, view_dir));
bn2 = normalize(cross(normal, bn));
//bn = normalize(cross(normal, view_dir));
//bn2 = normalize(cross(normal, bn));
rough_diff = max(roughness, _min_rough);
rough_spec = max(roughness3, _min_rough);
float shlick = clamp(metalness + (1 - metalness) * pow(1 - NdotV, 5), 0, 1);
flags = uint(v2.w);
li = vec3(0.);//qgl_AmbientLight.color.rgb * qgl_AmbientLight.intensity;
si = vec3(0.);
if (bitfieldExtract(flags, 0, 1) == 1) {
for (int i = 0; i < lights_count; ++i)
calcLight(lights_start + i, normal, v);
} else {
li = vec3(1.);
}
si *= shlick;
li *= (1 - shlick);
alpha = min(1, alpha * (1 + shlick));
@@ -215,13 +221,16 @@ void main(void) {
vec3 res_col = max(vec3(0), li * diffuse + si * spec_col + emission);
res_col = mix(res_col, env_col, env_spec * reflectivity);
if (bitfieldExtract(flags, 1, 1) == 1) {
float plen = length(pos.xyz);
float fog = 1 - exp(-plen / fog_decay);
fog = clamp(fog * fog_color.a * fog_density, 0, 1);
res_col = mix(res_col, fog_color.rgb, fog);
}
qgl_FragColor = vec4(res_col, alpha);
//qgl_FragColor.rgb = view_dir;
//qgl_FragColor.rgb = vec3((v2.w/15));
//qgl_FragColor.rgb = diffuse.rgb;
/*
#ifdef SPOT
vec4 wpos = vec4(world_dir * z, 1);

View File

@@ -1,6 +1,11 @@
// vert //
flat out uint object_flags;
void main(void) {
object_flags = qgl_ObjectFlags;
if (bitfieldExtract(object_flags, 2, 1) == 0)
return;
qgl_MaterialIndex = qgl_Material;
qgl_FragTexture = qgl_getFragTexture();
gl_Position = qgl_ftransform();
@@ -9,10 +14,14 @@ void main(void) {
// frag //
flat in uint object_flags;
float z_near = 0.1f;
const float _pe = 2.4e-7;
void main(void) {
if (bitfieldExtract(object_flags, 2, 1) == 0)
discard;
vec4 diffuse = qgl_materialTexture(QGL_MAP_DIFFUSE, qgl_FragTexture.xy, vec4(0));
if(diffuse.a < 0.5)
discard;

View File

@@ -32,12 +32,11 @@ const char qgl_vertex_head[] = "layout(location = 1 ) in vec3 qgl_Vertex
"layout(location = 3 ) in vec3 qgl_Tangent ;\n"
"layout(location = 4 ) in vec3 qgl_Bitangent ;\n"
"layout(location = 5 ) in vec2 qgl_Texture ;\n"
"layout(location = 6 ) in uint qgl_Material ;\n"
"layout(location = 6 ) in uvec4 qgl_ObjectIntegers;\n"
"layout(location = 7 ) in uint qgl_ObjectSelected;\n"
"layout(location = 8 ) in uint qgl_ObjectID ;\n"
"layout(location = 9 ) in vec4 qgl_ObjectColor ;\n"
"layout(location = 10) in mat4 qgl_ModelMatrix ;\n"
"layout(location = 14) in mat2x3 qgl_TextureMatrix ;\n"
"layout(location = 8 ) in vec4 qgl_ObjectColor ;\n"
"layout(location = 9 ) in mat4 qgl_ModelMatrix ;\n"
"layout(location = 13) in mat2x3 qgl_TextureMatrix ;\n"
"out vec2 qgl_FragTexture;\n"
"flat out uint qgl_MaterialIndex;\n"
"uniform mat4 qgl_ViewMatrix;\n"
@@ -46,6 +45,9 @@ const char qgl_vertex_head[] = "layout(location = 1 ) in vec3 qgl_Vertex
"mat3 qgl_getTangentMatrix() {return mat3(qgl_ViewMatrix * qgl_ModelMatrix);}\n"
"vec2 qgl_getFragTexture() {return (vec3(qgl_Texture, 1.f) * qgl_TextureMatrix).xy;}\n"
"vec4 qgl_ftransform() {return qgl_ViewProjMatrix * (qgl_ModelMatrix * vec4(qgl_Vertex, 1.f));}\n"
"#define qgl_Material qgl_ObjectIntegers[0]\n"
"#define qgl_ObjectID qgl_ObjectIntegers[1]\n"
"#define qgl_ObjectFlags qgl_ObjectIntegers[2]\n"
"";
const char qgl_fragment_head[] =

View File

@@ -61,8 +61,7 @@ void QGLEngineShaders::prepareDrawGeom(QOpenGLExtraFunctions * f) {
void QGLEngineShaders::prepareDrawObj(QOpenGLExtraFunctions * f) {
// qDebug() << "prepareDrawObj";
f->glEnableVertexAttribArray(material_loc);
f->glEnableVertexAttribArray(object_id_loc);
f->glEnableVertexAttribArray(integers_loc);
f->glEnableVertexAttribArray(color_loc);
for (int i = 0; i < 4; ++i)
f->glEnableVertexAttribArray(modelmatrix_loc + i);
@@ -70,8 +69,7 @@ void QGLEngineShaders::prepareDrawObj(QOpenGLExtraFunctions * f) {
f->glEnableVertexAttribArray(texturematrix_loc + i);
GLsizei size = sizeof(Object);
f->glVertexAttribIPointer(material_loc, 1, GL_UNSIGNED_INT, size, (const void *)material_offset);
f->glVertexAttribIPointer(object_id_loc, 1, GL_UNSIGNED_INT, size, (const void *)object_id_offset);
f->glVertexAttribIPointer(integers_loc, 4, GL_UNSIGNED_INT, size, (const void *)integers_offset);
f->glVertexAttribPointer(color_loc, 4, GL_FLOAT, GL_FALSE, size, (const void *)color_offset);
for (int i = 0; i < 4; ++i) {
f->glVertexAttribPointer(modelmatrix_loc + i,
@@ -90,8 +88,7 @@ void QGLEngineShaders::prepareDrawObj(QOpenGLExtraFunctions * f) {
(const void *)(texturematrix_offset + sizeof(QVector3D) * i));
}
f->glVertexAttribDivisor(material_loc, 1);
f->glVertexAttribDivisor(object_id_loc, 1);
f->glVertexAttribDivisor(integers_loc, 1);
f->glVertexAttribDivisor(color_loc, 1);
for (int i = 0; i < 4; ++i)
f->glVertexAttribDivisor(modelmatrix_loc + i, 1);

View File

@@ -34,9 +34,8 @@ const GLsizei bitangent_offset = sizeof(QVector3D) + tangent_offset;
const GLsizei tex_offset = sizeof(QVector3D) + bitangent_offset;
// object
const GLsizei material_offset = 0;
const GLsizei object_id_offset = sizeof(GLuint) + material_offset;
const GLsizei color_offset = sizeof(GLuint) + object_id_offset;
const GLsizei integers_offset = 0;
const GLsizei color_offset = sizeof(GLuint) * 4 + integers_offset;
const GLsizei modelmatrix_offset = sizeof(QVector4D) + color_offset;
const GLsizei texturematrix_offset = sizeof(QVector4D) * 4 + modelmatrix_offset;
@@ -48,11 +47,10 @@ const GLuint tangent_loc = 3; // qgl_Tangent
const GLuint bitangent_loc = 4; // qgl_Bitangent
const GLuint tex_loc = 5; // qgl_Texture
const GLuint material_loc = 6; // qgl_Material
const GLuint object_id_loc = 8; // qgl_ObjectID
const GLuint color_loc = 9; // qgl_ObjectColor
const GLuint modelmatrix_loc = 10; // qgl_ModelViewProjectionMatrix
const GLuint texturematrix_loc = 14; // qgl_TextureMatrix
const GLuint integers_loc = 6; // qgl_ObjectIntegers
const GLuint color_loc = 8; // qgl_ObjectColor
const GLuint modelmatrix_loc = 9; // qgl_ModelViewProjectionMatrix
const GLuint texturematrix_loc = 13; // qgl_TextureMatrix
const GLuint is_selected_loc = 7; // qgl_ObjectSelected
@@ -66,13 +64,23 @@ struct Vertex {
};
struct Object {
Object() {
material = object_id = 0;
material = object_id = flags = 0;
color = QVector4D(1, 1, 1, 1);
QMatrix4x4().copyDataTo(modelmatrix);
QMatrix2x3().transposed().copyDataTo(texturematrix);
}
GLuint material;
GLuint object_id;
union {
GLuint flags;
struct {
GLuint f_accept_light : 1;
GLuint f_accept_fog : 1;
GLuint f_accept_cast_shadow: 1;
GLuint f_accept_rec_shadow : 1;
};
};
GLuint _reserve;
QVector4D color;
GLfloat modelmatrix[16];
GLfloat texturematrix[6];

View File

@@ -242,6 +242,11 @@ void Renderer::fillObjectsBuffer(const ObjectBaseList & ol, RenderPass pass) {
so.color = QColor2QVector(o->color());
}
so.object_id = o->id();
so.flags = 0;
so.f_accept_light = o->isAcceptLight();
so.f_accept_fog = o->isAcceptFog();
so.f_accept_cast_shadow = o->isCastShadows();
so.f_accept_rec_shadow = o->isReceiveShadows();
o->worldTransform().transposed().copyDataTo(so.modelmatrix);
o->textureGLMatrix().copyDataTo(so.texturematrix);
// qDebug() << "load obj" << o->name() << o->textureMatrix() << tmat;

View File

@@ -110,7 +110,6 @@ public:
protected:
void fillObjectsBuffer(const ObjectBaseList & ol, RenderPass pass);
void reloadObjects();
void renderObjects(Scene & scene, RenderPass pass);
void renderLight(int first_wr_buff, bool clear_only);
void renderShadow(int index, Light * light);

View File

@@ -225,7 +225,10 @@ void ObjectBase::setVisible(bool v) {
void ObjectBase::setCastShadows(bool on) {
cast_shadow = on;
if (type_ == glLight) ((Light *)this)->apply();
if (type_ == glLight)
((Light *)this)->apply();
else
setObjectsChanged();
}

View File

@@ -84,7 +84,10 @@ public:
bool isReceiveShadows() const { return rec_shadow; }
bool isCastShadows() const { return cast_shadow; }
void setReceiveShadows(bool on) { rec_shadow = on; }
void setReceiveShadows(bool on) {
rec_shadow = on;
setObjectsChanged();
}
void setCastShadows(bool on);
void move(const QVector3D & dv) {
@@ -271,10 +274,16 @@ public:
QGenericMatrix<3, 2, float> textureGLMatrix() const;
bool isAcceptLight() const { return accept_light; }
void setAcceptLight(bool yes) { accept_light = yes; }
void setAcceptLight(bool yes) {
accept_light = yes;
setObjectsChanged();
}
bool isAcceptFog() const { return accept_fog; }
void setAcceptFog(bool yes) { accept_fog = yes; }
void setAcceptFog(bool yes) {
accept_fog = yes;
setObjectsChanged();
}
bool isSelected(bool check_parents = false) const;
void setSelected(bool yes);