Skip to content

Commit 7a79c96

Browse files
committed
opengl3: implement base image drawing
Signed-off-by: falkTX <falktx@falktx.com>
1 parent a1f7bc3 commit 7a79c96

File tree

4 files changed

+68
-20
lines changed

4 files changed

+68
-20
lines changed

dgl/OpenGL.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* DISTRHO Plugin Framework (DPF)
3-
* Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
3+
* Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
44
*
55
* Permission to use, copy, modify, and/or distribute this software for any purpose with
66
* or without fee is hereby granted, provided that the above copyright notice and this
@@ -42,11 +42,11 @@ ImageFormat asDISTRHOImageFormat(const GLenum format)
4242
{
4343
switch (format)
4444
{
45-
#ifdef DGL_USE_OPENGL3
45+
#if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
4646
case GL_RED:
47-
#else
47+
#else
4848
case GL_LUMINANCE:
49-
#endif
49+
#endif
5050
return kImageFormatGrayscale;
5151
case GL_BGR:
5252
return kImageFormatBGR;
@@ -69,11 +69,11 @@ GLenum asOpenGLImageFormat(const ImageFormat format)
6969
case kImageFormatNull:
7070
break;
7171
case kImageFormatGrayscale:
72-
#ifdef DGL_USE_OPENGL3
72+
#if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
7373
return GL_RED;
74-
#else
74+
#else
7575
return GL_LUMINANCE;
76-
#endif
76+
#endif
7777
case kImageFormatBGR:
7878
return GL_BGR;
7979
case kImageFormatBGRA:

dgl/src/OpenGL.cpp

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ START_NAMESPACE_DGL
4242

4343
struct 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);

dgl/src/WindowPrivateData.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct Window::PrivateData : IdleCallback {
4545
PuglView* view;
4646

4747
/** Reserved space for graphics context. */
48-
mutable uint8_t graphicsContext[sizeof(int) * 5];
48+
mutable uint8_t graphicsContext[sizeof(int) * 7];
4949

5050
/** The top-level widgets associated with this Window. */
5151
std::list<TopLevelWidget*> topLevelWidgets;

dgl/src/nanovg/nanovg_gl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int im
846846
#endif
847847
break;
848848
case NVG_TEXTURE_RGB:
849-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
849+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
850850
break;
851851
case NVG_TEXTURE_RGBA:
852852
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

0 commit comments

Comments
 (0)