Hello Guest

Recent Posts

Pages: 1 ... 3 4 [5] 6 7 ... 10
41
General Java Game Development / Re: (LWJGL) Application Deployment - Best practices?
« Last post by Evgeny on February 23, 2021, 20:22:23 »
Do you know about graal vm? I think it can help you, because bytecode will be compiled to native image. As result you will get executable file. It means that you should't think about jvm and other libreries. Native image will be contained everything to need to run your app.
42
Absolutely no posts here? I must confess, this a disappointment ???. Does this mean no one ever finishes his or her LWJGL projects around here?

43
OpenGL / Need help with offscreen rendering
« Last post by Imagyx on February 10, 2021, 21:40:25 »
Hello everyone.

I wanted to give it a try and switch from CPU-Raytracing with Java to using OpenGL and therefore LWJGL.
I've searched for days about one problem I cannot solve concerning the size of an image I want to save.
I setup GLFW and everything neccessary for a compute shader to render me  a simple scene on screen, which works   :)
But when I want to save into a BufferedImage bigger than my screen size, a part of the image is left black.
I read a lot about using glViewport, disabling GL_SCISSOR_TEST, using Renderbuffer and  I tried all of that with no better results.

I would appreciate it a lot if someone can help me with that, as I think that this is a simple question for any pro used to these kinds of things for a long time.
I know that I don't know much about OpenGL yet, still I want to keep going since the speedup is 1000x and more from my CPU tracer to what I have no with the GPU.
Thank you in advance for any help.


This is my setup:
Code: [Select]
createOffscreen();
int framebuffer = initShaders();
resetShaders();
glViewport(0, 0, width, height); // 3000x2040 e.g.
glDisable(GL_SCISSOR_TEST);
tracing();
saveImage(framebuffer);

with methods

Code: [Select]
private void createOffscreen() {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
long offscreen = glfwCreateWindow(width, height, "", NULL, NULL);
glfwMakeContextCurrent(offscreen);
createCapabilities();
}
private void saveImage(int framebuffer) {
glReadBuffer(framebuffer);
int bpp = 4; //RGBA
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
GL11.glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
BufferedImage offscreen = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
        int i = (x + (width * y)) * bpp;
        int r = buffer.get(i) & 0xFF;
        int g = buffer.get(i + 1) & 0xFF;
        int b = buffer.get(i + 2) & 0xFF;
        offscreen.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
    }
}
...ImageIO.write....
}
private void resetShaders() {
glShaderSource(quadProgramVs, templating(VERTEX_SHADER ,vertexShader.getText()));
glCompileShader(quadProgramVs);

glShaderSource(quadProgramFs, templating(FRAGMENT_SHADER, fragmentShader.getText()));
glCompileShader(quadProgramFs);

glLinkProgram(quadProgram);

glShaderSource(computeProgramShader, templating(COMPUTE_SHADER, computeShader.getText()));
glCompileShader(computeProgramShader);

glLinkProgram(computeProgram);

numGroupsX = (int) Math.ceil((double) width / 8);
numGroupsY = (int) Math.ceil((double) height / 8);
}

private int initShaders() {
glBindVertexArray(glGenVertexArrays());

int framebuffer = glGenTextures();
glBindTexture(GL_TEXTURE_2D, framebuffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, width, height);
glBindImageTexture(0, framebuffer, 0, false, 0, GL_WRITE_ONLY, GL_RGBA8);

// Create program to render framebuffer texture as fullscreen quad
quadProgram = glCreateProgram();

quadProgramVs = glCreateShader(GL_VERTEX_SHADER);
glAttachShader(quadProgram, quadProgramVs);

quadProgramFs = glCreateShader(GL_FRAGMENT_SHADER);
glAttachShader(quadProgram, quadProgramFs);

// Create ray tracing compute shader
computeProgram = glCreateProgram();
computeProgramShader = glCreateShader(GL_COMPUTE_SHADER);
glAttachShader(computeProgram, computeProgramShader);

return framebuffer;
}

private void tracing() {
// Trace the scene
glUseProgram(computeProgram);
glDispatchCompute(numGroupsX, numGroupsY, 1);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

glUseProgram(quadProgram);
glDrawArrays(GL_TRIANGLES, 0, 3);
}

44
OpenGL / Re: glfwSwapBuffers slow every 4th cycle
« Last post by Aisaaax on February 08, 2021, 10:37:22 »
Narrowed the problem down to LWJGUI/ImageView. For some reason, I can render lots of simple elements, like buttons, but if I render images it just hickups every X'th frame.
I have a suspicion that perhaps something like NanoVG or something else causes images (textures) to re-load periodically. But it is interesting that glSwapBuffers gets affected.
45
OpenGL / Re: Draw something in front of every thing else
« Last post by Aisaaax on February 08, 2021, 08:04:27 »
Code: [Select]
// draw some objects

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ZERO);

// Draw the topmost object

glDisable(GL_BLEND)

