Author Topic: Problems with shading and lights  (Read 10659 times)

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4177
    • View Profile
    • Portfolio
Re: Problems with shading and lights
« Reply #30 on: 2015-Nov-04 »
No, that was a mention about that michael song, it came to my mind looking at the big ABC letters on pic.
Damn I spent some time attempting, but I can´t even seem to make any light work with imported objects on latest glb.

Will the FOV fix the tunnel? I will look at your code.

Offline ProN3rd

  • Mc. Print
  • *
  • Posts: 38
    • View Profile
Re: Problems with shading and lights
« Reply #31 on: 2015-Nov-04 »
i tried fov 15, but the shadow still dissapears when the camera is fully in the shadow, it only took a bit longer since you moved the camera -200. at this point the player sphere is already out of the obstacle shadow.

oh dear I just removed the skybox and now i get very weired results: i dont get any shadow at all... ... until i step infront of the obstacle.

lol, its like the opposite effect, the shadow is illuminated and appears when i step with the camera "inside it". completly the opposite and weired. Maybe its not a bug after all, but still its not working and i would need a skybox in any case :D

but probably it has something to do with the skybox, why should i not see a shadow in the first place when it is removed?

Code: GLBasic [Select]
                //Draw skybox  
            //            X_CULLMODE -1
             //           X_DRAWOBJ skybox,0
             //           X_CLEAR_Z
               //         X_CULLMODE 1
 
« Last Edit: 2015-Nov-04 by ProN3rd »

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1540
    • View Profile
Re: Problems with shading and lights
« Reply #32 on: 2015-Nov-04 »
Shadow without skybox when:

Code: GLBasic [Select]
                X_CULLMODE 1
 

Offline ProN3rd

  • Mc. Print
  • *
  • Posts: 38
    • View Profile
Re: Problems with shading and lights
« Reply #33 on: 2015-Nov-04 »
ah yes i see, but just brings it back to the usual problem :)

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1540
    • View Profile
Re: Problems with shading and lights
« Reply #34 on: 2015-Nov-04 »
ah yes i see, but just brings it back to the usual problem :)

The original issue seems solved to me. Note the more polys, the less gets clipped. Another CPU intensive task.  :sick: :whistle: Sync code:


Code: GLBasic [Select]
//Utility Class
        TYPE Txyz
                x;y;z
        ENDTYPE

        GLOBAL pl AS Txyz       ;       pl.x = -0;              pl.y = 0;               pl.z = 10       //Player
        GLOBAL cam AS Txyz      ;       cam.x = -50;    cam.y = 0;              cam.z = 10      //Cam
        GLOBAL li AS Txyz       ;       li.x = -100;    li.y = -400;    li.z = 500      //Light
        GLOBAL pr AS Txyz       ;       pr.x = 0;               pr.y = 0;               pr.z = -250     //Road
        GLOBAL po AS Txyz       ;       po.x = 100;             po.y = -75;             po.z = 0        //Obstacle (Wall)

//Create Objects
        GLOBAL player = GENX_OBJ()      ; CreateSphere(player,5, 36, RGB(255,0,0)) // added more sides for shadow resolution
        GLOBAL skybox = GENX_OBJ()      ; CreateCube(skybox,3000,RGB(40,40,100))
        GLOBAL road = GENX_OBJ()        ; CreateCube(road,500,RGB(100,100,100))
        GLOBAL obstacle = GENX_OBJ()    ; CreateCube(obstacle,100,RGB(130,50,130))

                GLOBAL fov = 90
                GLOBAL cam_offset = 0
                GLOBAL speed = 1

//Mainloop
        WHILE TRUE
                                IF NOT KEY(57) // no clipping - good
                                        cam_offset = -200
                                        fov = 15
                                        speed = 4
                                ELSE // clipping - no good
                                        cam_offset = 0
                                        fov = 90
                                        speed = 1
                                ENDIF
                X_MAKE3D 1, 2000, fov
                X_CAMERAUP 0,0,1
                X_CAMERA cam.x + cam_offset, cam.y, cam.z, pl.x, pl.y, pl.z
                X_CULLMODE 1
//                              X_ENABLE_Z TRUE
                //Draw skybox
