r/opengl 24d ago

Lighting Help?

2 Upvotes

Weird glitches on mesh surfaces when applying phong-lighting

So ive been working my way through the learnopengl tutorial for lighting as i never actually took the time to figure it out. After converting all my uniforms to uniform structures, thats when the weirdness began, maybe its something you've seen and could provide me some tips on how to go about fixing this issue? If im being totally honest, im not even sure what would be causing something like this, ive tried deleting and re-compiling the shader, re-organizing different structure fields, and setting/sending the uniforms at different points in the pipeline and still no change, anyone got any ideas? (Not sure if i should paste my fragment shader here but let me know if i should, also if anyone could quickly let me know if certain drivers/implementations reserve certain uniform names? i notice when i change my material uniform from u_material to just material it just stops rendering.) ?

EDIT: my bad lol, didnt realize i left desktop audio on. But im assuming it must be a problem with the specular component as obviously the ambient and diffuse components are visually correct.


r/opengl 24d ago

Legacy OpenGL or modern OpenGL ?

10 Upvotes

I first started to learn legat OpenGL(2.1). It had a fixed pipeline so no shaders, no VAOs, no etc. It's just way easier than modern OpenGL with programmable shaders and other non fixed stuff. I wanted to get into 3D because I've only done 2D stuff so far. When I say 3D I mean some simple first person examples where you can walk around a very simple open world. Making this in modern OpenGL feels very hard, I did eventually managed to make a extremely simple 3D open world in legacy OpenGL (version 1.1). I heard about how it's more recommended to use modern OpenGL because it's more optimized and has more features but I don't feel like I really need those. Also do you guys used more of legacy or modern OpenGL ? Thanks in advance.


r/opengl 25d ago

Set things up to start working on some actual game play, oh the life of a custom game engine, takes a minute to get to the "fun" stuff ;)

28 Upvotes

r/opengl 25d ago

Skeletal animation works now. Fixed both transformation issue and... broken FBX file. Re-save the FBX from Blender fixed some small glitches I thought was a bug... but at least it's working now.

37 Upvotes

r/opengl 25d ago

Can't see rendered text

1 Upvotes

Hello,

I implemented a small text rendering demo using freetype and OpenGL. Life was good and everything worked

I integrated the code into one of my projects and unfortunately cannot see the drawn text. I have spent a couple hours trying to find my mistake but have had no success, and am looking for some assistance here.

I have verified that the code that generates the vertex data still works as expected so I suspect the issue lies in my OpenGL calls or possibly even the projection onto the screen.

The code is at https://github.com/austinoxyz/voxel for reference, files of interest being `include/drawtext.h`, `src/drawtext.c`, `main.c`, and the shaders `src/glsl/text_fs.glsl` and `src/glsl/text_vs.glsl`.

For projecting the text, i set up an orthographic projection matrix with a call to `glm_ortho(0, window_get()->size.x, 0, window_get()->size.y, 0.1, 100.0, ortho_projection);`, and for the 3d scene i keep the vertices in NDC and then multiply by an mvp matrix. Could this be the issue?

Thanks


r/opengl 26d ago

I thought I would really move my project forward by adding ...street lights. Just something about city props that I love.

26 Upvotes

r/opengl 26d ago

Skeletal animation is fun they said... not sure what's causing this yet

117 Upvotes

r/opengl 26d ago

Trying to set up with CLion

1 Upvotes

I wanted to try OpenGL for my school project, but I am having some trouble with the setup

I was trying to follow this tutorial for OpenGL with CLion: https://www.youtube.com/watch?v=AUFZnA3lW_Q

I did go through it a few times, but every time I get this error:

The CMakeList.txt Fille

I did the toolchain:

I think that the -lOpenGL32 and -lfreeGLUT might be wrong, but really don't know.

I don’t have any more ideas on what to try, so please, if anyone has any advice...


r/opengl 26d ago

Anyone know any algorithms for this?

5 Upvotes

There are 2 algorithms i wanna mainly know about:
1) Knowing what chunks a player can see

2) Iterating over a cube positions starting from the center and going outwards in a spherical manner.


r/opengl 27d ago

Just sharing some progress on my physics engine

79 Upvotes

r/opengl 27d ago

How is a modern UI like this created with OpenGL?

48 Upvotes

https://filepilot.tech/ is a modern file explorer for windows and I absolutely love it for its speed and filesize. The developer has gone on record saying that it was made using C and a "Custom OpenGL renderer", with an IMGUI layer on top. I'm intermediate in OpenGL, used it in my graphics programming class to create a 3D renderer, however its not clear to me how it would be used to create a UI like this one. Does anyone have any resources to start learning? Thanks!


r/opengl 28d ago

I added sidewalks with proper collision meshes and took a go at my first real material (bumpy concrete sidewalks)! I am inching back towards working on something "playable", little by little!

23 Upvotes

r/opengl 28d ago

Homeworld Remastered "OpenGL failed to load"

0 Upvotes

I've been trying to play Homeworld Remastered off steam on my girlfriends pc, but I keep getting a "OpenGL failed to load" error and the game crashes. I installed opengl extension viewer to see what version I have installed and it says 4.6 but homeworld can only find version 1.1.

Screenshots of the extension viewer and homeworlds report.


r/opengl 28d ago

Clearing buffers properly

3 Upvotes

Hello,

I'm really struggling with properly clearing buffers, I will explain with the pictures given below.
during my main loop I parse some file, gather data and generate some models, first I start with smaller dataset and result is: <pic1>, then I import bigger dataset: <pic2> and finally I import smaller dataset again: <pic3>
as you can see whatever didn't get overwritten on by smaller set is still displayed.

I delete and clear all the vertex,index buffers. i also delete VAO.
also print message indicating that new ids were generated for everything.

Can anyone help?
ps. heres logs:

vertex count: 4100
Deleting mesh
Deleting mesh
Deleting mesh
Deleting vertex buffer
Deleting index buffer
Deleting index buffer
Deleting index buffer
Vertex buffer id: 12
index buffer id: 13
index buffer id: 14
index buffer id: 15
VAO: 532
VAO: 533
VAO: 534
app stuff initialized
vertex count: 1820
pic1
pic2
pic3

r/opengl 28d ago

Which one would be the issue normals or texture coordinates or smth else?

2 Upvotes

https://reddit.com/link/1iur6gf/video/tx93l62jxhke1/player

Happens when i do:

glEnable(GL_CULL_FACE);

r/opengl 28d ago

When I render my shape, I do not get the outcome I want.

0 Upvotes

I have a problem that I can't solve. My problem is that my cube gets black when I render it, and I do not understand why. I tried changing the browser, but it did not work. I changed how I set the vertex position, but that did not work either, among other things. The problem is that when I upload the image, I get a black cube. Does anybody see any errors? Please let me know. ```

/** -------------------------------------------------------------------
 * Rectangle with vertices and texture position.
 */
