@@ -23,10 +23,20 @@ var key = [false, false, false, false]; //Vedi car.js per i codici tasti
2323
2424var gfxSettings = 'high' ; //Impostazione grafica
2525var alphaBlending = true ; // On/Off trasparenze
26- var showDepthBuffer = false ; //DEBUG: per visualizzare il depth buffer
2726var ambientLight = 0.2 ; //Illuminazione di base (ambiente)
2827var pointLightPosition = [ 10 , 40 , 0.0 ] ; //Posizione punto luce
2928
29+ //Proiezione dal punto luce verso il centro della scena per creare la shadow map
30+ let shadowProjectionSettings = {
31+ aspectRatio : 1 ,
32+ fieldOfViewRadians : degToRad ( 70 ) ,
33+ zNear : 35 ,
34+ zFar : 50 ,
35+ cameraPosition : pointLightPosition ,
36+ lookAtTarget : [ 3 , 0 , 0 ] , //Guardo al centro della scena
37+ lookUpVector : [ 0 , 0 , - 1 ]
38+ }
39+
3040var gl ;
3141var skybox ; //Lo skybox viene mantenuto separatamente in quanto non influenzato dall'illuminazione
3242var sceneObjects = new Array ( ) ; //Array contenente tutti gli oggetti della scena
@@ -80,45 +90,27 @@ function start(){
8090
8191// Metodo di rendering
8292function drawScene ( elapsed ) {
83- if ( ! gl . ext ) showDepthBuffer = false ; //Se l'estensione WEBGL_depth_texture nonè disponibile disattivo le ombre
84- if ( ! showDepthBuffer )
85- webglUtils . resizeCanvasToDisplaySize ( gl . canvas ) ;
93+ webglUtils . resizeCanvasToDisplaySize ( gl . canvas ) ;
8694 gl . enable ( gl . CULL_FACE ) ;
8795 gl . enable ( gl . DEPTH_TEST ) ;
8896 vCar . doStep ( key ) ; //Aggiornamento fisica della macchina
97+
8998 /* Rendering delle ombre dinamiche sulla scena */
9099 let depthProjectionMatrix = m4 . identity ( ) ; //Matrice per la trasformazione di vista della depth texture
91- //Utilizzo una proiezione dal punto luce verso il centro della scena per creare la shadow map
92- let shadowProjectionSettings = {
93- aspectRatio : 1 ,
94- fieldOfViewRadians : degToRad ( 70 ) ,
95- zNear : 35 ,
96- zFar : 50 ,
97- cameraPosition : pointLightPosition ,
98- lookAtTarget : [ 3 , 0 , 0 ] , //Guardo al centro della scena
99- lookUpVector : [ 0 , 0 , - 1 ]
100- }
101- let lightViewProjectionMatrix = getViewProjectionMatrixLookAt ( gl , shadowProjectionSettings ) ;
102- //Devo scalare e traslare la matrice texture perchè altrimenti viene utilizzato solamente il "quarto" di quadrante in alto a destra del frustrum di proiezione
103- depthProjectionMatrix = getManipulationMatrix ( depthProjectionMatrix , [ 0.5 , 0.5 , 0.5 ] , [ 0 , 0 , 0 ] , [ 0.5 , 0.5 , 0.5 ] ) ;
104- depthProjectionMatrix = m4 . multiply ( depthProjectionMatrix , lightViewProjectionMatrix ) ;
105- if ( ! showDepthBuffer ) {
100+ if ( gl . ext && gfxSettings === 'shadows' ) { //Calcola ombre solo se attive e impostazione su high (illuminazione abilitata) (e estensione disponibile)
101+ let lightViewProjectionMatrix = getViewProjectionMatrixLookAt ( gl , shadowProjectionSettings ) ;
102+ //Devo scalare e traslare la matrice texture perchè altrimenti viene utilizzato solamente il "quarto" di quadrante in alto a destra del frustrum di proiezione
103+ depthProjectionMatrix = getManipulationMatrix ( depthProjectionMatrix , [ 0.5 , 0.5 , 0.5 ] , [ 0 , 0 , 0 ] , [ 0.5 , 0.5 , 0.5 ] ) ;
104+ depthProjectionMatrix = m4 . multiply ( depthProjectionMatrix , lightViewProjectionMatrix ) ;
106105 gl . bindFramebuffer ( gl . FRAMEBUFFER , getDepthFramebuffer ( ) ) ;
107106 gl . viewport ( 0 , 0 , getDepthTextureSize ( ) , getDepthTextureSize ( ) ) ;
108- }
109- gl . clear ( gl . COLOR_BUFFER_BIT | gl . DEPTH_BUFFER_BIT ) ;
110- if ( gl . ext && ( showDepthBuffer || gfxSettings === 'shadows' ) ) { //Calcola ombre solo se attive e impostazione su high (illuminazione abilitata) (e estensione disponibile)
111- if ( ! showDepthBuffer )
112- setupShaders ( gl , 'shadowProjection' ) ; //Imposto lo shader per la proizione delle ombre
113- else
114- setupShaders ( gl , 'low' ) ; //Utilizzo uno shader base per il debug
107+ gl . clear ( gl . COLOR_BUFFER_BIT | gl . DEPTH_BUFFER_BIT ) ;
108+ setupShaders ( gl , 'shadowProjection' ) ; //Imposto lo shader per la proizione delle ombre
115109 sceneObjects . forEach ( ( element ) => {
116110 if ( ! element . noShadows ) //Se l'oggetto non deve creare ombre non lo considero
117111 renderElement ( gl , element , lightViewProjectionMatrix )
118112 } ) ;
119113 }
120- if ( showDepthBuffer )
121- return ; //Se è attivo il debug del'depthBuffer salto il normale processo di rendering
122114 /* Rendering della scena */
123115 gl . bindFramebuffer ( gl . FRAMEBUFFER , null ) ;
124116 gl . viewport ( 0 , 0 , gl . canvas . width , gl . canvas . height ) ;
@@ -129,7 +121,8 @@ function drawScene(elapsed) {
129121 } else {
130122 gl . disable ( gl . BLEND ) ;
131123 }
132-
124+
125+ /* Calcolo matrice viewProjection per la camera corrente */
133126 let viewProjectionMatrix ;
134127 switch ( cameraSettings . cameraMode ) {
135128 case ( CAMERA_MODE . MENU ) :
@@ -453,7 +446,7 @@ var loader = {
453446 url : filename ,
454447 dataType : 'text' ,
455448 } ) . fail ( function ( ) {
456- // alert('File [' + filename + "] non trovato!");
449+ alert ( 'File [' + filename + "] non trovato!" ) ;
457450 } ) ;
458451 } ,
459452
0 commit comments