About Store Forum Documentation Contact



Post Reply 
Custom Shader Learning Project
Author Message
3DRaddict Offline
Member

Post: #1
Custom Shader Learning Project
A source of inspiration for me has been Frank Luna's popular book "Introduction to 3D Game Programming with DirectX11" http://www.amazon.com/Introduction-3D-Ga...1936420228

For me to learn a new engine, it has always been easier to port over applications that I know and have built in previous engines. So with this in mind, and to enable me to use custom shaders within EE, I've decided to modify the examples in Frank's book for use with EE. I'm going to take it slowly, chapter by chapter, until I've understood and mastered the more advanced shaders. This exercise is important to me for the continuation of my "Surfing Simulation Project".

I've thought that at the same time as I proceed with this exercise I could enlighten the newer members of this community, who maybe experiencing the same learning difficulties as myself, with my efforts. I will provide the whole project (source and all) as I proceed through the book. Bare in mind that my code is probably a little outdated and 'simple' compared to all the latest OOP techniques, but it gets the job done and I try to keep it easy to follow. Note that it is not a tutorial on the use of EE (there are many here who are so much more accomplished and experienced in EE than myself to produce tutorials), but by following my code I feel sure you will find it easier to start getting to grips with the use of custom shaders in Esenthel.

So if you do not have Frank's book already, do purchase it and follow along.

The first example is a "Shapes Demo" which produces a simple scene using procedurally drawn shapes. This scene will be enhanced upon as I proceed through each chapter of the book. At the moment it is just produced in wireframe or flat color.
I use my own form of camera control which uses only the keyboard and not the mouse. The movement instructions are available on holding down [F1].

[Image: ShapesDemo.jpg]

ShapeDemo_executable

The next example is called "Light Demo" and again produces a simple scene depicting some procedurally drawn 'hilly land' with a flat plane of 'disturbed water' which is lit by three light types (directional, point, and spotlight). This is the first use of a custom shader which contains the HLSL code for these lights, and will be used as a lighting template for use in further shaders. I've enhanced the example by providing an EE GUI for controlling the lighting shader parameters.

[Image: LightingDemo.jpg]

LightDemo_executable

I've really had fun playing with this, and have already learnt a lot about the different effects that colored lighting can have on a scene. I guarantee that you will also find it fun and beneficial! You'll notice that I've marked the point light and spotlight
sliders as "INACTIVE". This is because the light translation and orientation is controlled via code. The point light circles above the scene, and the spotlight is attached to the camera and points in the camera direction.

Just a few notes on the project requirements:
1) Only Windows 32bit
2) Only DirectX 10+
3) For the shaders to compile you must duplicate my particular setup (or change to suit your own)
a) A folder called "Shader HLSL" must be added to your Esenthel "Editor/Projects" folder. This folder will contain all the custom shaders.
b) at the top of each shader code there is a #include "Main.h". This "Main.h" contains a reference to the folder containing the Esenthel-specific shader headers.
In my case:
Quote:/******************************************************************************/
// here you need to set the path to your "EsenthelEngineShader" folder
#include "C:/EsenthelEngine_DX10plus/Editor/Projects/EsenthelEngineShader/Main.h"
/******************************************************************************/
[/code]

c) The "EsenthelEngineShader" folder contains the Esenthel shader headers included in the "Main.h" file, which looks as follows:
#include "Defines.h"
#include "Engine Defines.h"
#include "2D Constants.h"
#include "Constants.h"
#include "Functions.h"
#include "Render Functions.h"
#include "Structs.h"
#include "2D Functions.h"
#include "Light.h"
#include "Engine Constants.h"
#include "Engine Functions.h"
#include "Ambient Occlusion.h"
#include "Water.h"
#include "Sky.h"
d) At the top of my Esenthel project Application "Main" code file there is the include:
#include"C:/EsenthelEngine_DX10plus/Editor/Projects/material_user_shader.enum.h"
This "material_user_shader.enum.h" file contains the following:
/******************************************************************************/
enum MATERIAL_USER_SHADER
{
MUS_DEFAULT,
MUS_CUSTOM ,
MUS_CUSTOM2,
MUS_CUSTOM3,
};
/******************************************************************************/
which reference the custom shader names used in the code.
e) And, finally, the code used to compile the shaders places the compiled shaders in a specific set of folders which must be manually added to your specific project folder(the one with the encrypted name).
I've supplied a folder called "Copy contents of this folder to your Project folder"...just copy and paste its contents for each new project you start.

