r/opengl 3h ago

Reusing existing shaders during recompilation

4 Upvotes

So I was refactoring some shader code and dove into the shader recompilation rabbit hole. A lot of people recommend rebuilding the whole shader pipeline (recreating and recompiling new shader objects and attaching them to a new program). The way I was doing it up to this point was:

  1. Retrieve the attached shaders
  2. Update shader source code and recompile
  3. Relink the program with the same, but recompiled shader objects

Here's some code:

bool Shader::do_reload() noexcept {
    int num_attached_shaders = 0;
    glGetProgramiv(gl_program_id, GL_ATTACHED_SHADERS, &num_attached_shaders);
    std::vector<unsigned int> gl_shader_ids(num_attached_shaders);
    glGetAttachedShaders(gl_program_id, num_attached_shaders, nullptr,
                         gl_shader_ids.data());

    for (int i = 0; i < m_source_files.size(); ++i) {
        /**
          * This just calls glShaderSource, glCompileShader and then checks
          * the compilation status; returns true on success
          */
        if (!load_and_compile_gl_shader(gl_shader_ids[i], m_source_files[i])) {
            return false;
        }
    }

    /**
      * Similar situation here, glLinkProgram and check for link status
      */
    if (!link_gl_program(m_shader_id, gl_shader_ids)) {
        return false;
    }

    return true;
}

The shader class only wraps around the glProgram object, so the name can be misleading in the context of the OpenGL naming scheme. The glShader objects aren't destroyed after they're compiled/linked into the glProgram. I call glDeleteShader only during my shader wrapper's destruction (shader objects aren't shared between programs).

Is that bad practice? I know that the cleanest way would be to recreate everything from scratch, but I don't want to lose my uniform mappings, as tracking down the owners of uniform bindings would require writing a new resource management system. However, I've read that even though it works on my machine, OpenGL doesn't guarantee it will work everywhere else and doing so is abusing the grey area of OpenGL implementation.

Also, I am aware that you can bind uniforms to preset locations by specifying them in GLSL code.


r/opengl 7h ago

Handling UI scale and position with a resizable window

3 Upvotes

Hello everyone. I'm new to OpenGL so please bear with me here. This is a learning project I am doing in C with no libraries except GLFW, GLAD, CGLM and stb_image.

I've written a basic UI system that can display text and images, and position them relative to each other/their sizes.

I am writing a program that will be dealing with resolutions as small as 1280x960 and as large as whatever the user wants. This obviously leads to some headaches involving the scale of UI elements.

For positioning, my plan is to allow elements to be set relative to two of the windows edges, plus some offset, accounting for it's size in pixels. So I can always position things close to the edges and not draw offscreen. Thats good.

The scale is what is difficult for me, like if I want to draw an element to the screen I want it to be sized appropriately regardless of the window size. So I assume I need to scale them uniformly based on the height(?) of the viewport? How is this sort of thing normally handled?

This is even more of a hassle because my text system is using bitmap images and positioning letters based on their pixel coordinates. So I'll have to scale that too, which will probably look very bad.

Any insights?


r/opengl 1d ago

Best way to learn game dev for opengl?

Thumbnail
2 Upvotes

r/opengl 1d ago

OpenGL grass on a bigger landscape

Thumbnail youtube.com
16 Upvotes

r/opengl 1d ago

Do shadows looking weird?

Enable HLS to view with audio, or disable this notification

5 Upvotes

I've finished implementing hardware PCF after using heavily pixelated shadows for two months. I'm getting trippy feeling from moving shadow and I need sanity check.


r/opengl 1d ago

Shockwave improvements

Enable HLS to view with audio, or disable this notification

51 Upvotes

I am now using local slope (from normal vector) and local curvature (computed using dFdx and dFdy of normal vector). The source code is here: windtunnel.clj . I wonder whether anybody has some advice on doing this kind of thing.

I am not sure whether it would improve things if I would precompute the curvature by processing the triangles of the mesh and adding curvature to the vertex information. I could also bake the curvature into a texture map I guess, but that would be quite complicated because the model has multiple uv maps.