//                        X_CULLMODE -1
//                        X_DRAWOBJ skybox, 0
//                        X_CLEAR_Z
//                        X_CULLMODE 1
                //Draw scene
                        X_MOVEMENT pr.x,pr.y,pr.z       ; X_DRAWOBJ road,0
                        X_MOVEMENT pl.x,pl.y,pl.z       ; X_DRAWOBJ player,0
                        X_MOVEMENT po.x,po.y,po.z       ; X_DRAWOBJ obstacle,0
                //Shadow light
                        X_SPOT_LT -3,0,li.x,li.y,li.z,0,0,0,360
                //Draw scene again
                        X_MOVEMENT pr.x,pr.y,pr.z       ; X_DRAWOBJ road,0
                        X_MOVEMENT pl.x,pl.y,pl.z       ; X_DRAWOBJ player,0
                        X_MOVEMENT po.x,po.y,po.z       ; X_DRAWOBJ obstacle,0
                X_MAKE2D
                SHOWSCREEN

                //Movement Player
                        IF KEY(200); INC pl.x, speed; INC cam.x, speed; ENDIF
                        IF KEY(208); DEC pl.x, speed; DEC cam.x, speed; ENDIF
        WEND

//Utility Functions
 
« Last Edit: 2015-Nov-04 by matchy »

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1540
    • View Profile
Re: Problems with shading and lights
« Reply #35 on: 2015-Nov-04 »
ah yes i see, but just brings it back to the usual problem :)

The original issue seems solved to me. Note also, the more polys on the boxes, the less gets clipped and more shadow resolution. Another CPU intensive task.  :sick: :whistle: Sync code:


Code: GLBasic [Select]
//Utility Class
        TYPE Txyz
                x;y;z
        ENDTYPE

        GLOBAL pl AS Txyz       ;       pl.x = -0;              pl.y = 0;               pl.z = 10       //Player
        GLOBAL cam AS Txyz      ;       cam.x = -50;    cam.y = 0;              cam.z = 10      //Cam
        GLOBAL li AS Txyz       ;       li.x = -100;    li.y = -400;    li.z = 500      //Light
        GLOBAL pr AS Txyz       ;       pr.x = 0;               pr.y = 0;               pr.z = -250     //Road
        GLOBAL po AS Txyz       ;       po.x = 100;             po.y = -75;             po.z = 0        //Obstacle (Wall)

//Create Objects
        GLOBAL player = GENX_OBJ()      ; CreateSphere(player,5, 36, RGB(255,0,0)) // added more sides for shadow resolution
        GLOBAL skybox = GENX_OBJ()      ; CreateCube(skybox,3000,RGB(40,40,100))
        GLOBAL road = GENX_OBJ()        ; CreateCube(road,500,RGB(100,100,100))
        GLOBAL obstacle = GENX_OBJ()    ; CreateCube(obstacle,100,RGB(130,50,130))

                GLOBAL fov = 90
                GLOBAL cam_offset = 0
                GLOBAL speed = 1

//Mainloop
        WHILE TRUE
                                IF NOT KEY(57) // no clipping - good
                                        cam_offset = -200
                                        fov = 15
                                        speed = 4
                                ELSE // clipping - no good
                                        cam_offset = 0
                                        fov = 90
                                        speed = 1
                                ENDIF
                X_MAKE3D 1, 2000, fov
                X_CAMERAUP 0,0,1
                X_CAMERA cam.x + cam_offset, cam.y, cam.z, pl.x, pl.y, pl.z
                X_CULLMODE 1
//                              X_ENABLE_Z TRUE
                //Draw skybox
