Bit of Help Porting this tutorial
I'm trying to use the code in this tutorial, but its in the wrong language (OPEN-GL) so i'm porting it into OPENGL-ES as best I can. This is what I have so far:
public void coordinates (GL10 gl, int x, int y, int width, int height){
int[] viewport = new int[4];
gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
int[] modelview = new int[16];
gl.glGetIntegerv(GL11.GL_MODELVIEW_MATRIX, modelview, 0);
int[] projection = new int[16];
gl.glGetIntegerv(GL11.GL_PROJECTION_MATRIX, projection, 0);
sety = viewport[3] - sety;
}
Got a bit thrown by the glReadPixel command because it doesn't accept this
gl.glReadPixels(setx, sety, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &setz);
Error states that GL_DEPTH_COMPONENT, GL_FLOAT and &setz can't be resolved. What is the OPENGL-ES alternative?
Also the last part of the tutorial, step 5, is that a whole new method or a compilation of the whole tutorial?
NOTE: setx and sety are private floats called at the top of my code
EDIT: I'm also having trouble with the gluUnProject command it seems to require more values than the opengl version in the tutorial.
(in opengl-es)
GLU.gluUnProject(winX, winY, winZ, model, modelOffset, project, projectOffset, view, viewOffset, obj, objOffset)
(and in the tutorial)
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
EDIT:
Okay I think I've pretty much solved my own question but I'm still having problems.
gl.glReadPixels
requires these variables
glReadPixels(int, int, int, int, int, int, Buffer)
Which is fine but
GLU.gluUnproject
Requires the x coordinate (returned as a buffer by readpixels) to be float.
Also in the NeHe tutorial UnProject returns three floats but here it seems to only leave room for one so I have no idea how to get the variables. This is my current glUnproject code which doesn't match the accepted layout yet.
GLU.gluUnProject(setx, newsety, setz, modelview, 0, projection, 0, viewport, 0, posx, 0, posy, 0, posz, 0);
these arguments are as such
(float, float, buffer, int[], int, int[], int, int[], int, float, int, float, int, float, int)]
When they should be
(float, float, float, float[], int, float[], int, int[], int, float[], int)
EDIT:
Right I decided I could do away with the readpixel command because my game is 2D and setting z to 0 should work OK
so the buffer problem is gone
I changed some of my variables to floats and GLU.gluUnProject now returns no errors. I still don't quite know how to return the posx, posy, posz variables however.
Here is my code as it stands.
public void Vector3 (GL11 gl, int x, int y){
int[] viewport = new int[4];
float[] modelview = new float[16];
float[] projection = new float[16];
float winx, winy, winz;
float posx, posy, posz;
float[] Vector3 = new float[3];
gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
gl.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelview, 0);
gl.glGetFloatv(GL11.GL_PROJECTION_MATRIX, projection, 0);
winx = setx;
winy = viewport[3] - sety;
winz = 0;
GLU.gluUnProject(winx, winy, winz, modelview, 0, projection, 0, viewport, 0, Vector3, 0);
return Vector3(posx, posy, posz);
}
setx, sety, setz are global variables and the return Vector3(posx, posy, posz); line currently returns errors.
Your help would be appreciated thankyou.
EDIT: Okay Think I got it
Here is some code (I included the motion event method because that is where the problem currently is and the draw method to give some context)
public synchronized void randomMethod(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
setx = event.getX();
sety = event.getY();
Vector3(null);
}
}
public void Vector3 (GL11 gl){
int[] viewport = new int[4];
float[] modelview = new float[16];
float[] projection = new float[16];
float winx, winy, winz;
float[] Vector3 = new float[3];
gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
gl.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelview, 0);
gl.glGetFloatv(GL11.GL_PROJECTION_MATRIX, projection, 0);
winx = setx;
winy = viewport[3] - sety;
winz = 0;
GLU.gluUnProject(winx, winy, winz, modelview, 0, projection, 0, viewport, 0, Vector3, 0);
posx = Vector3[1];
posy = Vector3[2];
posz = Vector3[3];
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT |
GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glTranslatef(posx, posy, posz);
gl.glScalef(100, 100, 0);
square.draw(gl);
gl.glPopMatrix();
}
My error log shows
07-30 10:41:03.220: ERROR/AndroidRuntime(340): Uncaught handler: thread main exiting due to uncaught exception
07-30 10:41:03.270: ERROR/AndroidRuntime(340): java.lang.NullPointerException
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at android.app.ui.GLSurfaceRenderer.Vector3(GLSurfaceRenderer.java:51)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at android.app.ui.GLSurfaceRenderer.randomMethod(GLSurfaceRenderer.java:40)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at android.app.ui.Practice.onTouchEvent(Practice.java:41)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at android.app.Activity.dispatchTouchEvent(Activity.java:2064)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at android.view.ViewRoot.handleMessage(ViewRoot.java:1690)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at android.os.Looper.loop(Looper.java:123)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at android.app.ActivityThread.main(ActivityThread.java:4310)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at java.lang.reflect.Method.invoke(Method.java:521)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at com.android.internal.os.ZygoteInit$MethodAndArgs开发者_StackOverflowCaller.run(ZygoteInit.java:860)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-30 10:41:03.270: ERROR/AndroidRuntime(340): at dalvik.system.NativeStart.main(Native Method)
I have a hunch that the problem may be caused by the way i've called Vector3 in randomMethod, but I can't think how else I could achieve the same thing (maybe its the null argument).
It is your null argument.
Vector3 is expecting an instance of GL11 but you are providing it with null, this means that when it comes to
gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
gl.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelview, 0);
gl.glGetFloatv(GL11.GL_PROJECTION_MATRIX, projection, 0);
gl = NULL
and you get a null pointer.
If you are doing this so you can purely get a Z coordinate then you are going to have to place this method somewhere where is has access to GL11
精彩评论