Hopefully, that's it... good luck!

CustomShaderLearningProject_latest project files 09-12-2014
(This post was last modified: 12-09-2014 06:37 PM by 3DRaddict.)
11-21-2014 10:20 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Pixel Perfect Offline
Member

Post: #2
RE: Custom Shader Learning Project
First and foremost ... many thanks for presenting this. This is indeed a great idea and I'm grateful to you for putting the time in to share this with others.

I will follow this with interest as I need to get experience in shader design and techniques.
11-21-2014 10:54 AM
Find all posts by this user Quote this message in a reply
Pherael Offline
Member

Post: #3
RE: Custom Shader Learning Project
I don't work with shaders at this moment, but I believe I find this very useful in future. Many thanks for sharing this!
11-21-2014 10:58 AM
Find all posts by this user Quote this message in a reply
georgatos7 Offline
Member

Post: #4
RE: Custom Shader Learning Project
Awesome tutorials thanks, i'll make sure to check them out. I have been searching for info and tuts here and there on the internet but i'm sure starting with a good book is the best way to go.

Edit : Wow you are amazing, awesome and clean code this is going to be very helpful thanks!!! I'm also looking forward to your project's progress.
(This post was last modified: 11-26-2014 02:24 AM by georgatos7.)
11-21-2014 11:25 AM
Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #5
RE: Custom Shader Learning Project
Great work on this. smile

one problem I had.
in lightning_land at line 261 the compilation fails, atleast for me due to truncation

outNormalW=mul(vtx.nrm(),transpose(gWorldInv));
is to my knowledge the same as
outNormalW=Transform(vtx.nrm(),gWorldInv);
//found inside EsenthelEngineShader/Functions.h


Also, for the ones wanting to use EE Containers and functions to get rid of windows header dependencies.
std::vector<> replaced with Memc<>
std::vector::assign replaced with Memc::swapOrder
std::vector::push_back replaced with Memc::New()=value/object;
std::vector::size replaced with Memc::elms()
std::vector::resize replaced with Memc::setNum();
cosf,sinf,atanf,acosf replaced with EE::Cos Sin Atan Acos
assert replaced with DYNAMIC_ASSERT
right click the application in the editor and remove all the includes. : )
(This post was last modified: 11-21-2014 01:04 PM by Zervox.)
11-21-2014 01:03 PM
Find all posts by this user Quote this message in a reply
Tottel Offline
Member

Post: #6
RE: Custom Shader Learning Project
Thanks, this is a great learning project!

I also have the book and I can heartily recommend it to anyone with an interest in graphics programming.

Also, thank you Zervox for making it more EE native- and friendly. pfft
(This post was last modified: 11-21-2014 01:52 PM by Tottel.)
11-21-2014 01:52 PM
Find all posts by this user Quote this message in a reply
3DRaddict Offline
Member

Post: #7
RE: Custom Shader Learning Project
South Africa has just had a 3 hour load shedding power cut in order to build up its electric power reserves following the recent collapse of a coal silo at our major power provider Eskom. The second such occurrance in a week. So the power is finally back on, and I've had a chance to read all your comments. I'm very happy this exercise meets with your approval.
Zervox, please feel free to indicate any changes to 'modernise' my code...I'll certainly learn from it!
11-21-2014 04:14 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #8
RE: Custom Shader Learning Project
Learning shading languages has always been on my "To Do" list, but it keeps getting pushed down by minor things like "Life". I've been on a business trip the last month, but when I get home, I'll check this out; now that it's in an EE project, I have little excuse to not learn on my next day off. I'm pretty excited about this, Raddict, especially after having seen the things you've been doing.

Thank you.
11-21-2014 11:34 PM
Find all posts by this user Quote this message in a reply
3DRaddict Offline
Member

Post: #9
RE: Custom Shader Learning Project
@Zervox:
I tried out your changes in the Shapes app... got a bit unstuck at this point:
std::vector::assign replaced with Memc::swapOrder

The swapOrder has 'int' parameters so I replaced:
meshData.Indices.assign(&i[0], &i[36]);
with:
meshData.Indices.swapOrder(meshData.Indices.index(&i[0]),meshData.Indices.index(&i[36]));

Compiled OK, but on running, the box was no longer there...
Any idea?

EDIT: Just did a quick VS debug and I see that in 'Main'
myBoxIndexCount = box.Indices.elms(); is returning a 0
whereas myBoxIndexCount = box.Indices.size(); returns the correct value of 36
Same with myBoxVertexCount
However, for the other shapes .elms() is returning the correct count

