Problems with shading and lights

Previous topic - Next topic


I am working on a 3d racing game. Everything works so far, got a nice racing course, a player who camera follows smoothly in loopings and such, as well as three enemies who drive on the track as well. Now, I wanted to apply lights and shading to everything and I read a lot in this forum and tries so many things, but cant make it work properly.

This is the code in short (often speaks more than thousand words):

Code (glbasic) Select

X_MAKE3D 1, 1000, 90

//Setting the camera
X_CAMERAUP Cam.front, Cam.side, Cam.up
X_CAMERA Cam.x, Cam.y, Cam.z, Cam.dx, Cam.dy,

//Light Rendering
FOREACH e IN Enemies[]  //Three enemies in total, shall have a 90degree light at their front vector
X_SPOT_LT e.number, e.color,e.x, e.y, e.z, e.frontx, e.fronty, e.frontz, 90

//Object Rendering
FOREACH o IN self.object[];   //All objects are rendered (Racing course, Enemies, Players etc..)
X_MOVEMENT o.x,o.y,o.z
X_DRAWOBJ o.obj ,0


I tried to include the command:
Code (glbasic) Select

X_SPOT_LT -3, color,x,y, z, 0,0, 0, 360

after the pipeline, and rendered the object agains. I also tried the value of "-2" for toon shading and took some code from the examples file of glbasic:

Code (glbasic) Select

X_LOADSHADER(13, "shader/toon.vert", "shader/toon.frag")
  DIMDATA cheap[], .5, 1
    DIMDATA nice[], .3, .6, .6, .6, .6, .6, .6, .7, 1, 1, 1, 1, 1, 1, 1, 1

however I cant get shadows working at all, and the lights  arenot very bright. I tried Cullmode 1 and -1 as well as using bump mapping and ambient light. I cant even describe what i did and what failed. I am just an amateuer and hope someone can explaine properly how can i achieve the following things:

draw the objects (racing course, vehicles)
4 bright Spotlights in total for each vehicle.

enclosed a little screenshot (with only 1 enemy, and without skybox) I reduced everything to the bare minimum for these testings:


I'm interested in your race track as I am trying something similar. Any chance of video demo?

Sh sh sh shadows  :sick:. It's tricky but the 3D samples folder that demonstrates it. As object need to be drawn twice, it's cuts the processing time in half.

Normal spotlight
Draw objects
Turn on shadow spot light
Draw objects (again same)
Cast shadow spot light color

For each of a maximum of seven spotlights (num 0 - 7) there is a shadow light and repeated objects drawn (doubled), then another shadow spotlight. :doubt:

Here's my shadow room low and hires tests:

From 3D Samples folder:
Code (glbasic) Select

// --------------------------------- //
// Project: Shadows
// Start: Thursday, January 19, 2006
// IDE Version: 3.011

// konstante Werte / Constant values
LOCAL image, none, shadow, sphere, donut, cube, spot_ang, lmode, phi, lx
LOCAL ly, lz, i, psi, px, py, pz, j

none = -1
image = 0

donut  = 0
cube   = 1
sphere = 2
// Create Primitives
CreateTorus (donut, 10, 20, 36, 12, 2, 2, RGB(255,255,255))
CreateCube  (cube, -1000, RGB(255, 255, 255))

// Grafiken laden / load graphics
LOADSPRITE      "image.bmp", image
//LOADBUMPTEXTURE "bump.bmp",  bump
LOCAL mx, my, b1, b2 // compiler warning

// naja... / well...
mx = mx + MOUSEAXIS(0)
// MOUSESTATE mx, my, b1, b2

// 1000 ist weit genug hier / 1000 is far enough here
X_MAKE3D 1, 1700, 45

// ein Winkel für alles / one angle for everything
phi = GETTIMERALL() / 250

// eine Lichtposition / a light position
lx = SIN(-phi*3)*300
ly = 20 //(SIN(3*phi)-COS(4*phi))*100
lz = COS(-phi*3)*300

// Maus X=drehen / Mouse X=turn
X_CAMERA 400*COS(mx),120,400*SIN(mx), 0,0,0

// Licht als Punkt zeichnen / Draw light as dot
X_DOT lx,ly,lz,32,RGB(255,255,255)

// Texture setzen / Set texture
X_SETTEXTURE image, none


X_SPOT_LT   0, RGB(255,255,255), lx,ly,lz, -lx,-ly,-lz, 360

// Würfel zeichnen / Draw Cube
X_DRAWOBJ cube, 0

// Donut zeichnen / Draw Donut
X_ROTATION 45, 1,0,0
X_DRAWOBJ donut,0

// Schatten einschalten / turn on Shadows
X_SPOT_LT  -3, 0, lx,ly,lz, -lx,-ly,-lz, 360

// neuzechnen von schatten-werfenden Objekten
// re-draw shadow casting objects
X_ROTATION 45, 1,0,0
X_DRAWOBJ donut,0

// Schatten zeichnen / draw shadows
X_SPOT_LT  -3, RGB(0,0,64), 0,0,0, 0,0,0, 0
// oder / or

// zum Zurechtfinden / for navigation
PRINT my, 0,0


Thank you, suddenly things works, cant describe how happy i am  :enc:

Of course I just begin to understand the process: did I get you right that for each light that shall cast a shadow I have to use the complete pipeline?
Such as...
Code (glbasic) Select

For each light
   Set Normal spotlight //(for example light Number 1 with player coordinates)
  // hmm, I included a light with -1 value here as well for bumpmapping, how can this be handled?
   Draw objects
   Turn on shadow spot light //(Number -3 with player coordinates?)
   Draw objects
   Cast shadow spot light color  // (player coordinates, how can i set color?)

