how to draw image in opengl android
i have to draw bitmap in to the my live wallpaper using this code but image will not loaded its showing null pointer exception.
public class Square {
private FloatBuffer vertexBuffer; // buffer holding the vertices
private float vertices[] = { -1.0f, -1.0f, 0.0f, // V1 - bottom left
-1.0f, 1.0f, 0.0f, // V2 - top left
1.0f, -1.0f, 0.0f, // V3 - bottom right
1.0f, 1.0f, 0.0f // V4 - top right
};
private FloatBuffer textureBuffer; // buffer holding the texture coordinates
private float texture[] = {
// Mapping coordinates for the vertices
0.0f, 1.0f, // top left (V2)
0.0f, 0.0f, // bottom left (V1)
1.0f, 1.0f, // top right (V4)
1.0f, 0.0f // bottom right (V3)
};
/** The texture pointer */
int[] textures = new int[1];
public Square() {
// a float has 4 bytes so we allocate for each coordinate 4 bytes
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
// allocates the memory from the byte buffer
vertexBuffer = byteBuffer.asFloatBuffer();
// fill the vertexBuffer with the vertices
vertexBuffer.put(vertices);
// set the cursor position to the beginning of the buffer
vertexBuffer.position(0);
byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
textureBuffer = byteBuffer.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
/**
* Load the texture for the square
*
* @param gl
* @param context
*/
public void loadGLTexture(GL10 gl, Context context) {
System.out.println("loaded");
// // loading texture
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.nehe);
//here is the problem when ever i command bitmap it will be working .with out command its //showing null pointer exception
// generate one texture pointer
gl.glGenTextures(1, textures, 0);
// ...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// create nearest filtered texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
// Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);
// Use Android GLUtils to specify a two-dimensional texture image from
// our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// //
// // // Clean up
bitmap.recycle();
}
/** The draw method for the square with the GL context */
public void draw(GL10 gl) {
// System.out.println("draw");
// bind the previously generated texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// Point to our buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Set the face rotation
gl.glFrontFace(GL10.GL_CW);
// Point to our vertex buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
// Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
}
this is my logcat output
10-14 11:43:43.118: WARN/dalvikvm(3050): threadid=8: thread exiting with uncaught exception (group=0x4001d868)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050): FATAL EXCEPTION: GLThread 9
10-14 11:43:43.118: ERROR/AndroidRuntime(3050): java.lang.NullPointerException
10-14 11:43:43.118: ERROR/AndroidRuntime(3050): at frankandrobot.glwallpapervideodemo.com.Square.loadGLTexture(Square.java:73)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050): at frankandrobot.glwallpapervideodemo.com.VideoRenderer.onSurfaceCreated(VideoRenderer.java:77)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050): at net.rbgrn.android.glwallpaperservice.GLThread.guardedRun(GLWallpaperService.java:664)
10-14 11:43:43.118: ERROR/AndroidRuntime(3050): at net.rbgrn.android.glwallpaperservice.GLThread.run(GLWallpaperService.java:538)
10-14 11:43:56.398: WARN/Email(1452): Exception detected: Read error: I/O error during system call, Connection reset by peer
10-14 11:43:56.398: WARN/Email(1452): Last network activities:
10-14 11:43:56.398: WARN/Email(1452): * OK Gimap ready for requests from 115.111.177.222 g2if3342858pbc.293
10-14 11:43:56.398: WARN/Email(1452): 1 CAPABILITY
10-14 11:43:56.398: WARN/Email(1452): * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
10-14 11:43:56.398: WARN/Email(1452): 1 OK Thats all she wrote! g2if3342858pbc.293
10-14 11:43:56.398: WARN/Email(1452): 2 ID ("name" "com.android.email" "os" "android" "os-version" "2.2; FRF91" "vendor" "nv开发者_Go百科idia" "x-android-device-model" "INB-10/n" "AGUID" "+TIbwSeLDHum85UXux4T+QJD51g=")
need some help to do this stuff.kindly guide me a easy way i'm new to android and opengl....
Try moving the nehe.png (or jpg or whatever) to res/raw instead and then use the following:
InputStream inStream = context.getResources().openRawResource(R.raw.nehe);
Bitmap bitmap = BitmapFactory.decodeStream(inStream);
obviously replacing:
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.nehe);
Can you tell us which line is numbered 73?
I guess that you didn't initialized textures
variable (for example):
int[] textures = new int[1];
Had the same problem and it was in the render class, not in the loadGLTexture. Was an error because the "Context context" is not properly defined/called everywhere else in the live wallpaper. Verify that the renderer class has a context e.g:
...
Private Square square;
Private Context context;
for the renderer class also need to put in the renderer method something like:
public Myrenderer(Context context){
this.context=context;
square = new Square();
}
and for the wallpaper service something like:
renderer = new render(getBaseContext());
You can also define a setContext method to define the context from the wallpaper service:
renderer = new MyRenderer();
renderer.setContext(getBaseContext());
setRenderer(renderer);
a working code : https://github.com/GLWallpaperService/GLWallpaperService/pull/7
精彩评论