made meshes tintable

This commit is contained in:
DaniTheSkunk 2022-11-29 09:18:06 +00:00
parent 80a28a15af
commit 18fc57b6a5
3 changed files with 24 additions and 11 deletions

View File

@ -2,6 +2,7 @@ package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.Vec2f; import com.danitheskunk.skunkworks.Vec2f;
import com.danitheskunk.skunkworks.Vec3f; import com.danitheskunk.skunkworks.Vec3f;
import com.danitheskunk.skunkworks.gfx.Color;
import com.danitheskunk.skunkworks.gfx.threedee.BaseRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.BaseRenderContext3D;
import com.danitheskunk.skunkworks.gfx.threedee.Mesh; import com.danitheskunk.skunkworks.gfx.threedee.Mesh;
@ -9,21 +10,33 @@ import static org.lwjgl.opengl.GL46.*;
public class RenderContext3D extends BaseRenderContext3D { public class RenderContext3D extends BaseRenderContext3D {
private final int texCoordIndex; private final int texCoordIndex;
private final int tintIndex;
private TextureAtlas textureAtlas; private TextureAtlas textureAtlas;
public RenderContext3D(int texCoordIndex) { public RenderContext3D(int texCoordIndex, int tintIndex) {
this.texCoordIndex = texCoordIndex; this.texCoordIndex = texCoordIndex;
this.tintIndex = tintIndex;
} }
@Override @Override
public void renderMesh(Mesh mesh) { public void renderMesh(Mesh mesh) {
renderMesh(mesh, Color.WHITE);
}
@Override
public void renderMesh(Mesh mesh, Color tint) {
var tex = (Texture) mesh.getTexture(); var tex = (Texture) mesh.getTexture();
var texArea = tex.getTexArea(); var texArea = tex.getTexArea();
var texOff = Vec2f.div(texArea.getPos(), textureAtlas.getSize()); var texOff = Vec2f.div(texArea.getPos(), textureAtlas.getSize());
var texMult = Vec2f.div(texArea.getSize(), textureAtlas.getSize()); var texMult = Vec2f.div(texArea.getSize(), textureAtlas.getSize());
glUniform4f(tintIndex,
tint.getR() / 255.0f,
tint.getG() / 255.0f,
tint.getB() / 255.0f,
tint.getA() / 255.0f
);
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
for(int i = 0; i < mesh.getTriangleCount(); ++i) { for(int i = 0; i < mesh.getTriangleCount(); ++i) {

View File

@ -40,10 +40,11 @@ public class Window extends BaseWindow {
#version 450 #version 450
layout(location = 1) in vec2 texCoord; layout(location = 1) in vec2 texCoord;
layout(binding = 0) uniform sampler2D tex; layout(binding = 0) uniform sampler2D tex;
layout(location = 5) uniform vec4 tint;
out vec4 color; out vec4 color;
void main() { void main() {
//color = vec4(vec2(texCoord).x/1000.f, 0.2f, 0.7f, 1.0f); //color = vec4(vec2(texCoord).x/1000.f, 0.2f, 0.7f, 1.0f);
color = texture(tex, texCoord); color = texture(tex, texCoord) * tint;
//color = vec4(0.0, texCoord.xy, 1.0); //color = vec4(0.0, texCoord.xy, 1.0);
//color = vec4(1.0, texCoord.x / 2, 1.0, 1.0); //color = vec4(1.0, texCoord.x / 2, 1.0, 1.0);
} }
@ -208,12 +209,11 @@ public class Window extends BaseWindow {
program.getUniformLocation("tint") program.getUniformLocation("tint")
); );
renderContext3D = new RenderContext3D(program.getAttribLocation( renderContext3D = new RenderContext3D(program3D.getAttribLocation(
"texCoord")); "texCoord"), program3D.getUniformLocation("tint"));
renderContext3D.setTextureAtlas(textureAtlas); renderContext3D.setTextureAtlas(textureAtlas);
glProgramUniform2f( glProgramUniform2f(program.getProgram(),
program.getProgram(),
program.getUniformLocation("windowSize"), program.getUniformLocation("windowSize"),
size.getX(), size.getX(),
size.getY() size.getY()
@ -312,8 +312,7 @@ public class Window extends BaseWindow {
private void mouseButtonCallback( private void mouseButtonCallback(
@SuppressWarnings("unused") long window, int button, int action, @SuppressWarnings("unused") long window, int button, int action, @SuppressWarnings(
@SuppressWarnings(
"unused") int mods "unused") int mods
) { ) {
switch(action) { switch(action) {
@ -385,8 +384,7 @@ public class Window extends BaseWindow {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, framebufferTex); glBindTexture(GL_TEXTURE_2D, framebufferTex);
glProgramUniform2f( glProgramUniform2f(programScaler.getProgram(),
programScaler.getProgram(),
programScaler.getUniformLocation("windowSize"), programScaler.getUniformLocation("windowSize"),
windowSize.getX(), windowSize.getX(),
windowSize.getY() windowSize.getY()

View File

@ -1,8 +1,10 @@
package com.danitheskunk.skunkworks.gfx.threedee; package com.danitheskunk.skunkworks.gfx.threedee;
import com.danitheskunk.skunkworks.Vec3f; import com.danitheskunk.skunkworks.Vec3f;
import com.danitheskunk.skunkworks.gfx.Color;
public interface IRenderContext3D { public interface IRenderContext3D {
void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3); void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3);
void renderMesh(Mesh mesh); void renderMesh(Mesh mesh);
void renderMesh(Mesh mesh, Color tint);
} }