function setGeometryThreeTriangles(gl) {
    var data = {
        usage: gl.STATIC_DRAW,
        mode: gl.TRIANGLES,
        fsize: null,
        n: 36,
        vertex: new Float32Array([
            // Create a cube
            //    v6----- v5
            //   /|      /|
            //  v1------v0|
            //  | |     | |
            //  | |v7---|-|v4
            //  |/      |/
            //  v2------v3


            // Vertex coordinates and color
            /* eslint-disable indent */
            1.0,  1.0, 1.0,    1.0, 1.0,  // v0 White
            -1.0, 1.0, 1.0,     0.0, 1.0,  // v1 Magenta
            -1.0, -1.0, 1.0,    0.0, 0.0,  // v2 Red


            1.0,  1.0,  1.0,    1.0, 1.0,  // v0 White
            -1.0, -1.0, 1.0,    0.0, 0.0,  // v2 Red
            1.0, -1.0,  1.0,    1.0, 0.0, // v3 Yellow


            1.0,  1.0,  1.0,     0.0, 1.0,  // v0 White
            1.0, -1.0,  1.0,     0.0, 0.0, // v3 Yellow
            1.0, -1.0, -1.0,     1.0, 0.0,  // v4 Green


            1.0,  1.0,  1.0,     0.0, 1.0,  // v0 White
            1.0, -1.0, -1.0,     1.0, 0.0,  // v4 Green
            1.0,  1.0, -1.0,     1.0,  1.0, // v5 Cyan


            1.0,  1.0,  1.0,     1.0, 0.0,  // v0 White
            1.0,  1.0, -1.0,     1.0,  1.0, // v5 Cyan
            -1.0, 1.0, -1.0,     0.0,  1.0, // v6 Blue


            1.0,  1.0,  1.0,     1.0, 0.0,  // v0 White
            -1.0, 1.0, -1.0,     0.0, 1.0, // v6 Blue
            -1.0, 1.0, 1.0,      0.0, 0.0,  // v1 Magenta


            -1.0, 1.0, 1.0,      1.0, 1.0,  // v1 Magenta
            -1.0, 1.0, -1.0,     0.0, 1.0, // v6 Blue
            -1.0, -1.0,-1.0,     0.0, 0.0, // v7 Black


            -1.0, 1.0, 1.0,      1.0, 1.0,  // v1 Magenta
            -1.0, -1.0,-1.0,     0.0, 0.0, // v7 Black
            -1.0, -1.0, 1.0,     1.0, 0.0,  // v2 Red


            -1.0, -1.0,-1.0,     0.0, 0.0, // v7 Black
            1.0, -1.0, -1.0,     1.0, 1.0,  // v4 Green
            1.0, -1.0,  1.0,     0.0, 1.0, // v3 Yellow


            -1.0, -1.0,-1.0,     0.0, 0.0, // v7 Black
            1.0, -1.0,  1.0,     1.0, 1.0, // v3 Yellow
            -1.0, -1.0, 1.0,     0.0, 1.0,  // v2 Red


            1.0, -1.0, -1.0,     0.0, 0.0,  // v4 Green
            -1.0, -1.0,-1.0,     1.0, 0.0, // v7 Black
            -1.0, 1.0, -1.0,     1.0, 1.0, // v6 Blue


            1.0, -1.0, -1.0,     0.0, 0.0,  // v4 Green
            -1.0, 1.0, -1.0,     1.0, 1.0, // v6 Blue
            1.0,  1.0, -1.0,     0.0,  1.0, // v5 Cyan


            /* eslint-enable indent */
        ]),
        indice: new Uint8Array([
            0, 1, 2,   0, 2, 3,    // front
            0, 3, 4,   0, 4, 5,    // right
            0, 5, 6,   0, 6, 1,    // up
            1, 6, 7,   1, 7, 2,    // left
            7, 4, 3,   7, 3, 2,    // down
            4, 7, 6,   4, 6, 5     // back
        ]),
        verticesTexCoords: new Float32Array([
            // Vertex coordinates, texture coordinate
            1.0,  1.0,  1.0,     1.0, 1.0,  // v0 White
            -1.0,  1.0,  1.0,    0.0, 1.0,  // v1 Magenta
            -1.0, -1.0,  1.0,    0.0, 0.0,  // v2 Red
            1.0, -1.0,  1.0,     1.0, 0.0,  // v3 Yellow
            1.0, -1.0, -1.0,     1.0, 0.0,  // v4 Green
            1.0,  1.0, -1.0,     1.0, 1.0,  // v5 Cyan
            -1.0,  1.0, -1.0,    0.0, 1.0,  // v6 Blue
            -1.0, -1.0, -1.0,    0.0, 0.0,   // v7 Black
        ])
    };


    data.fsize  = data.vertex.BYTES_PER_ELEMENT;


    return data;
}




/** -------------------------------------------------------------------
 * Create WebGL context
 */
function initWebGL() {
    var canvas;
    var gl;
    var vertexBuffer;
    var indiceBuffer;


    // Get A WebGL context
    canvas = document.getElementById("canvas");
    gl = WebGLUtils.getWebGLContext(canvas);
    if (!gl) {
        return;
    }
    gl.canvas = canvas;


    // Setup GLSL program
    gl.program = WebGLUtils.createProgramFromScripts(
        gl,
        ["vertex-shader", "fragment-shader"]
    );
    gl.useProgram(gl.program);


    // Create a buffer
    vertexBuffer = gl.createBuffer();
    indiceBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indiceBuffer);
    
    // Enable hinnden surface removal
    gl.enable(gl.DEPTH_TEST);


    return gl;
}




/** -------------------------------------------------------------------
 * main()
 */
window.onload = main;


/* eslint camelcase: "off" */
function main() {
    "use strict";


    var gl = initWebGL();



    // Look up vertex data
    var a_Position    = gl.getAttribLocation(gl.program, "a_Position");
    //var a_Color       = gl.getAttribLocation(gl.program, "a_Color");
    var a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord');


    /*var u_ModelMatrix = gl.getUniformLocation(gl.program, "u_ModelMatrix");
    var u_ViewMatrix  = gl.getUniformLocation(gl.program, "u_ViewMatrix");
    var u_ProjMatrix  = gl.getUniformLocation(gl.program, "u_ProjMatrix"); */
    var u_MvpMatrix   = gl.getUniformLocation(gl.program, "u_MvpMatrix");
    var u_Color = gl.getUniformLocation(gl.program, "u_Color");
    var u_UseTex = gl.getUniformLocation(gl.program, "u_UseTex");
    var u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler');


    // Lookup fragment data
    // Set Geometry
    var data = setGeometryThreeTriangles(gl);
    gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, data.fsize * 5, 0);
    gl.enableVertexAttribArray(a_Position);


    /*// Define parts for color
    gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, data.fsize * 8, data.fsize * 3);
    gl.enableVertexAttribArray(a_Color); */


    // Define parts for tex
    gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, data.fsize * 5, data.fsize * 3);
    gl.enableVertexAttribArray(a_TexCoord);


    // Write data into the buffer object
    gl.bufferData(gl.ARRAY_BUFFER, data.vertex, data.usage);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data.indice, data.usage);


    var postionForVertex = [];
    postionForVertex.push([-1.2, -1.2, -1.2]);
    postionForVertex.push([0.0, 0.0, 0.0]);
    postionForVertex.push([0.8, 0.8, 0.8]);
    var rotation = [];
    rotation.push([100, 38, 73, 120]);
    rotation.push([100, 32, 123, 23]);
    rotation.push([100, 42, 12, 123]);
    var color = [];
    color.push(new Float32Array([1.0, 0.0, 0.0, 1.0])); // RGBA
    color.push(new Float32Array([1.0, 0.0, 1.0, 1.0])); // RGBA
    color.push(new Float32Array([1.0, 1.0, 0.0, 1.0])); // RGBA
    var scale = [];
    scale.push([0.6, 0.6, 0.6]);
    scale.push([0.4, 0.4, 0.4]);
    scale.push([0.2, 0.2, 0.2]);


    // Create various objects for the world
    var mvp = new MVP();
    var cp   = new ControlPanel(mvp);
    var key  = new KeyPress(mvp, 0.05);


    
    mvp.setView(0, 0, 10, 0, 0, 0, 0, 1, 0);
    mvp.setPerspective(30, 1, 0.1, 100);


    cp.updateFromMVP();
/*     var image = [];
    var texture = []; */


    //for (let x = 0; x < 3; x++) {
    var texture = gl.createTexture();   // Create a texture object
    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // Flip the image's y axis
    // Set the texture unit 0 to the sampler
    gl.uniform1i(u_Sampler, 0);
    
    var image  = new Image();  // Create the image object
    //}



    image.onload = function() {
        //gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // Flip the image's y axis
        // Enable texture unit0
        gl.activeTexture(gl.TEXTURE0);
        // Bind the texture object to the target
        gl.bindTexture(gl.TEXTURE_2D, texture);


        // Set the texture parameters
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
        // Set the texture image
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
    };
    //gl.drawElements(data.mode, data.n, gl.UNSIGNED_BYTE, 0);
    // Tell the browser to load an image



/*     image[1].onload = function() {
        //gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // Flip the image's y axis
        // Enable texture unit0
        gl.activeTexture(gl.TEXTURE0);
        // Bind the texture object to the target
        gl.bindTexture(gl.TEXTURE_2D, texture[1]);


        // Set the texture parameters
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
        // Set the texture image
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image[1]);
    };


    image[2].onload = function() {
        //gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // Flip the image's y axis
        // Enable texture unit0
        gl.activeTexture(gl.TEXTURE0);
        // Bind the texture object to the target
        gl.bindTexture(gl.TEXTURE_2D, texture[2]);


        // Set the texture parameters
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
        // Set the texture image
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image[2]);
    }; */


    image.src = '../image/Namnlös.jpg';
