NDK绘制图片实例

#include "com_example_sample_GlBufferView.h"
#include <GLES/gl.h>
#include <GLES/glext.h>
#include <string.h>
#include <pthread.h>
#include <android/log.h>
#include <stdio.h>
#include <time.h>

#define TEXTURE_WIDTH  256
#define TEXTURE_HEIGHT 128
#define MY_SCREEN_WIDTH  90
#define MY_SCREEN_HEIGHT 72

#define SB_PIXELS_SIZE (sizeof(sb_data[0]) * MY_SCREEN_WIDTH * MY_SCREEN_HEIGHT*3)

static int s_w;
static int s_h;
static pthread_cond_t s_vsync_cond;
static pthread_mutex_t s_vsync_mutex;
static GLuint s_texture;

FILE *fp;

static unsigned char *sb_data=0;

static void render_bytes(unsigned char *pixels)
{
    int x, y;
        int idx=0;
         fp=fopen("/data/123","rb");
         if(fp!=NULL)
            {
                fread(pixels,1,19584,fp);
                fclose(fp);
            }

}
static void wait_vsync()
{
       pthread_mutex_lock(&s_vsync_mutex);
       pthread_cond_wait(&s_vsync_cond, &s_vsync_mutex);
       pthread_mutex_unlock(&s_vsync_mutex);
}

JNIEXPORT void JNICALL Java_com_example_sample_GlBufferView_native_1start(JNIEnv * env, jobject obj)
{
       /* init conditions */
       pthread_cond_init(&s_vsync_cond, NULL);
       pthread_mutex_init(&s_vsync_mutex, NULL);
       sb_data=(unsigned char *)malloc(SB_PIXELS_SIZE);
       int incr = 1;
       while (1) {

               /* game code goes here */
               wait_vsync();
       }
}
JNIEXPORT void JNICALL  Java_com_example_sample_GlBufferView_native_1gl_1resize(JNIEnv * env, jobject obj, jint w, jint h)
{
       glEnable(GL_TEXTURE_2D);
       glDisable(GL_BLEND);
       glGenTextures(1, &s_texture);
       glBindTexture(GL_TEXTURE_2D, s_texture);
       glTexParameterf(GL_TEXTURE_2D,
                       GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       glTexParameterf(GL_TEXTURE_2D,
                       GL_TEXTURE_MAG_FILTER, GL_LINEAR);

       glShadeModel(GL_SMOOTH);
       glColor4x(0x10000, 0x10000, 0x10000, 0x10000);

       int rect[4] = {0, TEXTURE_HEIGHT, TEXTURE_WIDTH, -TEXTURE_HEIGHT};
       glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, rect);
       glTexImage2D(GL_TEXTURE_2D,             /* target */
                       0,                      /* level */
                       GL_RGB,                 /* internal format */
                       TEXTURE_WIDTH,          /* width */
                       TEXTURE_HEIGHT,         /* height */
                       0,                      /* border */
                       GL_RGB,                 /* format */
                       GL_UNSIGNED_BYTE,/* type */
                       NULL);                  /* pixels */
       /* store the actual width of the screen */
       s_w = w;
       s_h = h;
}
JNIEXPORT void JNICALL Java_com_example_sample_GlBufferView_native_1gl_1render(JNIEnv * env, jobject obj)
{
       memset(sb_data, 0, SB_PIXELS_SIZE);

       render_bytes(sb_data);
       glClear(GL_COLOR_BUFFER_BIT);

       glTexSubImage2D(GL_TEXTURE_2D,          /* target */
                            0,                      /* level */
                            0,                      /* xoffset */
                            0,                      /* yoffset */
                            MY_SCREEN_WIDTH,        /* width */
                            MY_SCREEN_HEIGHT,       /* height */
                            GL_RGB,                 /* format */
                            GL_UNSIGNED_BYTE, /* type */
                            sb_data);              /* pixels */
       glDrawTexiOES(0, 0, 0, s_w, s_h);
       /* tell the other thread to carry on */
       pthread_cond_signal(&s_vsync_cond);
}

转贴:http://stackoverflow.com/questions/9093494/image-is-not-displaying-proper-using-opengl-in-android-ndk
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值