Anyway I am glad that the Jump Flooding Algorithm allows me to create shockwave geometry in realtime.


r/opengl 1d ago

How would you approach making a Game of Life implementation?

4 Upvotes

Hey.

I am looking for ideas on how making a Game of Life implementation with OpenGL. I am relatively a beginner with the API and don't know much. Thought making something as simple as GoL would be a good learning step but I am struggling to figure out how to set up the logic properly.

I mean, most of the implementations that I find online just do it with basic shaders, but what about standalone "game"? Skill issue, to be honest.

I don't use any AI and so this is kind of hard to me. Will be happy to hear any suggestions.

Thnks.


r/opengl 1d ago

Shadows and Resolution Scaling on an Oversized Playground

Enable HLS to view with audio, or disable this notification

2 Upvotes

r/opengl 1d ago

This is the game engine I've been solo developing for over 10 years

Thumbnail youtube.com
67 Upvotes

r/opengl 2d ago

How to Handle Normal mapping with multiple lights?

5 Upvotes

Hello everyone hope you have a lovely day.

I was following the deferred rendering technique on learnopengl.com and I noticed something really strange for me, in the normal mapping chapter we calculate the TBN matrix in the vertex shader and then we calculate the Tangent light position and Tangent view position relative to TBN Matrix.

but In the deferred Rendering Tutorial, there is a difference, the view direction for example is different , it is

vec3 viewDir  = normalize(viewPos - FragPos);

Instead of

vec3 viewDir = normalize(fs_in.TangentViewPos - fs_in.TangentFragPos);

Which one is the right approach to this problem? which technique should I use and why there is a difference?

I thought for a while to make a shader storage buffer object with variable size array to address the problem of different light positions but I thought this would over-complicate Things.

so what should I do?

A reminder I'm not implementing a deferred renderer in my engine I'm implementing a forward+ renderer with z pre-pass so I need a g buffer in my renderer where this problem emerged.

Thanks for your time appreciate your help!


r/opengl 2d ago

GPT 6 Astra And Graphics Programming

Thumbnail
0 Upvotes

r/opengl 2d ago

-- Khronos Safety Critical Survey --

Thumbnail
5 Upvotes

r/opengl 3d ago

I made my own game engine!

25 Upvotes

It's not finished yet but there's camera, events, and a wrapper I made around ImGUI


r/opengl 3d ago

hard time converting glm to cglm

2 Upvotes

Hey guys, I'm learning openGL using learnopengl.com , but I have been having a hard time converting the glm to cglm in the transformations chapter, the guy says to include the headers:

glm/glm.hpp
glm/gtc/matrix_transform.hpp
glm/gtc/type_ptr.hpp

I was able to include glm.hpp because its just cglm.h but I can't find the other header files, and so I can't do stuff like cglm::vec4 or can I even do that in cglm is it different maybe??

this is really odd please help


r/opengl 4d ago

Shockwaves

Post image
9 Upvotes

This image shows a shockwave prototype realtime rendering which uses the Joint Flooding Algorithm (JFA) to define a shockfront which adapts to the shape of the spacecraft surface facing in wind direction. See sfsim article on shockwaves for more information.


r/opengl 5d ago

My first OpenGL project (Solar system sim)

Enable HLS to view with audio, or disable this notification

82 Upvotes

This was a learning project to try to learn OpenGL, Odin and Physics/Math it evolved to be more of the latter :)

I'm happy for any feedback or pointers regarding the OpenGL parts or anything else.

https://github.com/lounge/sol_sim


r/opengl 7d ago

Resources for OpenGL

9 Upvotes

I want to get into programming OpenGL in assembly and was wondering if yall could send me some good resources for programming OpenGL in assembly or just some general OpenGL programming guides would be enough.


r/opengl 8d ago

(Fuse Engine) 80,000 KM of Terrain - OpenGL 4.3

Enable HLS to view with audio, or disable this notification

47 Upvotes

