diff --git a/qglengine/core/gltransform.cpp b/qglengine/core/gltransform.cpp index 21169ad..cafdd54 100644 --- a/qglengine/core/gltransform.cpp +++ b/qglengine/core/gltransform.cpp @@ -379,18 +379,12 @@ QQuaternion Transform::fromAxes(const QVector3D & xAxis, const QVector3D & yAxis QVector3D Transform::fromDirection(QVector3D d, float pitch) { 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(); ret[0] = M_PI - acos(d.z()); ret[1] = pitch * deg2rad; ret[2] = -atan2(d.x(), d.y()); - //if (ret[0] < 0.) ret[0] += M_2PI; - //if (ret[0] >= M_2PI) ret[0] -= M_2PI; - if (ret[2] < 0.) ret[2] += M_2PI; - if (ret[2] >= M_2PI) ret[2] -= M_2PI; + normalizeAngleRad(ret[0]); + normalizeAngleRad(ret[2]); return ret * rad2deg; } diff --git a/qglengine/core/gltypes.h b/qglengine/core/gltypes.h index 49d8376..14c8f9a 100644 --- a/qglengine/core/gltypes.h +++ b/qglengine/core/gltypes.h @@ -278,6 +278,8 @@ inline float cosABV(const QVector3D & v0, const QVector3D & v1) { if (l == 0.f) return 0.; 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);} QVector3D orthToVector(const QVector3D & v, const float & scale = 1.); QVector3D rotateVector(const QVector3D & v, const QVector3D & a); diff --git a/qglengine/globject.h b/qglengine/globject.h index 7ac17da..0e367cc 100644 --- a/qglengine/globject.h +++ b/qglengine/globject.h @@ -261,7 +261,7 @@ class Light: public AimedObject { friend class QGLView; friend class RendererBase; public: - enum Type {Omni, Directional, Cone}; + enum Type {Omni, Cone, Directional}; Light(); Light(const QVector3D & p, const QColor & c = Qt::white, float i = 1.); diff --git a/qglengine/renderer.cpp b/qglengine/renderer.cpp index 711556c..31783b4 100644 --- a/qglengine/renderer.cpp +++ b/qglengine/renderer.cpp @@ -46,7 +46,7 @@ Renderer::Renderer(QGLView * view_): RendererBase(view_), shader_files[srServiceFrame] = "service_frame.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[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_pos, bpLightPositions , "QGLLightPositionData" ); ShaderRole roles[] = {srLightOmniPass, srLightSpotPass}; + QOpenGLShaderProgram * prog = 0; for (int p = 0; p < 2; ++p) { - QOpenGLShaderProgram * prog = 0; if (!bindShader(roles[p], &prog)) continue; for (int i = 0; i < 5; ++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); prog->setUniformValue("lights_start", lights_start[pass.second]); prog->setUniformValue("lights_count", ll[pass.second].size()); - fbo_out.setWriteBuffer(wi); - fbo_out.bindColorTexture(ri, 7); + fbo_out.setWriteBuffer(1 + pass.second); glClearFramebuffer(Qt::black, false); 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(); /// apply hovers and selection frame if (edit_mode) { - rend_selection.drawSelection(fbo_out, ri); + rend_selection.drawSelection(fbo_out, 0); rend_service.renderService(); } 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())); } } diff --git a/qglengine/shaders/ds_final.glsl b/qglengine/shaders/ds_final.glsl new file mode 100644 index 0000000..b48e9bf --- /dev/null +++ b/qglengine/shaders/ds_final.glsl @@ -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; +} diff --git a/qglengine/shaders/ds_geom.glsl b/qglengine/shaders/ds_geom.glsl index 9d4b913..32e0a6a 100644 --- a/qglengine/shaders/ds_geom.glsl +++ b/qglengine/shaders/ds_geom.glsl @@ -26,6 +26,7 @@ uniform vec2 dt; uniform float z_near; const vec3 luma = vec3(0.299, 0.587, 0.114); +const float _pe = 2.4e-7; void main(void) { vec2 tc = qgl_FragTexture.xy; @@ -51,11 +52,15 @@ void main(void) { emission *= qgl_material[qgl_MaterialIndex].color_emission; 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[1] = vec4(normal .xyz, roughness ); + qgl_FragData[1] = vec4(normal .xyz, z); 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); //ivec2 itc = ivec2(gl_FragCoord.xy); diff --git a/qglengine/shaders/ds_light.glsl b/qglengine/shaders/ds_light.glsl index 5601c1b..eeba97b 100644 --- a/qglengine/shaders/ds_light.glsl +++ b/qglengine/shaders/ds_light.glsl @@ -17,7 +17,6 @@ in vec3 view_dir; uniform vec2 dt; uniform float z_near; uniform sampler2D tex_0, tex_1, tex_2, tex_3, tex_4; -uniform sampler2D tex_d, tex_sum; uniform int lights_start, lights_count; 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); ldist = length(ldir); ldir = normalize(ldir); + //ldir = vec3(0,0,1); halfV = normalize(ldir + v); NdotL = max(dot(n, ldir), 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) { 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.) { qgl_FragColor = vec4(0); return; } vec4 v0 = texelFetch(tex_0, tc, 0), - v1 = texelFetch(tex_1, tc, 0), v2 = texelFetch(tex_2, tc, 0), v3 = texelFetch(tex_3, 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.xyz = view_dir * z; vec3 v = normalize(-pos.xyz); - //vec2 sp = gl_FragCoord.xy * dt * 2 - vec2(1, 1); vec3 diffuse = v0.rgb; vec3 normal = v1.xyz; vec3 specular = v2.rgb; vec3 emission = v3.rgb; float reflectivity = v0.w; - float roughness = v1.w; float height = v2.w; + float roughness = v3.w; //bn = normalize(vec3(v3.w, v4.zw)); //bn2 = normalize(cross(n, bn)); @@ -128,17 +124,6 @@ void main(void) { for (int i = 0; i < lights_count; ++i) calcLight(lights_start + i, normal, v); - vec4 result = vec4(max(vec3(0), li * diffuse + si * specular + emission), 1); -#ifndef FIRST_PASS - 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)); + qgl_FragColor = vec4(max(vec3(0), li * diffuse + si * specular + emission), 1); + //qgl_FragColor.rgb = vec3(pos.xyz/100); } diff --git a/qglengine/widgets/object_editor.ui b/qglengine/widgets/object_editor.ui index 800ce96..0a6902f 100644 --- a/qglengine/widgets/object_editor.ui +++ b/qglengine/widgets/object_editor.ui @@ -351,12 +351,12 @@ - Directional + Cone - Cone + Directional