//                        X_CULLMODE -1
//                        X_DRAWOBJ skybox, 0
//                        X_CLEAR_Z
//                        X_CULLMODE 1
                //Draw scene
                        X_MOVEMENT pr.x,pr.y,pr.z       ; X_DRAWOBJ road,0
                        X_MOVEMENT pl.x,pl.y,pl.z       ; X_DRAWOBJ player,0
                        X_MOVEMENT po.x,po.y,po.z       ; X_DRAWOBJ obstacle,0
                //Shadow light
                        X_SPOT_LT -3,0,li.x,li.y,li.z,0,0,0,360
                //Draw scene again
                        X_MOVEMENT pr.x,pr.y,pr.z       ; X_DRAWOBJ road,0
                        X_MOVEMENT pl.x,pl.y,pl.z       ; X_DRAWOBJ player,0
                        X_MOVEMENT po.x,po.y,po.z       ; X_DRAWOBJ obstacle,0
                X_MAKE2D
                SHOWSCREEN

                //Movement Player
                        IF KEY(200); INC pl.x, speed; INC cam.x, speed; ENDIF
                        IF KEY(208); DEC pl.x, speed; DEC cam.x, speed; ENDIF
        WEND

//Utility Functions
 

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4177
    • View Profile
    • Portfolio
