Resolving conflicts and applying textures
This commit is contained in:
commit
e3a33d946c
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
|
|
||||||
# IntelliJ Idea folders
|
# IntelliJ Idea folders
|
||||||
.idea
|
|
||||||
out
|
out
|
||||||
|
|
||||||
|
|
||||||
|
|
2
.idea/.gitignore
vendored
Normal file
2
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
# Default ignored files
|
||||||
|
/workspace.xml
|
13
.idea/Helium-Engine.iml
Normal file
13
.idea/Helium-Engine.iml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="library" name="lwjgl-release-3.2" level="project" />
|
||||||
|
<orderEntry type="library" name="commons-io-2" level="project" />
|
||||||
|
</component>
|
||||||
|
</module>
|
5
.idea/codeStyles/codeStyleConfig.xml
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<state>
|
||||||
|
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||||
|
</state>
|
||||||
|
</component>
|
15
.idea/libraries/commons_io_2.xml
Normal file
15
.idea/libraries/commons_io_2.xml
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<component name="libraryTable">
|
||||||
|
<library name="commons-io-2">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="file://$USER_HOME$/Downloads/commons-io-2.6" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC>
|
||||||
|
<root url="file://$USER_HOME$/Downloads/commons-io-2.6/docs" />
|
||||||
|
</JAVADOC>
|
||||||
|
<SOURCES>
|
||||||
|
<root url="file://$USER_HOME$/Downloads/commons-io-2.6" />
|
||||||
|
</SOURCES>
|
||||||
|
<jarDirectory url="file://$USER_HOME$/Downloads/commons-io-2.6" recursive="false" />
|
||||||
|
<jarDirectory url="file://$USER_HOME$/Downloads/commons-io-2.6" recursive="false" type="SOURCES" />
|
||||||
|
</library>
|
||||||
|
</component>
|
13
.idea/libraries/lwjgl_release_3_2.xml
Normal file
13
.idea/libraries/lwjgl_release_3_2.xml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<component name="libraryTable">
|
||||||
|
<library name="lwjgl-release-3.2">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="file://$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES>
|
||||||
|
<root url="file://$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom" />
|
||||||
|
</SOURCES>
|
||||||
|
<jarDirectory url="file://$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom" recursive="false" />
|
||||||
|
<jarDirectory url="file://$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom" recursive="false" type="SOURCES" />
|
||||||
|
</library>
|
||||||
|
</component>
|
6
.idea/misc.xml
Normal file
6
.idea/misc.xml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/Helium-Engine.iml" filepath="$PROJECT_DIR$/.idea/Helium-Engine.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
124
.idea/uiDesigner.xml
Normal file
124
.idea/uiDesigner.xml
Normal file
|
@ -0,0 +1,124 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Palette2">
|
||||||
|
<group name="Swing">
|
||||||
|
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Button" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="RadioButton" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="CheckBox" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Label" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||||
|
<preferred-size width="-1" height="20" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
</group>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
BIN
resources/textures/thonk.png
Normal file
BIN
resources/textures/thonk.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 9.0 KiB |
120
src/Test.java
120
src/Test.java
|
@ -1,79 +1,83 @@
|
||||||
import org.hl.engine.graphics.Mesh;
|
import org.hl.engine.graphics.*;
|
||||||
import org.hl.engine.graphics.Renderer;
|
|
||||||
import org.hl.engine.graphics.Shader;
|
|
||||||
import org.hl.engine.graphics.Vertex;
|
|
||||||
import org.hl.engine.io.Display;
|
import org.hl.engine.io.Display;
|
||||||
import org.hl.engine.io.Input;
|
import org.hl.engine.io.Input;
|
||||||
import org.hl.engine.math.Vector3f;
|
import org.hl.engine.math.lalg.Vector3f;
|
||||||
|
import org.hl.engine.math.lalg.Vector2f;
|
||||||
import org.lwjgl.glfw.GLFW;
|
import org.lwjgl.glfw.GLFW;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class Test {
|
public class Test {
|
||||||
public final static int WIDTH = 640, HEIGHT = 480;
|
public final static int WIDTH = 640, HEIGHT = 480;
|
||||||
public final String windowName = "Game!";
|
public final String windowName = "Game!";
|
||||||
public Display display;
|
public Display display;
|
||||||
public Input i;
|
public Input i;
|
||||||
public Renderer renderer;
|
public Renderer renderer;
|
||||||
public Shader shader;
|
public Shader shader;
|
||||||
|
|
||||||
public Mesh mesh = new Mesh(new Vertex[] {
|
public Mesh mesh = new Mesh(new Vertex[] {
|
||||||
new Vertex(new Vector3f(-0.5F, 0.5F, 0.0F), new Vector3f(0, 0, 1.0F)),
|
new Vertex(new Vector3f(-0.5F, 0.5F, 0.0F), new Vector3f(0, 0, 1.0F), new Vector2f(0, 0)),
|
||||||
new Vertex(new Vector3f(-0.5F, -0.5F, 0.0F), new Vector3f(0, 0, 1.0F)),
|
new Vertex(new Vector3f(-0.5F, -0.5F, 0.0F), new Vector3f(0, 0, 1.0F), new Vector2f(0, 1)),
|
||||||
new Vertex(new Vector3f(0.5F, -0.5F, 0.0F), new Vector3f(1.0F, 0, 1.0F)),
|
new Vertex(new Vector3f(0.5F, -0.5F, 0.0F), new Vector3f(1.0F, 0, 1.0F), new Vector2f(1, 1)),
|
||||||
new Vertex(new Vector3f(0.5F, 0.5F, 0.0F), new Vector3f(1.0F, 0, 1.0F) ),
|
new Vertex(new Vector3f(0.5F, 0.5F, 0.0F), new Vector3f(1.0F, 0, 1.0F), new Vector2f(1, 0)),
|
||||||
|
|
||||||
}, new int[] {
|
}, new int[] {
|
||||||
0, 1, 2,
|
0, 1, 2,
|
||||||
0, 2, 3
|
0, 2, 3
|
||||||
|
|
||||||
});
|
}, new Material(new Texture("resources/textures/thonk.png")));
|
||||||
|
|
||||||
public void run() {
|
public void run() throws Exception {
|
||||||
init();
|
setup();
|
||||||
i = new Input(display);
|
i = new Input(display);
|
||||||
while (!(display.shouldClose()) && !i.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) {
|
while (!(display.shouldClose()) && !i.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) {
|
||||||
update();
|
loop();
|
||||||
render();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
close();
|
close();
|
||||||
|
|
||||||
}
|
}
|
||||||
public void init() {
|
|
||||||
// System.out.println("Initializing Game ");
|
|
||||||
display = new Display(WIDTH, HEIGHT, windowName);
|
|
||||||
shader = new Shader("/resources/shaders/mainVertex.glsl", "/resources/shaders/mainFragment.glsl");
|
|
||||||
renderer = new Renderer(shader);
|
|
||||||
display.setBackgroundColor(1F, 0, 0);
|
|
||||||
display.create();
|
|
||||||
mesh.create();
|
|
||||||
shader.create();
|
|
||||||
|
|
||||||
}
|
public void loop() {
|
||||||
private void update() {
|
update();
|
||||||
// System.out.println("Updating ");
|
render();
|
||||||
int frames = display.update();
|
}
|
||||||
display.setWindowName(display.getWindowName().substring(0, 4) + " (Frames : " + frames + ")");
|
|
||||||
|
|
||||||
i.reset();
|
|
||||||
|
|
||||||
}
|
public void setup() throws Exception {
|
||||||
|
// System.out.println("Initializing Game ");
|
||||||
|
display = new Display(WIDTH, HEIGHT, windowName);
|
||||||
|
display.create();
|
||||||
|
shader = new Shader("/resources/shaders/mainVertex.glsl", "/resources/shaders/mainFragment.glsl");
|
||||||
|
renderer = new Renderer(shader);
|
||||||
|
display.setBackgroundColor(1F, 1F, 1F);
|
||||||
|
mesh.create();
|
||||||
|
shader.create();
|
||||||
|
|
||||||
private void render() {
|
}
|
||||||
// System.out.println("Rendering ");
|
private void update() {
|
||||||
renderer.renderMesh(mesh);
|
// System.out.println("Updating ");
|
||||||
display.swapBuffers();
|
int frames = display.update();
|
||||||
|
display.setWindowName(display.getWindowName().substring(0, 4) + " (Frames : " + frames + ")");
|
||||||
|
|
||||||
}
|
i.reset();
|
||||||
|
|
||||||
private void close() {
|
}
|
||||||
display.destroy();
|
|
||||||
mesh.destroy();
|
|
||||||
shader.destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
private void render() {
|
||||||
new Test().run();
|
// System.out.println("Rendering ");
|
||||||
}
|
renderer.renderMesh(mesh);
|
||||||
|
display.swapBuffers();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void close() {
|
||||||
|
display.destroy();
|
||||||
|
mesh.destroy();
|
||||||
|
shader.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new Test().run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
107
src/org/hl/engine/graphics/Material.java
Normal file
107
src/org/hl/engine/graphics/Material.java
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
package org.hl.engine.graphics;
|
||||||
|
|
||||||
|
import org.hl.engine.math.lalg.Vector4f;
|
||||||
|
|
||||||
|
public class Material {
|
||||||
|
private static final Vector4f DEFAULT_COLOR = new Vector4f(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
|
|
||||||
|
private Vector4f ambientColor;
|
||||||
|
|
||||||
|
private Vector4f diffuseColor;
|
||||||
|
|
||||||
|
private Vector4f specularColor;
|
||||||
|
|
||||||
|
private float reflectance;
|
||||||
|
|
||||||
|
private Texture texture;
|
||||||
|
|
||||||
|
private Texture normalMap;
|
||||||
|
|
||||||
|
public Material() {
|
||||||
|
this.ambientColor = DEFAULT_COLOR;
|
||||||
|
this.diffuseColor = DEFAULT_COLOR;
|
||||||
|
this.specularColor = DEFAULT_COLOR;
|
||||||
|
this.texture = null;
|
||||||
|
this.reflectance = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Material(Vector4f color, float reflectance) {
|
||||||
|
this(color, color, color, null, reflectance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Material(Texture texture) {
|
||||||
|
this(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR, texture, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Material(Texture texture, float reflectance) {
|
||||||
|
this(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR, texture, reflectance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Material(Vector4f ambientColor, Vector4f diffuseColor, Vector4f specularColor, Texture texture, float reflectance) {
|
||||||
|
this.ambientColor = ambientColor;
|
||||||
|
this.diffuseColor = diffuseColor;
|
||||||
|
this.specularColor = specularColor;
|
||||||
|
this.texture = texture;
|
||||||
|
this.reflectance = reflectance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector4f getAmbientColor() {
|
||||||
|
return ambientColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAmbientColor(Vector4f ambientColor) {
|
||||||
|
this.ambientColor = ambientColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector4f getDiffuseColor() {
|
||||||
|
return diffuseColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDiffuseColor(Vector4f diffuseColor) {
|
||||||
|
this.diffuseColor = diffuseColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector4f getSpecularColor() {
|
||||||
|
return specularColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSpecularColor(Vector4f specularColor) {
|
||||||
|
this.specularColor = specularColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getReflectance() {
|
||||||
|
return reflectance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReflectance(float reflectance) {
|
||||||
|
this.reflectance = reflectance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isTextured() {
|
||||||
|
return this.texture != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Texture getTexture() {
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTexture(Texture texture) {
|
||||||
|
this.texture = texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasNormalMap() {
|
||||||
|
return this.normalMap != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Texture getNormalMap() {
|
||||||
|
return normalMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNormalMap(Texture normalMap) {
|
||||||
|
this.normalMap = normalMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void create() {
|
||||||
|
texture.create();
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,102 +10,134 @@ import java.nio.FloatBuffer;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
public class Mesh {
|
public class Mesh {
|
||||||
private Vertex[] vertices;
|
|
||||||
private int[] indices;
|
|
||||||
private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject;
|
|
||||||
|
|
||||||
// A group of vertices combined based on the indexes
|
private Vertex[] vertices;
|
||||||
public Mesh(Vertex[] vertices, int[] indices) {
|
private int[] indices;
|
||||||
this.vertices = vertices;
|
private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject, textureBufferObject;
|
||||||
this.indices = indices;
|
private Material material;
|
||||||
}
|
|
||||||
|
|
||||||
// Destroy the mesh
|
// A group of vertices combined based on the indexes
|
||||||
public void destroy () {
|
public Mesh(Vertex[] vertices, int[] indices, Material material) {
|
||||||
GL15.glDeleteBuffers(positionBufferObject);
|
this.vertices = vertices;
|
||||||
GL15.glDeleteBuffers(indicesBufferObject);
|
this.indices = indices;
|
||||||
GL15.glDeleteBuffers(colorBufferObject);
|
this.material = material;
|
||||||
|
}
|
||||||
GL30.glDeleteVertexArrays(vertexArrayObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
// getters for the mesh
|
|
||||||
|
|
||||||
public Vertex[] getVertices() {
|
|
||||||
return vertices;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int[] getIndices() {
|
|
||||||
return indices;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getVertexArrayObject() {
|
|
||||||
return vertexArrayObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPositionBufferObject() {
|
|
||||||
return positionBufferObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getIndicesBufferObject() {
|
|
||||||
return indicesBufferObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getColorBufferObject() {
|
|
||||||
return colorBufferObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void create() {
|
|
||||||
|
|
||||||
// Creates the mesh by formatting the vertices and indices and inputting them to OpenGL
|
|
||||||
vertexArrayObject = GL30.glGenVertexArrays();
|
|
||||||
GL30.glBindVertexArray(vertexArrayObject);
|
|
||||||
|
|
||||||
|
|
||||||
// Putting the position of the vertex into the buffer so the renderer can read it
|
// Destroy the mesh
|
||||||
|
public void destroy () {
|
||||||
|
GL15.glDeleteBuffers(positionBufferObject);
|
||||||
|
GL15.glDeleteBuffers(indicesBufferObject);
|
||||||
|
GL15.glDeleteBuffers(colorBufferObject);
|
||||||
|
GL30.glDeleteBuffers(textureBufferObject);
|
||||||
|
|
||||||
FloatBuffer positionBuffer = MemoryUtil.memAllocFloat(vertices.length * 3);
|
GL30.glDeleteVertexArrays(vertexArrayObject);
|
||||||
float[] positionData = new float[vertices.length * 3];
|
|
||||||
for (int i = 0; i < vertices.length; i ++ ) {
|
|
||||||
positionData[i * 3] = vertices[i].getPosition().getX();
|
|
||||||
positionData[i * 3 + 1] = vertices[i].getPosition().getY();
|
|
||||||
positionData[i * 3 + 2] = vertices[i].getPosition().getZ();
|
|
||||||
}
|
|
||||||
positionBuffer.put(positionData).flip();
|
|
||||||
|
|
||||||
positionBufferObject = storeData(positionBuffer, 0, 3);
|
material.getTexture().destroy();
|
||||||
|
|
||||||
// Putting the color into the buffer so renderer and shader can read it
|
}
|
||||||
|
|
||||||
FloatBuffer colorBuffer = MemoryUtil.memAllocFloat(vertices.length * 3);
|
// getters for the mesh
|
||||||
float[] colorData = new float[vertices.length * 3];
|
|
||||||
for (int i = 0; i < vertices.length; i ++ ) {
|
|
||||||
colorData[i * 3] = vertices[i].getColor().getX();
|
|
||||||
colorData[i * 3 + 1] = vertices[i].getColor().getY();
|
|
||||||
colorData[i * 3 + 2] = vertices[i].getColor().getZ();
|
|
||||||
}
|
|
||||||
colorBuffer.put(colorData).flip();
|
|
||||||
|
|
||||||
colorBufferObject = storeData(colorBuffer, 1, 3);
|
public Vertex[] getVertices() {
|
||||||
|
return vertices;
|
||||||
|
}
|
||||||
|
|
||||||
IntBuffer indicesBuffer = MemoryUtil.memAllocInt(indices.length);
|
public int[] getIndices() {
|
||||||
indicesBuffer.put(indices).flip();
|
return indices;
|
||||||
|
}
|
||||||
|
|
||||||
indicesBufferObject = GL15.glGenBuffers();
|
public int getVertexArrayObject() {
|
||||||
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferObject);
|
return vertexArrayObject;
|
||||||
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
|
}
|
||||||
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Storing data to the buffer at position index (helps with storing color / position)
|
public int getPositionBufferObject() {
|
||||||
|
return positionBufferObject;
|
||||||
|
}
|
||||||
|
|
||||||
private int storeData(FloatBuffer buffer, int index, int size) {
|
|
||||||
int bufferID = GL15.glGenBuffers();
|
|
||||||
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, bufferID);
|
|
||||||
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
|
|
||||||
GL20.glVertexAttribPointer(index, size, GL11.GL_FLOAT, false, 0, 0);
|
|
||||||
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
|
|
||||||
|
|
||||||
return bufferID;
|
public int getIndicesBufferObject() {
|
||||||
}
|
return indicesBufferObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getColorBufferObject() {
|
||||||
|
return colorBufferObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTextureBufferObject() {
|
||||||
|
return textureBufferObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Material getMaterial() {
|
||||||
|
return material;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void create() {
|
||||||
|
|
||||||
|
material.create();
|
||||||
|
|
||||||
|
// Creates the mesh by formatting the vertices and indices and inputting them to OpenGL
|
||||||
|
vertexArrayObject = GL30.glGenVertexArrays();
|
||||||
|
GL30.glBindVertexArray(vertexArrayObject);
|
||||||
|
|
||||||
|
|
||||||
|
// Putting the position of the vertex into the buffer so the renderer can read it
|
||||||
|
|
||||||
|
FloatBuffer positionBuffer = MemoryUtil.memAllocFloat(vertices.length * 3);
|
||||||
|
float[] positionData = new float[vertices.length * 3];
|
||||||
|
for (int i = 0; i < vertices.length; i ++ ) {
|
||||||
|
positionData[i * 3] = vertices[i].getPosition().getX();
|
||||||
|
positionData[i * 3 + 1] = vertices[i].getPosition().getY();
|
||||||
|
positionData[i * 3 + 2] = vertices[i].getPosition().getZ();
|
||||||
|
}
|
||||||
|
positionBuffer.put(positionData).flip();
|
||||||
|
|
||||||
|
positionBufferObject = storeData(positionBuffer, 0, 3);
|
||||||
|
|
||||||
|
// Putting the color into the buffer so renderer and shader can read it
|
||||||
|
|
||||||
|
FloatBuffer colorBuffer = MemoryUtil.memAllocFloat(vertices.length * 3);
|
||||||
|
float[] colorData = new float[vertices.length * 3];
|
||||||
|
for (int i = 0; i < vertices.length; i ++ ) {
|
||||||
|
colorData[i * 3] = vertices[i].getColor().getX();
|
||||||
|
colorData[i * 3 + 1] = vertices[i].getColor().getY();
|
||||||
|
colorData[i * 3 + 2] = vertices[i].getColor().getZ();
|
||||||
|
}
|
||||||
|
colorBuffer.put(colorData).flip();
|
||||||
|
|
||||||
|
colorBufferObject = storeData(colorBuffer, 1, 3);
|
||||||
|
|
||||||
|
// Putting the texture into the buffer so renderer and shader can read it
|
||||||
|
|
||||||
|
FloatBuffer textureBuffer = MemoryUtil.memAllocFloat(vertices.length * 2);
|
||||||
|
float[] textureData = new float[vertices.length * 2];
|
||||||
|
for (int i = 0; i < vertices.length; i ++ ) {
|
||||||
|
textureData[i * 2] = vertices[i].getTextureCoords().getX();
|
||||||
|
textureData[i * 2 + 1] = vertices[i].getTextureCoords().getY();
|
||||||
|
}
|
||||||
|
textureBuffer.put(textureData).flip();
|
||||||
|
|
||||||
|
textureBufferObject = storeData(textureBuffer, 2, 2);
|
||||||
|
|
||||||
|
IntBuffer indicesBuffer = MemoryUtil.memAllocInt(indices.length);
|
||||||
|
indicesBuffer.put(indices).flip();
|
||||||
|
|
||||||
|
indicesBufferObject = GL15.glGenBuffers();
|
||||||
|
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferObject);
|
||||||
|
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
|
||||||
|
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Storing data to the buffer at position index (helps with storing color / position)
|
||||||
|
|
||||||
|
private int storeData(FloatBuffer buffer, int index, int size) {
|
||||||
|
int bufferID = GL15.glGenBuffers();
|
||||||
|
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, bufferID);
|
||||||
|
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
|
||||||
|
GL20.glVertexAttribPointer(index, size, GL11.GL_FLOAT, false, 0, 0);
|
||||||
|
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
|
return bufferID;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,34 +1,41 @@
|
||||||
package org.hl.engine.graphics;
|
package org.hl.engine.graphics;
|
||||||
|
|
||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
|
import org.lwjgl.opengl.GL13;
|
||||||
import org.lwjgl.opengl.GL15;
|
import org.lwjgl.opengl.GL15;
|
||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.opengl.GL30;
|
||||||
|
|
||||||
public class Renderer {
|
public class Renderer {
|
||||||
|
|
||||||
private Shader shader;
|
private Shader shader;
|
||||||
|
|
||||||
public Renderer(Shader shader) {
|
public Renderer(Shader shader) {
|
||||||
this.shader = shader;
|
this.shader = shader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void renderMesh(Mesh mesh) {
|
public void renderMesh(Mesh mesh) {
|
||||||
|
|
||||||
// Renders the mesh by drawing it using triangles (least complicated)
|
// Renders the mesh by drawing it using triangles (least complicated)
|
||||||
GL30.glBindVertexArray(mesh.getVertexArrayObject());
|
GL30.glBindVertexArray(mesh.getVertexArrayObject());
|
||||||
GL30.glEnableVertexAttribArray(0);
|
GL30.glEnableVertexAttribArray(0);
|
||||||
GL30.glEnableVertexAttribArray(1);
|
GL30.glEnableVertexAttribArray(1);
|
||||||
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, mesh.getIndicesBufferObject());
|
GL30.glEnableVertexAttribArray(2);
|
||||||
|
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, mesh.getIndicesBufferObject());
|
||||||
|
|
||||||
shader.bind();
|
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||||
|
|
||||||
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0);
|
GL13.glBindTexture(GL11.GL_TEXTURE_2D, mesh.getMaterial().getTexture().getId());
|
||||||
|
|
||||||
shader.unbind();
|
shader.bind();
|
||||||
GL30.glDisableVertexAttribArray(0);
|
|
||||||
GL30.glDisableVertexAttribArray(1);
|
|
||||||
GL30.glBindVertexArray(0);
|
|
||||||
|
|
||||||
}
|
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getIndices().length, GL11.GL_UNSIGNED_INT, 0);
|
||||||
|
|
||||||
|
shader.unbind();
|
||||||
|
GL30.glDisableVertexAttribArray(0);
|
||||||
|
GL30.glDisableVertexAttribArray(1);
|
||||||
|
GL30.glDisableVertexAttribArray(2);
|
||||||
|
GL30.glBindVertexArray(0);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,84 +5,84 @@ import org.lwjgl.opengl.GL11;
|
||||||
import org.lwjgl.opengl.GL20;
|
import org.lwjgl.opengl.GL20;
|
||||||
|
|
||||||
public class Shader {
|
public class Shader {
|
||||||
private String vertexFile;
|
private String vertexFile;
|
||||||
private String fragmentFile;
|
private String fragmentFile;
|
||||||
|
|
||||||
private int vertexID, fragmentID, programID;
|
private int vertexID, fragmentID, programID;
|
||||||
|
|
||||||
public Shader(String vertexPath, String fragmentPath) {
|
public Shader(String vertexPath, String fragmentPath) {
|
||||||
vertexFile = FileUtils.loadAsString(vertexPath);
|
vertexFile = FileUtils.loadAsString(vertexPath);
|
||||||
fragmentFile = FileUtils.loadAsString(fragmentPath);
|
fragmentFile = FileUtils.loadAsString(fragmentPath);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void create() {
|
public void create() {
|
||||||
|
|
||||||
// Creates the program
|
// Creates the program
|
||||||
programID = GL20.glCreateProgram();
|
programID = GL20.glCreateProgram();
|
||||||
|
|
||||||
|
|
||||||
// loads the vertex shader
|
// loads the vertex shader
|
||||||
vertexID = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
|
vertexID = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
|
||||||
|
|
||||||
GL20.glShaderSource(vertexID, vertexFile);
|
GL20.glShaderSource(vertexID, vertexFile);
|
||||||
GL20.glCompileShader(vertexID);
|
GL20.glCompileShader(vertexID);
|
||||||
|
|
||||||
if (GL20.glGetShaderi(vertexID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
|
if (GL20.glGetShaderi(vertexID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
|
||||||
System.err.println("Vertex Shader: " + GL20.glGetShaderInfoLog(vertexID));
|
System.err.println("Vertex Shader: " + GL20.glGetShaderInfoLog(vertexID));
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// loads the fragment shader
|
// loads the fragment shader
|
||||||
fragmentID = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
|
fragmentID = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
GL20.glShaderSource(fragmentID, fragmentFile);
|
GL20.glShaderSource(fragmentID, fragmentFile);
|
||||||
GL20.glCompileShader(fragmentID);
|
GL20.glCompileShader(fragmentID);
|
||||||
|
|
||||||
if (GL20.glGetShaderi(fragmentID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
|
if (GL20.glGetShaderi(fragmentID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
|
||||||
System.err.println("Fragment Shader: " + GL20.glGetShaderInfoLog(fragmentID));
|
System.err.println("Fragment Shader: " + GL20.glGetShaderInfoLog(fragmentID));
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attach shaders to program
|
// Attach shaders to program
|
||||||
GL20.glAttachShader(programID, vertexID);
|
GL20.glAttachShader(programID, vertexID);
|
||||||
GL20.glAttachShader(programID, fragmentID);
|
GL20.glAttachShader(programID, fragmentID);
|
||||||
|
|
||||||
// Link the program
|
// Link the program
|
||||||
GL20.glLinkProgram(programID);
|
GL20.glLinkProgram(programID);
|
||||||
if (GL20.glGetProgrami(programID, GL20.GL_LINK_STATUS) == GL11.GL_FALSE) {
|
if (GL20.glGetProgrami(programID, GL20.GL_LINK_STATUS) == GL11.GL_FALSE) {
|
||||||
System.err.println("Program Linking: " + GL20.glGetProgramInfoLog(programID));
|
System.err.println("Program Linking: " + GL20.glGetProgramInfoLog(programID));
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the program
|
// Validate the program
|
||||||
GL20.glValidateProgram(programID);
|
GL20.glValidateProgram(programID);
|
||||||
if (GL20.glGetProgrami(programID, GL20.GL_VALIDATE_STATUS) == GL11.GL_FALSE) {
|
if (GL20.glGetProgrami(programID, GL20.GL_VALIDATE_STATUS) == GL11.GL_FALSE) {
|
||||||
System.err.println("Program Validation: " + GL20.glGetProgramInfoLog(programID));
|
System.err.println("Program Validation: " + GL20.glGetProgramInfoLog(programID));
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
GL20.glDeleteShader(vertexID);
|
GL20.glDeleteShader(vertexID);
|
||||||
GL20.glDeleteShader(fragmentID);
|
GL20.glDeleteShader(fragmentID);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind so we can use the shader
|
// Bind so we can use the shader
|
||||||
public void bind() {
|
public void bind() {
|
||||||
GL20.glUseProgram(programID);
|
GL20.glUseProgram(programID);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unbind the shader after use
|
// Unbind the shader after use
|
||||||
public void unbind() {
|
public void unbind() {
|
||||||
GL20.glUseProgram(0);
|
GL20.glUseProgram(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destroy the program
|
// Destroy the program
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
GL20.glDeleteProgram(programID);
|
GL20.glDeleteProgram(programID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
133
src/org/hl/engine/graphics/Texture.java
Normal file
133
src/org/hl/engine/graphics/Texture.java
Normal file
|
@ -0,0 +1,133 @@
|
||||||
|
package org.hl.engine.graphics;
|
||||||
|
import static org.lwjgl.opengl.GL46.*;
|
||||||
|
import static org.lwjgl.stb.STBImage.*;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
|
import org.lwjgl.system.MemoryStack;
|
||||||
|
|
||||||
|
public class Texture {
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
private int width;
|
||||||
|
|
||||||
|
private int height;
|
||||||
|
|
||||||
|
private int type;
|
||||||
|
|
||||||
|
private String fileName;
|
||||||
|
private ByteBuffer imageBuffer;
|
||||||
|
|
||||||
|
private int pixelFormat;
|
||||||
|
|
||||||
|
|
||||||
|
public Texture(int width, int height, int pixelFormat) {
|
||||||
|
this.type = 0;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.pixelFormat = pixelFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Texture(String fileName) {
|
||||||
|
this.type = 1;
|
||||||
|
this.fileName = fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Texture(ByteBuffer imageBuffer) {
|
||||||
|
type = 2;
|
||||||
|
this.imageBuffer = imageBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void create() {
|
||||||
|
if (this.type == 0) {
|
||||||
|
this.id = glGenTextures();
|
||||||
|
glBindTexture(GL_TEXTURE_2D, this.id);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, this.pixelFormat, this.width, this.height, 0, this.pixelFormat, GL_FLOAT, (ByteBuffer) null);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
} else if (this.type == 1) {
|
||||||
|
ByteBuffer buf;
|
||||||
|
try(MemoryStack stack = MemoryStack.stackPush()) {
|
||||||
|
IntBuffer w = stack.mallocInt(1);
|
||||||
|
IntBuffer h = stack.mallocInt(1);
|
||||||
|
IntBuffer channels = stack.mallocInt(1);
|
||||||
|
|
||||||
|
buf = stbi_load(this.fileName, w, h, channels, 4);
|
||||||
|
if(buf == null) {
|
||||||
|
System.err.println("Image file [" + this.fileName + "] not loaded: " + stbi_failure_reason());
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.width = w.get();
|
||||||
|
this.height = h.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.id = createTexture(buf);
|
||||||
|
|
||||||
|
stbi_image_free(buf);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ByteBuffer buf;
|
||||||
|
try(MemoryStack stack = MemoryStack.stackPush()) {
|
||||||
|
IntBuffer w = stack.mallocInt(1);
|
||||||
|
IntBuffer h = stack.mallocInt(1);
|
||||||
|
IntBuffer channels = stack.mallocInt(1);
|
||||||
|
|
||||||
|
buf = stbi_load_from_memory(this.imageBuffer, w, h, channels, 4);
|
||||||
|
if(buf == null) {
|
||||||
|
System.err.println("Image file not loaded: " + stbi_failure_reason());
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.width = w.get();
|
||||||
|
this.height = h.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.id = createTexture(buf);
|
||||||
|
|
||||||
|
stbi_image_free(buf);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private int createTexture(ByteBuffer buf) {
|
||||||
|
int textureID = glGenTextures();
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||||
|
|
||||||
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
|
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
|
return textureID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void destroy() {
|
||||||
|
glDeleteTextures(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,24 +1,35 @@
|
||||||
package org.hl.engine.graphics;
|
package org.hl.engine.graphics;
|
||||||
|
|
||||||
import org.hl.engine.math.Vector3f;
|
|
||||||
|
import org.hl.engine.math.lalg.*;
|
||||||
|
|
||||||
|
|
||||||
public class Vertex {
|
public class Vertex {
|
||||||
|
|
||||||
// Just a vertex
|
// Just a vertex
|
||||||
|
|
||||||
private Vector3f position;
|
private Vector3f position;
|
||||||
private Vector3f color;
|
private Vector3f color;
|
||||||
|
private Vector2f textureCoords;
|
||||||
|
|
||||||
public Vertex (Vector3f position, Vector3f color) {
|
|
||||||
this.position = position;
|
|
||||||
this.color = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector3f getPosition() {
|
public Vertex (Vector3f position, Vector3f color, Vector2f textureCoords) {
|
||||||
return position;
|
this.position = position;
|
||||||
}
|
this.color = color;
|
||||||
|
this.textureCoords = textureCoords;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector3f getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Vector3f getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector2f getTextureCoords() {
|
||||||
|
return textureCoords;
|
||||||
|
}
|
||||||
|
|
||||||
public Vector3f getColor() {
|
|
||||||
return color;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package org.hl.engine.io;
|
package org.hl.engine.io;
|
||||||
import org.hl.engine.math.Vector3f;
|
|
||||||
|
import org.hl.engine.math.lalg.Vector3f;
|
||||||
|
import org.lwjgl.glfw.GLFWErrorCallback;
|
||||||
import org.lwjgl.glfw.GLFWVidMode;
|
import org.lwjgl.glfw.GLFWVidMode;
|
||||||
import org.lwjgl.glfw.GLFWWindowSizeCallback;
|
import org.lwjgl.glfw.GLFWWindowSizeCallback;
|
||||||
import org.lwjgl.opengl.GL;
|
import org.lwjgl.opengl.GL;
|
||||||
|
@ -8,205 +10,211 @@ import org.lwjgl.opengl.GL11;
|
||||||
import static org.lwjgl.glfw.GLFW.*;
|
import static org.lwjgl.glfw.GLFW.*;
|
||||||
|
|
||||||
public class Display {
|
public class Display {
|
||||||
private int width, height;
|
|
||||||
private String windowName;
|
private int width, height;
|
||||||
private long window;
|
private String windowName;
|
||||||
public int frames;
|
private long window;
|
||||||
public int previousFrames = frames;
|
public int frames;
|
||||||
public long time;
|
public int previousFrames = frames;
|
||||||
public Input input;
|
public long time;
|
||||||
private Vector3f background = new Vector3f(0, 0, 0);
|
public Input input;
|
||||||
private GLFWWindowSizeCallback resizeCallback;
|
private Vector3f background = new Vector3f(0, 0, 0);
|
||||||
private boolean isResized;
|
private GLFWWindowSizeCallback resizeCallback;
|
||||||
private boolean isFullscreen;
|
private boolean isResized;
|
||||||
private int[] windowXPos = new int[1];
|
private boolean isFullscreen;
|
||||||
private int[] windowYPos = new int[1];
|
private int[] windowXPos = new int[1];
|
||||||
private GLFWVidMode videoMode;
|
private int[] windowYPos = new int[1];
|
||||||
private int savedPosX;
|
private GLFWVidMode videoMode;
|
||||||
private int savedPosY;
|
private int savedPosX;
|
||||||
private int savedWidth;
|
private int savedPosY;
|
||||||
private int savedHeight;
|
private int savedWidth;
|
||||||
|
private int savedHeight;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Constructor to create the display
|
// Constructor to create the display
|
||||||
public Display (int width, int height, String windowName) {
|
public Display (int width, int height, String windowName) {
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.windowName = windowName;
|
this.windowName = windowName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change the window name
|
// Change the window name
|
||||||
public void setWindowName(String windowName) {
|
public void setWindowName(String windowName) {
|
||||||
this.windowName = windowName;
|
this.windowName = windowName;
|
||||||
glfwSetWindowTitle(window, windowName);
|
glfwSetWindowTitle(window, windowName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters for size, name, window, time, and fullScreen
|
// Getters for size, name, window, time, and fullScreen
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
public String getWindowName() {
|
public String getWindowName() {
|
||||||
return windowName;
|
return windowName;
|
||||||
}
|
}
|
||||||
public long getWindow() {
|
public long getWindow() {
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
public long getTime() {
|
public long getTime() {
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFullscreen() {
|
public boolean isFullscreen() {
|
||||||
return isFullscreen;
|
return isFullscreen;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Makes the screen fullscreen or not based on the argument
|
// Makes the screen fullscreen or not based on the argument
|
||||||
public void setFullscreen(boolean fullscreen) {
|
public void setFullscreen(boolean fullscreen) {
|
||||||
isFullscreen = fullscreen;
|
isFullscreen = fullscreen;
|
||||||
isResized = true;
|
isResized = true;
|
||||||
GL11.glViewport(0, 0, width, height);
|
GL11.glViewport(0, 0, width, height);
|
||||||
if (isFullscreen) {
|
if (isFullscreen) {
|
||||||
|
|
||||||
int[] xpos = {0};
|
int[] xpos = {0};
|
||||||
int[] ypos = {0};
|
int[] ypos = {0};
|
||||||
glfwGetWindowPos(this.window, xpos, ypos);
|
glfwGetWindowPos(this.window, xpos, ypos);
|
||||||
savedPosX = xpos[0];
|
savedPosX = xpos[0];
|
||||||
savedPosY = ypos[0];
|
savedPosY = ypos[0];
|
||||||
|
|
||||||
savedWidth = width;
|
savedWidth = width;
|
||||||
savedHeight = height;
|
savedHeight = height;
|
||||||
glfwGetWindowPos(window, windowXPos, windowYPos);
|
glfwGetWindowPos(window, windowXPos, windowYPos);
|
||||||
glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, videoMode.width(), videoMode.height(), 0);
|
glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, videoMode.width(), videoMode.height(), 0);
|
||||||
} else {
|
} else {
|
||||||
glfwSetWindowMonitor(window, 0, savedPosX, savedPosY, savedWidth, savedHeight, 0);
|
glfwSetWindowMonitor(window, 0, savedPosX, savedPosY, savedWidth, savedHeight, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// resized getter
|
// resized getter
|
||||||
|
|
||||||
public boolean isResized() {
|
public boolean isResized() {
|
||||||
return isResized;
|
return isResized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Creates the window (should go in the init() function of your Main program)
|
// Creates the window (should go in the init() function of your Main program)
|
||||||
public void create() {
|
public void create() throws Exception {
|
||||||
|
|
||||||
// initializing glfw
|
GLFWErrorCallback.createPrint(System.err).set();
|
||||||
if (!glfwInit()) {
|
|
||||||
System.err.println("Failed to initialize GLFW! ");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
// initializing glfw
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
if (!glfwInit()) {
|
||||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE);
|
//System.err.println("Failed to initialize GLFW! ");
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
//System.exit(1);
|
||||||
|
throw new Exception("Failed to initialize GLFW! ");
|
||||||
|
}
|
||||||
|
|
||||||
//Creating window
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
|
||||||
window = glfwCreateWindow(this.width, this.height, this.windowName, isFullscreen ? glfwGetPrimaryMonitor():0, 0);
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE);
|
||||||
if (window == 0) {
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
System.err.println("Failed to create window! ");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setting size of window
|
//Creating window
|
||||||
|
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
||||||
|
window = glfwCreateWindow(this.width, this.height, this.windowName, isFullscreen ? glfwGetPrimaryMonitor():0, 0);
|
||||||
|
if (window == 0) {
|
||||||
|
//System.err.println("Failed to create window! ");
|
||||||
|
//System.exit(1);
|
||||||
|
throw new Exception("Failed to create window! ");
|
||||||
|
}
|
||||||
|
|
||||||
videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
// Setting size of window
|
||||||
windowXPos[0] = (videoMode.width() - this.width) / 2;
|
|
||||||
windowYPos[0] = (videoMode.height() - this.height ) / 2;
|
videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
||||||
glfwSetWindowPos(window, windowXPos[0], windowYPos[0]);
|
windowXPos[0] = (videoMode.width() - this.width) / 2;
|
||||||
|
windowYPos[0] = (videoMode.height() - this.height ) / 2;
|
||||||
|
glfwSetWindowPos(window, windowXPos[0], windowYPos[0]);
|
||||||
|
|
||||||
|
|
||||||
// Graphics
|
// Graphics
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
GL.createCapabilities();
|
GL.createCapabilities();
|
||||||
|
|
||||||
GL11.glEnable(GL11.GL_DEPTH_TEST);
|
GL11.glEnable(GL11.GL_DEPTH_TEST);
|
||||||
|
|
||||||
callBacks();
|
callBacks();
|
||||||
glfwShowWindow(window);
|
glfwShowWindow(window);
|
||||||
glfwSwapInterval(1);
|
glfwSwapInterval(1);
|
||||||
|
|
||||||
// setting time
|
// setting time
|
||||||
|
|
||||||
time = System.currentTimeMillis();
|
time = System.currentTimeMillis();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Creating the resize callback (all other callbacks were removed and are now in Input class)
|
// Creating the resize callback (all other callbacks were removed and are now in Input class)
|
||||||
private void callBacks() {
|
private void callBacks() {
|
||||||
|
|
||||||
resizeCallback = new GLFWWindowSizeCallback() {
|
resizeCallback = new GLFWWindowSizeCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void invoke(long window, int w, int h) {
|
public void invoke(long window, int w, int h) {
|
||||||
width = w;
|
width = w;
|
||||||
height = h;
|
height = h;
|
||||||
isResized = true;
|
isResized = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
glfwSetWindowSizeCallback(window, resizeCallback);
|
glfwSetWindowSizeCallback(window, resizeCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refreshes the screen, resets frame count
|
// Refreshes the screen, resets frame count
|
||||||
public int update() {
|
public int update() {
|
||||||
if (isResized) {
|
if (isResized) {
|
||||||
GL11.glViewport(0, 0, width, height);
|
GL11.glViewport(0, 0, width, height);
|
||||||
isResized = false;
|
isResized = false;
|
||||||
}
|
}
|
||||||
GL11.glClearColor(background.getX(), background.getY(), background.getZ(), 1.0F);
|
GL11.glClearColor(background.getX(), background.getY(), background.getZ(), 1.0F);
|
||||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
|
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
frames++;
|
frames++;
|
||||||
if (System.currentTimeMillis() > time + 1000) {
|
if (System.currentTimeMillis() > time + 1000) {
|
||||||
previousFrames = frames;
|
previousFrames = frames;
|
||||||
time = System.currentTimeMillis();
|
time = System.currentTimeMillis();
|
||||||
frames = 0;
|
frames = 0;
|
||||||
return frames;
|
return frames;
|
||||||
} else {
|
} else {
|
||||||
return previousFrames;
|
return previousFrames;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Terminates the program (making WindowShouldClose)
|
// Terminates the program (making WindowShouldClose)
|
||||||
|
|
||||||
public void terminate() {
|
public void terminate() {
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Completely DESTROYS the window
|
// Completely DESTROYS the window
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
resizeCallback.free();
|
resizeCallback.free();
|
||||||
glfwDestroyWindow(window);
|
glfwSetErrorCallback(null).free();
|
||||||
glfwTerminate();
|
glfwDestroyWindow(window);
|
||||||
}
|
glfwTerminate();
|
||||||
|
}
|
||||||
|
|
||||||
// switches the buffers (for rendering)
|
// switches the buffers (for rendering)
|
||||||
|
|
||||||
public void swapBuffers() {
|
public void swapBuffers() {
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// get whether the window should close
|
// get whether the window should close
|
||||||
public boolean shouldClose() {
|
public boolean shouldClose() {
|
||||||
return glfwWindowShouldClose(window);
|
return glfwWindowShouldClose(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
// changes the background color
|
// changes the background color
|
||||||
|
|
||||||
public void setBackgroundColor(float r, float g, float b) {
|
public void setBackgroundColor(float r, float g, float b) {
|
||||||
background.setVector(r, g, b);
|
background.setVector(r, g, b);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,115 +7,115 @@ import java.util.Arrays;
|
||||||
import static org.lwjgl.glfw.GLFW.*;
|
import static org.lwjgl.glfw.GLFW.*;
|
||||||
|
|
||||||
public class Input {
|
public class Input {
|
||||||
private boolean[] keys = new boolean[GLFW.GLFW_KEY_LAST];
|
private boolean[] keys = new boolean[GLFW.GLFW_KEY_LAST];
|
||||||
private boolean[] buttons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST];
|
private boolean[] buttons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST];
|
||||||
|
|
||||||
private int[] keyState = new int[GLFW.GLFW_KEY_LAST];
|
private int[] keyState = new int[GLFW.GLFW_KEY_LAST];
|
||||||
private int[] buttonState = new int[GLFW.GLFW_MOUSE_BUTTON_LAST];
|
private int[] buttonState = new int[GLFW.GLFW_MOUSE_BUTTON_LAST];
|
||||||
|
|
||||||
private long window;
|
private long window;
|
||||||
private Display display;
|
private Display display;
|
||||||
|
|
||||||
private boolean inWindow;
|
private boolean inWindow;
|
||||||
|
|
||||||
private double mouseX, mouseY;
|
private double mouseX, mouseY;
|
||||||
private double scrollX, scrollY;
|
private double scrollX, scrollY;
|
||||||
|
|
||||||
// Sets up the callbacks based on the window
|
// Sets up the callbacks based on the window
|
||||||
public Input(Display d) {
|
public Input(Display d) {
|
||||||
this.display = d;
|
this.display = d;
|
||||||
this.window = this.display.getWindow();
|
this.window = this.display.getWindow();
|
||||||
|
|
||||||
glfwSetKeyCallback(this.window, (window, key, scancode, action, mods) -> {
|
glfwSetKeyCallback(this.window, (window, key, scancode, action, mods) -> {
|
||||||
keys[key] = action != GLFW.GLFW_RELEASE;
|
keys[key] = action != GLFW.GLFW_RELEASE;
|
||||||
keyState[key] = action;
|
keyState[key] = action;
|
||||||
});
|
});
|
||||||
|
|
||||||
glfwSetMouseButtonCallback(this.window, (window, button, action, mods) -> {
|
glfwSetMouseButtonCallback(this.window, (window, button, action, mods) -> {
|
||||||
buttons[button] = action != GLFW.GLFW_RELEASE;
|
buttons[button] = action != GLFW.GLFW_RELEASE;
|
||||||
buttonState[button] = action;
|
buttonState[button] = action;
|
||||||
});
|
});
|
||||||
|
|
||||||
glfwSetCursorPosCallback(this.window, (window, xpos, ypos) -> {
|
glfwSetCursorPosCallback(this.window, (window, xpos, ypos) -> {
|
||||||
mouseX = xpos;
|
mouseX = xpos;
|
||||||
mouseY = ypos;
|
mouseY = ypos;
|
||||||
});
|
});
|
||||||
|
|
||||||
glfwSetCursorEnterCallback(this.window, (window, entered) -> {
|
glfwSetCursorEnterCallback(this.window, (window, entered) -> {
|
||||||
inWindow = entered;
|
inWindow = entered;
|
||||||
});
|
});
|
||||||
|
|
||||||
glfwSetScrollCallback(this.window, (window, xoffset, yoffset) -> {
|
glfwSetScrollCallback(this.window, (window, xoffset, yoffset) -> {
|
||||||
scrollX += xoffset;
|
scrollX += xoffset;
|
||||||
scrollY += yoffset;
|
scrollY += yoffset;
|
||||||
});
|
});
|
||||||
|
|
||||||
resetKeyboard();
|
resetKeyboard();
|
||||||
resetButtons();
|
resetButtons();
|
||||||
}
|
}
|
||||||
|
|
||||||
// All states (is<name>Down will return whether it has been held down but <name>Press only returns a press)
|
// All states (is<name>Down will return whether it has been held down but <name>Press only returns a press)
|
||||||
|
|
||||||
public boolean isKeyDown(int key) {
|
public boolean isKeyDown(int key) {
|
||||||
return keys[key];
|
return keys[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean keyPress(int key) {
|
public boolean keyPress(int key) {
|
||||||
return keyState[key] == GLFW.GLFW_PRESS;
|
return keyState[key] == GLFW.GLFW_PRESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean keyReleased(int key) {
|
public boolean keyReleased(int key) {
|
||||||
return keyState[key] == GLFW.GLFW_RELEASE;
|
return keyState[key] == GLFW.GLFW_RELEASE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isButtonDown(int button) {
|
public boolean isButtonDown(int button) {
|
||||||
return buttons[button];
|
return buttons[button];
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean buttonPress(int button) {
|
public boolean buttonPress(int button) {
|
||||||
return buttonState[button] == GLFW.GLFW_PRESS;
|
return buttonState[button] == GLFW.GLFW_PRESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean buttonReleased(int button) {
|
public boolean buttonReleased(int button) {
|
||||||
return buttonState[button] == GLFW.GLFW_RELEASE;
|
return buttonState[button] == GLFW.GLFW_RELEASE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resets keyboard and buttons so the presses will only be registered once
|
// Resets keyboard and buttons so the presses will only be registered once
|
||||||
private void resetKeyboard() {
|
private void resetKeyboard() {
|
||||||
Arrays.fill(keyState, -1);
|
Arrays.fill(keyState, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetButtons() {
|
private void resetButtons() {
|
||||||
Arrays.fill(buttonState, -1);
|
Arrays.fill(buttonState, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// This function should only be called after all input has been taken inside the loop. It must be called if keyPress and buttonPress should work.
|
// This function should only be called after all input has been taken inside the loop. It must be called if keyPress and buttonPress should work.
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
resetKeyboard();
|
resetKeyboard();
|
||||||
resetButtons();
|
resetButtons();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scroll, mouse, and window getters
|
// Scroll, mouse, and window getters
|
||||||
|
|
||||||
public double getMouseX() {
|
public double getMouseX() {
|
||||||
return mouseX;
|
return mouseX;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getMouseY() {
|
public double getMouseY() {
|
||||||
return mouseY;
|
return mouseY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean inWindow() {
|
public boolean inWindow() {
|
||||||
return inWindow;
|
return inWindow;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getScrollX() {
|
public double getScrollX() {
|
||||||
return scrollX;
|
return scrollX;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getScrollY() {
|
public double getScrollY() {
|
||||||
return scrollY;
|
return scrollY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
package org.hl.engine.math;
|
|
||||||
|
|
||||||
|
|
||||||
public class Vector3f {
|
|
||||||
private float x, y, z;
|
|
||||||
|
|
||||||
public Vector3f(float x, float y, float z) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
this.z = z;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setVector(float x, float y, float z) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
this.z = z;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getX() {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setX(float x) {
|
|
||||||
this.x = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getY() {
|
|
||||||
return y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setY(float y) {
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getZ() {
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setZ(float z) {
|
|
||||||
this.z = z;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
33
src/org/hl/engine/math/lalg/Vector2f.java
Normal file
33
src/org/hl/engine/math/lalg/Vector2f.java
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package org.hl.engine.math.lalg;
|
||||||
|
|
||||||
|
public class Vector2f {
|
||||||
|
private float x;
|
||||||
|
private float y;
|
||||||
|
|
||||||
|
// Just a vector if you know what I mean
|
||||||
|
public Vector2f (float x, float y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVector(float x, float y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setX(float x) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setY(float y) {
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
}
|
46
src/org/hl/engine/math/lalg/Vector3f.java
Normal file
46
src/org/hl/engine/math/lalg/Vector3f.java
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package org.hl.engine.math.lalg;
|
||||||
|
|
||||||
|
|
||||||
|
public class Vector3f {
|
||||||
|
private float x;
|
||||||
|
private float y;
|
||||||
|
private float z;
|
||||||
|
|
||||||
|
// Just a vector if you know what I mean
|
||||||
|
public Vector3f (float x, float y, float z) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVector(float x, float y, float z) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setX(float x) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setY(float y) {
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getZ() {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setZ(float z) {
|
||||||
|
this.z = z;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
55
src/org/hl/engine/math/lalg/Vector4f.java
Normal file
55
src/org/hl/engine/math/lalg/Vector4f.java
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
package org.hl.engine.math.lalg;
|
||||||
|
|
||||||
|
public class Vector4f {
|
||||||
|
private float x;
|
||||||
|
private float y;
|
||||||
|
private float z;
|
||||||
|
private float a;
|
||||||
|
|
||||||
|
// Just a vector if you know what I mean
|
||||||
|
public Vector4f (float x, float y, float z, float a) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
this.a = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVector(float x, float y, float z, float a) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
this.a = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setX(float x) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setY(float y) {
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getZ() {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setZ(float z) {
|
||||||
|
this.z = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getA() {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setA(float a) {
|
||||||
|
this.a = a;
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,22 +7,22 @@ import java.io.InputStreamReader;
|
||||||
|
|
||||||
public class FileUtils {
|
public class FileUtils {
|
||||||
|
|
||||||
// Reads a filepath and returns that as a String
|
// Reads a filepath and returns that as a String
|
||||||
public static String loadAsString(String filepath) {
|
public static String loadAsString(String filepath) {
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
try (BufferedReader reader = new BufferedReader(new
|
try (BufferedReader reader = new BufferedReader(new
|
||||||
InputStreamReader(FileUtils.class.getResourceAsStream(filepath)))) {
|
InputStreamReader(FileUtils.class.getResourceAsStream(filepath)))) {
|
||||||
String line = "";
|
String line = "";
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
result.append(line) .append("\n");
|
result.append(line) .append("\n");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("Couldn't get the file at " + filepath);
|
System.err.println("Couldn't get the file at " + filepath);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
#version 330 core
|
#version 410 core
|
||||||
|
|
||||||
in vec3 passColor;
|
|
||||||
|
|
||||||
out vec4 outColor;
|
|
||||||
|
layout(location = 0) in vec3 passColor;
|
||||||
|
layout(location = 1) in vec2 passTextureCoord;
|
||||||
|
|
||||||
|
layout(location = 0) out vec4 outColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
outColor = vec4(passColor, 1.0);
|
outColor = vec4(passColor, 1.0);
|
||||||
|
|
|
@ -1,11 +1,21 @@
|
||||||
#version 330 core
|
#version 410 core
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
layout(location = 0) in vec3 position;
|
layout(location = 0) in vec3 position;
|
||||||
layout(location = 1) in vec3 color;
|
layout(location = 1) in vec3 color;
|
||||||
|
layout(location = 2) in vec2 textureCoord;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
layout(location = 0) out vec3 passColor;
|
||||||
|
layout(location = 1) out vec2 passTextureCoord;
|
||||||
|
|
||||||
out vec3 passColor;
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = vec4(position, 1.0);
|
gl_Position = vec4(position, 1.0);
|
||||||
passColor = color;
|
passColor = color;
|
||||||
|
|
||||||
|
passTextureCoord = textureCoord;
|
||||||
}
|
}
|
Reference in New Issue
Block a user