git-svn-id: svn://db.shs.com.ru/libs@642 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2019-12-03 14:24:40 +00:00
parent c38678056d
commit b1e1ad4aea
8 changed files with 54 additions and 43 deletions

View File

@@ -379,18 +379,12 @@ QQuaternion Transform::fromAxes(const QVector3D & xAxis, const QVector3D & yAxis
QVector3D Transform::fromDirection(QVector3D d, float pitch) { QVector3D Transform::fromDirection(QVector3D d, float pitch) {
QVector3D ret; QVector3D ret;
//QMatrix3x3 m = QQuaternion::fromDirection(d, QVector3D()).toRotationMatrix();
//ret[0] = -atan2(m(0, 2), m(1, 2));
//ret[1] = acos (m(2, 2));
//ret[2] = atan2(m(2, 0), m(2, 1));
d.normalize(); d.normalize();
ret[0] = M_PI - acos(d.z()); ret[0] = M_PI - acos(d.z());
ret[1] = pitch * deg2rad; ret[1] = pitch * deg2rad;
ret[2] = -atan2(d.x(), d.y()); ret[2] = -atan2(d.x(), d.y());
//if (ret[0] < 0.) ret[0] += M_2PI; normalizeAngleRad(ret[0]);
//if (ret[0] >= M_2PI) ret[0] -= M_2PI; normalizeAngleRad(ret[2]);
if (ret[2] < 0.) ret[2] += M_2PI;
if (ret[2] >= M_2PI) ret[2] -= M_2PI;
return ret * rad2deg; return ret * rad2deg;
} }

View File

@@ -278,6 +278,8 @@ inline float cosABV(const QVector3D & v0, const QVector3D & v1) {
if (l == 0.f) return 0.; if (l == 0.f) return 0.;
return (QVector3D::dotProduct(v0, v1)) / l; return (QVector3D::dotProduct(v0, v1)) / l;
} }
inline void normalizeAngleRad(float & a) {while (a < 0.) a += M_2PI; while (a >= M_2PI) a -= M_2PI;}
inline void normalizeAngleDeg(float & a) {while (a < 0.) a += 360. ; while (a >= 360. ) a -= 360. ;}
inline QVector3D projection(const QVector3D & v, const QVector3D & to) {return to.normalized() * v.length() * cosABV(v, to);} inline QVector3D projection(const QVector3D & v, const QVector3D & to) {return to.normalized() * v.length() * cosABV(v, to);}
QVector3D orthToVector(const QVector3D & v, const float & scale = 1.); QVector3D orthToVector(const QVector3D & v, const float & scale = 1.);
QVector3D rotateVector(const QVector3D & v, const QVector3D & a); QVector3D rotateVector(const QVector3D & v, const QVector3D & a);

View File

