Featured post
c++ - Porting an iPhone game to Android - Textures and Buffers -
i have programmed game in c++. have managed compile using ndk, have notice couple of snags.
how load textures ndk opengl(since code there)? did use bitmapfactory load image, copy pixels bytebuffer allocated @ size of width * height * 4. sent array of bytebuffer native code , pulled out pointer using get/releaseprimiativearraycritical. don't think works crashes when renders. also, can't notice garbage compiler moaning 5.37 mb of space being taking when image 16 kb.
the other thing buffers, framebuffers , renderbuffers. notice isn't use of framebuffers , renderbuffers. glsurfaceview atuomatically this? , okay use own buffers?
well have figured out. turns out bitmapfactory scaling images non power of 2 size not work. glsurfaceview supports opengl es 1.0 way of doing things, draw on-screen. can use framebuffers, have link them texture , draw texture screen. used example , ported across native excluding depth buffer.
here's code:
java:
public class nativerenderer implements renderer { static { system.loadlibrary("test"); } private static native void init(); private static native void settexture(byte[] data, int width, int height); private static native void resize(int width, int height); private static native void render(); private context mcontext; public nativerenderer(context context) { mcontext = context; } public void ondrawframe(gl10 gl) { render(); } public void onsurfacechanged(gl10 gl, int width, int height) { resize(width, height); } public void onsurfacecreated(gl10 gl, eglconfig config) { init(); bytebuffer data; bitmapfactory.options opts = new bitmapfactory.options(); opts.inpreferredconfig = bitmap.config.argb_8888; //argb_888 default opts.indensity = 240; // needed images 512x512 (power of 2) opts.inscaled = false; // suggested add bitmap bitmap = bitmapfactory.decoderesource(mcontext.getresources(), r.drawable.image, opts); int width = bitmap.getwidth(); int height = bitmap.getheight(); data = bytebuffer.allocate(width * height * 4); log.i(tag, "allocate:" + (width * height * 4)); data.order(byteorder.nativeorder()); bitmap.copypixelstobuffer(data); data.position(0); bitmap.recycle(); log.i(tag, "data.hasarray():" + data.hasarray()); settexture(data.array(), width, height); } }
c++:
#include "your_company_nativerenderer.h" #include <android/log.h> #include <gles/gl.h> #include <gles/glext.h> #include <string.h> #include "functions.h" static gluint texture[2]; static gluint framebuffer; static gluint renderbuffer; static gluint texturebuffer; static int texturewidth; static int textureheight; static int swidth; static int sheight; static float hheight; void viewportframebuffer() { glviewport(0, 0, texturewidth, textureheight); glmatrixmode(gl_projection); glloadidentity(); static float viewportframebufferright = 160.0f + (320.0 / (float)swidth) * (texturewidth - swidth); static float viewportframebuffertop = hheight + ((2 * hheight) / (float)sheight) * (textureheight - sheight); glorthof(-160.0f, viewportframebufferright, -hheight, viewportframebuffertop, -1.0f, 1.0f); glmatrixmode(gl_modelview); glloadidentity(); } void viewportsurface() { glviewport(0, 0, swidth, sheight); glmatrixmode(gl_projection); glloadidentity(); glorthof(-160.0f, 160.0f, -hheight, hheight, -1.0f, 1.0f); glmatrixmode(gl_modelview); glloadidentity(); } jniexport void jnicall java_your_company_nativerenderer_init (jnienv *env, jclass obj) { __android_log_print(android_log_info, tag, "init()"); glgenframebuffersoes(1, &framebuffer); glbindframebufferoes(gl_framebuffer_oes, framebuffer); glenable(gl_texture_2d); //glenable(gl_blend); glgentextures(2, texture); texturebuffer = texture[0]; } jniexport void jnicall java_your_company_nativerenderer_settexture (jnienv *env, jclass obj, jbytearray array, jint width, jint height) { __android_log_print(android_log_info, tag, "settexture()"); void *image = env->getprimitivearraycritical(array, 0); unsigned int *imagedata = static_cast<unsigned int *>(image); flipimagevertically(imagedata, width, height); // flips bytes vertically glbindtexture(gl_texture_2d, texture[1]); gltexparameteri(gl_texture_2d,gl_texture_min_filter,gl_linear); gltexparameteri(gl_texture_2d,gl_texture_mag_filter,gl_linear); glteximage2d(gl_texture_2d, 0, gl_rgba, width, height, 0, gl_rgba, gl_unsigned_byte, image); env->releaseprimitivearraycritical(array, image, 0); } jniexport void jnicall java_your_company_nativerenderer_resize (jnienv *env, jclass obj, jint width, jint height) { __android_log_print(android_log_info, tag, "resize(width:%d, height:%d)", width, height); swidth = width; sheight = height; hheight = 240.0f + additiontoscaleplane(width, height); // used adjust spect ratio // iphone 2 : 3, galaxy s 5 : 3 __android_log_print(android_log_info, tag, "hheight:%g", hheight); texturewidth = powerof2bigger(width); textureheight = powerof2bigger(height); __android_log_print(android_log_info, tag, "texture:{w:%d, h:%d}", texturewidth, textureheight); glbindtexture(gl_texture_2d, texturebuffer); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_wrap_s, gl_clamp_to_edge); gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_clamp_to_edge); glcolor4ub(0, 0, 0, 255); glteximage2d(gl_texture_2d, 0, gl_rgba, texturewidth, textureheight, 0, gl_rgba, gl_unsigned_byte, null); int area[] = {0.0, 0.0, swidth, sheight}; // using draw_texture extension gltexparameteriv(gl_texture_2d, gl_texture_crop_rect_oes, area); glbindframebufferoes(gl_framebuffer_oes, framebuffer); glframebuffertexture2does(gl_framebuffer_oes, gl_color_attachment0_oes, gl_texture_2d, texturebuffer, 0); int status = glcheckframebufferstatusoes(gl_framebuffer_oes); if(status != gl_framebuffer_complete_oes) __android_log_print(android_log_error, tag, "framebuffer not complete: %x", status); glbindframebufferoes(gl_framebuffer_oes, framebuffer); } jniexport void jnicall java_your_company_nativerenderer_render (jnienv *env, jclass obj) { static float texcoord[] = { 0.0, 0.0, 0.0, 480.0/512.0, 320.0/512.0, 0.0, 320.0/512.0, 480.0/512.0, }; static float vertex[] = { -160.0, -240.0, -160.0, 240.0, 160.0, -240.0, 160.0, 240.0, }; //off-screen viewportframebuffer(); glbindframebufferoes(gl_framebuffer_oes, framebuffer); glclearcolor(1.0, 0.0, 0.0, 1.0); glclear(gl_color_buffer_bit); glenableclientstate(gl_vertex_array); glenableclientstate(gl_texture_coord_array); glbindtexture(gl_texture_2d, texture[1]); glvertexpointer(2, gl_float, 0, vertex); gltexcoordpointer(2, gl_float, 0, texcoord); gldrawarrays(gl_triangle_strip, 0, 4); gldisableclientstate(gl_vertex_array); gldisableclientstate(gl_texture_coord_array); glbindframebufferoes(gl_framebuffer_oes, 0); //on-screen viewportsurface(); glclearcolor(0.0, 0.0, 1.0, 1.0); glclear(gl_color_buffer_bit); /*static float texturecoord[] = { 0.0, 0.0, 0.0, sheight/(float)textureheight, swidth/(float)texturewidth, 0.0, swidth/(float)texturewidth, sheight/(float)textureheight, }; static float texturevertex[] = { -160.0, -hheight, -160.0, hheight, 160.0, -hheight, 160.0, hheight, };*/ //if there isn't draw_texture extension glbindtexture(gl_texture_2d, texturebuffer); gltexenvi(gl_texture_env,gl_texture_env_mode, gl_replace); glactivetexture(gl_texture0); gldrawtexioes(0, 0, 0, swidth, sheight); //glvertexpointer(2, gl_float, 0, texturevertex); //gltexcoordpointer(2, gl_float, 0, texturecoord); //gldrawarrays(gl_triangle_strip, 0, 4); //gldisableclientstate(gl_vertex_array); //gldisableclientstate(gl_texture_coord_array); glbindtexture(gl_texture_2d, 0); }
- Get link
- X
- Other Apps
Comments
Post a Comment