Hello Guest

bad performance with my LWJGL code

  • 1 Replies
  • 164 Views
*

Offline v22

  • *
  • 2
bad performance with my LWJGL code
« on: May 03, 2022, 15:06:23 »
Hello guys,
currently i am making a 2d game in java using lwjgl. I made a "graphics engine" first, but it seems like it has bad performance for a small amount of objects. So i am relative new to openGL and i cant find the issue. Maybe someone can help me out! Thanks so far :D

Also sorry for my english

Heres my code for initializing:
Code: [Select]
package Vafix;

import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
import static org.lwjgl.glfw.GLFW.GLFW_RESIZABLE;
import static org.lwjgl.glfw.GLFW.GLFW_VISIBLE;
import static org.lwjgl.glfw.GLFW.glfwCreateWindow;
import static org.lwjgl.glfw.GLFW.glfwDefaultWindowHints;
import static org.lwjgl.glfw.GLFW.glfwDestroyWindow;
import static org.lwjgl.glfw.GLFW.glfwInit;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.glfw.GLFW.glfwPollEvents;
import static org.lwjgl.glfw.GLFW.glfwSetErrorCallback;
import static org.lwjgl.glfw.GLFW.glfwShowWindow;
import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
import static org.lwjgl.glfw.GLFW.glfwSwapInterval;
import static org.lwjgl.glfw.GLFW.glfwTerminate;
import static org.lwjgl.glfw.GLFW.glfwWindowHint;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.system.MemoryUtil.NULL;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.constant.Constable;
import java.lang.module.ModuleDescriptor.Version;
import java.util.Arrays;
import java.util.Vector;

import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.w3c.dom.html.HTMLOListElement;

import FactoryIsland.Main;

public class Display{
private String title;
private int width, height;
private boolean rez;
private float red = 0.0f;

public static long winAddr;

private int programID;
private int vertexShader;
private int fragmentShader;
private vafix main = new Main();

private float[] vertices = {
1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
};

private int[] indices = {
0, 1, 2, 1, 3, 2
};

public Display(String title, int width, int height, boolean rez) {
this.title = title;
this.width = width;
this.height = height;
this.rez = rez;

init();
gmt.init(this);
loop();

glfwFreeCallbacks(winAddr);
glfwDestroyWindow(winAddr);

glfwTerminate();
glfwSetErrorCallback(null).free();
}

private void init() {
GLFWErrorCallback.createPrint(System.err).set();

if (!glfwInit()) {
throw new IllegalStateException("Err: Cannot init glfw!");
}

glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
if (this.rez) glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
else glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

winAddr = glfwCreateWindow(this.width, this.height, this.title, NULL, NULL);

if (winAddr == NULL) {
throw new IllegalStateException("Err: Window cannot be created!");
}

glfwMakeContextCurrent(winAddr);
GL.createCapabilities();
glfwSwapInterval(1);

glfwShowWindow(winAddr);
}

private void loop() {
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
shader("shader");
bind();

Model model = new Model(null, null, winAddr, "rect");
model.coords(new float[] {0.3f, 0.3f, 0.2f, 0.2f});
Model model2 = new Model(null, null, winAddr, "rect");
model2.coords(new float[] {0.2f, -0.5f, 0.2f, 0.2f});

main.init();

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);

long lastime = System.nanoTime();
double AmountOfTicks = 60;
double ns = 1000000000 / AmountOfTicks;
double delta = 0;
int frames = 0;
double time = System.currentTimeMillis();

while (!glfwWindowShouldClose(winAddr)) {

glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);

long now = System.nanoTime();
delta += (now - lastime) / ns;
lastime = now;

if(delta >= 1) {
glEnable(GL_TEXTURE_2D);
main.running();
frames++;
delta--;
if(System.currentTimeMillis() - time >= 1000) {
System.out.println("fps:" + frames);
time += 1000;
frames = 0;
}
}

glDisable(GL_TEXTURE_2D);

glfwSwapBuffers(winAddr);
}
}



private void shader(String filename) {
programID = glCreateProgram();

vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, readFile("./shaders/", filename+".vs"));
glCompileShader(vertexShader);
if(glGetShaderi(vertexShader, GL_COMPILE_STATUS) != 1) {
System.err.println("vertex shader error: "+glGetShaderInfoLog(vertexShader));
System.exit(1);
}

fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, readFile("./shaders/", filename+".fs"));
glCompileShader(fragmentShader);
if(glGetShaderi(fragmentShader, GL_COMPILE_STATUS) != 1) {
System.err.println("fragment shader error: "+glGetShaderInfoLog(fragmentShader));
System.exit(1);
}

glAttachShader(programID, vertexShader);
glAttachShader(programID, fragmentShader);

glBindAttribLocation(programID,0 , "vertices");
glLinkProgram(programID);
if((glGetProgrami(programID, GL_LINK_STATUS)) != 1) {
System.err.println("link program error: "+glGetProgramInfoLog(programID));
System.exit(1);
}
glValidateProgram(programID);
if((glGetProgrami(programID, GL_VALIDATE_STATUS)) != 1) {
System.err.println("validate program error: "+glGetProgramInfoLog(programID));
System.exit(1);
}
}

private void bind() {
glUseProgram(programID);
}

public static String readFile(String path, String filename) {
StringBuilder string = new StringBuilder();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(new File(path + filename)));
String line;
while((line = reader.readLine()) != null) {
string.append(line);
string.append("\n");
}
reader.close();
}catch(IOException e) {
e.printStackTrace();
}

return string.toString();
}

public void setUniform1f(String name, float value) {
int location = glGetUniformLocation(programID, name);
if(location != -1) {
glUniform1f(location, value);
}
}