@@ -261,7 +261,7 @@ class Light: public AimedObject {
friend class QGLView; friend class QGLView;
friend class RendererBase; friend class RendererBase;
public: public:
enum Type {Omni, Directional, Cone}; enum Type {Omni, Cone, Directional};
Light(); Light();
Light(const QVector3D & p, const QColor & c = Qt::white, float i = 1.); Light(const QVector3D & p, const QColor & c = Qt::white, float i = 1.);

View File

@@ -46,7 +46,7 @@ Renderer::Renderer(QGLView * view_): RendererBase(view_),
shader_files[srServiceFrame] = "service_frame.glsl"; shader_files[srServiceFrame] = "service_frame.glsl";
shader_files[srGeometryPass ] = "ds_geom.glsl"; shader_files[srGeometryPass ] = "ds_geom.glsl";
shader_files[srLightOmniPass] = "ds_light.glsl"; shader_defines[srLightOmniPass] << "FIRST_PASS"; shader_files[srLightOmniPass] = "ds_light.glsl";
shader_files[srLightSpotPass] = "ds_light.glsl"; shader_defines[srLightSpotPass] << "SPOT"; shader_files[srLightSpotPass] = "ds_light.glsl"; shader_defines[srLightSpotPass] << "SPOT";
shader_files[srFinalPass ] = "ds_final.glsl"; shader_files[srFinalPass ] = "ds_final.glsl";
@@ -133,13 +133,15 @@ void Renderer::initShaders() {
initUniformBuffer(shaders.value(srLightSpotPass), &buffer_lights , bpLightParameters, "QGLLightParameterData"); initUniformBuffer(shaders.value(srLightSpotPass), &buffer_lights , bpLightParameters, "QGLLightParameterData");
initUniformBuffer(shaders.value(srLightSpotPass), &buffer_lights_pos, bpLightPositions , "QGLLightPositionData" ); initUniformBuffer(shaders.value(srLightSpotPass), &buffer_lights_pos, bpLightPositions , "QGLLightPositionData" );
ShaderRole roles[] = {srLightOmniPass, srLightSpotPass}; ShaderRole roles[] = {srLightOmniPass, srLightSpotPass};
QOpenGLShaderProgram * prog = 0;
for (int p = 0; p < 2; ++p) { for (int p = 0; p < 2; ++p) {
QOpenGLShaderProgram * prog = 0;
if (!bindShader(roles[p], &prog)) continue; if (!bindShader(roles[p], &prog)) continue;
for (int i = 0; i < 5; ++i) for (int i = 0; i < 5; ++i)
prog->setUniformValue(QString("tex_%1").arg(i).toLatin1().constData(), i); prog->setUniformValue(QString("tex_%1").arg(i).toLatin1().constData(), i);
prog->setUniformValue("tex_d", 5); }
prog->setUniformValue("tex_sum", 7); if (bindShader(srFinalPass, &prog)) {
for (int i = 0; i < 5; ++i)
prog->setUniformValue(QString("tex_%1").arg(i).toLatin1().constData(), i);
} }
} }
@@ -259,21 +261,25 @@ void Renderer::renderScene() {
setUniformViewCorners(prog, cam); setUniformViewCorners(prog, cam);
prog->setUniformValue("lights_start", lights_start[pass.second]); prog->setUniformValue("lights_start", lights_start[pass.second]);
prog->setUniformValue("lights_count", ll[pass.second].size()); prog->setUniformValue("lights_count", ll[pass.second].size());
fbo_out.setWriteBuffer(wi); fbo_out.setWriteBuffer(1 + pass.second);
fbo_out.bindColorTexture(ri, 7);
glClearFramebuffer(Qt::black, false); glClearFramebuffer(Qt::black, false);
renderQuad(prog, quad, cam); renderQuad(prog, quad, cam);
piSwap(ri, wi);
} }
} }
if (bindShader(srFinalPass, &prog)) {
fbo_out.bindColorTexture(1, 0);
fbo_out.bindColorTexture(2, 1);
fbo_out.setWriteBuffer(0);
renderQuad(prog, quad);
}
fbo_out.release(); fbo_out.release();
/// apply hovers and selection frame /// apply hovers and selection frame
if (edit_mode) { if (edit_mode) {
rend_selection.drawSelection(fbo_out, ri); rend_selection.drawSelection(fbo_out, 0);
rend_service.renderService(); rend_service.renderService();
} else { } else {
fbo_out.blit(ri, 0, 0, fbo_out.rect(), QRect(QPoint(), view->size())); fbo_out.blit(0, 0, 0, fbo_out.rect(), QRect(QPoint(), view->size()));
} }
} }

View File

@@ -0,0 +1,19 @@
// vert //
void main(void) {
gl_Position = qgl_ftransform();
}
// frag //
uniform sampler2D tex_0, tex_1, tex_2, tex_3, tex_4;
const vec3 luma = vec3(0.299, 0.587, 0.114);
void main(void) {
ivec2 tc = ivec2(gl_FragCoord.xy);
vec4 v0 = texelFetch(tex_0, tc, 0);
vec4 v1 = texelFetch(tex_1, tc, 0);
qgl_FragColor = v0 + v1;
}

View File

