Skip to content

Commit

Permalink
Model Viewer: Fix grid rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed May 21, 2024
1 parent c66a63a commit 163985c
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 262 deletions.
16 changes: 10 additions & 6 deletions modules/bundle-lwjgl/src/main/java/com/shade/gl/UniformMat4.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import com.shade.util.NotNull;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL20;
import org.lwjgl.system.MemoryStack;

import java.nio.FloatBuffer;

public class UniformMat4 extends Uniform<Matrix4fc> {
private final FloatBuffer buffer = BufferUtils.createFloatBuffer(16);

private UniformMat4(@NotNull String name, int program, int location) {
super(name, program, location);
}
Expand All @@ -23,12 +21,18 @@ public static UniformMat4 create(@NotNull ShaderProgram program, @NotNull String
@NotNull
@Override
public Matrix4fc get() {
GL20.glGetUniformfv(program, location, buffer);
return new Matrix4f(buffer);
try (MemoryStack stack = MemoryStack.stackPush()) {
final FloatBuffer buffer = stack.mallocFloat(16);
GL20.glGetUniformfv(program, location, buffer);
return new Matrix4f(buffer);
}
}

@Override
public void set(@NotNull Matrix4fc value) {
GL20.glUniformMatrix4fv(location, false, value.get(buffer));
try (MemoryStack stack = MemoryStack.stackPush()) {
final FloatBuffer buffer = stack.mallocFloat(16);
GL20.glUniformMatrix4fv(location, false, value.get(buffer));
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.shade.decima.model.viewer;

import java.nio.Buffer;
import java.util.Collection;

public interface Image {
enum ColorSpace {
LINEAR,
SRGB
}

interface Channel {
String getName(); // R, G, B, A, etc.

ColorSpace getColorSpace();

Buffer getData(); // ByteBuffer, ShortBuffer, etc.
}

String getFormatName();

int getWidth();

int getHeight();

Collection<Channel> getChannels();

// ImageDescriptor(channels=[
// (name=R, colorSpace=SRGB, type=uint8),
// (name=G, colorSpace=SRGB, type=uint8),
// (name=B, colorSpace=SRGB, type=uint8),
// (name=A, colorSpace=LINEAR, type=uint8)
// ])
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.shade.decima.model.viewer.isr.Node;
import com.shade.decima.model.viewer.isr.impl.NodeModel;
import com.shade.decima.model.viewer.outline.OutlineDialog;
import com.shade.decima.model.viewer.renderer.GridRenderer;
import com.shade.decima.model.viewer.renderer.ModelRenderer;
import com.shade.decima.model.viewer.renderer.OutlineRenderer;
import com.shade.decima.model.viewer.renderer.ViewportRenderer;
import com.shade.gl.DebugGroup;
import com.shade.platform.model.Disposable;
import com.shade.platform.model.data.DataKey;
Expand Down Expand Up @@ -39,7 +39,7 @@ public class ModelViewport extends AWTGLCanvas implements Disposable {

private final Handler handler;
private final OutlineRenderer outlineRenderer;
private final ViewportRenderer viewportRenderer;
private final GridRenderer gridRenderer;
private final ModelRenderer modelRenderer;
private final Camera camera;

Expand All @@ -63,7 +63,7 @@ public ModelViewport(@NotNull Camera camera) {

this.handler = new Handler(robot);
this.outlineRenderer = new OutlineRenderer();
this.viewportRenderer = new ViewportRenderer();
this.gridRenderer = new GridRenderer();
this.modelRenderer = new ModelRenderer();
this.camera = camera;

Expand All @@ -90,16 +90,20 @@ public void initGL() {
GL.createCapabilities();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);

glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);

glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, true);
glDebugMessageControl(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PUSH_GROUP, GL_DONT_CARE, 0, false);
glDebugMessageControl(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_POP_GROUP, GL_DONT_CARE, 0, false);
glDebugMessageCallback(new DebugCallback(), 0);

try {
outlineRenderer.setup();
viewportRenderer.setup();
gridRenderer.setup();
modelRenderer.setup();
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand All @@ -122,31 +126,30 @@ public void paintGL() {
camera.update(delta, handler);
handler.clear();

try (var ignored = new DebugGroup("viewport")) {
viewportRenderer.update(delta, this);
}

outlineRenderer.bind(width, height);

try (var ignored = new DebugGroup("model")) {
try (var ignored = new DebugGroup("Render Model")) {
modelRenderer.setSelectionOnly(true);
modelRenderer.update(delta, this);
modelRenderer.render(delta, this);

modelRenderer.setSelectionOnly(false);
modelRenderer.update(delta, this);
modelRenderer.render(delta, this);
}

outlineRenderer.unbind();
outlineRenderer.update(delta, this);
try (var ignored = new DebugGroup("Render Grid")) {
gridRenderer.render(delta, this);
}

outlineRenderer.unbind();
outlineRenderer.render(delta, this);
lastFrameTime = currentFrameTime;
swapBuffers();
}

@Override
public void dispose() {
outlineRenderer.dispose();
viewportRenderer.dispose();
gridRenderer.dispose();
modelRenderer.dispose();

if (outlineDialog != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
public interface Renderer extends Disposable {
void setup() throws IOException;

void update(float dt, @NotNull ModelViewport viewport);
void render(float dt, @NotNull ModelViewport viewport);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.shade.decima.model.viewer.renderer;

import com.shade.decima.model.viewer.Camera;
import com.shade.decima.model.viewer.ModelViewport;
import com.shade.decima.model.viewer.shader.GridShaderProgram;
import com.shade.platform.model.Disposable;
import com.shade.util.NotNull;

import java.io.IOException;

import static org.lwjgl.opengl.GL11.*;

public class GridRenderer extends QuadRenderer {
private GridShaderProgram program;

@Override
public void setup() throws IOException {
super.setup();

program = new GridShaderProgram();
}

@Override
public void render(float dt, @NotNull ModelViewport viewport) {
final Camera camera = viewport.getCamera();

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

try (var program = (GridShaderProgram) this.program.bind()) {
program.getView().set(camera.getViewMatrix());
program.getProjection().set(camera.getProjectionMatrix());

super.render(dt, viewport);
}

glDisable(GL_BLEND);
}

@Override
public void dispose() {
super.dispose();

Disposable.dispose(program);
program = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ public void setup() throws IOException {
}

@Override
public void update(float dt, @NotNull ModelViewport viewport) {
public void render(float dt, @NotNull ModelViewport viewport) {
if (model == null) {
return;
}

glEnable(GL_DEPTH_TEST);

final Camera camera = viewport.getCamera();
final Matrix4fc viewMatrix = camera.getViewMatrix();
final Matrix4fc projectionMatrix = camera.getProjectionMatrix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ public void bind(int width, int height) {

public void unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_BLEND);
}

@Override
public void update(float dt, @NotNull ModelViewport viewport) {
public void render(float dt, @NotNull ModelViewport viewport) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorTextureId);

Expand All @@ -94,7 +95,7 @@ public void update(float dt, @NotNull ModelViewport viewport) {
program.colorSampler.set(0);
program.maskSampler.set(1);

super.update(dt, viewport);
super.render(dt, viewport);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void setup() throws IOException {
}

@Override
public void update(float dt, @NotNull ModelViewport viewport) {
public void render(float dt, @NotNull ModelViewport viewport) {
try (VAO ignored = vao.bind()) {
GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, 4);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.shade.decima.model.viewer.shader;

import com.shade.gl.Attribute;
import com.shade.gl.Shader;
import com.shade.gl.Shader.Type;
import com.shade.gl.ShaderProgram;
import com.shade.gl.UniformMat4;
import com.shade.util.NotNull;

import java.io.IOException;
import java.util.Map;

public class GridShaderProgram extends ShaderProgram {
private final UniformMat4 view;
private final UniformMat4 projection;

public GridShaderProgram() throws IOException {
super(
Map.of(
Shader.fromResource("grid.vert"), Type.VERTEX,
Shader.fromResource("grid.frag"), Type.FRAGMENT
),
Map.of(
"in_position", Attribute.Semantic.POSITION
)
);

this.view = UniformMat4.create(this, "view");
this.projection = UniformMat4.create(this, "projection");
}

@NotNull
public UniformMat4 getView() {
return view;
}

@NotNull
public UniformMat4 getProjection() {
return projection;
}
}
Loading

0 comments on commit 163985c

Please sign in to comment.