Hi
I would like to create a simple normal map & spec map shader, but I don't know how to do (as simple as possible).
can anyone help me ?
Here is my code :
// Project: 3D_normalspecmap
// Created: 2016-06-01
FoldStart
// set window properties
SetWindowTitle( "3D_normalspecmap" )
g_width = 1024
g_height = 768
SetWindowSize(g_width,g_height,0)
SetWindowAllowResize(1)
// SetSyncRate( 0,0 )
SetAntiAliasMode(1)
SetResolutionMode(1)
// set display properties
SetVirtualResolution(g_width,g_height)
SetOrientationAllowed( 1, 1, 1, 1 )
SetScissor(0,0,0,0)
SetCameraRange( 1, 0.1, 2000 )
SetGlobal3DDepth(10000)
Foldend
FoldStart
// enable the sky box
//SetSkyBoxVisible( 1 )
SetGenerateMipmaps(1)
// Load image
LoadShader(1, "shader_ns.vs", "shader_ns.ps")
LoadImage(1,"a_dif.jpg")
LoadImage(2,"a_norm.jpg")
LoadImage(3,"a_spec.jpg")
CreateObjectSphere(1,15,25,25)
SetObjectPosition(1,0,0,0)
SetObjectShader(1,1)
SetShaderConstantByName(1,"specint",1,0,0,0)
SetObjectImage(1,1,0)
SetObjectImage(1,2,1)
SetObjectImage(1,3,2)
FoldEnd
// light and light object
CreatePointLight(1,15,15,10,100,0,50,255)
CreateObjectBox(2,1,1,1)
SetObjectColorEmissive(2,0,50,255)
SetObjectColor(2,0,50,255,255)
SetObjectPosition(2,15,15,10)
SetCameraPosition(1, 0, 20, 20)
SetCameraLookAt( 1, 0,0,0,0 )
SetAmbientColor(28,28,28)
SetSunColor(80,60,20)
cameraMode = 1
dir as float =0.2
xx as float =30
b=10
do
Print( ScreenFPS() )
xx =xx+dir
if xx>20
dir=-0.2
elseif xx <-10
dir =0.2
endif
SetPointLightPosition(1,b,8,xx)
SetObjectPosition(2,b,8,xx)
SetObjectRotation(1,GetObjectAngleX(1),GetObjectAngleY(1)+1,GetObjectAngleZ(1))
// move the camera time dependant
speed# = 2.0
if ( GetRawKeyState( 16 ) ) then speed# = 0.1
if ( cameraMode = 1 ) then speed# = 0.125
speed# = speed# * GetFrameTime()*100.0
if ( GetRawKeyState( 38) ) then MoveCameraLocalZ( 1, speed# )
if ( GetRawKeyState( 40 ) ) then MoveCameraLocalZ( 1, -speed# )
if ( GetRawKeyState( 37 ) ) then MoveCameraLocalX( 1, -speed# )
if ( GetRawKeyState( 39 ) ) then MoveCameraLocalX( 1, speed# )
//if ( GetRawKeyState( 81 ) ) then MoveCameraLocalY( 1, -speed# )
//if ( GetRawKeyState( 69 ) ) then MoveCameraLocalY( 1, speed# )
if ( getvirtualbuttonstate(1) = 1 ) then MoveCameraLocalZ( 1, -speed# )
if ( getvirtualbuttonstate(2) = 1 ) then MoveCameraLocalZ( 1, speed# )
// rotate the camera
if ( GetPointerPressed() )
startx# = GetPointerX()
starty# = GetPointerY()
angx# = GetCameraAngleX(1)
angy# = GetCameraAngleY(1)
pressed = 1
endif
if ( GetPointerState() = 1 )
fDiffX# = (GetPointerX() - startx#)/4.0
fDiffY# = (GetPointerY() - starty#)/4.0
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endif
Sync()
loop
The shader.ps :
uniform vec4 agk_MeshDiffuse;
uniform sampler2D texture0; // diffuse
uniform sampler2D texture1; // normal
uniform sampler2D texture2; // spec
varying vec2 uv0Varying;
varying vec3 normalVarying;
varying vec3 posVarying;
varying highp vec4 worldPosition;
uniform vec3 ambient;
uniform vec3 specint;
varying mediump vec3 lightVarying;
varying mediump vec3 colorVarying;
// these must appear exactly as they are to work, spaces and all.
// they will be filled in by AGK
mediump vec3 GetPSLighting( mediump vec3 normal, highp vec3 pos );
mediump vec3 ApplyFog( mediump vec3 color, highp vec3 pointPos );
uniform vec4 clippingPlane;
void main()
{
// specular light
// mediump vec3 spec = texture2D(texture2, uv0Varying).rgb;
vec4 SpecColor = texture2D(texture2, uv0Varying);
vec3 spec = vec3(SpecColor.r * 2.0 - 1.0, SpecColor.b, SpecColor.g * 2.0 -1.0);
spec = normalize(spec);
// lights
mediump vec3 norm = normalize(normalVarying);
mediump vec3 light = lightVarying + GetPSLighting( norm, posVarying )*(1+spec*specint);
mediump vec3 color = (texture2D(texture0, uv0Varying).rgb) * light ;
// apply normalmap
vec4 normalColor = texture2D(texture2, uv0Varying);
vec3 normal = vec3(normalColor.r * 2.0 - 1.0, normalColor.b, normalColor.g * 2.0 -1.0);
normal = normalize(normal);
// Apply Fog
color = ApplyFog(color, posVarying );
gl_FragColor = vec4(color,1.0);
}
The shader.vs :
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
uniform vec4 uvBounds0;
varying vec3 posVarying;
varying vec3 normalVarying;
varying mediump vec2 uv0Varying;
varying mediump vec3 lightVarying;
varying mediump vec3 colorVarying;
varying highp vec4 worldPosition;
// this must appear exactly as it is for it to work, spaces and all.
// it will be filled in by AGK
mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );
uniform mat4 agk_WorldViewProj;
uniform mat3 agk_WorldNormal;
uniform mat4 agk_World;
uniform mat4 agk_ViewProj;
void main()
{
// highp vec4 pos = agk_World * vec4(position,1.0);
vec4 pos = agk_World * vec4(position,1.0);
gl_Position = agk_ViewProj * pos;
posVarying = pos.xyz;
worldPosition = pos;
vec3 norm = agk_WorldNormal * normal;
normalVarying = norm;
uv0Varying = uv * uvBounds0.xy + uvBounds0.zw;
lightVarying = GetVSLighting( norm, posVarying );
}
If you need the images, tell me
Thank you very much for your help.
AGK2 tier1 - http://www.dracaena-studio.com