Re: Problems with shading and lights
« Reply #36 on: 2015-Nov-05 »
Oh well, I tried it myself, found no solution, but here the test code.
Damn shadows, works fine till you get into its path,
-WASD to move camera, Q and E up and down.
-Arrows to move ball.
-5123 numpads to move light, 4 and 5 up and down.
That is what I get for being sure of the unknown. :-[ :x

edit:also, I could not get the imported objects to shade or take light at all. :(
Nice code structure ProN3rd!
« Last Edit: 2015-Nov-05 by erico »

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1540
    • View Profile
Re: Problems with shading and lights
« Reply #37 on: 2015-Nov-05 »
You're FOV is high but now I'll also have a go at the experimental challenge of a straight raceway road with some tunnel like objects with the aim of no shade clipping or missing.  :bed:

Offline ProN3rd

  • Mc. Print
  • *
  • Posts: 38
    • View Profile
Re: Problems with shading and lights
« Reply #38 on: 2015-Nov-05 »
nice example erico, this is exactly what i mean. Stepping with the cam in the shadow makes the shadow disspear

do you think its a bug, or what could it possibly mean?

for fov, i think you cant choose values that are so low like 15. The scene will be totally distorted. I guess usual values range from 60-100?

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4177
    • View Profile
    • Portfolio
Re: Problems with shading and lights
« Reply #39 on: 2015-Nov-05 »
Truly now I think it may be a bug, but I would not know for sure. (still trying to glup my ego on the last posts :puke:)
Heck, what is theory without practice anyways, I would not believe without code? ;/

Well, I tried many naive solutions, like extra objects and so on and nothing worked,
so I guess the baking/faking could do...

...but if you really must use this shadow system, I got into a way of it working but it restricts a lot. In case of your game, these restriction might not be a problem.

-Assume a fixed light position behind the camera, so that the shadows are projected towards the back of the stage.
(preferably, parent it to camera position so to simulate a linear light)
-keep the light positioned way up so that the rear part of the shadow is not too distorted in comparison to the front.
(you may need to use key 4 to push it up more then what is hardcoded)
-slice your tunnel like a bread bin, each part an object.
-try working with low POV so that you are visually into the shadow before the actual camera is.

Now, when you hit the shadow and it disapears, it is already outside the player´s view and it works.

You will need to play a lot with the elements to get what you want,
here is appended code with it working, use it with my last project, just add that GARAGEslice.ddd to media.
But I believe you get the idea.

edit: You might also be aware that while this works, while you are under a shadow, the player is being shaded but it does not cast its own shadows unless you are out of the shadow area. ye, camera in tunnel, player outside it without its own shadow. Haven´t tried a fix for this yet but you could simply stop drawing the shadow of the object that you are under its shadow and things should work out... it is already out of the camera view anyways  :good:  ;)

Code: GLBasic [Select]
// --------------------------------- //
// Project: GLB3D
// Start: Wednesday, November 04, 2015
// IDE Version: 14.006
 SETCURRENTDIR("Media")

//      ---     BOOT
X_LOADOBJ "WORLD.ddd",0         ; LOADSPRITE "WORLD.png",0
X_LOADOBJ "FLOOR.ddd",1         ; LOADSPRITE "FLOOR.png",1
X_LOADOBJ "PLAYER.ddd",2        ; LOADSPRITE "PLAYER.png",2
X_LOADOBJ "GARAGE.ddd",3        ; LOADSPRITE "GARAGE.png",3
X_LOADOBJ "BLOCO.ddd",4
X_LOADOBJ "BLOCOT.ddd",5
X_LOADOBJ "BLOCOU.ddd",6
X_LOADOBJ "GARAGEmask.ddd",7
X_LOADOBJ "GARAGEslice.ddd",8
LOADFONT "smalfont.png", 1

GLOBAL x,y,z, camx, camy        ; x=0 ; y=10 ; z=80
GLOBAL xo,yo,zo
GLOBAL lx,ly,lz                         ; lx=100 ; ly=150 ; lz=75

//      --------------------------------------------------------------- LOOP START
WHILE TRUE

//      --- set 3d and camera
X_MAKE3D 1,2000,60
X_CAMERA x,y,z, x+camx,y+camy,z-1000

//      --- draw
X_CULLMODE 1                    //WORLD
X_SETTEXTURE 0,-1
X_DRAWOBJ 0,0
X_CLEAR_Z
X_FOG RGB(182,141,99),FALSE,25, 125

X_SETTEXTURE 1, -1              //FLOOR
X_DRAWOBJ 1,0

X_SETTEXTURE 2, -1              //PLAYER
X_MOVEMENT xo,yo,zo
X_DRAWOBJ 2,0
X_MOVEMENT 0,0,0



X_SETTEXTURE 3, -1              //GARAGE
//X_DRAWOBJ 3,0
//X_DRAWOBJ 7,0
X_DRAWOBJ 8,0
X_MOVEMENT 0,0,-11
X_DRAWOBJ 8,0
X_MOVEMENT 0,0,-22
X_DRAWOBJ 8,0
X_MOVEMENT 0,0,-33
X_DRAWOBJ 8,0
X_MOVEMENT 0,0,0
                                                //blocks
//X_DRAWOBJ 4,0
//X_DRAWOBJ 5,0
//X_DRAWOBJ 6,0


//      --- light
        //X_AUTONORMALS 2
        //X_AMBIENT_LT 0, RGB(255,255,255)
//      X_SPOT_LT 1,RGB(255,0,0), x,y,z,0,0,0,360
        X_SPOT_LT -3,RGB(0,0,0),x+750,y+2500,z+750, 0,0,0,360



//      --- shadows
        X_MOVEMENT xo,yo,zo
        X_DRAWOBJ 2,0                           //PLAYER
        X_MOVEMENT 0,0,0
//      X_DRAWOBJ 3,0                           //GARAGE
//      X_DRAWOBJ 7,0
//      X_DRAWOBJ 4,0
//      X_DRAWOBJ 5,0
//      X_DRAWOBJ 6,0
IF z>28 THEN X_DRAWOBJ 8,0
X_MOVEMENT 0,0,-11
IF z>17 THEN X_DRAWOBJ 8,0
X_MOVEMENT 0,0,-22
IF z>6 THEN X_DRAWOBJ 8,0
X_MOVEMENT 0,0,-33
IF z>-5 THEN X_DRAWOBJ 8,0
X_MOVEMENT 0,0,0



//      --- set 2d and print info
X_MAKE2D

SETFONT 1
PRINT x,0,0
PRINT y,0,20
PRINT z,0,40
PRINT camx,0,60
PRINT camy,0,80

PRINT xo,0,120
PRINT yo,0,140
PRINT zo,0,160

PRINT lx,0,200
PRINT ly,0,220
PRINT lz,0,240

//      --- controls camera
//INC camx, MOUSEAXIS(0)
//DEC camy, MOUSEAXIS(1)

IF KEY(30)=1 THEN x=x-.5                        //left KEY
IF KEY(32)=1 THEN x=x+.5                        //right KEY

IF KEY(31)=1 THEN z=z+.5                        //up key
IF KEY(17)=1 THEN z=z-.5                        //down key

IF KEY(16)=1 THEN y=y-.5                        // q
IF KEY(18)=1 THEN y=y+.5                        // e

//      --- controls player
IF KEY(203)=1 THEN xo=xo-.5                     //left KEY
IF KEY(205)=1 THEN xo=xo+.5                     //right KEY

IF KEY(208)=1 THEN zo=zo+.5                     //up key
IF KEY(200)=1 THEN zo=zo-.5                     //down key

//      --- controls LIGHTS!
IF KEY(79)=1 THEN lx=lx-.5                      //left KEY
IF KEY(81)=1 THEN lx=lx+.5                      //right KEY

IF KEY(80)=1 THEN lz=lz+.5                      //up key
IF KEY(76)=1 THEN lz=lz-.5                      //down key

IF KEY(75)=1 THEN ly=ly+.5                      //up key
IF KEY(77)=1 THEN ly=ly-.5                      //down key

SHOWSCREEN
WEND
//      --------------------------------------------------------------- LOOP END

Edit extra: updates the code so the slices behind the camera are off so not to cause problem to shadows on objects outside the tunnel. Move camera with WASD and player with arrows. Light is now parented to the camera.
« Last Edit: 2015-Nov-05 by erico »

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4177
    • View Profile
    • Portfolio
Re: Problems with shading and lights
« Reply #40 on: 2015-Nov-06 »
Another thing you could try, so to keep a FOV high and/or a better balance all around, is to check if X_CAMERAUP helps on this front.
Is the shifted position affected? If not but the original, then you could rotate the whole world so as to use it as a "X_CAMERABACK" and it would give you more freedom.

I really hope this all helps out, I have had my mind into 3d in GLB for a while, all these experiments also help me clear up the catches. :)

