使用android源代码下的API

声明: 这个是 看到别人的代码,感觉不错,适合我这样的初学者,就加工了!

1,在android源代码的external目录下建立文件夹,如mkdir sno_opengl

2,在sno_opengl文件夹下,创建源代码,如tst_opengl.cpp和Android.mk
其中Android.mk的代码如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
       tst_opengl.cpp
LOCAL_SHARED_LIBRARIES := \
    libutils\
   libui \
   libEGL \
   libGLESv1_CM \
	libsurfaceflinger_client
 
LOCAL_MODULE := sno_opengl
LOCAL_MODULE_TAGS:= optional 
 
include $(BUILD_EXECUTABLE)


tst_poengl.cpp的源代码是:
(转自:http://blog.csdn.net/xiexh0921/article/category/1134272)
//android opengl es画三角形-native C实现  
//Native C开发opengl,网上这方面的资料很少,研究了几天才弄出来,现在贴出来供交流,转载请注明Hello Xiexh

#include <stdio.h>
#include <unistd.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include <ui/FramebufferNativeWindow.h>
#include <ui/EGLUtils.h>
#include <ui/DisplayInfo.h>
 
#include <surfaceflinger/SurfaceComposerClient.h>
 
 
using namespace android;
 
int        mWidth;
int        mHeight;
EGLDisplay mDisplay;
EGLDisplay mContext;
EGLDisplay mSurface;
sp<SurfaceControl>mFlingerSurfaceControl;
sp<Surface> mFlingerSurface;
 
 
int initGraphics()
{
      //create the native surface
      sp<SurfaceComposerClient>session = new SurfaceComposerClient();
      DisplayInfo dinfo;
      status_t status = session->getDisplayInfo(0, &dinfo);
      if(status)
           return-1;
   sp<SurfaceControl> control = session->createSurface(
           getpid(), 0, 400, 300, PIXEL_FORMAT_RGB_565);
   session->openTransaction();
   control->setLayer(0x40000000);
   session->closeTransaction();
 
   sp<Surface> s = control->getSurface();
 
      //initialize opengl and egl
      const EGLint attribs[] = 
      {
                 EGL_DEPTH_SIZE,0,
                 EGL_NONE
      };
      EGLint w, h, dummy;
     // EGLintnum Configs;
      EGLConfig config;
      EGLSurface surface;
      EGLContext context;
 
      EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
 
      eglInitialize(display,0, 0);
      EGLUtils::selectConfigForNativeWindow(display,attribs, s.get(), &config);
      surface= eglCreateWindowSurface(display, config, s.get(), NULL);
      context= eglCreateContext(display, config, NULL, NULL);
      eglQuerySurface(display,surface, EGL_WIDTH, &w);
      eglQuerySurface(display,surface, EGL_HEIGHT, &h);
 
      if(eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
           return NO_INIT;
 
      mDisplay = display;
      mContext = context;
      mSurface = surface;
      mWidth = w;
      mHeight = h;
      mFlingerSurfaceControl= control;
      mFlingerSurface= s;
 
      glViewport(0,0, mWidth, mHeight);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrthof(0,mWidth, 0, mHeight, 0, 1);
 
   return EGL_TRUE;
}
 
void draw() 
{
      //printf("startdrawing...\n");
   glColor4f(1,0,0,1);
 
   const GLfloat vertices[3][2] = {
       {100,  100},
           {200, 100},
           {100,200}
   };
 
   glEnableClientState(GL_VERTEX_ARRAY);
   glVertexPointer(2, GL_FLOAT, 0, vertices);
 
   glClearColor(0,0,0,0);
   glClear(GL_COLOR_BUFFER_BIT);
   glDrawArrays(GL_TRIANGLES, 0, 3);
     
   eglSwapBuffers(mDisplay, mSurface);
     
}
 
int main(int argc, char** argv) 
{
      if(!initGraphics())
      {
           printf("Graphicsinitialization failed.\n");
           return EXIT_FAILURE;
      }
      while(1){
           draw();
           usleep(100*1000);
      }
 
      //clearstatus
      eglMakeCurrent(mDisplay,EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
      eglDestroyContext(mDisplay,mContext);
      eglDestroySurface(mDisplay,mSurface);
      mFlingerSurface.clear();
      mFlingerSurfaceControl.clear();
      eglTerminate(mDisplay);
      return 0;
}




3,在源代码目录下执行 make sno_opengl
4,可以./adb push 进虚拟机,然后./sno_opengl可以出来。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值