anyway i make so much progress and I am greatful for your help. I couldnt get any results with toon shading yet. I wonder how it works. Check this video I just uploaded to youtube, where i am testing todays results:


Looks great! Is the track procedurally done?


Wow, for cool....even loops.  =D :good: :coke:

Now how is the track designed and constructed? How do you calculate track gravity/orientation?

Again, in regards to the toon shader; Are you settting X_SETSHADER?

Code (glbasic) Select

// --------------------------------- //
// Project: Shaders
// Start: Monday, September 25, 2006
// IDE Version: 3.262

LOCAL shaders$[], num, current$
IF X_LOADSHADER(13, "toon.vert", "toon.frag") = FALSE
PRINT "Shader failed to load: ", 100,100

CreateTorus(0, 5, 10, 12, 9, 2, 2, RGB(0xff, 0xff, 0xff))
// CreateSphere(0, 10, 9, RGB(0xff, 0xff, 0xff) )
DRAWRECT 0,0,640,480,RGB(255,128,0)
X_MAKE3D 1,500, 45
X_CAMERA 0,0,-100, 0,0,0

X_SPOT_LT 0, RGB(255,255,255),100,100,-1000, -.1,-.1,1, 360

X_ROTATION 45, 1,1,0

Ian Price

That looks totally awesome!!!  :good:

Keep us posted on your progress :)
I came. I saw. I played.


Thanks guys for the nice words, but i couldnt have done it without the amazing support of you and all the examples here in the GL Basic community.

The track is generated procedurally. I learned a lot from the "Pseudo 3D" example from the Showroom and the S Zero example. The track is basically build of various segments (which can be straight, curve or looping). Each segment uses of course more inputs than just that, for example the lenght, the roadwidth, the height, and the exact amount of curveture as well as some looping paramenters can be adjusted for each segment. A segment consists of various subsegments, which are the actual triangles that get drawn. So if i need a track that goes forward, curve, forward, looping etc.. i can easly build it within seconds :)

For the orientation each vehicle has three perpendicular unity normal vectors that get rotated according to the normal of the collision surface via a rotation matrix. Smooth camera movement is a bit tricky, i still couldnt find the optimal solution.

At the weekend I will have more time to continue with the project, gonna try out X_Shader command.


Oh I see now and I will have to check out those showroom samples. The similar race track I had was segmented but only a flat road and any road segment can be added that would go on forever rather than in track loop, like a forever runner phone game. So I am guessing the segments, along with the textures, are imported files or constructed in real time? How is the mapping of the road done? Is it manual or auto seed generated map?  :-[

get ready for cartoon racers  =D


Quote from: matchy on 2015-Oct-23
Oh I see now and I will have to check out those showroom samples. The similar race track I had was segmented but only a flat road and any road segment can be added that would go on forever rather than in track loop, like a forever runner phone game. So I am guessing the segments, along with the textures, are imported files or constructed in real time? How is the mapping of the road done? Is it manual or auto seed generated map?  :-[

get ready for cartoon racers  =D

The track is one object (ok basically there are three, but not that important now) and gets build from lots of vertexes at the start of the program. Let me write some pseude code so you get the idea what happens before the mainloop:

Code (glbasic) Select

Function BuildTrack:

LOCAL starttrack = 0

startobj track
addSegment(starttrack, straight,lenght = 200, newheight = 100, number of subsegments etc...)
addSegment(starttrack,curve,lenght = 200, newheight = 0,  number of subsegments etc...)
addSegment(starttrack,looping,lenght = 200, newheight = 0, etc... getting the idea)
addmoreSegments ....

Function AddSegment: byref starttrack, all the other  parameters...,
calculate all x, y,z and whatever needed for subsegments according to the input parameters
calculate the end of this segment to byref it for the next segment later
for i = 0 to number of subsegments
    addvertexes x,y,z  //just usal tex coordinates like when drawing a rectangular surface from 4x vertexes
    addmorevertexes etc....

return startrack (= end of this segment)

OK, now the three objects are: Road, Borders and Decorating things make the bottom and the outer sides. Since Road and Borders only are used for collision testing later and they have different collision formulas, i had to split up the track into more than one objects, so I could "talk" the the borders and the road independently.


I'm working on a pseudo 3D racing game at the moment - although its more or less exploring how to read in track, tile, static and border information efficiently.

So far, the track layout is just a list of codes, lengths and height changes.


Quote from: ProN3rd on 2015-Oct-23
The track is one object...

I appreciate your thorough respones champ.  8) I'm still wondering where's the layout data for the whole track to be mapped? Is there or will it have/need a track designer?  ::)


the actual data for each segment that flows into the createtrack function is saved and loaded using a txt file. Because i dont like to code hard values in the program or type them into the textfile manually, i build a small editor, so you can actually build a track from any kind of segments in real time and save everything, back to the txt file if it needs to be adjusted later. however the editor is buggy at the moment and by no means any better than dealing with plain numbers, but shall be a part of the final programm so one can make custom tracks  =D

here is a screenshot that highlights some features. the obj setting is the "world" editor, where i can include primitives and objects. The track editor is a different thing. As mentioned the track is made of three objects.


Ah well that's cool and hope to see a video of that.  ;) This is something that I am trying to avoid and have been able to created maps and landscapes from automated algos, such as using mazes. Even a developer tool version for testing or a user end version. Like in my zombie games, there is an internal and editor online.  8) Then I moved on to racing, then you present this explains why I'm so interested in your method.  :whistle:


i think such an algorythm can be easly modfied so the buildtrack function gets just random values and creates a new track every time


Gee! :O :O
That editor does look cool! :good: