package Ġlearn opengl Ċ Ċ import Ġfeature a . input . Input EventType Ċ import Ġfeature a . keyboard . KeyEvent Ċ import Ġfeature a . keyboard . KeyEvent Source Ċ import Ġfeature a . keyboard . Key EventType Ċ import Ġfeature a . math .* Ċ import Ġfeature a . opengl .* Ċ import Ġfeature a . shader . compile Ċ import Ġlearn opengl . Camera Movement .* Ċ import Ġlearn opengl . Resources . container P ng Ċ import Ġlearn opengl . Resources . container Specular P ng Ċ import Ġlearn opengl . Resources . object Shader Ċ import Ġkotlin . math . cos Ċ import Ġkotlin . math . sin Ċ Ċ fun Ġtest Colors () Ġ= Ġbootstrap Test Ġ{ ĊĠĠĠ Ġ/* ĊĠĠĠ Ġemission Texture Ġ= Ġ" images / container E mission . jpg " ĊĠĠĠ Ġemission Texture ?. also Ġ{ Ġloader . load Resource ( it ) Ġ} ĊĠĠĠ Ġ*/ ĊĠĠĠ Ġval Ġprojection Matrix : ĠMatrix Ġ= ĠMatrix () ĊĠĠĠ Ġval Ġcamera : ĠCamera Ġ= ĠCamera ( pos Ġ= ĠVector ( 0 f , Ġ 0 f , Ġ 3 f )) ĊĊĠĠĠ Ġ// Ġval Ġcam Front : ĠVector Ġget () Ġ= Ġcamera . Front ĊĠĠĠ Ġval Ġmouse : ĠPoint Ġ= ĠPoint (- 1 f , Ġ- 1 f ) ĊĠĠĠ Ġvar Ġtotal Time : ĠFloat Ġ= Ġ 0 f ĊĊĠĠĠ Ġ// Ġval Ġcube Model Ġ= ĠMatrix () ĊĠĠĠ Ġlateinit Ġvar Ġcube Buffer : ĠBuffer ĊĠĠĠ Ġlateinit Ġvar Ġcube Shader Program : ĠProgram ĊĊĠĠĠ Ġval Ġlight Model : ĠMatrix Ġ= ĠMatrix () ĊĠĠĠ Ġval Ġlight Pos : ĠVector Ġ= ĠVector ( 1 . 2 f , Ġ 1 . 0 f , Ġ 2 . 0 f ) ĊĠĠĠ Ġ// Ġval Ġlight Color : ĠVector Ġ= ĠVector () ĊĠĠĠ Ġlateinit Ġvar Ġlight Buffer : ĠBuffer ĊĠĠĠ Ġlateinit Ġvar Ġlight Shader Program : ĠProgram ĊĊĠĠĠ Ġloader . load Resource ( container P ng ) ĊĠĠĠ Ġloader . load Resource ( container Specular P ng ) ĊĊĠĠĠ Ġinit Ġ{ ĊĠĠĠĠĠĠĠ Ġdiffuse Texture Path Ġ= Ġcontainer P ng ĊĠĠĠĠĠĠĠ Ġspecular Texture Path Ġ= Ġcontainer Specular P ng ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġload Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġcube ĊĠĠĠĠĠĠĠ Ġcube Buffer Ġ= Ġgl . create Buffer ( 0 , Ġ 0 ) ĊĠĠĠĠĠĠĠ Ġgl . bind Buffer ( ARRAY _ BUFFER , Ġcube Buffer ) ĊĠĠĠĠĠĠĠ Ġgl . buffer Data ( ARRAY _ BUFFER , Ġcube Vertices With Normals And Uv , ĠSTATIC _ DRAW ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program Ġ= Ġgl . create Program () ĊĠĠĠĠĠĠĠ Ġcube Shader Program . compile ( object Shader ) ĊĊĠĠĠĠĠĠĠ Ġ// Ġlight ĊĠĠĠĠĠĠĠ Ġlight Buffer Ġ= Ġgl . create Buffer ( 0 , Ġ 0 ) ĊĠĠĠĠĠĠĠ Ġgl . bind Buffer ( ARRAY _ BUFFER , Ġlight Buffer ) ĊĠĠĠĠĠĠĠ Ġgl . buffer Data ( ARRAY _ BUFFER , Ġcube Vertices , ĠSTATIC _ DRAW ) ĊĠĠĠĠĠĠĠ Ġlight Shader Program Ġ= Ġgl . create Program () ĊĠĠĠĠĠĠĠ Ġlight Shader Program . compile ( Resources . light Shader ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġupdate Ġ{ Ġelapsed Time : ĠFloat Ġ-> ĊĠĠĠĠĠĠĠ Ġ// Ġsetup ĊĠĠĠĠĠĠĠ Ġtotal Time Ġ+= Ġelapsed Time ĊĠĠĠĠĠĠĠ Ġval Ġcam Pos : ĠVector Ġ= Ġcamera . position ĊĠĠĠĠĠĠĠ Ġval Ġtotal Time InSeconds : ĠFloat Ġ= Ġtotal Time Ġ/ Ġ 1 _ 0 0 0 f ĊĊĠĠĠĠĠĠĠ Ġ// Ġmove Ġthe Ġlight Ġsource Ġaround Ġthe Ġscene Ġover Ġtime Ġusing Ġsin ĊĠĠĠĠĠĠĠ Ġlight Pos . x Ġ= Ġ 1 . 0 f Ġ+ Ġsin ( total Time InSeconds ) Ġ* Ġ 2 . 0 f ĊĠĠĠĠĠĠĠ Ġlight Pos . y Ġ= Ġsin ( total Time InSeconds Ġ/ Ġ 2 . 0 f ) Ġ* Ġ 1 . 0 f ĊĊĠĠĠĠĠĠĠ Ġ// Ġbackground ĊĠĠĠĠĠĠĠ Ġgl . enable ( DEPTH _ TEST ) ĊĠĠĠĠĠĠĠ Ġgl . clear Color ( 0 . 1 f , Ġ 0 . 1 f , Ġ 0 . 1 f , Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġgl . clear ( COLOR _ BUFFER _ BIT Ġor ĠDEP TH _ BUFFER _ BIT ) ĊĊĠĠĠĠĠĠĠ Ġprojection Matrix . assign Perspective ( test . near , Ġtest . far , Ġcamera . zoom , Ġwindow . surface . size . aspect Ratio ) ĊĠĠĠĠĠĠĠ Ġlight Model . assign Translation ( light Pos ). scale ( 0 . 2 f ) ĊĊĠĠĠĠĠĠĠ Ġ/* ĊĠĠĠĠĠĠĠ Ġlight Color . assign ( sin ( total Time InSeconds Ġ* Ġ 2 f ), Ġsin ( total Time InSeconds Ġ* Ġ 0 . 7 f ), Ġsin ( total Time InSeconds Ġ* Ġ 1 . 3 f )) ĊĠĠĠĠĠĠĠ Ġval Ġdiffuse Ġ= Ġlight Color Ġ* Ġ 0 . 5 f ĊĠĠĠĠĠĠĠ Ġval Ġambient Ġ= Ġdiffuse Ġ* Ġ 0 . 2 f ĊĠĠĠĠĠĠĠ Ġ*/ ĊĊĠĠĠĠĠĠĠ Ġ// Ġcube ĊĠĠĠĠĠĠĠ Ġgl . use Program ( cube Shader Program ) ĊĠĠĠĠĠĠĠ Ġgl . uniform ( gl . get UniformLocation ( cube Shader Program , Ġ" projection "), Ġprojection Matrix ) ĊĠĠĠĠĠĠĠ Ġgl . uniform ( gl . get UniformLocation ( cube Shader Program , Ġ" view "), Ġcamera . view Matrix ()) ĊĠĠĠĠĠĠĠ Ġ// Ġgl . uniform Matrix 4 fv ( gl . get UniformLocation ( cube Shader , Ġ" model "), Ġfalse , Ġcube Model ) ĊĠĠĠĠĠĠĠ Ġgl . uniform ( gl . get UniformLocation ( cube Shader Program , Ġ" camera Position "), Ġcam Pos . x , Ġcam Pos . y , Ġcam Pos . z ) ĊĠĠĠĠĠĠĠ Ġtest . diffuse Texture Path ?. also Ġ{ Ġcube Shader Program . bind Texture (" material . diffuse ", Ġ 0 , Ġit , Ġtest . sampling ) Ġ} ĊĠĠĠĠĠĠĠ Ġtest . specular Texture Path ?. also Ġ{ Ġcube Shader Program . bind Texture (" material . specular ", Ġ 1 , Ġit , Ġtest . sampling ) Ġ} ĊĠĠĠĠĠĠĠ Ġgl . uniform ( gl . get UniformLocation ( cube Shader Program , Ġ" material . sh in iness "), Ġ 3 2 . 0 f ) ĊĊĠĠĠĠĠĠĠ Ġ// Ġdirection al Ġlight ĊĠĠĠĠĠĠĠ Ġgl . uniform ( gl . get UniformLocation ( cube Shader Program , Ġ" dir Light . direction "), Ġ- 0 . 2 f , Ġ- 1 . 0 f , Ġ- 0 . 3 f ) ĊĠĠĠĠĠĠĠ Ġgl . uniform ( gl . get UniformLocation ( cube Shader Program , Ġ" dir Light . ambient "), Ġ 0 . 0 5 f , Ġ 0 . 0 5 f , Ġ 0 . 0 5 f ) ĊĠĠĠĠĠĠĠ Ġgl . uniform ( gl . get UniformLocation ( cube Shader Program , Ġ" dir Light . diffuse "), Ġ 0 . 4 f , Ġ 0 . 4 f , Ġ 0 . 4 f ) ĊĠĠĠĠĠĠĠ Ġgl . uniform ( gl . get UniformLocation ( cube Shader Program , Ġ" dir Light . specular "), Ġ 0 . 5 f , Ġ 0 . 5 f , Ġ 0 . 5 f ) ĊĠĠĠĠĠĠĠ Ġ// Ġpoint Ġlight Ġ 1 ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s [" point Lights [ 0 ]. position "] Ġ= Ġlight Positions [ 0 ] ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 0 ]. ambient ", Ġ 0 . 0 5 f , Ġ 0 . 0 5 f , Ġ 0 . 0 5 f ) ĊĊĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 0 ]. diffuse ", Ġ 0 . 8 f , Ġ 0 . 8 f , Ġ 0 . 8 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 0 ]. specular ", Ġ 1 . 0 f , Ġ 1 . 0 f , Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 0 ]. constant ", Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 0 ]. linear ", Ġ 0 . 0 9 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 0 ]. quadr atic ", Ġ 0 . 0 3 2 f ) ĊĠĠĠĠĠĠĠ Ġ// Ġpoint Ġlight Ġ 2 ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 1 ]. position ", Ġlight Positions [ 1 ]) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 1 ]. ambient ", Ġ 0 . 0 5 f , Ġ 0 . 0 5 f , Ġ 0 . 0 5 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 1 ]. diffuse ", Ġ 0 . 8 f , Ġ 0 . 8 f , Ġ 0 . 8 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 1 ]. specular ", Ġ 1 . 0 f , Ġ 1 . 0 f , Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 1 ]. constant ", Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 1 ]. linear ", Ġ 0 . 0 9 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 1 ]. quadr atic ", Ġ 0 . 0 3 2 f ) ĊĠĠĠĠĠĠĠ Ġ// Ġpoint Ġlight Ġ 3 ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 2 ]. position ", Ġlight Positions [ 2 ]) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 2 ]. ambient ", Ġ 0 . 0 5 f , Ġ 0 . 0 5 f , Ġ 0 . 0 5 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 2 ]. diffuse ", Ġ 0 . 8 f , Ġ 0 . 8 f , Ġ 0 . 8 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 2 ]. specular ", Ġ 1 . 0 f , Ġ 1 . 0 f , Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 2 ]. constant ", Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 2 ]. linear ", Ġ 0 . 0 9 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 2 ]. quadr atic ", Ġ 0 . 0 3 2 f ) ĊĠĠĠĠĠĠĠ Ġ// Ġpoint Ġlight Ġ 4 ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 3 ]. position ", Ġlight Positions [ 3 ]) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 3 ]. ambient ", Ġ 0 . 0 5 f , Ġ 0 . 0 5 f , Ġ 0 . 0 5 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 3 ]. diffuse ", Ġ 0 . 8 f , Ġ 0 . 8 f , Ġ 0 . 8 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 3 ]. specular ", Ġ 1 . 0 f , Ġ 1 . 0 f , Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 3 ]. constant ", Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 3 ]. linear ", Ġ 0 . 0 9 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" point Lights [ 3 ]. quadr atic ", Ġ 0 . 0 3 2 f ) ĊĠĠĠĠĠĠĠ Ġ// Ġspot Light ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" spot Light . position ", Ġcamera . position ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" spot Light . direction ", Ġcamera . front ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" spot Light . ambient ", Ġ 0 . 0 f , Ġ 0 . 0 f , Ġ 0 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" spot Light . diffuse ", Ġ 1 . 0 f , Ġ 1 . 0 f , Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" spot Light . specular ", Ġ 1 . 0 f , Ġ 1 . 0 f , Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" spot Light . constant ", Ġ 1 . 0 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" spot Light . linear ", Ġ 0 . 0 9 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" spot Light . quadr atic ", Ġ 0 . 0 3 2 f ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" spot Light . cut Off ", Ġcos ( 1 2 . 5 f . to Radians ())) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s . set (" spot Light . outer Cut Off ", Ġcos ( 1 7 . 5 f . to Radians ())) ĊĊĠĠĠĠĠĠĠ Ġgl . bind Buffer ( ARRAY _ BUFFER , Ġcube Buffer ) ĊĠĠĠĠĠĠĠ Ġcube Shader Program . enable Attributes () ĊĊĠĠĠĠĠĠĠ Ġval Ġaxis : ĠVector Ġ= ĠVector ( 1 . 0 f , Ġ 0 . 3 f , Ġ 0 . 5 f ) ĊĠĠĠĠĠĠĠ Ġfor Ġ(( index , Ġcube Position ) Ġin Ġcube Positions . with Index ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcube Shader Program . uniform s [" model "] Ġ= ĠMatrix Ġ{ Ġtranslate ( cube Position ); Ġrotate ( axis , ĠAngle ( 2 0 . 0 f Ġ* Ġindex )) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġgl . draw Arrays ( TRI ANG LES , Ġ 0 , Ġ 3 6 ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġ// Ġlight ĊĠĠĠĠĠĠĠ Ġgl . use Program ( light Shader Program ) ĊĠĠĠĠĠĠĠ Ġlight Shader Program . uniform s [" projection "] Ġ= Ġprojection Matrix ĊĠĠĠĠĠĠĠ Ġlight Shader Program . uniform s [" view "] Ġ= Ġcamera . view Matrix () ĊĊĠĠĠĠĠĠĠ Ġ// Ġ>> ĊĠĠĠĠĠĠĠ Ġ/* ĊĠĠĠĠĠĠĠ Ġgl . uniform Matrix 4 fv ( gl . get UniformLocation ( light Shader , Ġ" model "), Ġfalse , Ġlight Model ) ĊĠĠĠĠĠĠĠ Ġgl . bind Buffer ( ARRAY _ BUFFER , Ġlight ) ĊĠĠĠĠĠĠĠ Ġgl . bind Attributes ( light Shader Source ) ĊĠĠĠĠĠĠĠ Ġgl . draw Arrays ( TRI ANG LES , Ġ 0 , Ġ 3 6 ) ĊĠĠĠĠĠĠĠ Ġ*/ ĊĠĠĠĠĠĠĠ Ġfor Ġ( light Position Ġin Ġlight Positions ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlight Shader Program . uniform s [" model "] Ġ= ĠMatrix Ġ{ Ġtranslate ( light Position ); Ġscale ( 0 . 2 f ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġgl . bind Buffer ( ARRAY _ BUFFER , Ġlight Buffer ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlight Shader Program . enable Attributes () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġgl . draw Arrays ( TRI ANG LES , Ġ 0 , Ġ 3 6 ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ// Ġ<< ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġinput Ġ{ Ġevent Ġ-> ĊĠĠĠĠĠĠĠ Ġval Ġx Ġ= Ġevent . x 2 ĊĠĠĠĠĠĠĠ Ġval Ġy Ġ= Ġevent . y 2 ĊĠĠĠĠĠĠĠ Ġif Ġ( event . type Ġ== ĠInput EventType . DR AG ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcamera . process Input ( x Ġ- Ġmouse . x , Ġmouse . y Ġ- Ġy ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġmouse . assign ( x , Ġy ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġkeyboard Ġ{ Ġevent : ĠKeyEvent Ġ-> ĊĠĠĠĠĠĠĠ Ġif Ġ( event . type Ġ== ĠKey EventType . PRESS ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwhen Ġ( event . source ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠKeyEvent Source . W Ġ-> Ġcamera . process Keyboard ( FORWARD , Ġapp . elapsed Time ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠKeyEvent Source . S Ġ-> Ġcamera . process Keyboard ( BACK WARD , Ġapp . elapsed Time ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠKeyEvent Source . A Ġ-> Ġcamera . process Keyboard ( LEFT , Ġapp . elapsed Time ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠKeyEvent Source . D Ġ-> Ġcamera . process Keyboard ( RIGHT , Ġapp . elapsed Time ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ Ċ } Ċ