63 unsigned char const * data)
65 if (width == 0 || height == 0)
LFATAL(
"Cannot handle zero image width or height");
70 char const * frag_shader;
73 case V4L2_PIX_FMT_YUYV:
76 itsGLtextureFmt = GL_RGBA;
79 case V4L2_PIX_FMT_RGB32:
82 itsGLtextureFmt = GL_RGBA;
85 case V4L2_PIX_FMT_GREY:
88 itsGLtextureFmt = GL_LUMINANCE;
91 case V4L2_PIX_FMT_RGB24:
94 itsGLtextureFmt = GL_LUMINANCE;
97 case V4L2_PIX_FMT_BGR24:
98 case V4L2_PIX_FMT_SRGGB8:
99 case V4L2_PIX_FMT_RGB565:
100 case V4L2_PIX_FMT_MJPEG:
101 case V4L2_PIX_FMT_UYVY:
102 case V4L2_PIX_FMT_SBGGR16:
103 case V4L2_PIX_FMT_SGRBG16:
104 case V4L2_PIX_FMT_NV12:
105 case V4L2_PIX_FMT_YUV444:
114 itsLocation = glGetUniformLocation(itsProgram->id(),
"s_texture");
118 if (width != itsTextureWidth || height != itsTextureHeight || fmt != itsFormat || !itsTexture)
121 if (width != itsTextureWidth)
125 case V4L2_PIX_FMT_YUYV: itsGLtextureWidth = width / 2;
break;
128 case V4L2_PIX_FMT_RGB32: itsGLtextureWidth = width;
break;
131 case V4L2_PIX_FMT_GREY: itsGLtextureWidth = width;
break;
134 case V4L2_PIX_FMT_RGB24: itsGLtextureWidth = width * 3;
break;
140 itsTexture.reset(
new jevois::GPUtexture(itsGLtextureWidth, height, itsGLtextureFmt,
false));
141 LDEBUG(
"Input texture for " << width <<
'x' << height <<
' ' <<
jevois::fccstr(fmt) <<
" ready.");
144 itsTextureWidth = width; itsTextureHeight = height; itsFormat = fmt;
147#ifdef JEVOIS_PLATFORM_PRO
148 if (itsTextureDmaBuf) itsTextureDmaBuf.reset();
152 itsTexture->setPixels(data);
283 glm::mat4
const & pvm)
285 if (itsTextureWidth == 0)
throw std::runtime_error(
"You must call set() before draw()");
289 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
293 glGetIntegerv(GL_VIEWPORT, viewport);
295 if (w == 0 ||
h == 0)
298 unsigned int winw = viewport[2], winh = viewport[3];
299 itsDrawWidth = itsTextureWidth; itsDrawHeight = itsTextureHeight;
301 x = (winw - itsDrawWidth) / 2; y = (winh - itsDrawHeight) / 2; w = itsDrawWidth;
h = itsDrawHeight;
306 int const yy = viewport[3] - y -
h;
309 if (itsDrawX != x || itsDrawY != y || itsDrawWidth != w || itsDrawHeight !=
h)
314 float const tx = x - 0.5 * viewport[2];
315 float const ty = yy - 0.5 * viewport[3];
316 float const bx = tx + w;
317 float const by = ty +
h;
322 GLfloat
const vertices[] = { tx, by, 0.0f, 0.0f, 0.0f,
323 tx, ty, 0.0f, 0.0f, 1.0f,
324 bx, ty, 0.0f, 1.0f, 1.0f,
325 bx, by, 0.0f, 1.0f, 0.0f };
326 static GLushort
const indices[] = { 0, 1, 2, 0, 2, 3 };
328 if (itsVertexArray) { glDeleteVertexArrays(1, &itsVertexArray); glDeleteBuffers(2, itsVertexBuffers); }
329 glGenVertexArrays(1, &itsVertexArray);
332 glBindVertexArray(itsVertexArray);
335 if (itsVertexBuffers[0]) glDeleteBuffers(2, itsVertexBuffers);
336 glGenBuffers(2, itsVertexBuffers);
339 glBindBuffer(GL_ARRAY_BUFFER, itsVertexBuffers[0]);
340 glBufferData(GL_ARRAY_BUFFER,
sizeof(vertices), vertices, GL_STATIC_DRAW);
343 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, itsVertexBuffers[1]);
344 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(indices), indices, GL_STATIC_DRAW);
347 glEnableVertexAttribArray(0);
348 glEnableVertexAttribArray(1);
351 GL_CHECK(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(GLfloat), 0));
354 GL_CHECK(glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(GLfloat), (
void const *)(3 *
sizeof(GLfloat))));
357 glBindVertexArray(0);
360 itsDrawX = x; itsDrawY = y; itsDrawWidth = w; itsDrawHeight =
h;
363 if (!itsProgram)
throw std::runtime_error(
"You must call set() before draw()");
366 glUseProgram(itsProgram->id());
369 glBindVertexArray(itsVertexArray);
372 GL_CHECK(glActiveTexture(GL_TEXTURE0));
374 GL_CHECK(glBindTexture(GL_TEXTURE_2D, itsTexture->Id));
375#ifdef JEVOIS_PLATFORM_PRO
376 else if (itsTextureDmaBuf)
377 GL_CHECK(glBindTexture(GL_TEXTURE_EXTERNAL_OES, itsTextureDmaBuf->Id));
379 else throw std::runtime_error(
"You must call set() before draw()");
382 glUniform1i(itsLocation, 0);
386 glUniform2f(glGetUniformLocation(itsProgram->id(),
"tdim"), GLfloat(itsTextureWidth), GLfloat(itsTextureHeight));
387 glUniformMatrix4fv(glGetUniformLocation(itsProgram->id(),
"pvm"), 1, GL_FALSE, glm::value_ptr(pvm));
391 glUniform1f(glGetUniformLocation(itsProgram->id(),
"twirlamount"), itsTwirl);
392 glUniform1f(glGetUniformLocation(itsProgram->id(),
"alpha"), itsAlpha);
399 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
402 glBindVertexArray(0);
Simple class to hold an OpenGL texture with in-GPU pixel format conversion and DMABUF acceleration.
Simple class to hold an OpenGL texture.