@@ -42,7 +42,7 @@ START_NAMESPACE_DGL
4242
4343struct OpenGL3GraphicsContext : GraphicsContext
4444{
45- mutable int prog, color, pos;
45+ mutable int prog, color, pos, tex, texok ;
4646 mutable uint w, h;
4747};
4848
@@ -525,7 +525,37 @@ static void setupOpenGLImage(const OpenGLImage& image, GLuint textureId)
525525{
526526 DISTRHO_SAFE_ASSERT_RETURN (image.isValid (),);
527527
528+ const ImageFormat imageFormat = image.getFormat ();
529+ GLint intformat = GL_RGBA;
530+
531+ #ifdef DGL_USE_GLES2
532+ // GLESv2 does not support BGR
533+ DISTRHO_SAFE_ASSERT_RETURN (imageFormat != kImageFormatBGR && imageFormat != kImageFormatBGRA ,);
534+ #endif
535+
536+ #ifdef DGL_USE_OPENGL3
537+ switch (imageFormat)
538+ {
539+ case kImageFormatBGR :
540+ case kImageFormatRGB :
541+ intformat = GL_RGB;
542+ break ;
543+ case kImageFormatGrayscale :
544+ #if defined(DGL_USE_GLES3)
545+ intformat = GL_R8;
546+ #elif defined(DGL_USE_OPENGL3)
547+ intformat = GL_RED;
548+ #else
549+ intformat = GL_LUMINANCE;
550+ #endif
551+ break ;
552+ default :
553+ break ;
554+ }
555+ #else
528556 glEnable (GL_TEXTURE_2D);
557+ #endif
558+
529559 glBindTexture (GL_TEXTURE_2D, textureId);
530560
531561 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -538,14 +568,22 @@ static void setupOpenGLImage(const OpenGLImage& image, GLuint textureId)
538568
539569 glPixelStorei (GL_PACK_ALIGNMENT, 1 );
540570 glPixelStorei (GL_UNPACK_ALIGNMENT, 1 );
541- glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA,
571+
572+ glTexImage2D (GL_TEXTURE_2D,
573+ 0 ,
574+ intformat,
542575 static_cast <GLsizei>(image.getWidth ()),
543576 static_cast <GLsizei>(image.getHeight ()),
544577 0 ,
545- asOpenGLImageFormat (image.getFormat ()), GL_UNSIGNED_BYTE, image.getRawData ());
578+ asOpenGLImageFormat (imageFormat),
579+ GL_UNSIGNED_BYTE,
580+ image.getRawData ());
546581
547582 glBindTexture (GL_TEXTURE_2D, 0 );
583+
584+ #ifdef DGL_USE_COMPAT_OPENGL
548585 glDisable (GL_TEXTURE_2D);
586+ #endif
549587}
550588
551589#ifdef DGL_USE_COMPAT_OPENGL
@@ -611,28 +649,31 @@ static void drawOpenGLImage(const GraphicsContext& context,
611649
612650 const OpenGL3GraphicsContext& gl3context = static_cast <const OpenGL3GraphicsContext&>(context);
613651
614- // TODO implement this
615-
616652 const GLfloat color[4 ] = { 1 .0f , 1 .0f , 1 .0f , 1 .0f };
617653 glUniform4fv (gl3context.color , 1 , color);
618654
619- glEnable (GL_TEXTURE_2D);
620- glBindTexture (GL_TEXTURE_2D, textureId);
621-
622655 const GLfloat x = (static_cast <double >(pos.getX ()) / gl3context.w ) * 2 - 1 ;
623656 const GLfloat y = (static_cast <double >(pos.getY ()) / gl3context.h ) * -2 + 1 ;
624657 const GLfloat w = (static_cast <double >(image.getWidth ()) / gl3context.w ) * 2 ;
625658 const GLfloat h = (static_cast <double >(image.getHeight ()) / gl3context.h ) * -2 ;
626659
660+ glActiveTexture (GL_TEXTURE0);
661+ glBindTexture (GL_TEXTURE_2D, textureId);
662+ glUniform1i (gl3context.texok , 1 );
663+
627664 const GLfloat vertices[] = { x, y, x, y + h, x + w, y + h, x + w, y };
628665 glVertexAttribPointer (gl3context.pos , 2 , GL_FLOAT, GL_FALSE, 0 , vertices);
629666 glEnableVertexAttribArray (gl3context.pos );
630667
668+ const GLfloat vtex[] = { 0 .f , 0 .f , 0 .f , 1 .f , 1 .f , 1 .f , 1 .f , 0 .f };
669+ glVertexAttribPointer (gl3context.tex , 2 , GL_FLOAT, GL_FALSE, 0 , vtex);
670+ glEnableVertexAttribArray (gl3context.tex );
671+
631672 const GLubyte order[] = { 0 , 1 , 2 , 0 , 2 , 3 };
632673 glDrawElements (GL_TRIANGLES, ARRAY_SIZE (order), GL_UNSIGNED_BYTE, order);
633674
675+ glUniform1i (gl3context.texok , 0 );
634676 glBindTexture (GL_TEXTURE_2D, 0 );
635- glDisable (GL_TEXTURE_2D);
636677 #else
637678 drawOpenGLImage (image, pos, textureId, setupCalled);
638679
@@ -1054,7 +1095,10 @@ const GraphicsContext& Window::PrivateData::getGraphicsContext() const noexcept
10541095 static constexpr const char * const src = DGL_SHADER_HEADER
10551096 " precision mediump float;"
10561097 " uniform vec4 color;"
1057- " void main() { gl_FragColor = color; }" ;
1098+ " uniform sampler2D stex;"
1099+ " uniform bool texok;"
1100+ " varying vec2 vtex;"
1101+ " void main() { gl_FragColor = texok ? texture2D(stex, vtex) : color; }" ;
10581102
10591103 glShaderSource (fragment, 1 , &src, nullptr );
10601104 glCompileShader (fragment);
@@ -1066,7 +1110,9 @@ const GraphicsContext& Window::PrivateData::getGraphicsContext() const noexcept
10661110 {
10671111 static constexpr const char * const src = DGL_SHADER_HEADER
10681112 " attribute vec4 pos;"
1069- " void main() { gl_Position = pos; }" ;
1113+ " attribute vec2 tex;"
1114+ " varying vec2 vtex;"
1115+ " void main() { gl_Position = pos; vtex = tex; }" ;
10701116
10711117 glShaderSource (vertex, 1 , &src, nullptr );
10721118 glCompileShader (vertex);
@@ -1084,7 +1130,9 @@ const GraphicsContext& Window::PrivateData::getGraphicsContext() const noexcept
10841130
10851131 gl3context.prog = program;
10861132 gl3context.color = glGetUniformLocation (program, " color" );
1133+ gl3context.texok = glGetUniformLocation (program, " texok" );
10871134 gl3context.pos = glGetAttribLocation (program, " pos" );
1135+ gl3context.tex = glGetAttribLocation (program, " tex" );
10881136 }
10891137
10901138 const PuglArea size = puglGetSizeHint (view, PUGL_CURRENT_SIZE);
0 commit comments