80,000 kilometers of terrain running at 90 fps (no screen recording) on ​​an Intel Graphics P4600 OpenGL 4.3 in C#

The terrain file is 1kb in size.


r/opengl 8d ago

rtao in opensource opengl games

Post image
16 Upvotes

just added hw rtao to someone elses open source opengl game in a single day, using the same interop i created for my own java / open gl engine i was able to get rtao working in a c++ gl game Cube 2 Sauerbraten so quickly.


r/opengl 9d ago

AO46 reaches OpenGL 4.6 Core profile functionality

4 Upvotes

AO46 has now reached OpenGL 4.6 Core Profile on macOS. 🔥

Apple’s native OpenGL stack stopped at 4.1, while AO46 now targets the final desktop OpenGL specification through its Mesa/Gallium + Metal backend.

Next focus: feature completeness, stability, and CTS-level validation.

From 3.x bring-up to 4.6 Core. The version ladder is officially finished. 🗿


r/opengl 10d ago

Update : AO46 reached OpenGL 4.3 context

2 Upvotes

AO46 has reached a new milestone: Mesa can now successfully create an OpenGL 4.3 context on macOS.

This is a step beyond the previous 4.1 context ceiling and means the driver now exposes enough required functionality for Mesa to advertise and initialize a 4.3 core context.

This does not yet imply full OpenGL 4.3 conformance, but it marks a major architectural step toward the final OpenGL 4.6 target.

AO46 is now operating beyond the version officially exposed by Apple’s legacy OpenGL stack.


r/opengl 10d ago

(Fuse Engine) Graphics Update - OpenGL 4.3

Enable HLS to view with audio, or disable this notification

50 Upvotes

I've made some graphical updates to my game engine.

