Textures added

This commit is contained in:
firefighter198
2021-09-13 01:21:35 +02:00
parent af0ea4ef24
commit 7e02628b42
28 changed files with 668 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
+21
View File
@@ -0,0 +1,21 @@
<component name="libraryTable">
<library name="lwjgl">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/jars/lwjgl.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jars/lwjgl_util.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jars/slick-util.jar!/" />
</CLASSES>
<JAVADOC />
<NATIVE>
<root url="file://$PROJECT_DIR$/lib/natives" />
<root url="file://$PROJECT_DIR$/lib/natives" />
<root url="file://$PROJECT_DIR$/lib/natives" />
<root url="file://$PROJECT_DIR$/lib/natives" />
<root url="file://$PROJECT_DIR$/lib/natives" />
<root url="file://$PROJECT_DIR$/lib/natives" />
<root url="file://$PROJECT_DIR$/lib/natives" />
<root url="file://$PROJECT_DIR$/lib/natives" />
</NATIVE>
<SOURCES />
</library>
</component>
+6
View 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="true" project-jdk-name="adopt-openj9-1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Rendr.iml" filepath="$PROJECT_DIR$/Rendr.iml" />
</modules>
</component>
</project>
+124
View 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>
+12
View File
@@ -0,0 +1,12 @@
<?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" level="project" />
</component>
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,13 @@
#version 400 core
in vec3 color;
in vec2 passUvs;
out vec4 outColor;
uniform sampler2D textureSampler;
void main(void)
{
outColor = texture(textureSampler, passUvs);
}
@@ -0,0 +1,13 @@
#version 400 core
in vec3 position;
in vec2 uvs;
out vec3 color;
out vec2 passUvs;
void main(void)
{
gl_Position = vec4(position.x, position.y, position.z, 1.0);
passUvs = uvs;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 MiB

+64
View File
@@ -0,0 +1,64 @@
package EngineTest;
import RenderEngine.DisplayManager;
import RenderEngine.Loader;
import models.RawModel;
import RenderEngine.Renderer;
import Shaders.StaticShader;
import models.TexturedModel;
import org.lwjgl.opengl.Display;
import textures.ModelTexture;
public class MainGameLoop
{
/**
* A test class of the render engine
* @param args
*/
public static void main(String[] args)
{
DisplayManager.createDisplay("Rendr");
Loader loader = new Loader();
Renderer renderer = new Renderer();
StaticShader shader = new StaticShader();
float[] vertices =
{
-.5f, .5f, 0,
-.5f, -.5f, 0,
.5f, -.5f, 0,
.5f, .5f, 0
};
int[] indices =
{
0, 1, 3,
3, 1, 2,
};
float[] uvs =
{
0, 0,
0, 1,
1, 1,
1, 0
};
RawModel model = loader.loadToVAO(vertices, indices, uvs);
ModelTexture texture = new ModelTexture(loader.loadTexture("mossyBricks"));
TexturedModel texturedModel = new TexturedModel(model, texture);
while(!Display.isCloseRequested())
{
renderer.prepare(0, 0, 0);
shader.start();
renderer.render(texturedModel);
shader.stop();
DisplayManager.updateDisplay();
}
shader.clear();
loader.clear();
DisplayManager.closeDisplay();
}
}
@@ -0,0 +1,42 @@
package RenderEngine;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
public class DisplayManager
{
private static final int width = 1280, height = 700;
private static final int fps = 60;
//creates a new display with openGL viewport in it
public static void createDisplay(String title)
{
ContextAttribs attribs = new ContextAttribs(3, 2)
.withForwardCompatible(true).withProfileCore(true);
try
{
Display.setDisplayMode(new DisplayMode(width, height));
Display.create(new PixelFormat(), attribs);
Display.setTitle(title);
} catch (LWJGLException e)
{
e.printStackTrace();
}
GL11.glViewport(0, 0, width, height);
}
//updates the display (limited to the given fps)
public static void updateDisplay()
{
Display.sync(fps);
Display.update();
}
//destroys the display
public static void closeDisplay()
{
Display.destroy();
}
}
+132
View File
@@ -0,0 +1,132 @@
package RenderEngine;
import models.RawModel;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;
public class Loader
{
private List<Integer> vaos = new ArrayList<>();
private List<Integer> vbos = new ArrayList<>();
private List<Integer> textures = new ArrayList<>();
public RawModel loadToVAO(float[] vertices, int[] indices, float[] uvs)
{
//create a new vao
int vaoID = createVAO();
bindIndexBuffer(indices);
storeDataToAttList(0, 3, vertices);
storeDataToAttList(1, 2, uvs);
unbindVAO();
return new RawModel(vaoID, indices.length);
}
public int loadTexture(String fileName)
{
Texture texture = null;
try
{
texture = TextureLoader.getTexture("PNG", new FileInputStream("res/"+fileName+".png"));
} catch (IOException e)
{
e.printStackTrace();
}
int textureID = texture.getTextureID();
textures.add(textureID);
return textureID;
}
public void clear()
{
//delete all the vaos from memory
for(int vao:vaos)
{
GL30.glDeleteVertexArrays(vao);
}
//delete all the vbos from memory
for(int vbo:vbos)
{
GL15.glDeleteBuffers(vbo);
}
//delete all the textures from memory
for(int texture:textures)
{
GL11.glDeleteTextures(texture);
}
}
private int createVAO()
{
//generate a new vao and store as an int pointer
int vaoID = GL30.glGenVertexArrays();
vaos.add(vaoID);
//bind the current vao
GL30.glBindVertexArray(vaoID);
return vaoID;
}
private void storeDataToAttList(int attNumber, int coordinateSize, float[] data)
{
//generate a new vbo and store as an int pointer
int vboID = GL15.glGenBuffers();
vbos.add(vboID);
//bind the vbo as an array buffer
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
//prepare a float buffer to be load into the vbo
FloatBuffer buffer = storeDataToFloatBuffer(data);
//load in the float buffer as an static object
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
//add the vbo to the given list in the already bound vao
GL20.glVertexAttribPointer(attNumber, coordinateSize, GL11.GL_FLOAT, false, 0, 0);
//unbind the vbo
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
private void unbindVAO()
{
GL30.glBindVertexArray(0);
}
private void bindIndexBuffer(int[] indices)
{
int vboID = GL15.glGenBuffers();
vbos.add(vboID);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
IntBuffer buffer = storeDataToIntBuffer(indices);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
}
private IntBuffer storeDataToIntBuffer(int[] data)
{
IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
private FloatBuffer storeDataToFloatBuffer(float[] data)
{
//create a new float buffer with size of the data array
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
//put all the data into the float buffer
buffer.put(data);
//turn the float buffer from write to read mode
buffer.flip();
return buffer;
}
}
+37
View File
@@ -0,0 +1,37 @@
package RenderEngine;
import models.RawModel;
import models.TexturedModel;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
public class Renderer
{
public void prepare(float r, float g, float b)
{
GL11.glClearColor(r, g, b, 1);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
}
public void render(TexturedModel texturedModel)
{
RawModel model = texturedModel.getRawModel();
//bind the models vao
GL30.glBindVertexArray(model.getVaoID());
//vertex data is in list 0
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturedModel.getTexture().getID());
//render the model in triangles
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
//unbind and disable everything
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0);
}
}
+79
View File
@@ -0,0 +1,79 @@
package Shaders;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public abstract class ShaderProgram
{
private int programID;
private int vertexShaderID;
private int fragmentShaderID;
public ShaderProgram(String vertexFile, String fragmentFile)
{
vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
programID = GL20.glCreateProgram();
GL20.glAttachShader(programID, vertexShaderID);
GL20.glAttachShader(programID, fragmentShaderID);
bindAttributes();
GL20.glLinkProgram(programID);
GL20.glValidateProgram(programID);
}
public void start()
{
GL20.glUseProgram(programID);
}
public void stop()
{
GL20.glUseProgram(0);
}
public void clear()
{
stop();
GL20.glDetachShader(programID, vertexShaderID);
GL20.glDetachShader(programID, fragmentShaderID);
GL20.glDeleteShader(vertexShaderID);
GL20.glDeleteShader(fragmentShaderID);
GL20.glDeleteProgram(programID);
}
protected abstract void bindAttributes();
protected void bindAttribute(int attribute, String variableName)
{
GL20.glBindAttribLocation(programID, attribute, variableName);
}
private static int loadShader(String file, int type)
{
StringBuilder shaderSource = new StringBuilder();
try
{
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while((line = reader.readLine()) != null)
{
shaderSource.append(line).append("\n");
}
reader.close();
} catch (IOException e)
{
e.printStackTrace();
}
int shaderID = GL20.glCreateShader(type);
GL20.glShaderSource(shaderID, shaderSource);
GL20.glCompileShader(shaderID);
return shaderID;
}
}
+18
View File
@@ -0,0 +1,18 @@
package Shaders;
public class StaticShader extends ShaderProgram
{
private static final String vertexFile = "src/shaders/vertexShader.glsl", fragmentFile = "src/shaders/fragmentShader.glsl";
public StaticShader()
{
super(vertexFile, fragmentFile);
}
@Override
protected void bindAttributes()
{
bindAttribute(0, "position");
bindAttribute(1, "uvs");
}
}
+13
View File
@@ -0,0 +1,13 @@
#version 400 core
in vec3 color;
in vec2 passUvs;
out vec4 outColor;
uniform sampler2D textureSampler;
void main(void)
{
outColor = texture(textureSampler, passUvs);
}
+13
View File
@@ -0,0 +1,13 @@
#version 400 core
in vec3 position;
in vec2 uvs;
out vec3 color;
out vec2 passUvs;
void main(void)
{
gl_Position = vec4(position.x, position.y, position.z, 1.0);
passUvs = uvs;
}
+24
View File
@@ -0,0 +1,24 @@
package models;
public class RawModel
{
private int vaoID;
private int vertexCount;
//stores information about the vao of an model
public RawModel(int vaoID, int vertexCount)
{
this.vaoID = vaoID;
this.vertexCount = vertexCount;
}
public int getVaoID()
{
return vaoID;
}
public int getVertexCount()
{
return vertexCount;
}
}
+25
View File
@@ -0,0 +1,25 @@
package models;
import textures.ModelTexture;
public class TexturedModel
{
private RawModel rawModel;
private ModelTexture texture;
public TexturedModel(RawModel rawModel, ModelTexture texture)
{
this.rawModel = rawModel;
this.texture = texture;
}
public RawModel getRawModel()
{
return rawModel;
}
public ModelTexture getTexture()
{
return texture;
}
}
+16
View File
@@ -0,0 +1,16 @@
package textures;
public class ModelTexture
{
private int textureID;
public ModelTexture(int id)
{
this.textureID = id;
}
public int getID()
{
return textureID;
}
}