Skip to content

Commit

Permalink
[API] Up OpenGL impl
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaiR committed Dec 26, 2021
1 parent cbf8ecc commit 384e24a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 27 deletions.
12 changes: 10 additions & 2 deletions imgui-binding/src/main/java/imgui/ImDrawData.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public ImVec4 getCmdListCmdBufferClipRect(final int cmdListIdx, final int cmdBuf
*/

public ByteBuffer getCmdListIdxBufferData(final int cmdListIdx) {
final int idxBufferCapacity = getCmdListIdxBufferSize(cmdListIdx) * SIZEOF_IM_DRAW_IDX;
final int idxBufferCapacity = getCmdListIdxBufferSize(cmdListIdx) * sizeOfImDrawIdx();
if (dataBuffer.capacity() < idxBufferCapacity) {
dataBuffer.clear();
dataBuffer = ByteBuffer.allocateDirect(idxBufferCapacity + RESIZE_FACTOR).order(ByteOrder.nativeOrder());
Expand All @@ -123,7 +123,7 @@ public ByteBuffer getCmdListIdxBufferData(final int cmdListIdx) {
*/

public ByteBuffer getCmdListVtxBufferData(final int cmdListIdx) {
final int vtxBufferCapacity = getCmdListVtxBufferSize(cmdListIdx) * SIZEOF_IM_DRAW_VERT;
final int vtxBufferCapacity = getCmdListVtxBufferSize(cmdListIdx) * sizeOfImDrawVert();
if (dataBuffer.capacity() < vtxBufferCapacity) {
dataBuffer.clear();
dataBuffer = ByteBuffer.allocateDirect(vtxBufferCapacity + RESIZE_FACTOR).order(ByteOrder.nativeOrder());
Expand All @@ -141,6 +141,14 @@ public ByteBuffer getCmdListVtxBufferData(final int cmdListIdx) {
memcpy(vtxBuffer, IM_DRAW_DATA->CmdLists[cmdListIdx]->VtxBuffer.Data, vtxBufferCapacity);
*/

public static native int sizeOfImDrawVert(); /*
return (int)sizeof(ImDrawVert);
*/

public static native int sizeOfImDrawIdx(); /*
return (int)sizeof(ImDrawIdx);
*/

///////// End of Render Methods

/**
Expand Down
90 changes: 65 additions & 25 deletions imgui-lwjgl3/src/main/java/imgui/gl3/ImGuiImplGl3.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public void init() {
* 4.1 410 "#version 410 core"
* 4.2 420 "#version 410 core"
* 4.3 430 "#version 430 core"
* ES 3.0 300 "#version 300 es" = WebGL 2.0
* ---------------------------------------
* </pre>
* <p>
Expand Down Expand Up @@ -138,8 +139,14 @@ public void renderDrawData(final ImDrawData drawData) {

// Will project scissor/clipping rectangles into framebuffer space
drawData.getDisplaySize(displaySize); // (0,0) unless using multi-viewports
drawData.getDisplayPos(displayPos);
drawData.getFramebufferScale(framebufferScale); // (1,1) unless using retina display which are often (2,2)

final float clipOffX = displayPos.x;
final float clipOffY = displayPos.y;
final float clipScaleX = framebufferScale.x;
final float clipScaleY = framebufferScale.y;

// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
final int fbWidth = (int) (displaySize.x * framebufferScale.x);
final int fbHeight = (int) (displaySize.y * framebufferScale.y);
Expand All @@ -148,8 +155,6 @@ public void renderDrawData(final ImDrawData drawData) {
return;
}

drawData.getDisplayPos(displayPos);

backupGlState();
bind(fbWidth, fbHeight);

Expand All @@ -162,29 +167,31 @@ public void renderDrawData(final ImDrawData drawData) {
for (int cmdBufferIdx = 0; cmdBufferIdx < drawData.getCmdListCmdBufferSize(cmdListIdx); cmdBufferIdx++) {
drawData.getCmdListCmdBufferClipRect(cmdListIdx, cmdBufferIdx, clipRect);

final float clipRectX = (clipRect.x - displayPos.x) * framebufferScale.x;
final float clipRectY = (clipRect.y - displayPos.y) * framebufferScale.y;
final float clipRectZ = (clipRect.z - displayPos.x) * framebufferScale.x;
final float clipRectW = (clipRect.w - displayPos.y) * framebufferScale.y;

if (clipRectX < fbWidth && clipRectY < fbHeight && clipRectZ >= 0.0f && clipRectW >= 0.0f) {
// Apply scissor/clipping rectangle
glScissor((int) clipRectX, (int) (fbHeight - clipRectW), (int) (clipRectZ - clipRectX), (int) (clipRectW - clipRectY));

// Bind texture, Draw
final int textureId = drawData.getCmdListCmdBufferTextureId(cmdListIdx, cmdBufferIdx);
final int elemCount = drawData.getCmdListCmdBufferElemCount(cmdListIdx, cmdBufferIdx);
final int idxBufferOffset = drawData.getCmdListCmdBufferIdxOffset(cmdListIdx, cmdBufferIdx);
final int vtxBufferOffset = drawData.getCmdListCmdBufferVtxOffset(cmdListIdx, cmdBufferIdx);
final int indices = idxBufferOffset * ImDrawData.SIZEOF_IM_DRAW_IDX;

glBindTexture(GL_TEXTURE_2D, textureId);

if (glVersion >= 320) {
glDrawElementsBaseVertex(GL_TRIANGLES, elemCount, GL_UNSIGNED_SHORT, indices, vtxBufferOffset);
} else {
glDrawElements(GL_TRIANGLES, elemCount, GL_UNSIGNED_SHORT, indices);
}
final float clipMinX = (clipRect.x - clipOffX) * clipScaleX;
final float clipMinY = (clipRect.y - clipOffY) * clipScaleY;
final float clipMaxX = (clipRect.z - clipOffX) * clipScaleX;
final float clipMaxY = (clipRect.w - clipOffY) * clipScaleY;

if (clipMaxX <= clipMinX || clipMaxY <= clipMinY) {
continue;
}

// Apply scissor/clipping rectangle (Y is inverted in OpenGL)
glScissor((int) clipMinX, (int) (fbHeight - clipMaxY), (int) (clipMaxX - clipMinX), (int) (clipMaxY - clipMinY));

// Bind texture, Draw
final int textureId = drawData.getCmdListCmdBufferTextureId(cmdListIdx, cmdBufferIdx);
final int elemCount = drawData.getCmdListCmdBufferElemCount(cmdListIdx, cmdBufferIdx);
final int idxBufferOffset = drawData.getCmdListCmdBufferIdxOffset(cmdListIdx, cmdBufferIdx);
final int vtxBufferOffset = drawData.getCmdListCmdBufferVtxOffset(cmdListIdx, cmdBufferIdx);
final int indices = idxBufferOffset * ImDrawData.SIZEOF_IM_DRAW_IDX;

glBindTexture(GL_TEXTURE_2D, textureId);

if (glVersion >= 320) {
glDrawElementsBaseVertex(GL_TRIANGLES, elemCount, GL_UNSIGNED_SHORT, indices, vtxBufferOffset);
} else {
glDrawElements(GL_TRIANGLES, elemCount, GL_UNSIGNED_SHORT, indices);
}
}
}
Expand Down Expand Up @@ -288,6 +295,9 @@ private void createShaders() {
if (glslVersionValue < 130) {
vertShaderSource = getVertexShaderGlsl120();
fragShaderSource = getFragmentShaderGlsl120();
} else if (glslVersionValue == 300) {
vertShaderSource = getVertexShaderGlsl300es();
fragShaderSource = getFragmentShaderGlsl300es();
} else if (glslVersionValue >= 410) {
vertShaderSource = getVertexShaderGlsl410Core();
fragShaderSource = getFragmentShaderGlsl410Core();
Expand Down Expand Up @@ -484,6 +494,23 @@ private String getVertexShaderGlsl130() {
+ "}\n";
}

private String getVertexShaderGlsl300es() {
return glslVersion + "\n"
+ "precision highp float;\n"
+ "layout (location = 0) in vec2 Position;\n"
+ "layout (location = 1) in vec2 UV;\n"
+ "layout (location = 2) in vec4 Color;\n"
+ "uniform mat4 ProjMtx;\n"
+ "out vec2 Frag_UV;\n"
+ "out vec4 Frag_Color;\n"
+ "void main()\n"
+ "{\n"
+ " Frag_UV = UV;\n"
+ " Frag_Color = Color;\n"
+ " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
+ "}\n";
}

private String getVertexShaderGlsl410Core() {
return glslVersion + "\n"
+ "layout (location = 0) in vec2 Position;\n"
Expand Down Expand Up @@ -526,6 +553,19 @@ private String getFragmentShaderGlsl130() {
+ "}\n";
}

private String getFragmentShaderGlsl300es() {
return glslVersion + "\n"
+ "precision mediump float;\n"
+ "uniform sampler2D Texture;\n"
+ "in vec2 Frag_UV;\n"
+ "in vec4 Frag_Color;\n"
+ "layout (location = 0) out vec4 Out_Color;\n"
+ "void main()\n"
+ "{\n"
+ " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
+ "}\n";
}

private String getFragmentShaderGlsl410Core() {
return glslVersion + "\n"
+ "in vec2 Frag_UV;\n"
Expand Down

0 comments on commit 384e24a

Please sign in to comment.