[DEV] tutorial 24 'render GUI' implementation

This commit is contained in:
Edouard DUPIN 2020-04-24 22:58:08 +02:00
parent 4069fd6ae0
commit 58441b9743
9 changed files with 175 additions and 3 deletions

BIN
res/socuwan.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 KiB

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.atriaSoft.etk.math.Vector2f;
import org.atriaSoft.etk.math.Vector3f;
import org.lwjgl.openvr.Texture;
@ -12,6 +13,8 @@ import entities.Camera;
import entities.Entity;
import entities.Light;
import entities.Player;
import guis.GuiRenderer;
import guis.GuiTexture;
import models.RawModel;
import models.TexturedModel;
import renderEngine.DisplayManager;
@ -111,15 +114,22 @@ public class MainGameLoop {
Camera camera = new Camera(player);
List<GuiTexture> guis = new ArrayList<GuiTexture>();
GuiTexture gui = new GuiTexture(loader.loadTexture("socuwan"), new Vector2f(0.5f, 0.5f), new Vector2f(0.25f, 0.25f));
guis.add(gui);
GuiRenderer guiRenderer = new GuiRenderer(loader);
MasterRenderer renderer = new MasterRenderer();
manager.setDrawer(new DisplayManagerDraw() {
@Override
public void draw() {
//entity.increasePosition(0.0f, 0, -0.01f);
//entity.increaseRotation(0, 0, 0.01f);
//entity.increaseRotation(0.01f, 0.02f, 0.0f);
camera.move();
player.move(terrain);
camera.move();
renderer.processTerrain(terrain);
renderer.processEntity(player);
for (Entity entity : entities) {
@ -127,9 +137,11 @@ public class MainGameLoop {
renderer.processEntity(entity);
}
renderer.render(light, camera);
guiRenderer.render(guis);
}
});
manager.loop();
guiRenderer.cleanUp();
renderer.cleanUp();
loader.cleanUp();
manager.unInit();

52
src/guis/GuiRenderer.java Normal file
View File

@ -0,0 +1,52 @@
package guis;
import java.util.List;
import org.atriaSoft.etk.math.Matrix4f;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import models.RawModel;
import renderEngine.Loader;
import toolbox.Maths;
public class GuiRenderer {
private final RawModel quad;
private GuiShader shader;
public GuiRenderer(Loader loader) {
float[] positions = {-1, 1, -1, -1, 1, 1, 1, -1};
quad = loader.loadToVAO(positions);
shader = new GuiShader();
}
public void render(List<GuiTexture> guis) {
shader.start();
//System.out.println("Render Gui " + guis.size());
GL30.glBindVertexArray(quad.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glDisable(GL11.GL_DEPTH_TEST);
for (GuiTexture gui: guis) {
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, gui.getTexture());
Matrix4f matrix = Maths.createTransformationMatrix(gui.getPosition(), gui.getScale());
shader.loadTransformation(matrix);
GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, quad.getVertexCount());
}
GL11.glDisable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
shader.stop();
}
public void cleanUp() {
shader.cleanUp();
}
}

35
src/guis/GuiShader.java Normal file
View File

@ -0,0 +1,35 @@
package guis;
import org.atriaSoft.etk.math.Matrix4f;
import shaders.ShaderProgram;
public class GuiShader extends ShaderProgram {
private static final String VERTEX_FILE = "src/guis/guiVertexShader.txt";
private static final String FRAGMENT_FILE = "src/guis/guiFragmentShader.txt";
private int location_transformationMatrix;
public GuiShader() {
super(VERTEX_FILE, FRAGMENT_FILE);
}
public void loadTransformation(Matrix4f matrix){
super.loadMatrix(location_transformationMatrix, matrix);
}
@Override
protected void getAllUniformLocations() {
location_transformationMatrix = super.getUniformLocation("transformationMatrix");
}
@Override
protected void bindAttributes() {
super.bindAttribute(0, "position");
}
}

33
src/guis/GuiTexture.java Normal file
View File

@ -0,0 +1,33 @@
package guis;
import org.atriaSoft.etk.math.Vector2f;
public class GuiTexture {
private int texture;
private Vector2f position;
private Vector2f scale;
public GuiTexture(int texture, Vector2f position, Vector2f scale) {
this.texture = texture;
this.position = position;
this.scale = scale;
}
public int getTexture() {
return texture;
}
public void setTexture(int texture) {
this.texture = texture;
}
public Vector2f getPosition() {
return position;
}
public void setPosition(Vector2f position) {
this.position = position;
}
public Vector2f getScale() {
return scale;
}
public void setScale(Vector2f scale) {
this.scale = scale;
}
}

View File

@ -0,0 +1,11 @@
#version 400 core
in vec2 textureCoords;
out vec4 out_Color;
uniform sampler2D guiTexture;
void main(void){
out_Color = texture(guiTexture,textureCoords);
}

View File

@ -0,0 +1,13 @@
#version 400 core
in vec2 position;
out vec2 textureCoords;
uniform mat4 transformationMatrix;
void main(void){
gl_Position = transformationMatrix * vec4(position, 0.0, 1.0);
textureCoords = vec2((position.x+1.0)/2.0, 1 - (position.y+1.0)/2.0);
}

View File

@ -50,8 +50,17 @@ public class Loader {
return new RawModel(vaoID, indices.length);
}
public RawModel loadToVAO(float[] positions) {
int vaoID = createVAO();
this.storeDataInAttributeList(0, 2, positions);
unbindVAO();
return new RawModel(vaoID, positions.length/2);
}
public int loadTexture(String filename) {
int textureId = loadPNGTexture("res/" + filename + ".png", GL13.GL_TEXTURE0);
String fullname = "res/" + filename + ".png";
System.out.println("Load texture : " + fullname);
int textureId = loadPNGTexture(fullname, GL13.GL_TEXTURE0);
this.textures.add(textureId);
return textureId;
}

View File

@ -38,4 +38,11 @@ public class Maths {
matrix.translate(new Vector3f(-camarePos.x,-camarePos.y,-camarePos.z));
return matrix;
}
public static Matrix4f createTransformationMatrix(Vector2f translation, Vector2f scale) {
Matrix4f matrix = new Matrix4f();
matrix.setIdentity();
matrix.translate(new Vector3f(translation.x, translation.y, 0));
matrix.scale(new Vector3f(scale.x, scale.y, 1f));
return matrix;
}
}