Viable engine?

Started by MrAdam, May 02, 2012, 05:04:46 PM

Previous topic - Next topic

MrAdam

Thaks a lot for the help. I got everything working great so far with a detailed model and transparency.
But I've run in to some problems with using shaders. On our old engine (jmonkeyengine) we used shaders for the model instead of textures. I can see that your engine supports shaders, but I have not been able to find a good example for a simple lighting shader. The only thing I really need is just to make the car "shine" more, like real car paint.

You can see below how it looks so far:


EgonOlsen

If you have the former shader available, you should be able to port it to be used by jPCT-AE. After all, it's all standard GLSL code. You will most likely have to make it use jPCT-AE's naming conventions for uniforms and attributes that can be found here: http://www.jpct.net/jpct-ae/doc/com/threed/jpct/GLSLShader.html

MrAdam

Quote from: EgonOlsen on May 18, 2012, 11:00:22 AM
If you have the former shader available, you should be able to port it to be used by jPCT-AE. After all, it's all standard GLSL code. You will most likely have to make it use jPCT-AE's naming conventions for uniforms and attributes that can be found here: http://www.jpct.net/jpct-ae/doc/com/threed/jpct/GLSLShader.html
Thanks. I used a default shader in JME, which I've tried porting already using jPCT naming conventions for shaders, but there's a lot of stuff that simply won't compile in it.

I just need a simple reflection shader for this project though.

EgonOlsen

Why shouldn't it compile? It's all the same GLSL based stuff...maybe the types don't match or something like that. You might have to convert them. Can you post the shader code that won't compile?

MrAdam

Quote from: EgonOlsen on May 18, 2012, 11:08:25 AM
Why shouldn't it compile? It's all the same GLSL based stuff...maybe the types don't match or something like that. You might have to convert them. Can you post the shader code that won't compile?
Well, right now Im trying to port this shader: http://www.davidcornette.com/glsl/glsl.html
It's simple, and should look fine. Although, I can't seem to fint the jPCT equivalent of gl_NormalMatrix

EgonOlsen

#20
The equivalent of gl_NormalMatrix is modelViewMatrix...well, not really but as long as you are only using uniform scaling (which you do, because jPCT doesn't allow for aynthing else), you can simply replace something like


vec3 n = normalize(gl_NormalMatrix * gl_Normal);


by


vec3 n = normalize(modelViewMatrix * vec4(normal,0.0)).xyz;

MrAdam

Quote from: EgonOlsen on May 18, 2012, 11:23:24 AM
The equivalent of gl_NormalMatrix is modelViewMatrix...well, not really but as long as you are only using uniform scaling (which you do, because jPCT doesn't allow for aynthing else), you can simply replace something like


vec3 n = normalize(gl_NormalMatrix * gl_Normal);


by


vec3 n = normalize(modelViewMatrix * vec4(normal,0.0)).xyz;


Thanks. Im 100% new to shaders. Theres a few more functions and variables It can't seem to find:

05-18 11:32:05.278: I/jPCT-AE(27583): ERROR: 0:9: 'ftransform' : no matching overloaded function found

EgonOlsen

OpenGL ES misses a lot of stuff that the desktop version has. Maybe it helps to compare the glsl code of the parallax mapping shaders that are included as an example in both versions (HelloShader iirc) so see how the desktop stuff translates to mobile.

MrAdam

Quote from: EgonOlsen on May 18, 2012, 12:18:17 PM
OpenGL ES misses a lot of stuff that the desktop version has. Maybe it helps to compare the glsl code of the parallax mapping shaders that are included as an example in both versions (HelloShader iirc) so see how the desktop stuff translates to mobile.
Okay, can't get the shader to work. I've searched the web for a simple reflection shader, but no luck.
Do you have any sources for a simple reflection shader?

EgonOlsen

Here's a rough port of the shader in your link:

vertex:

uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;

uniform vec3 lightPositions[8];
uniform vec3 diffuseColors[8];

attribute vec4 position;
attribute vec3 normal;
attribute vec2 texture0;

varying vec3 vnormal;
varying vec4 pos;
varying vec4 diffuseColor;
varying vec2 texCoord;

void main() {
  vnormal = normalize(modelViewMatrix * vec4(normal, 0.0)).xyz;
  pos = modelViewMatrix * position;
  diffuseColor = vec4(diffuseColors[0], 0.0);
  texCoord = texture0.xy;
  gl_Position = modelViewProjectionMatrix * position;
}


fragment:

precision highp float;

uniform sampler2D textureUnit0;
uniform vec3 lightPositions[8];

varying vec3 vnormal;
varying vec4 pos;
varying vec4 diffuseColor;
varying vec2 texCoord;

void main() {
  vec4 color = texture2D(textureUnit0, texCoord);
 
  // Adjust this...
  vec4 matspec = vec4(0.8, 0.8, 0.8, 0.0);
  vec4 lightspec = vec4(1.0, 1.0, 1.0, 0.0);
  float shininess = 0.45;
  // ...
 
  vec4 lpos = vec4(lightPositions[0], 0.0);
  vec4 s = -normalize(pos-lpos);

  vec3 light = s.xyz;
  vec3 n = normalize(vnormal);
  vec3 r = -reflect(light, n);
  r = normalize(r);
  vec3 v = -pos.xyz;
  v = normalize(v);
   
  vec4 diffuse  = color * max(0.0, dot(n, s.xyz)) * diffuseColor;
  vec4 specular = lightspec * matspec * pow(max(0.0, dot(r, v)), shininess);

  gl_FragColor = diffuse + specular;
}


To get different specular effects, adjust the settings inside the comments...especially the shininess value. Note that you can't use this shader on transparent objects as it is. You would have to write a version that takes alpha into account if that is needed.

Hope this helps...

MrAdam

Thanks! I really appreciate your help.

Although, there seems to be a problem with alpha in the shader. I've checked all the vec4's, and none of them has an alpha that would explain this :-/

EgonOlsen

Not sure where this comes from....but you can set to any value you like by adding gl_FragColor.a =<alpha>; as the last line.

MrAdam

#27
Thanks! Works perfectly now ;-)

There seems to be a problem with parts of the model looking "cracked" from some angles.


Ive tried changing glDither and glTrilinear as that might have been the cause, but to no help :-/

EgonOlsen

I'm not sure what you mean. Maybe you can highlight it in the screen shot somehow.

MrAdam

Quote from: EgonOlsen on May 20, 2012, 01:57:10 PM
I'm not sure what you mean. Maybe you can highlight it in the screen shot somehow.
Try looking at the right side door. Its full of small white lines/cracks