Compare commits

..

3 Commits

Author SHA1 Message Date
DaniTheSkunk 007be3e9f3 started adding bone stuff 2022-12-12 02:06:58 +00:00
DaniTheSkunk db610763ac added Gamepad 2022-12-12 02:06:44 +00:00
DaniTheSkunk 9fa8eb11bb framebuffer class 2022-12-12 02:06:12 +00:00
14 changed files with 336 additions and 63 deletions

View File

@ -6,6 +6,7 @@ import com.danitheskunk.skunkworks.audio.nodes.Mixer;
import com.danitheskunk.skunkworks.audio.nodes.Node; import com.danitheskunk.skunkworks.audio.nodes.Node;
import com.danitheskunk.skunkworks.audio.nodes.SamplePlayer; import com.danitheskunk.skunkworks.audio.nodes.SamplePlayer;
import com.danitheskunk.skunkworks.audio.nodes.TTS; import com.danitheskunk.skunkworks.audio.nodes.TTS;
import com.danitheskunk.skunkworks.backends.gl.Gamepad;
import com.danitheskunk.skunkworks.gfx.IRenderContext; import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture; import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.font.IFont; import com.danitheskunk.skunkworks.gfx.font.IFont;
@ -34,6 +35,7 @@ public abstract class BaseGame {
protected final SamplePlayer samplePlayer; protected final SamplePlayer samplePlayer;
protected final TTS tts; protected final TTS tts;
protected final IWindow window; protected final IWindow window;
protected final BaseGamepad gamepad;
/** /**
* Create with given window size and title * Create with given window size and title
@ -52,6 +54,7 @@ public abstract class BaseGame {
engine = new Engine(); engine = new Engine();
window = engine.openWindow(windowSize, windowTitle); window = engine.openWindow(windowSize, windowTitle);
gamepad = window.getGamepad(0);
//todo: load from .jar //todo: load from .jar
debugFont = window.loadFontTileset("fonts/ega-8x14.png"); debugFont = window.loadFontTileset("fonts/ega-8x14.png");
rootNode = new NodeRoot(); rootNode = new NodeRoot();
@ -153,6 +156,7 @@ public abstract class BaseGame {
currentFrameTime += delta; currentFrameTime += delta;
engine.tick(); engine.tick();
window.tick(); window.tick();
gamepad.tick();
//todo: frame rate control //todo: frame rate control
if(currentFrameTime >= 1.0 / 60.0) { if(currentFrameTime >= 1.0 / 60.0) {
rootNode.tick(); rootNode.tick();

View File

@ -0,0 +1,57 @@
package com.danitheskunk.skunkworks;
public abstract class BaseGamepad {
protected final float[] axes;
protected final boolean[] buttonDown;
protected final boolean[] buttonPressed;
protected final int axesCount;
protected final int buttonCount;
public BaseGamepad(int axesCount, int buttonCount) {
this.axesCount = axesCount;
this.buttonCount = buttonCount;
axes = new float[axesCount];
buttonDown = new boolean[buttonCount];
buttonPressed = new boolean[buttonCount];
}
public int getAxesCount() {
return axesCount;
}
public int getButtonCount() {
return buttonCount;
}
public float getAxis(int i) {
return axes[i];
}
public boolean getButtonDown(int i) {
return buttonDown[i];
}
public boolean getButtonPressed(int i) {
return buttonPressed[i];
}
public void setAxis(int i, float value) {
axes[i] = value;
}
public void setButtonDown(int i, boolean value) {
buttonDown[i] = value;
}
public void setButtonPressed(int i, boolean value) {
buttonPressed[i] = value;
}
public void clearButtonPressed() {
for(int i = 0; i < buttonCount; ++i) {
buttonPressed[i] = false;
}
}
public abstract void tick();
}

View File

@ -1,6 +1,10 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.gfx.*; import com.danitheskunk.skunkworks.backends.gl.Gamepad;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image;
import com.danitheskunk.skunkworks.gfx.NineSlice;
import com.danitheskunk.skunkworks.gfx.font.IFont; import com.danitheskunk.skunkworks.gfx.font.IFont;
import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D;
@ -9,6 +13,8 @@ import java.util.List;
public interface IWindow { public interface IWindow {
Engine getEngine(); Engine getEngine();
BaseGamepad getGamepad(int id);
Vec2i getMousePos(); Vec2i getMousePos();
boolean isMouseClicked(int button); boolean isMouseClicked(int button);

View File

@ -1,8 +1,12 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
import org.lwjgl.assimp.AIMatrix4x4;
public final class Mat4f { public final class Mat4f {
private final double[] data; private final double[] data;
public static Mat4f IDENTITY = new Mat4f();
public Mat4f() { public Mat4f() {
data = new double[16]; data = new double[16];
set(0, 0, 1.0); set(0, 0, 1.0);
@ -11,6 +15,32 @@ public final class Mat4f {
set(3, 3, 1.0); set(3, 3, 1.0);
} }
public static Mat4f fromAiMat(AIMatrix4x4 aiMat) {
var mat = new Mat4f();
mat.set(0, 0, aiMat.a1());
mat.set(1, 0, aiMat.a2());
mat.set(2, 0, aiMat.a3());
mat.set(3, 0, aiMat.a4());
mat.set(0, 1, aiMat.b1());
mat.set(1, 1, aiMat.b2());
mat.set(2, 1, aiMat.b3());
mat.set(3, 1, aiMat.b4());
mat.set(0, 2, aiMat.c1());
mat.set(1, 2, aiMat.c2());
mat.set(2, 2, aiMat.c3());
mat.set(3, 2, aiMat.c4());
mat.set(0, 3, aiMat.d1());
mat.set(1, 3, aiMat.d2());
mat.set(2, 3, aiMat.d3());
mat.set(3, 3, aiMat.d4());
return mat;
}
public static Mat4f perspective( public static Mat4f perspective(
double fovy, double aspect, double zNear, double zFar double fovy, double aspect, double zNear, double zFar
) { ) {

View File

@ -15,14 +15,6 @@ public class Test extends BaseGame {
public Test() { public Test() {
super(new Vec2i(40 * 12, 22 * 12), "Skunkworks"); super(new Vec2i(40 * 12, 22 * 12), "Skunkworks");
var path = "C:\\Program Files (x86)" +
"\\Steam\\steamapps\\common\\Unreal " +
"Tournament\\System\\UnrealTournament.ini";
var data = engine.loadDataWatched(path);
data.onReload(() -> System.out.println(data.get("URL", "Host")));
var fontThin = window.loadFontTileset("fonts\\thin-6x12.png"); var fontThin = window.loadFontTileset("fonts\\thin-6x12.png");
var fontThin2 = window.loadFontTileset("fonts\\thin-12x12.png"); var fontThin2 = window.loadFontTileset("fonts\\thin-12x12.png");
term = new Terminal(new Vec2i(40, 22), fontThin2, fontThin); term = new Terminal(new Vec2i(40, 22), fontThin2, fontThin);

View File

@ -2,10 +2,12 @@ package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.audio.ISample; import com.danitheskunk.skunkworks.audio.ISample;
import com.danitheskunk.skunkworks.nodes.NodeSprite; import com.danitheskunk.skunkworks.nodes.NodeSprite;
import org.lwjgl.glfw.GLFW;
public class TestNode extends BaseGame { public class TestNode extends BaseGame {
private final ISample kick; private final ISample kick;
private final NodeSprite sprite; private final NodeSprite sprite;
float[] axes;
public TestNode() { public TestNode() {
super(new Vec2i(1280, 720), "Skunkworks"); super(new Vec2i(1280, 720), "Skunkworks");
@ -15,13 +17,26 @@ public class TestNode extends BaseGame {
sprite.setTexture(loadTexture("demoassets/test.png")); sprite.setTexture(loadTexture("demoassets/test.png"));
sprite.setPos(new Vec2f(100, 100)); sprite.setPos(new Vec2f(100, 100));
rootNode.add(sprite); rootNode.add(sprite);
//doThing();
}
void doThing() {
sprite.tweenPos(new Vec2f(800, 400), 120).delay(60).then(() -> { sprite.tweenPos(new Vec2f(800, 400), 120).delay(60).then(() -> {
System.out.println("yay! got there! now lets go home"); System.out.println("yay! got there! now lets go home");
sprite.tweenPos(new Vec2f(100, 100), 120); sprite.tweenPos(new Vec2f(100, 100), 120).then(this::doThing);
playSample(kick); playSample(kick);
}); });
} }
@Override
protected void update(double delta) {
sprite.setPos(new Vec2f(640 + 640 * gamepad.getAxis(0), 360 + 360 * gamepad.getAxis(1)));
if(gamepad.getButtonPressed(0)) {
playSample(kick);
}
}
public static void main(String[] args) { public static void main(String[] args) {
new TestNode().run(); new TestNode().run();
} }

View File

@ -0,0 +1,5 @@
package com.danitheskunk.skunkworks.backends.gl;
public class BasePipeline {
}

View File

@ -0,0 +1,69 @@
package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.Vec2i;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL32.glFramebufferTexture;
public class Framebuffer {
private final int framebuffer;
private final int framebufferTex;
public Framebuffer(Vec2i size) {
framebuffer = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
framebufferTex = glGenTextures();
glBindTexture(GL_TEXTURE_2D, framebufferTex);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
size.getX(),
size.getY(),
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
0
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
framebufferTex,
0
);
int framebufferDepthTex = glGenTextures();
glBindTexture(GL_TEXTURE_2D, framebufferDepthTex);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_DEPTH_COMPONENT,
size.getX(),
size.getY(),
0,
GL_DEPTH_COMPONENT,
GL_UNSIGNED_BYTE,
0
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture(GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
framebufferDepthTex,
0
);
}
public void bind() {
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
}
public void bindTexture() {
glBindTexture(GL_TEXTURE_2D, framebufferTex);
}
public static void unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
}

View File

@ -0,0 +1,31 @@
package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.BaseGamepad;
import org.lwjgl.glfw.GLFW;
public class Gamepad extends BaseGamepad {
private final int jid;
public Gamepad(int jid) {
super(GLFW.glfwGetJoystickAxes(jid).limit(), GLFW.glfwGetJoystickButtons(jid).limit());
if(!GLFW.glfwJoystickPresent(jid) || !GLFW.glfwJoystickIsGamepad(jid)) {
throw new RuntimeException("gamepad not found");
}
this.jid = jid;
}
@Override
public void tick() {
var axesBuf = GLFW.glfwGetJoystickAxes(jid);
var buttonBuf = GLFW.glfwGetJoystickButtons(jid);
for(int i = 0; i < axesCount; ++i) {
axes[i] = axesBuf.get(i);
}
for(int i = 0; i < buttonCount; ++i) {
var val = buttonBuf.get() != 0;
buttonPressed[i] = !buttonDown[i] && val;
buttonDown[i] = val;
}
}
}

View File

@ -98,8 +98,7 @@ public class Window extends BaseWindow {
out_texCoord = texCoord; out_texCoord = texCoord;
} }
"""; """;
private final int framebuffer; private final Framebuffer framebuffer;
private final int framebufferTex;
private final Program program; private final Program program;
private final Program program3D; private final Program program3D;
private final Program programScaler; private final Program programScaler;
@ -140,7 +139,6 @@ public class Window extends BaseWindow {
GL.createCapabilities(); GL.createCapabilities();
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
//glOrtho(0.0f, size.getX(), size.getY(), 0.0f, 0.0f, 1.0f);
glEnable(GL_COLOR); glEnable(GL_COLOR);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -150,51 +148,7 @@ public class Window extends BaseWindow {
glfwSetWindowSizeCallback(window, this::windowSizeCallback); glfwSetWindowSizeCallback(window, this::windowSizeCallback);
glfwSetMouseButtonCallback(window, this::mouseButtonCallback); glfwSetMouseButtonCallback(window, this::mouseButtonCallback);
framebuffer = new Framebuffer(size);
framebuffer = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
framebufferTex = glGenTextures();
glBindTexture(GL_TEXTURE_2D, framebufferTex);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
size.getX(),
size.getY(),
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
0
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
framebufferTex,
0
);
int framebufferDepthTex = glGenTextures();
glBindTexture(GL_TEXTURE_2D, framebufferDepthTex);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_DEPTH_COMPONENT,
size.getX(),
size.getY(),
0,
GL_DEPTH_COMPONENT,
GL_UNSIGNED_BYTE,
0
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture(GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
framebufferDepthTex,
0
);
glDrawBuffers(GL_COLOR_ATTACHMENT0);
textureAtlas = new TextureAtlas(); textureAtlas = new TextureAtlas();
program = new Program(vertexSource, fragmentSource); program = new Program(vertexSource, fragmentSource);
program3D = new Program(vertexSource3D, fragmentSource3D); program3D = new Program(vertexSource3D, fragmentSource3D);
@ -226,9 +180,13 @@ public class Window extends BaseWindow {
System.out.println(GL11.glGetInteger(GL_MAX_TEXTURE_SIZE)); System.out.println(GL11.glGetInteger(GL_MAX_TEXTURE_SIZE));
} }
@Override
public BaseGamepad getGamepad(int id) {
return new Gamepad(id);
}
@Override @Override
public Vec2i getMousePos() { public Vec2i getMousePos() {
//todo: scale mouse to stretchMode
double[] x = {0.0}; double[] x = {0.0};
double[] y = {0.0}; double[] y = {0.0};
glfwGetCursorPos(window, x, y); glfwGetCursorPos(window, x, y);
@ -342,7 +300,7 @@ public class Window extends BaseWindow {
public IRenderContext renderStart() { public IRenderContext renderStart() {
program.use(); program.use();
textureAtlas.bind(); textureAtlas.bind();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); framebuffer.bind();
glViewport(0, 0, size.getX(), size.getY()); glViewport(0, 0, size.getX(), size.getY());
textureAtlas.update(); textureAtlas.update();
glClearColor(0.f, 0.f, 0.f, 0.0f); glClearColor(0.f, 0.f, 0.f, 0.0f);
@ -356,7 +314,7 @@ public class Window extends BaseWindow {
public IRenderContext3D renderStart3D() { public IRenderContext3D renderStart3D() {
program3D.use(); program3D.use();
textureAtlas.bind(); textureAtlas.bind();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); framebuffer.bind();
glViewport(0, 0, size.getX(), size.getY()); glViewport(0, 0, size.getX(), size.getY());
textureAtlas.update(); textureAtlas.update();
glClearColor(0.f, 0.f, 0.f, 0.0f); glClearColor(0.f, 0.f, 0.f, 0.0f);
@ -376,14 +334,14 @@ public class Window extends BaseWindow {
//glEnd(); //glEnd();
glBindFramebuffer(GL_FRAMEBUFFER, 0); Framebuffer.unbind();
glViewport(0, 0, windowSize.getX(), windowSize.getY()); glViewport(0, 0, windowSize.getX(), windowSize.getY());
programScaler.use(); programScaler.use();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, framebufferTex); framebuffer.bindTexture();
glProgramUniform2f(programScaler.getProgram(), glProgramUniform2f(programScaler.getProgram(),
programScaler.getUniformLocation("windowSize"), programScaler.getUniformLocation("windowSize"),
windowSize.getX(), windowSize.getX(),

View File

@ -0,0 +1,47 @@
package com.danitheskunk.skunkworks.gfx.threedee;
import com.danitheskunk.skunkworks.Mat4f;
import org.lwjgl.assimp.AIBone;
import java.util.ArrayList;
import java.util.List;
public class Bone {
private String name;
private List<VertexWeight> vertexWeights;
private Mat4f offsetMatrix;
public Bone() {
vertexWeights = new ArrayList<>();
offsetMatrix = Mat4f.IDENTITY;
}
public static Bone fromAIBone(AIBone aiBone) {
var bone = new Bone();
var weightBuffer = aiBone.mWeights();
bone.name = aiBone.mName().dataString();
while(weightBuffer.hasRemaining()) {
var weight = VertexWeight.fromAiWeight(weightBuffer.get());
bone.vertexWeights.add(weight);
}
bone.offsetMatrix = Mat4f.fromAiMat(aiBone.mOffsetMatrix());
var node = aiBone.mNode();
if(node.address() != 0) {
System.out.println(node.mName().dataString());
}
return bone;
}
public VertexWeight getVertexWeight(int i) {
return vertexWeights.get(i);
}
public int getVertexWeightCount() {
return vertexWeights.size();
}
}

View File

@ -4,6 +4,7 @@ import com.danitheskunk.skunkworks.Vec2f;
import com.danitheskunk.skunkworks.Vec3f; import com.danitheskunk.skunkworks.Vec3f;
import com.danitheskunk.skunkworks.Vec3i; import com.danitheskunk.skunkworks.Vec3i;
import com.danitheskunk.skunkworks.gfx.ITexture; import com.danitheskunk.skunkworks.gfx.ITexture;
import org.lwjgl.assimp.AIBone;
import org.lwjgl.assimp.AIMesh; import org.lwjgl.assimp.AIMesh;
import java.util.ArrayList; import java.util.ArrayList;
@ -27,6 +28,7 @@ public class Mesh {
var vertexBuffer = aiMesh.mVertices(); var vertexBuffer = aiMesh.mVertices();
var faceBuffer = aiMesh.mFaces(); var faceBuffer = aiMesh.mFaces();
var texCoordsBuffer = aiMesh.mTextureCoords(0); var texCoordsBuffer = aiMesh.mTextureCoords(0);
var boneBuffer = aiMesh.mBones();
while(vertexBuffer.remaining() > 0) { while(vertexBuffer.remaining() > 0) {
var vert = vertexBuffer.get(); var vert = vertexBuffer.get();
@ -51,6 +53,13 @@ public class Mesh {
mesh.addUV(new Vec2f(x, 1.0 - y)); mesh.addUV(new Vec2f(x, 1.0 - y));
} }
while(boneBuffer.remaining() > 0) {
var aiBone = AIBone.create(boneBuffer.get());
var bone = Bone.fromAIBone(aiBone);
//System.out.printf("bone: %s\n", bone.mName().dataString());
}
return mesh; return mesh;
} }

View File

@ -58,6 +58,9 @@ public class Model {
System.out.printf("tex %dx%d\n", img.getWidth(), img.getHeight()); System.out.printf("tex %dx%d\n", img.getWidth(), img.getHeight());
} }
var node = ai.mRootNode();
walkNodes(node);
var numMeshes = ai.mNumMeshes(); var numMeshes = ai.mNumMeshes();
var meshPB = ai.mMeshes(); var meshPB = ai.mMeshes();
@ -75,6 +78,16 @@ public class Model {
faces += aiMesh.mNumFaces(); faces += aiMesh.mNumFaces();
} }
/*
var animationBuffer = ai.mAnimations();
while(animationBuffer.hasRemaining()) {
var aiAnim = AIAnimation.create(animationBuffer.get());
System.out.printf("anim: %s\n", aiAnim.mName().dataString());
}
*/
//window.setDebug(true); //window.setDebug(true);
System.out.printf("numMeshes: %d, faces: %d, textures: %d\n", System.out.printf("numMeshes: %d, faces: %d, textures: %d\n",
@ -84,6 +97,18 @@ public class Model {
); );
} }
private void walkNodes(AINode node) {
System.out.println(node.mName().dataString());
var childBuffer = node.mChildren();
if(childBuffer == null) {
return;
}
while(childBuffer.hasRemaining()) {
var child = AINode.create(childBuffer.get());
walkNodes(child);
}
}
public int getMeshCount() { public int getMeshCount() {
return meshes.length; return meshes.length;
} }

View File

@ -0,0 +1,25 @@
package com.danitheskunk.skunkworks.gfx.threedee;
import org.lwjgl.assimp.AIVertexWeight;
public class VertexWeight {
private final int index;
private final float weight;
public VertexWeight(int index, float weight) {
this.index = index;
this.weight = weight;
}
public static VertexWeight fromAiWeight(AIVertexWeight aiWeight) {
return new VertexWeight(aiWeight.mVertexId(), aiWeight.mWeight());
}
public int getIndex() {
return index;
}
public float getWeight() {
return weight;
}
}