【发布时间】:2014-09-08 17:05:54
【问题描述】:
我正在尝试从原始数据中绘制纹理,其中元素是无符号字符(或无符号整数),每个 3 个后续元素(RGB)代表像素数据。 我想要的是 *在我的手机屏幕 (1920*1080)* 上显示这个 rgb 原始数据 (width=640 ,height =480)。 这是我的代码! 我正在使用 egl 准备图形环境,如下所示:
bool Render::initializing(ANativeWindow* window)
{
const EGLint attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE,
8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_NONE };
EGLDisplay display;
EGLConfig config;
EGLint numConfigs;
EGLint format;
EGLSurface surface;
EGLContext context;
EGLint width;
EGLint height;
GLfloat ratio;
Log::info("Initializing context");
if ((display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY) {
Log::error("eglGetDisplay() returned error %d", eglGetError());
return false;
}
if (!eglInitialize(display, 0, 0)) {
Log::error("eglInitialize() returned error %d", eglGetError());
return false;
}
if (!eglChooseConfig(display, attribs, &config, 1, &numConfigs)) {
Log::error("eglChooseConfig() returned error %d", eglGetError());
destroy();
return false;
}
if (!eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format)) {
Log::error("eglGetConfigAttrib() returned error %d", eglGetError());
destroy();
return false;
}
Log::info("Setting buffer geometry");
ANativeWindow_setBuffersGeometry(window, 0, 0, format);
Log::info("End Setting buffer geometry");
if (!(surface = eglCreateWindowSurface(display, config, window, 0))) {
Log::error("eglCreateWindowSurface() returned error %d", eglGetError());
destroy();
return false;
}
if (!(context = eglCreateContext(display, config, 0, 0))) {
Log::error("eglCreateContext() returned error %d", eglGetError());
destroy();
return false;
}
if (!eglMakeCurrent(display, surface, surface, context)) {
Log::error("eglMakeCurrent() returned error %d", eglGetError());
destroy();
return false;
}
if (!eglQuerySurface(display, surface, EGL_WIDTH, &width)
|| !eglQuerySurface(display, surface, EGL_HEIGHT, &height)) {
Log::error("eglQuerySurface() returned error %d", eglGetError());
destroy();
return false;
}
//If Landscape
/*if (!eglQuerySurface(display, surface, EGL_WIDTH, &width) ||
!eglQuerySurface(display, surface, EGL_HEIGHT, &height)) {
Log::error("eglQuerySurface() returned error %d", eglGetError());
destroy();
return false;
}*/
//
_display = display;
_surface = surface;
_context = context;
} }
这用于绘制 rgb 原始数据: void Render::drawRGBData(unsigned char *rgb) {
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &gl_rgb_tex);
glBindTexture(GL_TEXTURE_2D, gl_rgb_tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 640, 480, 0, GL_RGB,
GL_UNSIGNED_BYTE, (GLubyte*) rgb);
//**Drawing***/
float tricoords[12] = { 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
1.0, 0.0 };
float texcoords[12] = { 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
1.0, 0.0 };
/*float tricoords[12] = { -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0,
1.0, -1.0 };
float texcoords[12] = { 0-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0,
1.0, -1.0 };*/
glBindTexture(GL_TEXTURE_2D, gl_rgb_tex);
glEnableClientState(GL_VERTEX_ARRAY); //enable gl vertex pointer to be used in draw fct
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, tricoords);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
glDrawArrays(GL_TRIANGLES, 0, 8);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
最后一个方法在循环中调用,对于每个新接收到的 rgb 数据。 我这样做是为了交换缓冲区:
if (!eglSwapBuffers(renderer->_display, renderer->_surface)) {
Log::error("eglSwapBuffers() returned error %d", eglGetError());
}
这是我在手机屏幕上看到的截图,分辨率为 1080x1920。
http://s1.postimg.org/hh4z4j2gv/Screenshot_2014_07_17_17_55_17_Convert_Image.jpg
问题出在绘制纹理的过程中,并将像素从原始数据正确映射到要显示的纹理。
【问题讨论】:
-
您提交了 6 个顶点,前 3 个是顺时针三角形,后 3 个不是有效三角形。您的 texcoords 似乎也不正确。当您只有 6 个索引时,您正在为 8 个索引调用 glDrawArrays... 注释掉的 tricoords 似乎是正确的。
标签: android c++ opengl-es texture2d