This commit is contained in:
0x01fe committed 2024-09-22 22:11:28 -05:00
commit 2d257839c3
17 files changed
+13161

No files matched your search

+133
View File
@@ -0,0 +1,133 @@
package org.example;
import org.lwjgl.*;
import org.lwjgl.Version;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
import java.nio.*;
import java.io.*;
import java.text.MessageFormat;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
import com.mokiat.data.front.parser.*;
import org.lwjgl.system.MemoryStack;
public class Game
{
private long window;
public void run()
{
System.out.println("Running LWJGL Version " + Version.getVersion() + ".");
init();
loop();
// Free the window callbacks and destroy the window
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
// Terminate GLFW and free the error callback
glfwTerminate();
glfwSetErrorCallback(null).free();
}
public void init()
{
GLFWErrorCallback.createPrint(System.err).set();
if (!glfwInit())
throw new IllegalStateException("Unable to Initialize GLFW");
// Configure GLFW
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
// Create the window
window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL);
if ( window == NULL )
throw new RuntimeException("Failed to create the GLFW window");
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
});
// Get the thread stack and push a new frame
try ( MemoryStack stack = stackPush() ) {
IntBuffer pWidth = stack.mallocInt(1); // int*
IntBuffer pHeight = stack.mallocInt(1); // int*
// Get the window size passed to glfwCreateWindow
glfwGetWindowSize(window, pWidth, pHeight);
// Get the resolution of the primary monitor
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// Center the window
glfwSetWindowPos(
window,
(vidmode.width() - pWidth.get(0)) / 2,
(vidmode.height() - pHeight.get(0)) / 2
);
} // the stack frame is popped automatically
// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);
// Make the window visible
glfwShowWindow(window);
// Load Objects
try (InputStream in = this.getClass().getResourceAsStream("/objs/cube.obj")) {
// Create an OBJParser and parse the resource
final IOBJParser parser = new OBJParser();
final OBJModel model = parser.parse(in);
// Use the model representation to get some basic info
System.out.println(MessageFormat.format(
"OBJ model has {0} vertices, {1} normals, {2} texture coordinates, and {3} objects.",
model.getVertices().size(),
model.getNormals().size(),
model.getTexCoords().size(),
model.getObjects().size()));
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
private void loop()
{
GL.createCapabilities();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
while ( !glfwWindowShouldClose(window) )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
public static void main(String[] args)
{
new Game().run();
}
}
@@ -0,0 +1,80 @@
package org.example;
import static org.lwjgl.opengl.GL20.*;
public class ShaderProgram {
private final int programId;
private int vertexShaderId;
private int fragmentShaderId;
public ShaderProgram() throws Exception {
programId = glCreateProgram();
if (programId == 0) {
throw new Exception("Could not create Shader");
}
}
public void createVertexShader(String shaderCode) throws Exception {
vertexShaderId = createShader(shaderCode, GL_VERTEX_SHADER);
}
public void createFragmentShader(String shaderCode) throws Exception {
fragmentShaderId = createShader(shaderCode, GL_FRAGMENT_SHADER);
}
protected int createShader(String shaderCode, int shaderType) throws Exception {
int shaderId = glCreateShader(shaderType);
if (shaderId == 0) {
throw new Exception("Error creating shader. Type: " + shaderType);
}
glShaderSource(shaderId, shaderCode);
glCompileShader(shaderId);
if (glGetShaderi(shaderId, GL_COMPILE_STATUS) == 0) {
throw new Exception("Error compiling Shader code: " + glGetShaderInfoLog(shaderId, 1024));
}
glAttachShader(programId, shaderId);
return shaderId;
}
public void link() throws Exception {
glLinkProgram(programId);
if (glGetProgrami(programId, GL_LINK_STATUS) == 0) {
throw new Exception("Error linking Shader code: " + glGetProgramInfoLog(programId, 1024));
}
if (vertexShaderId != 0) {
glDetachShader(programId, vertexShaderId);
}
if (fragmentShaderId != 0) {
glDetachShader(programId, fragmentShaderId);
}
glValidateProgram(programId);
if (glGetProgrami(programId, GL_VALIDATE_STATUS) == 0) {
System.err.println("Warning validating Shader code: " + glGetProgramInfoLog(programId, 1024));
}
}
public void bind() {
glUseProgram(programId);
}
public void unbind() {
glUseProgram(0);
}
public void cleanup() {
unbind();
if (programId != 0) {
glDeleteProgram(programId);
}
}
}
+221
View File
@@ -0,0 +1,221 @@
# Blender v2.79 (sub 0) OBJ File: 'axis.blend'
# www.blender.org
v -11.748000 -0.360000 1.268000
v -11.276000 -0.360000 1.268000
v -10.424000 -0.360000 0.228000
v -9.568000 -0.360000 1.268000
v -9.092000 -0.360000 1.268000
v -10.184000 -0.360000 -0.048000
v -9.008000 -0.360000 -1.460000
v -9.484000 -0.360000 -1.460000
v -10.424000 -0.360000 -0.332000
v -11.352000 -0.360000 -1.460000
v -11.824000 -0.360000 -1.460000
v -10.660000 -0.360000 -0.048000
v -9.484000 0.373033 -1.460000
v -10.424000 0.373033 -0.332000
v -11.748000 0.373033 1.268000
v -10.660000 0.373033 -0.048000
v -9.568000 0.373033 1.268000
v -10.184000 0.373033 -0.048000
v -11.824000 0.373033 -1.460000
v -9.008000 0.373033 -1.460000
v -10.424000 0.373033 0.228000
v -11.352000 0.373033 -1.460000
v -9.092000 0.373033 1.268000
v -11.276000 0.373033 1.268000
v -0.620000 -0.380000 11.376000
v 1.116000 -0.380000 9.000000
v -1.344000 -0.380000 9.000000
v -1.344000 -0.380000 9.352000
v 0.392000 -0.380000 9.352000
v -1.344000 -0.380000 11.728000
v 1.008000 -0.380000 11.728000
v 1.008000 -0.380000 11.376000
v 0.392000 0.384923 9.352000
v -1.344000 0.384923 11.728000
v -0.620000 0.384923 11.376000
v -1.344000 0.384923 9.352000
v 1.116000 0.384923 9.000000
v 1.008000 0.384923 11.728000
v 1.008000 0.384923 11.376000
v -1.344000 0.384923 9.000000
v -0.250000 10.788000 0.104000
v -0.250000 11.728000 -0.628000
v -0.250000 11.728000 -1.100000
v -0.250000 10.444000 -0.088000
v -0.250000 9.000000 -0.088000
v -0.250000 9.000000 0.304000
v -0.250000 10.440000 0.304000
v -0.250000 11.728000 1.316000
v -0.250000 11.728000 0.844000
v 0.304321 9.000000 -0.088000
v 0.304321 9.000000 0.304000
v 0.304321 10.440000 0.304000
v 0.304321 10.444000 -0.088000
v 0.304321 11.728000 1.316000
v 0.304321 10.788000 0.104000
v 0.304321 11.728000 -0.628000
v 0.304321 11.728000 -1.100000
v 0.304321 11.728000 0.844000
v 0.500000 -0.500000 -0.500000
v 0.500000 0.500000 -0.500000
v 0.500000 -0.500000 0.500000
v 0.500000 0.500000 0.500000
v -0.500000 -0.500000 -0.500000
v -0.500000 0.500000 -0.500000
v -0.500000 -0.500000 0.500000
v -0.500000 0.500000 0.500000
v -8.500000 0.500000 0.500000
v -8.500000 -0.500000 0.500000
v -8.500000 -0.500000 -0.500000
v -8.500000 0.500000 -0.500000
v 0.500000 8.500000 -0.500000
v 0.500000 8.500000 0.500000
v -0.500000 8.500000 0.500000
v -0.500000 8.500000 -0.500000
v 0.500000 0.500000 8.500000
v 0.500000 -0.500000 8.500000
v -0.500000 -0.500000 8.500000
v -0.500000 0.500000 8.500000
s off
f 6 5 4
f 6 4 3
f 3 2 1
f 3 1 12
f 6 3 12
f 7 6 12
f 7 12 9
f 9 12 11
f 7 9 8
f 10 9 11
f 18 17 23
f 18 21 17
f 21 15 24
f 21 16 15
f 18 16 21
f 20 16 18
f 20 14 16
f 14 19 16
f 20 13 14
f 22 19 14
f 6 23 5
f 9 13 8
f 10 14 9
f 7 18 6
f 1 16 12
f 11 22 10
f 2 15 1
f 5 17 4
f 4 21 3
f 3 24 2
f 8 20 7
f 12 19 11
f 32 31 30
f 32 30 25
f 25 30 29
f 26 25 29
f 26 29 28
f 26 28 27
f 39 34 38
f 39 35 34
f 35 33 34
f 37 33 35
f 37 36 33
f 37 40 36
f 28 40 27
f 25 39 32
f 32 38 31
f 26 35 25
f 29 36 28
f 30 33 29
f 27 37 26
f 31 34 30
f 44 42 43
f 44 41 42
f 41 48 49
f 41 47 48
f 44 47 41
f 45 47 44
f 45 46 47
f 53 57 56
f 53 56 55
f 55 58 54
f 55 54 52
f 53 55 52
f 50 53 52
f 50 52 51
f 58 41 49
f 50 46 45
f 55 42 41
f 54 49 48
f 51 47 46
f 52 48 47
f 56 43 42
f 57 44 43
f 53 45 44
f 60 61 59
f 65 78 66
f 66 68 65
f 64 59 63
f 65 59 61
f 60 72 62
f 67 69 68
f 63 70 64
f 65 69 63
f 64 67 66
f 72 74 73
f 64 71 60
f 62 73 66
f 66 74 64
f 75 77 76
f 61 77 65
f 66 75 62
f 62 76 61
f 6 18 23
f 9 14 13
f 10 22 14
f 7 20 18
f 1 15 16
f 11 19 22
f 2 24 15
f 5 23 17
f 4 17 21
f 3 21 24
f 8 13 20
f 12 16 19
f 28 36 40
f 25 35 39
f 32 39 38
f 26 37 35
f 29 33 36
f 30 34 33
f 27 40 37
f 31 38 34
f 58 55 41
f 50 51 46
f 55 56 42
f 54 58 49
f 51 52 47
f 52 54 48
f 56 57 43
f 57 53 44
f 53 50 45
f 60 62 61
f 65 77 78
f 66 67 68
f 64 60 59
f 65 63 59
f 60 71 72
f 67 70 69
f 63 69 70
f 65 68 69
f 64 70 67
f 72 71 74
f 64 74 71
f 62 72 73
f 66 73 74
f 75 78 77
f 61 76 77
f 66 78 75
f 62 75 76
+30
View File
@@ -0,0 +1,30 @@
# Blender 4.1.1
# www.blender.org
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
vn -0.0000 1.0000 -0.0000
vn -0.0000 -0.0000 1.0000
vn -1.0000 -0.0000 -0.0000
vn -0.0000 -1.0000 -0.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 -1.0000
s 0
f 5//1 3//1 1//1
f 3//2 8//2 4//2
f 7//3 6//3 8//3
f 2//4 8//4 6//4
f 1//5 4//5 2//5
f 5//6 2//6 6//6
f 5//1 7//1 3//1
f 3//2 7//2 8//2
f 7//3 5//3 6//3
f 2//4 4//4 8//4
f 1//5 3//5 4//5
f 5//6 1//6 2//6
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+18
View File
@@ -0,0 +1,18 @@
# Blender 4.1.1
# www.blender.org
mtllib tetrahedron.mtl
o Tetrahedron
v 0.589834 1.455189 -0.731109
v -0.654905 0.362953 1.561846
v -1.236534 -0.687860 -0.998915
v 1.301606 -1.130282 0.168178
vn -0.3405 -0.8402 0.4221
vn -0.7515 0.6526 -0.0971
vn 0.7139 0.3971 0.5767
vn 0.3781 -0.2096 -0.9017
s 0
usemtl
f 3//1 4//1 2//1
f 2//2 1//2 3//2
f 1//3 2//3 4//3
f 1//4 4//4 3//4
+52
View File
@@ -0,0 +1,52 @@
#version 330 core
out vec4 FragColor;
uniform float light_power;
uniform vec3 lightColor;
uniform vec3 objectColor;
uniform vec3 viewPos;
in vec3 FragPos;
in vec3 Normal;
in vec3 LightPos;
// const float light_power = 12.0;
const float ambientStrength = 0.2;
const float shininess = 64.0;
const float screenGamma = 2.2; // Assume the monitor is calibrated to the sRGB color space
void main()
{
// FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
vec3 lightDir = LightPos - FragPos;
float distance = length(lightDir);
vec3 norm = normalize(Normal);
vec3 ambient = ambientStrength * lightColor;
distance = distance * distance;
lightDir = normalize(lightDir);
float lambertian = max(dot(norm, lightDir), 0.0);
vec3 diffuse = lambertian * lightColor;
float specular = 0.0;
if (lambertian > 0.0)
{
vec3 viewDir = normalize(-FragPos);
vec3 halfDir = normalize(lightDir + viewDir);
float specAngle = max(dot(halfDir, norm), 0.0);
specular = pow(specAngle, shininess);
}
vec3 colorLinear = (ambient + (diffuse * light_power) + (specular * light_power)) * objectColor;
// vec3 colorGammaCorrected = pow(colorLinear, vec3(1.0 / screenGamma));
FragColor = vec4(colorLinear, 1.0);
// vec3 norm = normalize(Normal);
}
+22
View File
@@ -0,0 +1,22 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
//layout (location = 1) in vec3 aColor;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform vec3 lightPos;
out vec3 FragPos;
out vec3 Normal;
out vec3 LightPos;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
FragPos = vec3(view * model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(view * model))) * aNormal;
LightPos = vec3(view * vec4(lightPos, 1.0));
}
+11
View File
@@ -0,0 +1,11 @@
#version 330 core
out vec4 FragColor;
uniform vec3 lightColor;
void main()
{
FragColor = vec4(lightColor, 1.0);
}
+15
View File
@@ -0,0 +1,15 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
}