EDIT #2:
Ok, I seem to have fixed the above discrepancy:
Replaced the meshData.Indices.swapOrder() with:
PHP Code:
for( int j=0j<36j++)
      {
         
meshData.Indices.add(i[j]);
      } 
And likewise for meshData.Vertices.swapOrder() :
PHP Code:
for( int i=0i<24i++)
      {
         
meshData.Vertices.add(v[i]);
      } 

So I now have an EE enhanced version of the "Shapes" project, as well as my original
Windows std::vector<> version grin

I haven't yet tried any changes in the "Lighting" project, but will do so.



I've also not been able to give the same water ripple effect as in the book:
[Image: ripple.jpg]

My "ripples" are showing elongated and not symmetrical and round like above.
Perhaps someone could investigate this? smile
(This post was last modified: 11-22-2014 03:47 PM by 3DRaddict.)
11-22-2014 08:23 AM
Visit this user's website Find all posts by this user Quote this message in a reply
3DRaddict Offline
Member

Post: #10
RE: Custom Shader Learning Project
I've now eliminated all Windows header requirements from both 'Shapes' and 'Lighting' (with thanks to Zervox smile ) So the project is now entirely EE compatible.
The project file download has been updated accordingly.
11-23-2014 07:05 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #11
RE: Custom Shader Learning Project
my first bet would be the update and disturb math,or the indexes, to me, it seems to be accessing the wrong indexes of the waves triangles.
(This post was last modified: 11-24-2014 08:10 AM by Zervox.)
11-24-2014 08:10 AM
Find all posts by this user Quote this message in a reply
3DRaddict Offline
Member

Post: #12
RE: Custom Shader Learning Project
[Image: LitSkullDemo.jpg]

LitSkullDemo_executable

This demo/code illustrates the three-point lighting system that is commonly used in film and photography to get better lighting than just one light source can provide; it consists of a primary light source called thekey light, a secondary fill light usually aiming in the side direction from the key light, and a back light.The three directional lights are activated by means of the GUI buttons, which change a 'numLights' variable value from 0,1,2,3 accordingly. This value is communicated to the custom lighting shaders, which then take the requisite action. The skull model, being highly detailed, shows off this type of lighting system to good effect.
The model itself is loaded from a giant text file which contains only vertex position, vertex normal and all the triangle indices. To load it I used the Esenthel 'FileText' object, which I found incredibly easy to use.
This led me to another new Esenthel operation (for me): The loading of the skull data file occurs at initialization and takes up to 10 seconds, leaving the screen black and empty in the process. On advice from the Forum, I added a secondary thread to carry out this loading process, which then enabled me to display a loading message and progress bar. This, too, turned out to be a relatively simple operation, thanks to the Esenthel code functions.

So, newcomers, my code is there for you to examine and take apart... I am not a code purist, but I'm sure parts of it will assist you in understanding how, practically, to tackle some of the Esenthel functions, which can be a little overwhelming to Esenthel newbies like myself.

You'll note that the 'skull.txt' exists in a 'Models' folder off your project 'Game' folder.
The 'CustomShaderLearningProject' project file can be downloaded from my initial post on this thread. This download will always be the latest version.
11-25-2014 01:13 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Tottel Offline
Member

Post: #13
RE: Custom Shader Learning Project
Nice work!

You can make it load a lot faster by using a binary file. I haven't used this in EE yet, but it seems like you can just use the File class for this.
11-25-2014 02:04 PM
Find all posts by this user Quote this message in a reply
3DRaddict Offline
Member

Post: #14
RE: Custom Shader Learning Project
@ Tottel
I understand that, but I'm just going through the book chapter by chapter, and don't want to jump ahead. A text file was used in the book's demo, so that's why I used it. (besides, I learnt how to use a thread in the process!) Binary files are loaded at a later stage. Thanks anyway for the suggestion.smile
(This post was last modified: 11-25-2014 03:14 PM by 3DRaddict.)
11-25-2014 03:10 PM
Visit this user's website Find all posts by this user Quote this message in a reply
georgatos7 Offline
Member

Post: #15
RE: Custom Shader Learning Project
Awesome update, thanks.
(This post was last modified: 11-26-2014 02:39 AM by georgatos7.)
11-26-2014 02:23 AM
Find all posts by this user Quote this message in a reply
Post Reply