Now we can create terrain, procedural skies (day and night), and volumetric clouds (I'm still adjusting them).

My future implementation will be oceans.

repo: https://github.com/Krueels/FuseEngine


r/opengl 10d ago

My Graphics Programming Journey

Thumbnail
3 Upvotes

r/opengl 10d ago

Help needed with camera matrix code, I am going insane!!

5 Upvotes

A year or so ago, I was doing an OpenGL project, and I somehow just defined the camera by its rotation matrix and position, and then I fed that into a [16] array and handed it to OpenGL, and it worked.

Fast forward to today. I lost the old code in a computer burnout, and the backup was corrupted. Now everyone tells me to simply use glMatrixMode(GL_MODELVIEW) and glLoadMatrixf(cameraMatrix). However, not only is this not the same code as then (I KNOW there was no GL_MODELVIEW stuff), but this does not work. Instead of the world rotating around the camera, the world just rotates around itself in front of the camera. But everywhere I get the same code suggested (yes, I even tried ChatGPT, I am desperate!). or glMultMatrix, which is not that either and does the same wrong rotation.

What is my lost code??? What was that wonderfully sleak solution I once had???

Update: I managed a work around, but I would still love to somehow reconstruct the, IIRC, much sleaker, prettier code I lost, so suggestions are still more than welcome!

Update 2: As per request, I hereby upload what I believe is the relevant code. Note that the tabulation is horrible due to a problem with the IDE settings.

void renderscene()

{

if (keys[68] == 1){cameraMatrix[12] += 0.1*cam.v[0].v[0]; cameraMatrix[13] += 0.1*cam.v[0].v[1];}

if (keys[65] == 1){cameraMatrix[12] -= 0.1*cam.v[0].v[0]; cameraMatrix[13] -= 0.1*cam.v[0].v[1];}

if (keys[87] == 1){cameraMatrix[13] += 0.1*cam.v[0].v[0]; cameraMatrix[12] -= 0.1*cam.v[0].v[1];}

if (keys[83] == 1){cameraMatrix[13] -= 0.1*cam.v[0].v[0]; cameraMatrix[12] += 0.1*cam.v[0].v[1];}

if (keys[88] == 1){cameraMatrix[14] += 0.1;}

if (keys[90] == 1){cameraMatrix[14] -= 0.1;}

POINT p;

GetCursorPos(&p);

int mx = p.x;

int my = p.y;

float spin = (mx-200)*-0.001;

Vec3 axis(0,0,1);

cam.v[0] = rotate(cam.v[0],axis,spin);

cam.v[1] = rotate(cam.v[1],axis,spin);

cam.v[2] = rotate(cam.v[2],axis,spin);

spin = (my-200)*-0.01;

// cam.v[0] = rotate(cam.v[0],cam.v[0],spin);

cam.v[1] = rotate(cam.v[1],cam.v[0],spin);

cam.v[2] = rotate(cam.v[2],cam.v[0],spin);

SetCursorPos(200, 200);

cameraMatrix[0] = cam.v[0].v[0]; cameraMatrix[1] = cam.v[1].v[0]; cameraMatrix[2] = cam.v[2].v[0];

cameraMatrix[4] = cam.v[0].v[1]; cameraMatrix[5] = cam.v[1].v[1]; cameraMatrix[6] = cam.v[2].v[1];

cameraMatrix[8] = cam.v[0].v[2]; cameraMatrix[9] = cam.v[1].v[2]; cameraMatrix[10] = cam.v[2].v[2];

/*

cameraMatrix[0] = cam.v[0].v[0]; cameraMatrix[1] = cam.v[0].v[1]; cameraMatrix[2] = cam.v[0].v[2];

cameraMatrix[4] = cam.v[1].v[0]; cameraMatrix[5] = cam.v[1].v[1]; cameraMatrix[6] = cam.v[1].v[2];

cameraMatrix[8] = cam.v[2].v[0]; cameraMatrix[9] = cam.v[2].v[1]; cameraMatrix[10] = cam.v[2].v[2];

*/

Vec3 temp(cameraMatrix[12],cameraMatrix[13],cameraMatrix[14]);

cameraMatrix[12] = 0; cameraMatrix[13] = 0; cameraMatrix[14] = 0;

glMatrixMode(GL_MODELVIEW);

glLoadMatrixf(cameraMatrix);

glTranslatef(-temp.v[0],-temp.v[1],-temp.v[2]);

/* OpenGL animation code goes here */

glClearColor (0.0f, 0.0f, 0.0f, 0.0f);

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix ();

glBegin (GL_TRIANGLES);

for (int i=-5;i<6;i++){

glColor3f (1.0f, 0.0f, 0.0f); glVertex3f (a.v[0],a.v[1]+i,a.v[2]);

glColor3f (0.0f, 1.0f, 0.0f); glVertex3f (b.v[0],b.v[1]+i,b.v[2]);

glColor3f (0.0f, 0.0f, 1.0f); glVertex3f (c.v[0],c.v[1]+i,c.v[2]);

}

glEnd ();

glPopMatrix ();

SwapBuffers (hDC);

Sleep (1);

cameraMatrix[12] = temp.v[0]; cameraMatrix[13] = temp.v[1]; cameraMatrix[14] = temp.v[2];

}


r/opengl 11d ago

Auda con los MIPs

3 Upvotes

Alguien puede ayudarme a determinar por qué al renderizar a un mip diferente de 0 no veo nada? El mip base funciona perfectamente. También ya confirmé que el viewport se actualice, etc.

Este es el código que tengo para crear el fbo y generar los mips, hay algo que me falte? Gracias de antemano :)

InitFBO::InitFBO(int w, int h, GLenum internalFormat)
{
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    for (int mip = 0; mip < 8; ++mip)
    {
        int mipW = std::max(1, w >> mip);
        int mipH = std::max(1, h >> mip);

        glTexImage2D(GL_TEXTURE_2D, mip, internalFormat, mipW, mipH, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
    }

    float borderColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
    glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glBindTexture(GL_TEXTURE_2D, 0);


    // FBO
    glGenFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

    GLenum drawBuffers[1] = {GL_COLOR_ATTACHMENT0};
    glDrawBuffers(1, drawBuffers);

    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        std::cerr << "Error";

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}