#define one 1.0f typedef unsigned char byte; float rotateQuad; GLuint texture[1]; void *bitmapData; static GLfloat buffer[12] = { one,one,0, -one,one,0, one,-one,0, -one,-one,0, }; static GLfloat texCoords[8] = { 0,0,0,one,one,0,one,one, }; void Java_com_ldj_hellondk_NdkGLRenderer_onNdkSurfaceCreated (JNIEnv* env, jobject obj) { // 启用阴影平滑 glShadeModel(GL_SMOOTH); // 黑色背景 glClearColor(0, 0, 0, 0); // 设置深度缓存 glClearDepthf(1.0f); // 启用深度测试 glEnable(GL_DEPTH_TEST); // 所作深度测试的类型 glDepthFunc(GL_LEQUAL); // 告诉系统对透视进行修正 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); //允许2D贴图,纹理 glEnable(GL_TEXTURE_2D); //IntBuffer intBuffer = IntBuffer.allocate(1); // 创建纹理 glGenTextures(1, &texture[0]); //glGenTextures(1, intBuffer); //texture = intBuffer.get(); // 设置要使用的纹理 //glBindTexture(GL10.GL_TEXTURE_2D, texture); //纹理绑定 glBindTexture(GL_TEXTURE_2D, texture[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 179, 176, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmapData); // 线形滤波 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } void Java_com_ldj_hellondk_NdkGLRenderer_onNdkSurfaceChanged (JNIEnv* env, jobject obj, jint width, jint height) { //图形最终显示到屏幕的区域的位置、长和宽 glViewport (0,0,width,height); //指定矩阵 glMatrixMode (GL_PROJECTION); //将当前的矩阵设置为glMatrixMode指定的矩阵 glLoadIdentity (); glOrthof (-2, 2, -2, 2, -2, 2); } void Java_com_ldj_hellondk_NdkGLRenderer_onNdkDrawFrame (JNIEnv* env, jobject obj) { //启用顶点设置功能,之后必须要关闭功能 glEnableClientState (GL_VERTEX_ARRAY); glEnableClientState (GL_COLOR_ARRAY); //清屏 glClearColor (0,0,1,1); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); glBindTexture(GL_TEXTURE_2D, texture[0]);/// glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FIXED, 0, texCoords); glFrontFace(GL_CW); glRotatef(rotateQuad, 1.0f, 0.0f, 0.0f); glVertexPointer(3, GL_FLOAT, 0, buffer); //glColorPointer(4, GL_FLOAT, 0 , colors); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); //glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices); //关闭顶点设置功能 glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState (GL_COLOR_ARRAY); } void Java_com_ldj_hellondk_NdkGLRenderer_setBitmap (JNIEnv* env, jobject obj, jbyteArray bytes) { jbyte* buffer = env->GetByteArrayElements(bytes,0); bitmapData = buffer; }
以上是C++代码
转贴:http://bbs.51cto.com/thread-834543-1.html