Main Menu
Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - take2316

#1
Support / Re: Shadow mapping approach
March 20, 2013, 04:28:24 PM
So if I'm implementing shadow mapping, then that would be a shared shader, right? That is, if I did
world.setGlobalShader(shadowShader);
and wanted to pass in a uniform, would I need to implement IRenderHook?
#2
Support / Re: Shadow mapping approach
March 20, 2013, 08:46:45 AM
Wow... I had interpreted this documentation incorrectly or just read it too fast:

QuoteWhile this class is similar to the one in desktop jPCT, it works a little different. Especially, it's not an implementation of IRenderHook, so if you want to set uniforms per Object3D on a shared shader, you have to write an additional IRenderHook to do so. However, it's ensured that IRenderHook's beforeRendering()-method will be called before the shader will be initialized, so you can safely set your uniforms there.

I'm supposed to do something like:

chair.setShader(shader);
chair.setRenderHook(new CustomShader(vertexSphereShader, fragmentSphereShader, chair.getShader()));


and have CustomShader just set the uniform values in the beforeRendering() method:

mShader.setUniform("mixValue", mixValue);

and of course, I have to pass in the shader to the CustomShader...

UPDATE: all this is wrong! I can just call setUniform without having to use IRenderHook... Is this because it's not a "shared shader" (what is a shared shader anyway?)
#3
Support / Re: Shadow mapping approach
March 20, 2013, 06:42:24 AM
Here's my initial implementation of IRenderHook. It seems to execute with no errors, but the shaders are not getting executed... If I use GLSLShader with the same shader sources, it executes fine. Any ideas on what might be going wrong?

package com.arproject;
import java.util.ArrayList;
import com.threed.jpct.*;
import android.opengl.*;

public class CustomShader implements IRenderHook {

private int prg=0;
private int fragShade = 0, vertShade = 0;
private boolean init=false;

private String mVertexSource, mFragmentSource;

public CustomShader(String vertexSource, String fragmentSource) {
Logger.log("Created Custom Shader!", Logger.MESSAGE);
mVertexSource = vertexSource;
mFragmentSource = fragmentSource;
init();
}

@Override
public void afterRendering(int arg0) {
// TODO Auto-generated method stub
GLES20.glUseProgram(0);
}

@Override
public void beforeRendering(int arg0) {
// TODO Auto-generated method stub
if (!init) {
init();
}
GLES20.glUseProgram(prg);
}

@Override
public void onDispose() {
// TODO Auto-generated method stub
GLES20.glDeleteShader(vertShade);
GLES20.glDeleteShader(fragShade);
GLES20.glDeleteProgram(prg);
init = false;
}

@Override
public boolean repeatRendering() {
// TODO Auto-generated method stub
return false;
}

@Override
public void setCurrentObject3D(Object3D arg0) {
// TODO Auto-generated method stub

}

@Override
public void setCurrentShader(GLSLShader arg0) {
// TODO Auto-generated method stub

}

@Override
public void setTransparency(float arg0) {
// TODO Auto-generated method stub

}

private void init() {
prg = GLES20.glCreateProgram();
fragShade = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
if(fragShade == 0)
{
Logger.log("Failed to create fragShade in CustomShader!", Logger.MESSAGE);
Logger.log("Info: " + GLES20.glGetShaderInfoLog(prg), Logger.ERROR);
return;
}
GLES20.glShaderSource(fragShade, mFragmentSource);

vertShade = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
if(vertShade == 0)
{
Logger.log("Failed to create vertShade in CustomShader!", Logger.MESSAGE);
Logger.log("Info: " + GLES20.glGetShaderInfoLog(prg), Logger.ERROR);
return;
}
GLES20.glShaderSource(vertShade, mVertexSource);

int[] status = new int[1];
GLES20.glCompileShader(vertShade);
GLES20.glGetShaderiv(vertShade, GLES20.GL_COMPILE_STATUS, status, 0);
if(status[0] == 0) {
Logger.log("Vertex shader failed to compile in CustomShader!", Logger.MESSAGE);
Logger.log("Info: " + GLES20.glGetShaderInfoLog(prg), Logger.ERROR);
return;
}
GLES20.glCompileShader(fragShade);
GLES20.glGetShaderiv(fragShade, GLES20.GL_COMPILE_STATUS, status, 0);
if(status[0] == 0) {
Logger.log("Fragment shader failed to compile in CustomShader!", Logger.MESSAGE);
Logger.log("Info: " + GLES20.glGetShaderInfoLog(prg), Logger.ERROR);
return;
}

GLES20.glAttachShader(prg, vertShade);
GLES20.glAttachShader(prg, fragShade);

GLES20.glLinkProgram(prg);
GLES20.glGetProgramiv(prg, GLES20.GL_LINK_STATUS, status, 0);
if(status[0] == 0) {
Logger.log("Failed to link program in CustomShader!", Logger.MESSAGE);
Logger.log("Info: " + GLES20.glGetShaderInfoLog(prg), Logger.ERROR);
return;
}

Logger.log("Shader compiled and linked in CustomShader!", Logger.MESSAGE);
Logger.log("Info: " + GLES20.glGetShaderInfoLog(prg), Logger.MESSAGE);

init=true;
}
}
#4
Support / Shadow mapping approach
March 20, 2013, 01:53:49 AM
I'm trying to implement simple shadow mapping for jPCT-AE. I saw some of the recent forum posts about how shadows will soon be implemented?... but I just wanted to know if my approach is correct. (I'm trying to implement this for a school project that's due in a little over 48 hours!)