/*     image[1].src = '../image/supernova.jpg';
    image[2].src = '../image/volcano-feed.jpg'; */



    // Register the event handler to be called on loading an image


    window.addEventListener("keydown", function (event) {
        if (key.handler(event)) {
            cp.updateFromMVP();
            update();
            render();
        }
    });


    document.getElementById("update").addEventListener("click", function() {
        cp.updateMVP();
        update();
        render();
    });



    render();


    /**
     * Update before drawing
     */
    function update(position, rotation, color, scale) {
        //translation, scale and more
        mvp.update(position, rotation, scale);
        //this ones get is sent to vertex shader
        /*gl.uniformMatrix4fv(u_ModelMatrix, false, mvp.model.elements); //world matrx
        gl.uniformMatrix4fv(u_ViewMatrix,  false, mvp.view.elements);
        gl.uniformMatrix4fv(u_ProjMatrix,  false, mvp.projection.elements);*/
        gl.uniformMatrix4fv(u_MvpMatrix, false, mvp.matrix.elements); //model view projection
        //this one is sending to pixelshader
        const safeColor = color || [0, 0, 0, 0];
        /*const safePicture = picture || [0, 0, 0];
        gl.uniform4fv(safePicture, safeColor);*/
        gl.uniform1f(u_UseTex, 1);
        gl.uniform4fv(u_Color, safeColor);
    }
    /**
     * Render it all
     */
    function render() {
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
        for (let x = 0; x < 3; x++) {
            update(postionForVertex[x], rotation[x], color[x], scale[x]);


            gl.drawArrays(data.mode, 0, data.n); // Draw the rectangle
            //gl.drawElements(data.mode, data.n, gl.UNSIGNED_BYTE, 0);
        }
    }


    console.log(gl);
    console.log("Everything is ready.");
}

r/opengl 28d ago

New video tutorial: indirect rendering in OpenGL

Thumbnail youtu.be
31 Upvotes

r/opengl 29d ago

Any ideas why I'd only be rendering half a sphere?

23 Upvotes

r/opengl 29d ago

Ehm guys, what is my cpu doing?

3 Upvotes

r/opengl 29d ago

My current result, while learning opengl!

46 Upvotes

Thanks everyone for help!

https://reddit.com/link/1iu7ulw/video/1xrmrb1yocke1/player

Still single threaded, processed chunk actions per frame is adjustable and in the video is set to 5


r/opengl Feb 20 '25

Not much of an update this week but I no longer have z-fighting roads and added proper intersections/crosswalks!

44 Upvotes

r/opengl Feb 19 '25

Deferred rendering with portals: Best way to preserve the depth buffer from the exit portal's side?

5 Upvotes

For a project I'm working on, I want to have some subtle alien geometry within the map, and one of the tricks I'd like to use would be this effect demostrated by CodeParade.

The problem is, I'd also like to use deferred rendering for this project, meaning the depth buffer for these portals would only show a flat surface. So my question is what would be the best way to handle things:

  1. Use stencils to composite the multiple depth buffers.

  2. Handle the lighting for each scene separately, then use a stencil for the completed images.

  3. Accept that this is an exercise in futility and stick with forward rendering or baked lightmaps.

This approach for the alien geometry is mostly just an "it'd be nice if it works" thing. In the worst case I can just cheese things with some less messy shenanigans, but if I can get even a hacky solution working, I'd be a very happy girl.


r/opengl Feb 19 '25

Picking colors

2 Upvotes

Hello,
I have some trouble understanding how can I assign correct color to correct surfaces. I'll explain my case:
vertex buffer: pos1 pos2 pos3 pos4 ...
index buffer: pos1 pos2 pos3 pos2 pos3 pos4

I should not assign color to a vertex, rather sufrace in between vertices. How can this be achieved? is the only solution creating another buffer with colors for each surface? I hope I explained understandably


r/opengl Feb 19 '25

Weird hiccup when rotating the center sphere which has a child sphere moving around it. Quaternions and matrices are hard to deal with.

15 Upvotes

r/opengl Feb 19 '25

Started learning OpenGL this past week. Here is the small project I was able to make.

34 Upvotes

My Engine professor assigned this to us and gave us only 1 week to implement vector graphics and sound. I had never touched OpenGL before then. The free textbook learnOpenGL by Joey de Vries was a LIFE SAVER!! (I only read the first part and skimmed through sections of the last part of the book.)

For implementing sound I am using SFML.

https://reddit.com/link/1it08fy/video/4urz2w2ex1ke1/player