• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Atmospheric Scattering Issue (GLSL)

1 post in this topic


recently I have been trying to implement Sean O'Neil's atmospheric scattering shaders into a java project I have been working on (using jmonkey engine). I have the atmosphere from space shader working perfectly but for some reason I cannot get the ground from space shader working correctly. To see the planet at all I have to up the number of samples taken to ~400 and this is terribly inefficient and causes the color of the planet to be far too...orange. If anyone has any insight on this I would be extremely appreciative. I will include (what I think is relevant) relevant code/shaders. Thanks so much for the time!

The result I am getting using a diffuse color of (.85,.85,.85):

(what the color actually looks like sans shading: color.png )






Starting here is how I render the spheres: 

sun = new DirectionalLight();
		 Vector3f LIGHT_DIRECTION = new Vector3f(0.5f, 0.5f, -0.5f).normalize();
		Mesh plan = makePlanet();
		pla = new Geometry("Mesh" , plan);
		mat_P = new Material(assetManager, "GroundFromSpace.j3md");
		Mesh atmosphere = makeAtmosphere();
		atm = new Geometry("Mesh", atmosphere);
		mat_A = new Material(assetManager, "SkyFromSpace.j3md");

public Mesh makePlanet(){
	planet = new Planet(4, new Vector3f(0,0,0));
	Mesh pl = new Sphere(128,128,planet.getRadius());
	return pl;

public Mesh makeAtmosphere(){
	Mesh atmosphere = new Sphere(128, 128, planet.getOuterRadius());
	return atmosphere;

the "Planet Class" simply contains all of the parameters (which I have checked many times to match them to O'Neil's). The setUpMaterial methods simply fetch these variables and pass them along to the shaders. 


Here is the vertex shader (GroundFromSpace):

uniform mat4 g_WorldViewProjectionMatrix;
uniform mat4 g_WorldMatrix;
uniform vec3 m_v3CameraPos;		// The camera's current position
uniform vec3 m_v3LightPos;		// The direction vector to the light source
uniform vec3 m_v3InvWavelength;	// 1 / pow(wavelength, 4) for the red, green, and blue channels
uniform float m_fCameraHeight;	// The camera's current height
uniform float m_fCameraHeight2;	// fCameraHeight^2
uniform float m_fOuterRadius;		// The outer (atmosphere) radius
uniform float m_fOuterRadius2;	// fOuterRadius^2
uniform float m_fInnerRadius;		// The inner (planetary) radius
uniform float m_fInnerRadius2;	// fInnerRadius^2
uniform float m_fKrESun;			// Kr * ESun
uniform float m_fKmESun;			// Km * ESun
uniform float m_fKr4PI;			// Kr * 4 * PI
uniform float m_fKm4PI;			// Km * 4 * PI
uniform float m_fScale;			// 1 / (fOuterRadius - fInnerRadius)
uniform float m_fScaleDepth;		// The scale depth (i.e. the altitude at which the atmosphere's average density is found)
uniform float m_fScaleOverScaleDepth;	// fScale / fScaleDepth
attribute vec4 inPosition;
uniform int m_nSamples;
uniform float m_fSamples;
varying vec4 c1;
varying vec4 c0;

float scale(float fCos)
	float x = 1.0 - fCos;
	return m_fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));

void main(void)
	// Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
	vec3 v3Pos = vec3(g_WorldMatrix * inPosition);
	vec3 v3Ray = v3Pos - m_v3CameraPos;
	float fFar = length(v3Ray);
	v3Ray /= fFar;

	// Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
	float B = 2.0 * dot(m_v3CameraPos, v3Ray);
	float C = m_fCameraHeight2 - m_fOuterRadius2;
	float fDet = max(0.0, B*B - 4.0 * C);
	float fNear = 0.5 * (-B - sqrt(fDet));

	// Calculate the ray's starting position, then calculate its scattering offset
	vec3 v3Start = m_v3CameraPos + v3Ray * fNear;
	fFar -= fNear;
	float fDepth = exp((m_fInnerRadius - m_fOuterRadius) / m_fScaleDepth);
	float fCameraAngle = dot(-v3Ray, v3Pos) / length(v3Pos);
	float fLightAngle = dot(m_v3LightPos, v3Pos) / length(v3Pos);
	float fCameraScale = scale(fCameraAngle);
	float fLightScale = scale(fLightAngle);
	float fCameraOffset = fDepth*fCameraScale;
	float fTemp = (fLightScale + fCameraScale);

	// Initialize the scattering loop variables
	float fSampleLength = fFar / m_fSamples;
	float fScaledLength = fSampleLength * m_fScale;
	vec3 v3SampleRay = v3Ray * fSampleLength;
	vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5;

	// Now loop through the sample rays
	vec3 v3FrontColor = vec3(0.0, 0.0, 0.0);
	vec3 v3Attenuate;
	for(int i=0; i<m_nSamples; i++)
		float fHeight = length(v3SamplePoint);
		float fDepth = exp(m_fScaleOverScaleDepth * (m_fInnerRadius - fHeight));
		float fScatter = fDepth*fTemp - fCameraOffset;
		v3Attenuate = exp(-fScatter * (m_v3InvWavelength * m_fKr4PI + m_fKm4PI));
		v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
		v3SamplePoint += v3SampleRay;

	gl_FrontColor = vec4(v3FrontColor * (m_v3InvWavelength * m_fKrESun + m_fKmESun),1.0);
	gl_FrontSecondaryColor = vec4(v3Attenuate,1.0);
	gl_Position = g_WorldViewProjectionMatrix * inPosition;


and here is the pixel shader (note that inColor is jmonkey's version of gl_Color and inPosition^^ is their version of gl_Vertex):

attribute vec4 inColor;
varying vec4 c0, c1;
void main (void)
	gl_FragColor = inColor + 0.85 * gl_SecondaryColor;

Again, thanks a lot for the time, any help whatsoever is appreciated. 

Edited by multifractal

Share this post

Link to post
Share on other sites

UPDATE: I solved the issue. It was a simple careless mistake: I wasn't passing the square of outer radius into the shader. That fixed it all functioned as it should. Thanks for the consideration, 


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0