// Framebuffer fb
// Camera lightCam represents the light's view
// Texture shadowMap
// GLSLShader depthShader

// render shadow map
fb.setRenderTarget(shadowMap);
world.setCameraTo(lightCam);
world.setGlobalShader(depthShader); // doesn't override the shader for objects that called setShader!
world.renderScene(fb);
world.draw(fb);
fb.removeRenderTarget();
world.setGlobalShader(null); // is this how you remove the global shader?
world.setCameraTo(cam); // set back to regular camera
...
// regular render pass
world.renderScene(fb);
world.draw(fb);
fb.display();


Then I'll have to pass in the light's model view projection matrix to the regular shadow mapping shader. Since GLSLShader does not support passing in your own uniforms, I'll have to create an implementation of IRenderHook... right? Or is there a way to pass in the model-view-projection matrix for the light source(s) via GLSLShader without implementing IRenderHook?
#5
Thanks so much, it works now. I'm new to OpenGL ES, so I didn't realize there was that requirement. I guess the GPU on the Galaxy S3 phone was just more forgiving...
#6
I can successfully run my shader code on my Android 4.1.1 Samsung Galaxy S3 device, but when I try it on an Android 2.3.4 LG Revolution, I get this error: "ERROR: Failed to load and compile fragment shaders!"

Here are my vertex and fragment shaders:

Vertex shader:

uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;

attribute vec4 position;
attribute vec3 normal;

varying vec2 texCoords;

void main() {
// apparently, jPCT doesn't support gl_NormalMatrix, but it's okay:
// see: http://www.jpct.net/forum2/index.php/topic,2723.msg20171.html#msg20171
// and: http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/
vec3 n = normalize(modelViewMatrix * vec4(normal,0.0)).xyz;

vec3 u = (modelViewMatrix * position).xyz;

vec3 r = u - 2.0 * dot(u,n) * n;
float m = 2.0 * sqrt(r.x*r.x + r.y*r.y + (r.z+1.0)*(r.z+1.0));
texCoords.x = r.x/m + 0.5;
texCoords.y = r.y/m + 0.5;

gl_Position = modelViewProjectionMatrix * position;
}


Fragment shader:

uniform sampler2D textureUnit0;

varying vec2 texCoords;

void main() {
gl_FragColor = texture2D(textureUnit0, texCoords);
}


Do you have any idea why this might be happening?
#7
Support / Re: setRenderHook(shader) not compiling
March 11, 2013, 04:29:37 AM
Thanks, it works now. I was following the wiki page: http://www.jpct.net/wiki/index.php/Shaders but had forgotten that jPCT-AE might work differently.
#8
Support / setRenderHook(shader) not compiling
March 10, 2013, 01:43:48 AM
Eclipse is giving me this error for the following code:
The method setRenderHook(IRenderHook) in the type Object3D is not applicable for the arguments (GLSLShader)


GLSLShader shader = new GLSLShader(vertex, fragment);
cube.setRenderHook(shader);
world.addObject(cube);


I tried casting shader to an IRenderHook, but when the code runs, I get this fatal exception error: E/AndroidRuntime(30731): java.lang.ClassCastException: com.threed.jpct.GLSLShader cannot be cast to com.threed.jpct.IRenderHook

Any ideas on how to fix this?
#9
I think I just figured it out, a couple seconds after my post! I removed plane.calcTextureWrapSpherical() and it looks like the entire image is shown now in the texture!
#10
Bugs / BitmapHelper rescale not working correctly?
March 09, 2013, 09:15:10 PM
I'm creating a texture from a 640x480 image and want to rescale it to 256x256 for the texture. So I tried doing:
Texture texture = new Texture(BitmapHelper.rescale(b, 256, 256);
where b is the 640x480 Bitmap image. However, when I load it into a plane, only the middle of the image is shown in the texture:
Object3d plane = Primitives.getPlane(1, 100);

I verified this by saving the bitmap to my sdcard on the Android phone and indeed the saved bitmap shows the full picture. However, the texture looks like it's only the very middle portion of the bitmap. Is this how BitmapHelper.rescale works, i.e. saving the center portion first?
#11
I tried following the wiki code again, but this time I used OpenGL ES 2.0 (I was using 1.1 before) and it seems like it might be working without flickering. However, I'm unsure if the environment mapping is really working. When I call setEnvmapped, the texture looks exactly the same as when I do not call setEnvmapped. Here's the 3 relevant lines of code:

cube.calcTextureWrapSpherical(); // if this line is gone, then the cube is simply black
cube.setTexture("texture");
cube.setEnvmapped(Object3D.ENVMAP_ENABLED); // this line doesn't seem to change the appearance of the texture!


Any ideas on how to check if the environment mapping is working?
#12
Support / SetEnvmapped gives a flickering texture
March 06, 2013, 05:44:28 AM
I'm following the Integrating jPCT-AE with Vuforia code (http://www.jpct.net/wiki/index.php/Integrating_JPCT-AE_with_Vuforia), and want to use environment mapping for the textures. So I added
cube.setEnvmapped(Object3D.ENVMAP_ENABLED);
but what I get is a flashing (flickering) texture. Could someone point me in the right direction to use environment mapping?