public void setUniform1i(String name, int value) {
int location = glGetUniformLocation(programID, name);
if(location != -1) {
glUniform1f(location, value);
}
}

public int getWidth() {
return this.width;
}

public int getHeight() {
return this.height;
}

public long getAddr() {
return this.winAddr;
}
}

And here rendering:

Code: [Select]
Error 500 when i paste it here :( Im going to replying to myself and make it there
and the Main class:
Code: [Select]
package FactoryIsland;

import java.util.Arrays;

import Vafix.Animation;
import Vafix.Display;
import Vafix.Model;
import Vafix.Texture;
import Vafix.gmt;
import Vafix.vafix;

public class Main implements vafix{

static final int WIDTH = 1000;
static final int HEIGHT = 800;

int[][] terrain = Terrain.generateTerrain();

public static void main(String[] args) {
Display win = new Display("Game", WIDTH, HEIGHT, false);

}

@Override
public void init() {

}

@Override
public void running() {
DrawTiles.draw(terrain);
}
}


*

Offline v22

  • *
  • 2
Re: rendering code
« Reply #1 on: May 03, 2022, 15:07:48 »
Code: [Select]
package Vafix;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBTextureMirrorClampToEdge;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.NVBindlessMultiDrawIndirectCount;

public class gmt {

static FloatBuffer vecBuffer;
static IntBuffer indBuffer;

private static int drawCount = 0;
private static int v_id;
private static int i_id;
private static int t_id;
private static Display d;

public static void setColor(int r, int g, int b, int a) {
d.setUniform1f("r", (float)r/255.0f);
d.setUniform1f("g", (float)g/255.0f);
d.setUniform1f("b", (float)b/255.0f);
d.setUniform1f("a", (float)a/255.0f);
}

public static void fillRect(int x1, int y1, int width1, int height1) {
float x = (float)x1/(float)d.getWidth()*2.0f-1.0f;
float y = ((float)y1/(float)d.getHeight()*2.0f-1.0f)*-1.0f;
float width = (float)width1/(float)d.getWidth()*2.0f;
float height = (float)height1/(float)d.getHeight()*2.0f;

int[] indices = {0, 2, 3, 0, 1, 3};
float[] vertices = {
x, y, 0.0f, 0.0f, 0.0f,
x + width, y, 0.0f, 0.0f, 0.0f,
x, y - height, 0.0f, 0.0f, 0.0f,
x + width, y - height, 0.0f, 0.0f, 0.0f,
};
//render(vertices, indices, false, GL_TRIANGLES);
}

public static void stripRect(int x1, int y1, int width1, int height1, int depth) {
float x = (float)x1/(float)d.getWidth()*2.0f-1.0f;
float y = ((float)y1/(float)d.getHeight()*2.0f-1.0f)*-1.0f;
float width = (float)width1/(float)d.getWidth()*2.0f;
float height = (float)height1/(float)d.getHeight()*2.0f;

glLineWidth((float)depth);

int[] indices = {0, 1, 1, 3, 2, 0, 2, 3};
float[] vertices = {
x, y, 0.0f, 0.0f, 0.0f,
x + width, y, 0.0f, 0.0f, 0.0f,
x, y - height, 0.0f, 0.0f, 0.0f,
x + width, y - height, 0.0f, 0.0f, 0.0f,
};
//render(vertices, indices, false, GL_LINES);
}

public static void drawImage(int x1, int y1, int width1, int height1, Texture tex) {
float x = (float)x1/(float)d.getWidth()*2.0f-1.0f;
float y = ((float)y1/(float)d.getHeight()*2.0f-1.0f)*-1.0f;
float width = (float)width1/(float)d.getWidth()*2.0f;
float height = (float)height1/(float)d.getHeight()*2.0f;

int[] indices = {0, 1, 2, 3, 1, 2};
float[] vertices = {
x, y, 0.0f,                  1.0f, 0.0f,
x + width, y, 0.0f,          0.0f, 0.0f,
x, y - height, 0.0f,         1.0f, 1.0f,
x + width, y - height, 0.0f, 0.0f, 1.0f,
};
// float[] texCoords = {
// 0.0f, 0.0f,
// 1.0f, 0.0f,
// 1.0f, 1.0f,
// 1.0f, 1.0f,
// 0.0f, 1.0f,
// 0.0f, 0.0f,
// };

d.setUniform1i("TEX_SAMPLER", 0);
glActiveTexture(GL_TEXTURE0);
tex.bind();

vecBuffer.put(vertices);
vecBuffer.flip();
indBuffer.put(indices);
indBuffer.flip();

render(indices.length, true, GL_TRIANGLES);
}

private static void render(int drawCount, boolean tex, int mode) {

glBindBuffer(GL_ARRAY_BUFFER, v_id);
glBufferData(GL_ARRAY_BUFFER, vecBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, v_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_id);

glTexCoordPointer(2, GL_FLOAT, 0, 0);

glVertexAttribPointer(0, 3, GL_FLOAT, false, 5*Float.BYTES, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 5*Float.BYTES, 3*Float.BYTES);
glEnableVertexAttribArray(1);

glDrawElements(mode, drawCount, GL_UNSIGNED_INT, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

public static void init(Display display) {
d = display;
glfwMakeContextCurrent(d.winAddr);
vecBuffer = BufferUtils.createFloatBuffer(4*5);
indBuffer = BufferUtils.createIntBuffer(6);

v_id = glGenBuffers();
t_id = glGenBuffers();
i_id = glGenBuffers();
}
}


The DrawTiles.draw() method just calls gmt.drawImage() for every tile
« Last Edit: May 03, 2022, 15:12:33 by v22 »