2012-02-01 53 views
1

我想通過android NDK顯示使用OpenGL ES的圖像..我想要做的只是讀取尺寸爲90x72的RGB數據的二進制文件。並試圖顯示在模擬器的形象。但outpu形象是這樣的.. enter image description here圖像顯示不正確使用Android中的OpenGL ndk

而真正的圖像enter image description here

我不知道哪裏是我的代碼的問題...我的JNI代碼如下..

#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); 
} 

可有人告訴我哪裏是我的代碼的問題..提前

感謝

回答

2

您正在閱讀19584字節,但您的紋理是90 * 72 * 3 = 19440字節。 你的像素是緊密排列在行之間嗎?看起來你對每一行的兩個像素填充或寬度不正確。

+0

感謝您的回覆,現在我可以顯示圖像..我試圖顯示沒有填充位的720x480圖像.. – geeta 2012-02-01 11:39:58