@@ -26,6 +26,7 @@ uniform vec2 dt;
uniform float z_near; uniform float z_near;
const vec3 luma = vec3(0.299, 0.587, 0.114); const vec3 luma = vec3(0.299, 0.587, 0.114);
const float _pe = 2.4e-7;
void main(void) { void main(void) {
vec2 tc = qgl_FragTexture.xy; vec2 tc = qgl_FragTexture.xy;
@@ -51,11 +52,15 @@ void main(void) {
emission *= qgl_material[qgl_MaterialIndex].color_emission; emission *= qgl_material[qgl_MaterialIndex].color_emission;
float height = dot(qgl_materialTexture(QGL_MAP_RELIEF, tc, vec4(0)).rgb, luma); float height = dot(qgl_materialTexture(QGL_MAP_RELIEF, tc, vec4(0)).rgb, luma);
float z = gl_FragCoord.z;
z = z + z - 1;
z = ((_pe - 2.) * z_near) / (z + _pe - 1.); // infinite depth
qgl_FragData[0] = vec4(diffuse .rgb, reflectivity); qgl_FragData[0] = vec4(diffuse .rgb, reflectivity);
qgl_FragData[1] = vec4(normal .xyz, roughness ); qgl_FragData[1] = vec4(normal .xyz, z);
qgl_FragData[2] = vec4(specular.rgb, height ); qgl_FragData[2] = vec4(specular.rgb, height );
qgl_FragData[3] = vec4(emission.rgb, 0/*bn.x*/); qgl_FragData[3] = vec4(emission.rgb, roughness/*bn.x*/);
//qgl_FragData[4] = vec4(speed.xy, bn.yz); //qgl_FragData[4] = vec4(speed.xy, bn.yz);
//ivec2 itc = ivec2(gl_FragCoord.xy); //ivec2 itc = ivec2(gl_FragCoord.xy);

View File

@@ -17,7 +17,6 @@ in vec3 view_dir;
uniform vec2 dt; uniform vec2 dt;
uniform float z_near; uniform float z_near;
uniform sampler2D tex_0, tex_1, tex_2, tex_3, tex_4; uniform sampler2D tex_0, tex_1, tex_2, tex_3, tex_4;
uniform sampler2D tex_d, tex_sum;
uniform int lights_start, lights_count; uniform int lights_start, lights_count;
const vec3 luma = vec3(0.299, 0.587, 0.114); const vec3 luma = vec3(0.299, 0.587, 0.114);
@@ -33,6 +32,7 @@ void calcLight(in int index, in vec3 n, in vec3 v) {
ldir = lpos.xyz - (pos.xyz * lpos.w); ldir = lpos.xyz - (pos.xyz * lpos.w);
ldist = length(ldir); ldist = length(ldir);
ldir = normalize(ldir); ldir = normalize(ldir);
//ldir = vec3(0,0,1);
halfV = normalize(ldir + v); halfV = normalize(ldir + v);
NdotL = max(dot(n, ldir), 1E-6); NdotL = max(dot(n, ldir), 1E-6);
NdotH = max(dot(n, halfV), 1E-6); NdotH = max(dot(n, halfV), 1E-6);
@@ -90,32 +90,28 @@ void calcLight(in int index, in vec3 n, in vec3 v) {
void main(void) { void main(void) {
ivec2 tc = ivec2(gl_FragCoord.xy); ivec2 tc = ivec2(gl_FragCoord.xy);
float z = texelFetch(tex_d, tc, 0).r; vec4 v1 = texelFetch(tex_1, tc, 0);
float z = v1.w;
if (z == 1.) { if (z == 1.) {
qgl_FragColor = vec4(0); qgl_FragColor = vec4(0);
return; return;
} }
vec4 v0 = texelFetch(tex_0, tc, 0), vec4 v0 = texelFetch(tex_0, tc, 0),
v1 = texelFetch(tex_1, tc, 0),
v2 = texelFetch(tex_2, tc, 0), v2 = texelFetch(tex_2, tc, 0),
v3 = texelFetch(tex_3, tc, 0), v3 = texelFetch(tex_3, tc, 0),
v4 = texelFetch(tex_4, tc, 0); v4 = texelFetch(tex_4, tc, 0);
z = z + z - 1;
z = ((_pe - 2.) * z_near) / (z + _pe - 1.); // infinite depth
pos.w = 1; pos.w = 1;
pos.xyz = view_dir * z; pos.xyz = view_dir * z;
vec3 v = normalize(-pos.xyz); vec3 v = normalize(-pos.xyz);
//vec2 sp = gl_FragCoord.xy * dt * 2 - vec2(1, 1);
vec3 diffuse = v0.rgb; vec3 diffuse = v0.rgb;
vec3 normal = v1.xyz; vec3 normal = v1.xyz;
vec3 specular = v2.rgb; vec3 specular = v2.rgb;
vec3 emission = v3.rgb; vec3 emission = v3.rgb;
float reflectivity = v0.w; float reflectivity = v0.w;
float roughness = v1.w;
float height = v2.w; float height = v2.w;
float roughness = v3.w;
//bn = normalize(vec3(v3.w, v4.zw)); //bn = normalize(vec3(v3.w, v4.zw));
//bn2 = normalize(cross(n, bn)); //bn2 = normalize(cross(n, bn));
@@ -128,17 +124,6 @@ void main(void) {
for (int i = 0; i < lights_count; ++i) for (int i = 0; i < lights_count; ++i)
calcLight(lights_start + i, normal, v); calcLight(lights_start + i, normal, v);
vec4 result = vec4(max(vec3(0), li * diffuse + si * specular + emission), 1); qgl_FragColor = vec4(max(vec3(0), li * diffuse + si * specular + emission), 1);
#ifndef FIRST_PASS //qgl_FragColor.rgb = vec3(pos.xyz/100);
result += texelFetch(tex_sum, tc, 0);
#endif
qgl_FragColor = result;
//qgl_FragColor.rgb = vec3(1);
//qgl_FragData[4] = vec4(speed.xy, bn.yz);
//ivec2 itc = ivec2(gl_FragCoord.xy);
//qgl_FragData[0].rgb = vec3(dot(n,vec3(0,0,1)));
//qgl_FragData[0].rgb = diffuse.rgb * dot(n,vec3(0,0,1));
} }

View File

@@ -351,12 +351,12 @@
</item> </item>
<item> <item>
<property name="text"> <property name="text">
<string>Directional</string> <string>Cone</string>
</property> </property>
</item> </item>
<item> <item>
<property name="text"> <property name="text">
<string>Cone</string> <string>Directional</string>
</property> </property>
</item> </item>
</widget> </widget>