Cubemapping + SkyBox
Needs some tinkering for edge alignment, as it's being done in a "quick and simple" approach rather than accurate but a good base.
Cube Mapping (something absent from AGK) is useful for more than just fake reflections, but other things, such-as light and shadow projection depending on how you want to use it.
This is more of a template to add to OTHER Shaders (and I used Texture Stage 7 specifically for that reason).
Cubemap Pixel Shader
#version 110
uniform sampler2D texture0;
uniform sampler2D texture7;
varying vec3 posVarying;
varying vec3 normalVarying;
varying vec2 uvVarying;
uniform vec3 agk_CamPos;
vec2 sampleCube( const vec3 v, out float faceIndex )
{
vec3 vABS = abs( v );
float ma;
vec2 uv;
if( vABS.z >= vABS.x && vABS.z >= vABS.y )
{
faceIndex = v.z < 0.0 ? 5.0 : 4.0;
ma = 0.5 / vABS.z;
uv = vec2( v.z < 0.0 ? -v.x : v.x, -v.y );
}
else
{
if( vABS.y >= vABS.x )
{
faceIndex = v.y < 0.0 ? 3.0 : 2.0;
ma = 0.5 / vABS.y;
uv = vec2( v.x, v.y < 0.0 ? -v.z : v.z );
}
else
{
faceIndex = v.x < 0.0 ? 1.0 : 0.0;
ma = 0.5 / vABS.x;
uv = vec2( v.x < 0.0 ? v.z : -v.z, -v.y );
}
}
return uv * ma + 0.5;
}
void main()
{
float faceIndex;
vec4 pixelColor;
vec4 textureColor = texture2D( texture0, uvVarying );
vec2 uv = sampleCube( posVarying, faceIndex );
vec3 I = normalize( posVarying - agk_CamPos );
vec3 R = reflect( I, normalize( normalVarying ) );
switch( int( faceIndex ) )
{
case 5:
pixelColor = texture2D( texture7, vec2( 0.0, 0.0 ) + ( uv * vec2( 0.33, 0.50 ) ) );
break;
case 4:
pixelColor = texture2D( texture7, vec2( 0.33, 0.0 ) + ( uv * vec2( 0.33, 0.50 ) ) );
break;
case 3:
pixelColor = texture2D( texture7, vec2( 0.66, 0.0 ) + ( uv * vec2( 0.33, 0.50 ) ) );
break;
case 2:
pixelColor = texture2D( texture7, vec2( 0.0, 0.5 ) + ( uv * vec2( 0.33, 0.50 ) ) );
break;
case 1:
pixelColor = texture2D( texture7, vec2( 0.33, 0.5 ) + ( uv * vec2( 0.33, 0.50 ) ) );
break;
case 0:
pixelColor = texture2D( texture7, vec2( 0.66, 0.5 ) + ( uv * vec2( 0.33, 0.50 ) ) );
break;
}
gl_FragColor = pixelColor * textureColor;
}
Cubemap Vertex Shader
#version 110
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
uniform vec4 uvBounds0;
uniform mat4 agk_Proj;
uniform mat4 agk_View;
uniform mat4 agk_World;
varying vec3 posVarying;
varying vec3 normalVarying;
varying vec2 uvVarying;
void main()
{
uvVarying = uv * uvBounds0.xy + uvBounds0.zw;
normalVarying = mat3( transpose( inverse( agk_World ) ) ) * normal;
posVarying = vec3( agk_World * vec4( position, 1.0 ) );
gl_Position = ( ( agk_Proj * agk_View ) * agk_World ) * vec4( position, 1.0 );
}
Program
// Project: Cube Mapping
// Created: 24-07-23
#Insert "..//Common//Common.agc"
SetErrorMode(2) // Debug Mode
Type TimeStructure
Previous As Float
Current As Float
Duration As Float
Elapsed As Float
Frequency As Float
EndType
Function TimeStructure( Frequency As Float )
Local Out As TimeStructure
Out.Previous = Timer()
Out.Current = Out.Previous
Out.Frequency = Frequency
EndFunction Out
Function Time( In Ref As TimeStructure )
In.Previous = In.Current
In.Current = Timer() * 1000.0
In.Elapsed = In.Current - In.Previous
Inc In.Duration, In.Elapsed
If In.Duration >= In.Frequency
Dec In.Duration, In.Frequency
ExitFunction True
EndIf
EndFunction False
Type Vector
X As Float
Y As Float
Z As Float
W As Float
EndType
Function Vector( X As Float, Y As Float, Z As Float, W As Float )
Local Out As Vector
Out.X = X
Out.Y = Y
Out.Z = Z
Out.W = W
EndFunction Out
Initialise()
Repeat : Until Main() = False
Release()
End
Function Initialise()
SetWindowTitle( "Evolved Shader // Cube Mapping Sample" )
SetWindowSize( 1280, 720, False )
SetVirtualResolution( 1280, 720 )
SetScissor( Disable, Disable, Disable, Disable )
SetSyncRate( Disable, Enable ) // Sync Rate Unlimited, CPU Performance Mode
UseNewDefaultFonts( Enable )
Global CubeMap As Integer[]
CubeMap.Insert( LoadImage( ".//Textures//Cube0.png" ) )
Global CubeMapShader As Integer
CubeMapShader = LoadShader( ".//Shaders//cubemap.vertex.glsl", ".//Shaders//cubemap.pixel.glsl" )
Global Cube As Integer
Cube = CreateObjectBox( 10.0, 10.0, 10.0 )
Global CubeTexture As Integer : CubeTexture = LoadImage( ".//Textures//Floor.png" )
SetObjectImage( Cube, CubeTexture, 0 )
SetObjectImage( Cube, CubeMap[0], 7 )
SetObjectShader( Cube, CubeMapShader )
Global Rotation As Vector
Global SkyboxShader As Integer
SkyBoxShader = LoadShader( ".//Shaders/skybox.vertex.glsl", ".//Shaders/skybox.pixel.glsl" )
Global Skybox As Integer
Skybox = CreateObjectBox( 50.0, 50.0, 50.0 )
SetObjectImage( SkyBox, CubeMap[0], 7 )
SetObjectShader( SkyBox, SkyBoxShader )
SetObjectCullMode( SkyBox, 2 )
Global Update As TimeStructure
Update = TimeStructure( 16.67 )
EndFunction
Function Main()
If GetRawKeyPressed( KEY_ESCAPE )
ExitFunction False
EndIf
If Time( Update )
Inc Rotation.Y, 0.4
Inc Rotation.Z, 0.2
EndIf
SetObjectRotation( Cube, Rotation.X, Rotation.Y, Rotation.Z )
Print( ScreenFPS() )
Sync()
EndFunction True
Function Release()
DeleteImage( CubeMap[0] ) : CubeMap.Length = -1
DeleteImage( CubeTexture )
DeleteObject( SkyBox )
DeleteObject( Cube )
DeleteShader( CubeMapShader )
DeleteShader( SkyboxShader )
EndFunction