Author Topic: Viable engine?  (Read 28056 times)

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: Viable engine?
« Reply #15 on: May 18, 2012, 09:44:53 am »
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:


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Viable engine?
« Reply #16 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

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: Viable engine?
« Reply #17 on: May 18, 2012, 11:04:27 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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Viable engine?
« Reply #18 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?

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: Viable engine?
« Reply #19 on: May 18, 2012, 11:12:47 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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Viable engine?
« Reply #20 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

Code: [Select]
vec3 n = normalize(gl_NormalMatrix * gl_Normal);

by

Code: [Select]
vec3 n = normalize(modelViewMatrix * vec4(normal,0.0)).xyz;
« Last Edit: May 18, 2012, 11:25:02 am by EgonOlsen »

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: Viable engine?
« Reply #21 on: May 18, 2012, 11:33:23 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

Code: [Select]
vec3 n = normalize(gl_NormalMatrix * gl_Normal);

by

Code: [Select]
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Viable engine?
« Reply #22 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.

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: Viable engine?
« Reply #23 on: May 18, 2012, 04:46:57 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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Viable engine?
« Reply #24 on: May 18, 2012, 05:53:53 pm »
Here's a rough port of the shader in your link:

vertex:
Code: [Select]
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:
Code: [Select]
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...

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: Viable engine?
« Reply #25 on: May 20, 2012, 10:26:05 am »
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 :-/

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Viable engine?
« Reply #26 on: May 20, 2012, 11:41:40 am »
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.

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: Viable engine?
« Reply #27 on: May 20, 2012, 12:05:48 pm »
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 :-/
« Last Edit: May 20, 2012, 12:35:52 pm by MrAdam »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Viable engine?
« Reply #28 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.

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: Viable engine?
« Reply #29 on: May 20, 2012, 01:58:09 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