Hi guys
I've got a little program which uses multiple shaders and cameras, and when it comes to rendering them, the screen flickers like mad! The frame rate is fine (around 100fps on my machine), but the screen flickering is really annoying!
I'm new to using multiple shaders and cameras, so I'm probably missing something REALLY obvious... =\
Any help is appreciated
Type game_object
flt_x as Float
flt_y as Float
flt_z as Float
flt_angle_x as Float
flt_angle_y as Float
flt_angle_z as Float
flt_scale_x as Float
flt_scale_y as Float
flt_scale_z as Float
str_object as String
dwd_texture as DWord
str_shader as String
str_texture as String
bol_shader_applied as Boolean
EndType
Type light_emmiter
flt_x as Float
flt_y as Float
flt_z as Float
flt_range as Float
bte_r as Byte
bte_g as Byte
bte_b as Byte
Endtype
Dim game_objects( 9 ) as game_object
game_objects( 1 ).flt_x = -50
game_objects( 1 ).flt_y = -50
game_objects( 1 ).flt_z = 0
game_objects( 1 ).flt_angle_x = 0
game_objects( 1 ).flt_angle_y = 0
game_objects( 1 ).flt_angle_z = 0
game_objects( 1 ).flt_scale_x = 100
game_objects( 1 ).flt_scale_y = 100
game_objects( 1 ).flt_scale_z = 100
game_objects( 1 ).dwd_texture = 21
game_objects( 1 ).str_object = "Cube"
game_objects( 1 ).str_shader = "Refraction"
game_objects( 1 ).str_texture = "glass"
game_objects( 2 ).flt_x = 50
game_objects( 2 ).flt_y = -50
game_objects( 2 ).flt_z = 0
game_objects( 2 ).flt_angle_x = 0
game_objects( 2 ).flt_angle_y = 0
game_objects( 2 ).flt_angle_z = 0
game_objects( 2 ).flt_scale_x = 100
game_objects( 2 ).flt_scale_y = 100
game_objects( 2 ).flt_scale_z = 100
game_objects( 2 ).dwd_texture = 221
game_objects( 2 ).str_object = "Cube"
game_objects( 2 ).str_shader = "Reflection"
game_objects( 2 ).str_texture = "floor"
game_objects( 3 ).flt_x = -50
game_objects( 3 ).flt_y = 50
game_objects( 3 ).flt_z = 0
game_objects( 3 ).flt_angle_x = 0
game_objects( 3 ).flt_angle_y = 0
game_objects( 3 ).flt_angle_z = 0
game_objects( 3 ).flt_scale_x = 100
game_objects( 3 ).flt_scale_y = 100
game_objects( 3 ).flt_scale_z = 100
game_objects( 3 ).dwd_texture = 221
game_objects( 3 ).str_object = "Cube"
game_objects( 3 ).str_shader = "Normal"
game_objects( 3 ).str_texture = "floor"
game_objects( 4 ).flt_x = 50
game_objects( 4 ).flt_y = 50
game_objects( 4 ).flt_z = 0
game_objects( 4 ).flt_angle_x = 0
game_objects( 4 ).flt_angle_y = 0
game_objects( 4 ).flt_angle_z = 0
game_objects( 4 ).flt_scale_x = 100
game_objects( 4 ).flt_scale_y = 100
game_objects( 4 ).flt_scale_z = 100
game_objects( 4 ).dwd_texture = 221
game_objects( 4 ).str_object = "Cube"
game_objects( 4 ).str_shader = "ReflectNormal"
game_objects( 4 ).str_texture = "floor"
game_objects( 5 ).flt_x = 0
game_objects( 5 ).flt_y = 0
game_objects( 5 ).flt_z = 0
game_objects( 5 ).flt_angle_x = 0
game_objects( 5 ).flt_angle_y = 0
game_objects( 5 ).flt_angle_z = 0
game_objects( 5 ).flt_scale_x = 100
game_objects( 5 ).flt_scale_y = 100
game_objects( 5 ).flt_scale_z = 100
game_objects( 5 ).dwd_texture = 21
game_objects( 5 ).str_object = "Cube"
game_objects( 5 ).str_shader = "HeatHaze"
game_objects( 5 ).str_texture = "glass"
Dim light_emmiters( 3 ) as light_emmiter
light_emmiters( 0 ).flt_x = 0
light_emmiters( 0 ).flt_y = 0
light_emmiters( 0 ).flt_z = 0
light_emmiters( 0 ).flt_range = 500
light_emmiters( 0 ).bte_r = 255
light_emmiters( 0 ).bte_g = 255
light_emmiters( 0 ).bte_b = 255
light_emmiters( 1 ).flt_x = 200
light_emmiters( 1 ).flt_y = 0
light_emmiters( 1 ).flt_z = 0
light_emmiters( 1 ).flt_range = 500
light_emmiters( 1 ).bte_r = 255
light_emmiters( 1 ).bte_g = 0
light_emmiters( 1 ).bte_b = 0
light_emmiters( 2 ).flt_x = -200
light_emmiters( 2 ).flt_y = 0
light_emmiters( 2 ).flt_z = 0
light_emmiters( 2 ).flt_range = 500
light_emmiters( 2 ).bte_r = 0
light_emmiters( 2 ).bte_g = 0
light_emmiters( 2 ).bte_b = 255
Global dwd_number_of_lights as DWord = 3
// Shaders and Effects
Global bte_sky_box_effect as Byte = 1
Global bte_refraction_effect as Byte = 2
Global bte_reflection_effect as Byte = 3
Global bte_normal_mapping_shader as Byte = 4
Global bte_normal_reflect_shader as Byte = 5
Global bte_heat_haze_shader as Byte = 6
Global bte_bloom_shader as Byte = 7
Global bte_quad_effect as Byte = 10
// Cameras
Global bte_free_camera as Byte = 0
Global bte_refraction_camera as Byte = 0
Global bte_reflection_camera as Byte = 0
Global bte_normal_reflect_camera as Byte = 0
Global bte_bloom_camera as Byte = 0
// Dynamic Textures
Global dwd_refraction_texture as DWord = 1
Global dwd_reflection_texture as DWord = 2
Global dwd_reflection_cube_map as DWord = 3
Global dwd_normal_reflect_texture as Dword = 4
Global dwd_bloom_texture as DWord = 5
// Regular Textures
Global dwd_floor_default_texture as DWord = 8
Global dwd_floor_normal_texture as DWord = 9
// Objects
Global dwd_bloom_quad_object as DWord = 9999
// Errors
Global dwd_objects_on_screen as DWord = 0
Global wrd_error_code as Word = 0
// Settings
Global str_reflect_effect as String = "Enabled"
Global str_refract_effect as String = "Enabled"
Global str_normal_effect as String = "Enabled"
Global str_normal_reflect_effect as String = "Enabled"
Global str_heat_haze_effect as String = "Enabled"
Global str_bloom_effect as String = "Disabled"
Global bol_is_night_vision as Boolean = 0
Global dwd_night_vision_texture as DWord = 99
Global dwd_darkness_texture as DWord = 98
Global bte_night_vision_alpha as Byte = 0
Global bol_is_dark as Boolean = 0
Global flt_motion_velocity as Float = 0
Global str_shader_detail as String = "High"
Global flt_shader_detail as Float = 0.1
// -------------------------------------------------------------------------------------------------
// Main Loop
// -------------------------------------------------------------------------------------------------
Sync On
AutoCam Off
Position Camera 0, 0, -200
Sync Rate 0
Do
If str_shader_detail = "Low" then flt_shader_detail = 0.25
If str_shader_detail = "Medium" then flt_shader_detail = 0.5
If str_shader_detail = "High" then flt_shader_detail = 1.0
If str_refract_effect = "Enabled" or str_heat_haze_effect = "Enabled" then Refraction_Camera()
If str_reflect_effect = "Enabled" or str_normal_reflect_effect = "Enabled" then Reflection_Camera()
If str_bloom_effect = "Enabled" then Bloom()
If LeftKey() = 1 then Position Camera Camera Position X() - 10, Camera Position Y(), Camera Position Z()
If RightKey() = 1 then Position Camera Camera Position X() + 10, Camera Position Y(), Camera Position Z()
Sky_Box()
Get_Object( 1 )
Get_Object( 2 )
Get_Object( 3 )
Get_Object( 4 )
Get_Object( 5 )
Direct_X_Update()
Text 32, 32, "FPS: " + STR$( Screen FPS() )
FastSync
Loop
Function Sky_Box()
If Object Exist( 9 ) = 0
Make Object Sphere 9, 2000, 5, 5
Set Object Cull 9, 0
Endif
Get_Content( 30, "cube0", "DDS", 2 )
Texture Object 9, 0, 30
Get_Content( bte_sky_box_effect, "skybox", "FX", 0 )
Set Object Effect 9, bte_sky_box_effect
EndFunction
// -------------------------------------------------------------------------------------------------
// Get Object
// -------------------------------------------------------------------------------------------------
Function Get_Object( dwd_id as DWord )
// Get Textures of Object and Normal Map
Get_Content( game_objects( dwd_id ).dwd_texture, game_objects( dwd_id ).str_texture, "DDS", 0 )
Get_Content( game_objects( dwd_id ).dwd_texture + 1, game_objects( dwd_id ).str_texture + "_n", "DDS", 0 )
// Object Functions
If Object Exist( dwd_id ) = 0
// Create Object and Scale according to Game Object Array
Make Object Plane dwd_id, 100, 100
Scale Object dwd_id, game_objects( dwd_id ).flt_scale_x, game_objects( dwd_id ).flt_scale_y, game_objects( dwd_id ).flt_scale_z
INC dwd_objects_on_screen, 1
Else
// Rotate and Position the Object
Rotate Object dwd_id, game_objects( dwd_id ).flt_angle_x, game_objects( dwd_id ).flt_angle_y, game_objects( dwd_id ).flt_angle_z
Position Object dwd_id, game_objects( dwd_id ).flt_x, game_objects( dwd_id ).flt_y, game_objects( dwd_id ).flt_z
Endif
// Apply Textures and Shaders for "Normal" Objects
If game_objects( dwd_id ).str_shader = "Normal"
If str_normal_effect <> "Enabled"
Texture Object dwd_id, game_objects( dwd_id ).dwd_texture
Else
Texture Object dwd_id, 0, game_objects( dwd_id ).dwd_texture
Texture Object dwd_id, 1, game_objects( dwd_id ).dwd_texture + 1
Normal_Mapping( dwd_id )
Endif
Endif
// Apply Textures and Shaders for "ReflectNormal" Objects
If game_objects( dwd_id ).str_shader = "ReflectNormal"
If str_normal_reflect_effect <> "Enabled"
Texture Object dwd_id, game_objects( dwd_id ).dwd_texture
Else
Texture Object dwd_id, 0, game_objects( dwd_id ).dwd_texture
Texture Object dwd_id, 1, game_objects( dwd_id ).dwd_texture + 1
Reflective_Normal_Mapping( dwd_id )
Endif
Endif
// Apply Textures and Shaders for "Reflection" Objects
If game_objects( dwd_id ).str_shader = "Reflection"
If str_reflect_effect <> "Enabled"
Texture Object dwd_id, game_objects( dwd_id ).dwd_texture
Set Alpha Mapping On dwd_id, 50
Set Object Transparency dwd_id, 6
Else
Reflection( dwd_id )
Endif
Endif
// Apply Textures and Shaders for "Refraction" Objects
If game_objects( dwd_id ).str_shader = "Refraction"
If str_refract_effect <> "Enabled"
Texture Object dwd_id, game_objects( dwd_id ).dwd_texture
Set Alpha Mapping On dwd_id, 50
Set Object Transparency dwd_id, 6
Else
Refraction( dwd_id )
Endif
Endif
// Apply Textures and Shaders for "Heat Haze" Objects
If game_objects( dwd_id ).str_shader = "HeatHaze"
If str_heat_haze_effect <> "Enabled"
Set Alpha Mapping On dwd_id, 0
Set Object Transparency dwd_id, 6
Else
Heat_Haze( dwd_id )
Endif
Endif
INC game_objects( dwd_id ).flt_angle_y, 1
EndFunction
// -------------------------------------------------------------------------------------------------
// Get Content
// -------------------------------------------------------------------------------------------------
Function Get_Content( dwd_id as DWord, str_file as String, str_file_type as String, int_value as Integer )
// Image Files
If str_file_type = "DDS" or str_file_type = "PNG"
// If the Image does not Exist
If Image Exist( dwd_id ) = 0
// If the File Exists
If File Exist( "data/media/" + str_file + "." + str_file_type ) = 1
// Load the Image
Load Image "data/media/" + str_file + "." + str_file_type, dwd_id, int_value
Else
// Otherwise return an error code
wrd_error_code = 1
Endif
Endif
Endif
// Effect Files
If str_file_type = "FX"
// If the Effect does not Exist
If Effect Exist( dwd_id ) = 0
// If the File Exists
If File Exist( "data/effects/" + str_file + ".FX" ) = 1
// Load the Effect
Load Effect "data/effects/" + str_file + ".FX", dwd_id, int_value
Else
// Otherwise return an error code
wrd_error_code = 2
Endif
Endif
Endif
// Camera Effects
If str_file_type = "CFX"
// If the Effect does not Exist
If Camera Effect Exist( dwd_id ) = 0
// If the File Exists
If File Exist( "data/effects/" + str_file + ".FX" ) = 1
// Load the Effect
Load Camera Effect "data/effects/" + str_file + ".FX", dwd_id, 0
Else
// Otherwise return an error code
wrd_error_code = 3
Endif
Endif
Endif
EndFunction
// -------------------------------------------------------------------------------------------------
// Normal Mapping
// -------------------------------------------------------------------------------------------------
Function Normal_Mapping( dwd_id as Dword )
// Get Normal Map Shader
Get_Content( bte_normal_mapping_shader, "NormalMapping", "FX", 0 )
// Apply Effect to Object
Set Object Effect dwd_id, bte_normal_mapping_shader
Set Effect Technique bte_normal_mapping_shader, "DBLight3"
// Create new Element based on Object's Effect, for applying different lights to an object
Element = Get Object Effect( dwd_id, -1 )
// Create a Vector
null = Make Vector4( 1 )
// Apply Default Ambient Light Colour
Set Vector4 1, 1.0, 1.0, 1.0, 1.0
Set Effect Constant Vector bte_normal_mapping_shader, "AmbientColor", 1
// Cycle through all lights
For int_i = 0 to 2
// Set Vector to the Light Position and Range, then Apply this to the Effect
Set Vector4 1, light_emmiters( int_i ).flt_x, light_emmiters( int_i ).flt_y, light_emmiters( int_i ).flt_z, light_emmiters( int_i ).flt_range
Set Effect Constant Vector Element "LightPos", int_i, 1
// Set Vector to the Light Colour, then Apply this to the Effect
Set Vector4 1, light_emmiters( int_i ).bte_r / 255, light_emmiters( int_i ).bte_g / 255, light_emmiters( int_i ).bte_b / 255, 0.0
Set Effect Constant Vector Element "LightColor", int_i, 1
Next int_i
EndFunction
// -------------------------------------------------------------------------------------------------
// Reflective Normal Mapping
// -------------------------------------------------------------------------------------------------
Function Reflective_Normal_Mapping( dwd_id as Dword )
// Get Reflective Normal Map Shader
Get_Content( bte_normal_reflect_shader, "EnvironmentBump", "FX", 0 )
// Apply Effect to Object
Set Object Effect dwd_id, bte_normal_reflect_shader
If Dynamic Cube Map Exist( dwd_reflection_cube_map ) = 1
Apply Cube Map to Object dwd_id, 2, dwd_reflection_cube_map
Endif
EndFunction
Function Render_Environment_Normal_Map()
// Get Reflective Normal Map Camera
If bte_normal_reflect_camera = 0
bte_normal_reflect_camera = Get_Free_Camera()
Endif
If Camera Exist( bte_normal_reflect_camera ) = 0
Make Camera bte_normal_reflect_camera
Set Camera to Image bte_normal_reflect_camera, dwd_normal_reflect_texture, Screen Width() * flt_shader_detail, Screen Height() * flt_shader_detail
Else
Position Camera bte_normal_reflect_camera, Camera Position X(), Camera Position Y(), Camera Position Z()
Rotate Camera bte_normal_reflect_camera, Camera Angle X(), Camera Angle Y(), Camera Angle Z()
Move Camera bte_normal_reflect_camera, 1
Point Camera bte_normal_reflect_camera, Camera Position X(), Camera Position Y(), Camera Position Z()
If Dynamic Cube Map Exist( dwd_normal_reflect_texture ) = 0
Make Dynamic Cube Map dwd_normal_reflect_texture, 512 * flt_shader_detail
Else
Render Dynamic Cube Map dwd_normal_reflect_texture, -10, Camera Position X(), Camera Position Y(), Camera Position Z()
Endif
Endif
EndFunction
Function Refraction( dwd_id as DWord )
Get_Content( bte_refraction_effect, "Refraction", "FX", 1 )
Set Object Effect dwd_id, bte_refraction_effect
Texture Object dwd_id, 0, dwd_refraction_texture
Texture Object dwd_id, 1, game_objects( dwd_id ).dwd_texture
EndFunction
Function Refraction_Camera()
If bte_refraction_camera = 0
bte_refraction_camera = Get_Free_Camera()
Endif
If Camera Exist( bte_refraction_camera ) = 0
Make Camera bte_refraction_camera
Color Backdrop bte_refraction_camera, RGB( 255, 255, 255 )
Set Camera to Image bte_refraction_camera, dwd_refraction_texture, Screen Width() * flt_shader_detail, Screen Height() * flt_shader_detail
Set Camera Range bte_refraction_camera, 1, 15000
Else
Position Camera bte_refraction_camera, Camera Position X(), Camera Position Y(), Camera Position Z()
Rotate Camera bte_refraction_camera, Camera Angle X(), Camera Angle Y(), Camera Angle Z()
Endif
EndFunction
Function Reflection( dwd_id as DWord )
Get_Content( bte_reflection_effect, "Reflection", "FX", 1 )
Set Object Effect dwd_id, bte_reflection_effect
Texture Object dwd_id, 0, dwd_reflection_texture
If Dynamic Cube Map Exist( dwd_reflection_cube_map ) = 1
Apply Cube Map to Object dwd_id, 1, dwd_reflection_cube_map
Endif
EndFunction
Function Reflection_Camera()
If bte_reflection_camera = 0
bte_reflection_camera = Get_Free_Camera()
Endif
If Camera Exist( bte_reflection_camera ) = 0
Make Camera bte_reflection_camera
Color Backdrop bte_reflection_camera, RGB( 255, 255, 255 )
Set Camera to Image bte_reflection_camera, dwd_reflection_texture, Screen Width() * flt_shader_detail, Screen Height() * flt_shader_detail
Set Camera Range bte_reflection_camera, 1, 15000
Else
If Dynamic Cube Map Exist( dwd_reflection_cube_map ) = 0
Make Dynamic Cube Map dwd_reflection_cube_map, 64
Endif
Position Camera bte_reflection_camera, Camera Position X(), Camera Position Y(), Camera Position Z()
Rotate Camera bte_reflection_camera, Camera Angle X(), Camera Angle Y(), Camera Angle Z()
Endif
EndFunction
Function Get_Free_Camera()
bte_free_camera = 0
For int_i = 1 to 10
If Camera Exist( int_i ) = 0
If bte_free_camera = 0
bte_free_camera = int_i
Endif
Endif
Next int_i
EndFunction bte_free_camera
Function Heat_Haze( dwd_id as DWord )
Get_Content( bte_heat_haze_shader, "HeatHaze", "FX", 1 )
If Effect Exist( bte_heat_haze_shader ) = 1
Set Object Effect dwd_id, bte_heat_haze_shader
Texture Object dwd_id, 0, dwd_refraction_texture
Texture Object dwd_id, 1, game_objects( dwd_id ).dwd_texture
Endif
EndFunction
Function Bloom()
If bte_bloom_camera = 0
bte_bloom_camera = Get_Free_Camera()
Endif
If Camera Exist( bte_bloom_camera ) = 0
Make Camera bte_bloom_camera
Color Backdrop bte_bloom_camera, RGB( 128, 128, 128 )
Endif
Get_Content( bte_bloom_shader, "Bloom", "CFX", 0 )
Set Camera Effect bte_bloom_camera, bte_bloom_shader, dwd_bloom_texture
Get_Content( bte_quad_effect, "Quad", "FX", 0 )
If Object Exist( dwd_bloom_quad_object ) = 0
Make Object Plain dwd_bloom_quad_object, 2, 2, 1
Endif
Set Object Effect dwd_bloom_quad_object, bte_quad_effect
null = Make Vector4( 1 )
Set Vector4 1, Screen Width() * flt_shader_detail, Screen Height() * flt_shader_detail, 0, 0
Set Effect Constant Vector bte_quad_effect, "ViewSize", 1
Texture Object dwd_bloom_quad_object, 0, dwd_bloom_texture
Position Camera bte_bloom_camera, Camera Position X(), Camera Position Y(), Camera Position Z()
Rotate Camera bte_bloom_camera, Camera Angle X(), Camera Angle Y(), Camera Angle Z()
EndFunction
Function Direct_X_Update()
// Show All Objects (used for shaders)
For int_i = 1 to dwd_objects_on_screen
If Object Exist( int_i ) = 1
Show Object int_i
Endif
Next int_i
// Update Refraction
If str_refract_effect = "Enabled" or str_heat_haze_effect = "Enabled"
If Camera Exist( bte_refraction_camera ) = 1
Sync Mask 2^bte_refraction_camera
FastSync
Endif
Endif
// Update Reflection
If str_reflect_effect = "Enabled" or str_normal_reflect_effect = "Enabled"
If Dynamic Cube Map Exist( dwd_reflection_cube_map ) = 1
Render Dynamic Cube Map dwd_reflection_cube_map, -1, Camera Position X(), Camera Position Y(), Camera Position Z()
Endif
// Reflect Camera
If str_reflect_effect = "Enabled"
If Camera Exist( bte_reflection_camera ) = 1
Sync Mask 2^bte_reflection_camera
FastSync
Endif
Endif
// Normal Reflect Camera
If str_normal_reflect_effect = "Enabled"
If Camera Exist( bte_normal_reflect_camera ) = 1
Sync Mask 2^bte_normal_reflect_camera
FastSync
Endif
Endif
Endif
// Update Bloom
If str_bloom_effect = "Enabled"
Hide Object dwd_bloom_quad_object
Sync Camera bte_bloom_cameras
Show Object dwd_bloom_quad_object
Endif
// Hide Objects for Shaders
If str_reflect_effect = "Enabled" or str_refract_effect = "Enabled" or str_normal_reflect_effect = "Enabled" or str_heat_haze_effect = "Enabled" or str_bloom_effect = "Enabled"
For int_i = 1 to dwd_objects_on_screen
If Object Exist( int_i ) = 1
Hide Object int_i
Endif
Next int_i
Endif
// Sync Bloom Camera
If str_bloom_effect = "Enabled"
If Camera Exist( bte_bloom_camera ) = 1
Sync Mask 2^bte_bloom_camera
FastSync
Endif
Endif
Sync Mask 2^0
EndFunction
Self Destruction Computer Entertainment