Image is not displaying proper using OpenGL in android ndk

时间:2022-01-24 21:21:54

I am trying to display an image using OpenGL ES through android NDK.. What I am trying to do is just reading a binary file of RGB data of size 90x72. and trying to display that image on emulator.. But outpu image is this..

Image is not displaying proper using OpenGL in android ndk

while the real image is 

Image is not displaying proper using OpenGL in android ndk

I don't know where is the problem in my code... My JNI code is as follows..

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

reply:

You are reading 19584 bytes, but your texture is 90*72*3 = 19440 bytes. Are your pixels tightly packed between lines? It seems that you two pixel padding for each line or the width is incorrect.
转帖:http://*.com/questions/9093494/image-is-not-displaying-proper-using-opengl-in-android-ndk