Offline ProN3rd

  • Mc. Print
  • *
  • Posts: 38
    • View Profile
Re: Problems with shading and lights
« Reply #41 on: 2015-Nov-07 »
thanks erico, this helps a lot, glad to hear you also making progress

i was so busy the last days gonna check this all out in detail later.

yes cameraup, is quite shifted, i prefer to move the camera instead of objects, i heared this doenst make a difference and camera movement is more logical to me, but i guess the usual way is to move objects. Not sure about that

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4177
    • View Profile
    • Portfolio
Re: Problems with shading and lights
« Reply #42 on: 2015-Nov-07 »
...
oh dear I just removed the skybox and now i get very weired results: i dont get any shadow at all... ... until i step infront of the obstacle.

lol, its like the opposite effect, the shadow is illuminated and appears when i step with the camera "inside it". completly the opposite and weired. Maybe its not a bug after all, but still its not working and i would need a skybox in any case :D

but probably it has something to do with the skybox, why should i not see a shadow in the first place when it is removed?
...

You need to set the cullmode for shadows to happen, so if the polygons are facing outwards, which is correct, you get the shadow casted, if it is inverted, you will get that effect you described. The only way I believe this could happen on your case regarding the sky box, is that its cullmode is wrong, but that should not be a problem because you shall not re-render the skybox under the shadow light at all. Remember what you re-render on your scene after the shadow light are only the objects which are casting shadow, no need for the ones receiving it.

Notice on my code that I´m not re-rendering the floor neither the skybox, they both need no shadow casted, you may also add the X_CLEAR_Z command after you draw the skybox so you guarantee that it dosen´t receive shadows either.

Also, the skybox should have its surfaces facing inside, otherwise use cullmode 2 to ´flip´ it.
In my example I already modeled the cube the former way, so cullmode 1 works as it should.

Messing with realtime 3d is always fun. :good:
« Last Edit: 2015-Nov-07 by erico »

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5013
  • O Rly?
    • View Profile
Re: Problems with shading and lights
« Reply #43 on: 2015-Nov-10 »
It's a ooooooold Problem! It was never fixed i guess...
Try to render with OpenGL.
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline ProN3rd

  • Mc. Print
  • *
  • Posts: 38
    • View Profile
Re: Problems with shading and lights
« Reply #44 on: 2015-Nov-11 »
i had some problems with clipping of the skybox and i read somewhere that i need the following Open GL Command to include right after i set the texture of the skycube:

Code: GLBasic [Select]
                glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
                glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
 

if i could just find a way to make it work.  :giveup:  I have some example from the 2d scaling system which makes GL_LineWidth work, but i cant derive from that how I would figure out how to make the above commands work. Any help is very appreciated  :nw: