Browse Source

opengl3: implement base image drawing

Signed-off-by: falkTX <falktx@falktx.com>
pull/506/head
falkTX 6 months ago
parent
commit
7a79c968a5
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
4 changed files with 68 additions and 20 deletions
  1. +7
    -7
      dgl/OpenGL.hpp
  2. +59
    -11
      dgl/src/OpenGL.cpp
  3. +1
    -1
      dgl/src/WindowPrivateData.hpp
  4. +1
    -1
      dgl/src/nanovg/nanovg_gl.h

+ 7
- 7
dgl/OpenGL.hpp View File

@@ -1,6 +1,6 @@
/*
* DISTRHO Plugin Framework (DPF)
* Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
@@ -42,11 +42,11 @@ ImageFormat asDISTRHOImageFormat(const GLenum format)
{
switch (format)
{
#ifdef DGL_USE_OPENGL3
#if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
case GL_RED:
#else
#else
case GL_LUMINANCE:
#endif
#endif
return kImageFormatGrayscale;
case GL_BGR:
return kImageFormatBGR;
@@ -69,11 +69,11 @@ GLenum asOpenGLImageFormat(const ImageFormat format)
case kImageFormatNull:
break;
case kImageFormatGrayscale:
#ifdef DGL_USE_OPENGL3
#if defined(DGL_USE_OPENGL3) && !defined(DGL_USE_GLES2)
return GL_RED;
#else
#else
return GL_LUMINANCE;
#endif
#endif
case kImageFormatBGR:
return GL_BGR;
case kImageFormatBGRA:


+ 59
- 11
dgl/src/OpenGL.cpp View File

@@ -42,7 +42,7 @@ START_NAMESPACE_DGL

struct OpenGL3GraphicsContext : GraphicsContext
{
mutable int prog, color, pos;
mutable int prog, color, pos, tex, texok;
mutable uint w, h;
};

@@ -525,7 +525,37 @@ static void setupOpenGLImage(const OpenGLImage& image, GLuint textureId)
{
DISTRHO_SAFE_ASSERT_RETURN(image.isValid(),);

const ImageFormat imageFormat = image.getFormat();
GLint intformat = GL_RGBA;

#ifdef DGL_USE_GLES2
// GLESv2 does not support BGR
DISTRHO_SAFE_ASSERT_RETURN(imageFormat != kImageFormatBGR && imageFormat != kImageFormatBGRA,);
#endif

#ifdef DGL_USE_OPENGL3
switch (imageFormat)
{
case kImageFormatBGR:
case kImageFormatRGB:
intformat = GL_RGB;
break;
case kImageFormatGrayscale:
#if defined(DGL_USE_GLES3)
intformat = GL_R8;
#elif defined(DGL_USE_OPENGL3)
intformat = GL_RED;
#else
intformat = GL_LUMINANCE;
#endif
break;
default:
break;
}
#else
glEnable(GL_TEXTURE_2D);
#endif

glBindTexture(GL_TEXTURE_2D, textureId);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -538,14 +568,22 @@ static void setupOpenGLImage(const OpenGLImage& image, GLuint textureId)

glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,

glTexImage2D(GL_TEXTURE_2D,
0,
intformat,
static_cast<GLsizei>(image.getWidth()),
static_cast<GLsizei>(image.getHeight()),
0,
asOpenGLImageFormat(image.getFormat()), GL_UNSIGNED_BYTE, image.getRawData());
asOpenGLImageFormat(imageFormat),
GL_UNSIGNED_BYTE,
image.getRawData());

glBindTexture(GL_TEXTURE_2D, 0);

#ifdef DGL_USE_COMPAT_OPENGL
glDisable(GL_TEXTURE_2D);
#endif
}

#ifdef DGL_USE_COMPAT_OPENGL
@@ -611,28 +649,31 @@ static void drawOpenGLImage(const GraphicsContext& context,

const OpenGL3GraphicsContext& gl3context = static_cast<const OpenGL3GraphicsContext&>(context);

// TODO implement this

const GLfloat color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
glUniform4fv(gl3context.color, 1, color);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureId);

const GLfloat x = (static_cast<double>(pos.getX()) / gl3context.w) * 2 - 1;
const GLfloat y = (static_cast<double>(pos.getY()) / gl3context.h) * -2 + 1;
const GLfloat w = (static_cast<double>(image.getWidth()) / gl3context.w) * 2;
const GLfloat h = (static_cast<double>(image.getHeight()) / gl3context.h) * -2;

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureId);
glUniform1i(gl3context.texok, 1);

const GLfloat vertices[] = { x, y, x, y + h, x + w, y + h, x + w, y };
glVertexAttribPointer(gl3context.pos, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(gl3context.pos);

const GLfloat vtex[] = { 0.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f, 0.f };
glVertexAttribPointer(gl3context.tex, 2, GL_FLOAT, GL_FALSE, 0, vtex);
glEnableVertexAttribArray(gl3context.tex);

const GLubyte order[] = { 0, 1, 2, 0, 2, 3 };
glDrawElements(GL_TRIANGLES, ARRAY_SIZE(order), GL_UNSIGNED_BYTE, order);

glUniform1i(gl3context.texok, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
#else
drawOpenGLImage(image, pos, textureId, setupCalled);

@@ -1054,7 +1095,10 @@ const GraphicsContext& Window::PrivateData::getGraphicsContext() const noexcept
static constexpr const char* const src = DGL_SHADER_HEADER
"precision mediump float;"
"uniform vec4 color;"
"void main() { gl_FragColor = color; }";
"uniform sampler2D stex;"
"uniform bool texok;"
"varying vec2 vtex;"
"void main() { gl_FragColor = texok ? texture2D(stex, vtex) : color; }";

glShaderSource(fragment, 1, &src, nullptr);
glCompileShader(fragment);
@@ -1066,7 +1110,9 @@ const GraphicsContext& Window::PrivateData::getGraphicsContext() const noexcept
{
static constexpr const char* const src = DGL_SHADER_HEADER
"attribute vec4 pos;"
"void main() { gl_Position = pos; }";
"attribute vec2 tex;"
"varying vec2 vtex;"
"void main() { gl_Position = pos; vtex = tex; }";

glShaderSource(vertex, 1, &src, nullptr);
glCompileShader(vertex);
@@ -1084,7 +1130,9 @@ const GraphicsContext& Window::PrivateData::getGraphicsContext() const noexcept

gl3context.prog = program;
gl3context.color = glGetUniformLocation(program, "color");
gl3context.texok = glGetUniformLocation(program, "texok");
gl3context.pos = glGetAttribLocation(program, "pos");
gl3context.tex = glGetAttribLocation(program, "tex");
}

const PuglArea size = puglGetSizeHint(view, PUGL_CURRENT_SIZE);


+ 1
- 1
dgl/src/WindowPrivateData.hpp View File

@@ -45,7 +45,7 @@ struct Window::PrivateData : IdleCallback {
PuglView* view;

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

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


+ 1
- 1
dgl/src/nanovg/nanovg_gl.h View File

@@ -846,7 +846,7 @@ static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int im
#endif
break;
case NVG_TEXTURE_RGB:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
break;
case NVG_TEXTURE_RGBA:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);


Loading…
Cancel
Save