This is the simplest example.
Basically when you render an object, it renders into a separate buffer, and then gets "Mixed" with what is currently in the frame buffer (i.e. the results of your previous renders). glBlendFunc(GL_ONE, GL_ZERO); says that when mixing, the current render call will be at strength 1-out-of-1 and the current buffer contents will be at 0-out-of-1 strengths.

Don't forget to restore the blendFunc to the default values before your next rendering call, because otherwise the same rule will affect everything in your scene.
Also you can either enable blending only for one object, draw it and disable it, like I did. Or you can enable some rules for all the objects, which could either make your code more or less efficient. The thing is, in some scenes blending could allow you to omit drawing a lot of fragments, especially when you aren't using depth testing.

You can get a lot with blending, including drawing objects "Below" everything as well as above, and of course draw transparency.

https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glBlendFunc.xhtml
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glBlendFuncSeparate.xhtml
46
Lightweight Java Gaming Library / mouse speed depends on fps
« Last post by vavris on February 05, 2021, 19:41:36 »
I'm working on a pseudo-3D raycasting game and I want to implement looking with the mouse. I tried to make it independent from fps by multiplying the mouse input with the time between frames. Here's my code:
Code: [Select]
    public void mouseLook() {
        GLFW.glfwGetCursorPos(Window.window, mouseX, mouseY);
        mouseX.rewind();
        mouseY.rewind();

        viewAngle += (mouseX.get(0) - (Window.width / 2.0)) * Window.frameTime;
        if (viewAngle > 2 * Math.PI) viewAngle -= 2 * Math.PI;
        if (viewAngle < 0) viewAngle += 2 * Math.PI;
        dX = Math.cos(viewAngle);
        dY = Math.sin(viewAngle);
        GLFW.glfwSetCursorPos(Window.window, Window.width / 2.0, Window.height / 2.0);
    }
What am I doing wrong?
47
OpenGL / Re: glfwSwapBuffers slow every 4th cycle
« Last post by Aisaaax on February 04, 2021, 10:16:02 »
update:
I tried limiting the loop, by adding some basic FPS control there. Basically I check the time at the end of the loop, and if it's not yet the time to have another frame - I simply wait. My idea was that perhaps the videodriver gets a bit overwhelmed by all the calls and chugs periodically.

Unfortunately, this didn't solve the issue. But now the stutter happens every 8 cycles instead of 4.
So the slower the loop runs - the less frequent those stutters are. But they are still simply inexcusable - 0.3 seconds is as you understand - way too much. It's like 3FPS which is 10 times worse than what I'm aiming at.

Also I want to stress that rendering seems completely fine otherwise.
48
OpenGL / glfwSwapBuffers slow every 4th cycle
« Last post by Aisaaax on February 04, 2021, 09:33:56 »
Hello!
I've recently made another project on OpenGL-ES for ARM with LWJGL. And I am using GLFW now (I was previously using EGL rendering with no issue)

But I noticed noticeable stuttering in my program. I investigated it and found that every 4th cycle of my main loop - glfwSwapBuffers(window) takes A LOT longer than in other cycles. We are talking of the difference between 0.0003 seconds and 0.28 seconds.

I have no idea what caused this. As you can see from the code (below), I am explicitly testing the time it takes swapBuffers to work. I can guarantee that the rest of my program (which is extremely small BTW) doesn't really take that much time at all, and nothing should cause any issues. I think.
I'm using LWJGUI, which is also a first time for me, but I tested its performance and it seems like it's doing completely OK.

Vsync is off, but I think double-buffering is on.

Here's my loop:
Code: [Select]
while (running)
        {
            context.updateGlfwWindow();
            Vector2i windowSize = context.getFramebufferSize();
            glClearColor(1, 1, 1, 1);
            glViewport(0, 0, windowSize.x, windowSize.y);
            glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

            renderer.render(frame, context);

            glfwPollEvents();

            double timeStart  = System.nanoTime() / 1000_000_000.0;

            glfwSwapBuffers(window);

            double timeEnd = System.nanoTime() / 1000_000_000.0;
            double delta = timeEnd - timeStart;
            System.err.printf("%f\n", delta);
           
            initializer.getSystemEventProcessor().processEvents(frame, context);

            initializer.getGuiEventProcessor().processEvents();
        }

Here's the sample output (timings). notice that the dip happens exactly every 4th time.
Code: [Select]
0.000372
0.000378
0.000370
0.274361
0.000344
0.000367
0.000395
0.275922
0.000393
0.000365
0.000370
0.274104
0.0003989
0.000477
0.000393
0.273459
49
OpenGL / Click on objects to select them
« Last post by Mihai_Ionut_Floares on January 26, 2021, 22:59:41 »
Can I check for what objects I click?
50
Lightweight Java Gaming Library / Re: Interpreting Crash Logs
« Last post by mudlee on January 24, 2021, 16:21:34 »
Found the problem. Looks like if I malloc it instead of calloc, somewhy it generates this error. I'm not sure why cleaning up the allocated memory works though.
Pages: 1 ... 3 4 [5] 6 7 ... 10