www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: Locust on June 01, 2017, 09:40:40 am

Title: Car Reflections
Post by: Locust 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?
Title: Re: Car Reflections
Post by: EgonOlsen 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.
Title: Re: Car Reflections
Post by: Locust 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.
Title: Re: Car Reflections
Post by: EgonOlsen 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 (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 (http://graphics.stanford.edu/papers/envmap/Figs/rnl_probe.gif)
Title: Re: Car Reflections
Post by: Locust 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);
Title: Re: Car Reflections
Post by: EgonOlsen 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...
Title: Re: Car Reflections
Post by: Locust 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.
(https://picload.org/image/rpgoorli/normal.png)
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.
(https://picload.org/image/rpgoorlw/blend.png)

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?
Title: Re: Car Reflections
Post by: EgonOlsen 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.
Title: Re: Car Reflections
Post by: Locust 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.
(https://picload.org/image/rpgoipcr/add.png)
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
Title: Re: Car Reflections
Post by: EgonOlsen on June 22, 2017, 12:34:40 pm
Then your shader isn't active...
Title: Re: Car Reflections
Post by: Locust 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?

Title: Re: Car Reflections
Post by: EgonOlsen 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.
Title: Re: Car Reflections
Post by: Locust 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.
Title: Re: Car Reflections
Post by: EgonOlsen 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.
Title: Re: Car Reflections
Post by: Locust 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.
Title: Re: Car Reflections
Post by: EgonOlsen on June 29, 2017, 02:10:33 pm
So this is one of these "render-on-top-of-a-camera-image"-apps? I wasn't aware of this.

Have you tried without the camera stuff on top? Maybe something is wrong in the way in which you are combining both under ES 2.0...!?
Title: Re: Car Reflections
Post by: Locust on June 30, 2017, 08:53:13 am
Ok the actual version change seems to actually work.
The problem is the apparently the actual shader. Once I set the shader to the Object it doesnt show up anymore.
Any Idea if anything went wrong in the shader code?
Title: Re: Car Reflections
Post by: EgonOlsen on June 30, 2017, 10:03:06 am
Are you getting some log output about the shader compilation? What's your testing device?
Title: Re: Car Reflections
Post by: Locust on June 30, 2017, 10:56:54 am
Device: Samsung Galaxy Tab S2 (9.7, LTE) (SM-T819)

Now the logcat limited to Shader. The shaderloading logs are manually set between the creation of the shader, textureloading and setting the shader to the object. Weird that there arent any outputs between them. That cant be right.

Code: [Select]
I/Adreno: QUALCOMM build                   : 2e6580f, I3fd0b1e797
                                           Build Date                       : 10/04/16
                                           OpenGL ES Shader Compiler Version: XE031.06.00.02
                                           Local Branch                     :
                                           Remote Branch                    :
                                           Remote Branch                    :
                                           Reconstruct Branch               :
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShader.src
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShader.src
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderTex0Amb.src
                                                                     
                                                                     [ 06-30 10:22:56.456 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderTex0Amb.src
                                                                     
                                                                     [ 06-30 10:22:56.466 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderTex0.src
                                                                     
                                                                     [ 06-30 10:22:56.486 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderTex0.src
                                                                     
                                                                     [ 06-30 10:22:56.486 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderTex1.src
                                                                     
                                                                     [ 06-30 10:22:56.556 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderTex1.src
                                                                     
                                                                     [ 06-30 10:22:56.566 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderTex0Light0.src
                                                                     
                                                                     [ 06-30 10:22:56.656 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderTex0Light0.src
                                                                     
                                                                     [ 06-30 10:22:56.666 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderFog.src
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderFog.src
                                                                     
                                                                     [ 06-30 10:22:56.706 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderFogLight0.src
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderFogLight0.src
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderDepth.src
                                                                     
                                                                     [ 06-30 10:22:56.836 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderDepth.src
                                                                     
                                                                     [ 06-30 10:22:56.836 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/Shaderloading: begin
I/Shaderloading: end

I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShader.src
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShader.src
                                                                     
                                                                     [ 06-30 10:22:57.506 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderTex0Amb.src
                                                                     
                                                                     [ 06-30 10:22:57.516 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderTex0Amb.src
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderTex0.src
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderTex0.src
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderTex1.src
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderTex1.src
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderTex0Light0.src
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderTex0Light0.src
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderFog.src
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderFog.src
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderFogLight0.src
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderFogLight0.src
I/jPCT-AE: Default fragment shader is: /defaultFragmentShader.src
I/jPCT-AE: Loading /defaultFragmentShaderDepth.src
I/jPCT-AE: Default vertex shader is: /defaultVertexShader.src
I/jPCT-AE: Loading /defaultVertexShaderDepth.src
                                                                     
                                                                     [ 06-30 10:22:57.586 10014:10149 W/         ]
                                                                     Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
Title: Re: Car Reflections
Post by: EgonOlsen on June 30, 2017, 12:58:40 pm
Shaders will be compiled at render time, i.e. the first time the object with that shader is about to be rendered. Depending on the graphics driver, grabbing the log for "shader" might or might not show a potential problem. Can you post the complete log (maybe limited to the app).