Author Topic: Car Reflections  (Read 5866 times)

Offline Locust

  • byte
  • *
  • Posts: 13
    • View Profile
Car Reflections
« on: June 01, 2017, 09:40:40 am »
Hey

I have a 3d model of a car with a single texture and to make it look good I need to have some kind of reflections on the metal.

Whats the best way to achieve a more realistic look?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Car Reflections
« Reply #1 on: June 01, 2017, 01:51:23 pm »
The easiest way would be to use a custom shader and second texture for a spherical environment map. That wouldn't give you proper reflections, but it might look good enough. I can provide you with a basic shader example for that it that helps.

Offline Locust

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Car Reflections
« Reply #2 on: June 01, 2017, 02:16:04 pm »
The easiest way would be to use a custom shader and second texture for a spherical environment map. That wouldn't give you proper reflections, but it might look good enough. I can provide you with a basic shader example for that it that helps.

That would be great, since I havent touched shaders yet.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Car Reflections
« Reply #3 on: June 02, 2017, 08:46:14 am »
Here are some example shaders. I'm not entirely sure if they compile, because I merged them together from two other ones without testing them, but anyway...

http://www.jpct.net/download/tmp/shaders.zip

Usage would be to create an instance of GLSLShader with them and assign that to the car object. The car object itself needs two texture layers (look at TextureInfo for this), one is the car's normal texture and the other would be something like this: http://graphics.stanford.edu/papers/envmap/Figs/rnl_probe.gif

Offline Locust

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Car Reflections
« Reply #4 on: June 16, 2017, 03:22:10 pm »
This is the code I use for creating the shader and loading the textures.

I dont seem to get the desired result with this. I also tried different Modes when adding the second texture to the TextureInfo, aswell as changing the order in which the textures are assigned to the TextureInfo.
The transparency on the result is also messed up.

Am I doing something wrong?

Code: [Select]
            String vertex = Loader.loadTextFile(mContext.getResources().openRawResource(R.raw.reflection_vert));
            String fragment = Loader.loadTextFile(mContext.getResources().openRawResource(R.raw.reflection_frag));
            GLSLShader shader = new GLSLShader(vertex,fragment);

            Texture texture = new Texture(mContext.getResources().openRawResource(R.raw.hatchback_diffuse));
            texture.keepPixelData(true);
            if(!TextureManager.getInstance().containsTexture("car_diffuse"))
                TextureManager.getInstance().addTexture("car_diffuse", texture);

            Texture environment = new Texture(mContext.getResources().openRawResource(R.raw.environment));
            if(!TextureManager.getInstance().containsTexture("environment"))
                TextureManager.getInstance().addTexture("environment",environment);

            TextureInfo textureInfo = new TextureInfo(TextureManager.getInstance().getTextureID("car_diffuse"));
            textureInfo.add(TextureManager.getInstance().getTextureID("environment"),TextureInfo.MODE_ADD);

            o3d.setTexture(textureInfo);
            o3d.setShader(shader);

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Car Reflections
« Reply #5 on: June 16, 2017, 05:18:49 pm »
It might be that the shaders don't support transparency as they are, but that can be added easily. However, before dealing with that, I would like to know what exactly are your desired results and what you get instead. A screen shot might help...

Offline Locust

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Car Reflections
« Reply #6 on: June 22, 2017, 09:27:11 am »
I actually fixed the transparency issues, that was an error on my end. So the car model is part of an augmented reality app and I want it to better blend in to the real world.

The first picture shows the car with the simple texture and no shader.

The 2nd picture shows the car with the shader and MODE_BLEND and you can see that it is just darkening the mode in some areas. I tried different modes which looked even worse.


Also to get it more realistic is there an option to make it use specular lightning? I know there is setSpecularLightning but does it do anything if I have a shader applied?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Car Reflections
« Reply #7 on: June 22, 2017, 10:52:02 am »
Actually, the blending mode has no impact if you are using a shader, because the shader does the blending. That happens here, in the fragment shader:

texture2D(textureUnit0, texCoord1)+texture2D(textureUnit1, texCoord)


Replacing the + by * gives you modulate blending. You are free to play around with some combination of vertex colors and both textures' colors to get something pleasing.

For specular, you would have to modify the lighting calculation in the shader. A simple hack might be to use the square root of the lighting's intensity instead of the intensity itself. If that's not good enough, you have to switch to per pixel lighting somehow.

Offline Locust

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Car Reflections
« Reply #8 on: June 22, 2017, 11:57:27 am »
Actually, the blending mode has no impact if you are using a shader, because the shader does the blending.
Hmm thats super weird then, because it definitely changes the outcome for me.
e.g. heres the same  scene just with add instead of  blend.

For specular, you would have to modify the lighting calculation in the shader. A simple hack might be to use the square root of the lighting's intensity instead of the intensity itself. If that's not good enough, you have to switch to per pixel lighting somehow.

Gonna look into that
« Last Edit: June 22, 2017, 12:06:04 pm by Locust »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Car Reflections
« Reply #9 on: June 22, 2017, 12:34:40 pm »
Then your shader isn't active...

Offline Locust

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Car Reflections
« Reply #10 on: June 22, 2017, 03:30:28 pm »
hmm youre right, I still had to change the Open GL ES version, I did change the lines mentioned here:
http://www.jpct.net/wiki/index.php?title=OpenGL_ES_2.0_support

But now I get a complete black screen instead, are there any major incompatibilities between the standard OpenGL version and 2.0?


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Car Reflections
« Reply #11 on: June 22, 2017, 03:57:43 pm »
No, actually there aren't. You aren't even supposed to notice the switch to 2.0 except for the added features.
How does your init code looks (GLSurfaceView and FrameBuffer creation) like? Are you getting any log output.

Offline Locust

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Car Reflections
« Reply #12 on: June 29, 2017, 09:20:03 am »
I did the changes to the init according to this link:
http://www.jpct.net/wiki/index.php?title=OpenGL_ES_2.0_support

Code: [Select]
mGLView = new GLSurfaceView(getApplication());
mGLView.setEGLContextClientVersion(2);

mGLView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
Code: [Select]
fb = new FrameBuffer(w, h);
I couldnt find any relevent log output either. The 3D models are just not showing.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Car Reflections
« Reply #13 on: June 29, 2017, 01:07:53 pm »
That setEGLConfigChooser(...) call isn't part of the Wiki page, or is it?

Try to use an instance of jPCT-AE's NVDepthConfigChooser instead.

Offline Locust

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Car Reflections
« Reply #14 on: June 29, 2017, 01:30:29 pm »
Code: [Select]
mGLView.setEGLConfigChooser(new NVDepthConfigChooser(mGLView));
Using this line instead just results in a black screen, while the other config atleast showed the camera image. Both dont show any 3d Models though.