patriciogonzalezvivo / lygia_of_examples

18 stars 4 forks source link

Trying to get examples to run on macOS. #1

Closed ofTheo closed 1 year ago

ofTheo commented 1 year ago

Hey @patriciogonzalezvivo I gave the instructions a try with the nightly build of OF on macOS

Nothing seemed to render as is, but I also didn't get any error messages.

Changing the GL version to 3.2 or 4.1 gave me useful error messages, but didn't change the visual output.

This is what I changed the main.cpp to ( otherwise it is using the non-programmable renderer )

//Use ofGLFWWindowSettings for more options like multi-monitor fullscreen
ofGLWindowSettings settings;
settings.setSize(1024, 768);
settings.windowMode = OF_WINDOW; //can also be OF_FULLSCREEN
settings.setGLVersion(3, 2);
auto window = ofCreateWindow(settings);

ofRunApp(window, make_shared<ofApp>());
ofRunMainLoop();

then the errors I get:

[ error ] ofShader: setupShaderFromSource(): GL_VERTEX_SHADER shader failed to compile
[ error ] ofShader: GL_VERTEX_SHADER shader reports:
ERROR: 0:1: '' :  #version required and missing.
ERROR: 0:5: 'attribute' : syntax error: syntax error

[ error ] ofShader: GL_VERTEX_SHADER, offending line 1:
        1   uniform mat4    modelViewProjectionMatrix;
        2   
        3   uniform vec2    u_resolution;

[ error ] ofShader: setupShaderFromSource(): GL_FRAGMENT_SHADER shader failed to compile
[ error ] ofShader: GL_FRAGMENT_SHADER shader reports:
ERROR: 0:2: '' :  #version required and missing.

[ error ] ofShader: GL_FRAGMENT_SHADER, offending line 2:
        1   
        2   #ifdef GL_ES
        3   precision highp float;
        4   #endif

[ error ] ofShader: checkProgramLinkStatus(): program failed to link
[ error ] ofShader: ofShader: program reports:
ERROR: One or more attached shaders not successfully compiled

[ error ] ofShader: setupShaderFromSource(): GL_VERTEX_SHADER shader failed to compile
[ error ] ofShader: GL_VERTEX_SHADER shader reports:
ERROR: 0:1: '' :  #version required and missing.
ERROR: 0:5: 'attribute' : syntax error: syntax error

[ error ] ofShader: GL_VERTEX_SHADER, offending line 1:
        1   uniform mat4    modelViewProjectionMatrix;
        2   
        3   uniform vec2    u_resolution;

[ error ] ofShader: setupShaderFromSource(): GL_FRAGMENT_SHADER shader failed to compile
[ error ] ofShader: GL_FRAGMENT_SHADER shader reports:
ERROR: 0:2: '' :  #version required and missing.

[ error ] ofShader: GL_FRAGMENT_SHADER, offending line 2:
        1   
        2   #ifdef GL_ES
        3   precision mediump float;
        4   #endif

[ error ] ofShader: checkProgramLinkStatus(): program failed to link
[ error ] ofShader: ofShader: program reports:
ERROR: One or more attached shaders not successfully compiled

Is there a specific version of OpenGL I should be setting and if so do we need to inject #version 150 or #version 410 etc into the lygia shaders

When I get the shader error I do see a tiny white dot at the screen center - I think it is because the billboard mesh is in normalized coords where as OF would need screen res coords:

ie: instead of this being -1 to 1 it should be -ofGetWidth()/2 to ofGetWidth()/2 https://github.com/patriciogonzalezvivo/lygia_of_examples/blob/main/src/ofApp.cpp#L19-L31

Maybe I am missing something basic though. What was the setup you used?

patriciogonzalezvivo commented 1 year ago

Hi! Thanks for give it a go!

I run it without issues in a Ubuntu 20.04 (intel) with the integrated intel's GPU card. It makes sense that running it against 3.2 or 4.1 will break because the billboard.vert uses attributes.

I'm adding #version 120 to the top of all shaders https://github.com/patriciogonzalezvivo/lygia_of_examples/commit/cd2a963dfa041cd87d195f1a5f72a4e113785da2. Lygia shouldn't be bounded to versions with the caveat that I should add a version check to all functions that do sampling to change the use of texture2D(..) for texture(...). Ex: https://github.com/patriciogonzalezvivo/lygia/blob/main/sample/derivative.glsl#L10-L12

ofTheo commented 1 year ago

Ahh, good to know. If it’s running on Ubuntu as is, it should work on macOS.

I didn’t see any shader errors with the example out of the box so it might be something fairly straightforward.

Will give it a try and report back! :)

patriciogonzalezvivo commented 1 year ago

Thanks you for giving it a try. Maybe with the #version 120 at the top... now it works?

ofTheo commented 1 year ago

Wohoo - got it working!

Screen Shot 2022-10-30 at 8 34 29 AM

So for macOS the change needed was to make the billboard.vert

#version 120
#ifdef GL_ES
precision mediump float;
#endif

varying vec2 v_texcoord;
varying vec4 v_position;

void main() {
   // get the texture coordinates
    v_texcoord = gl_MultiTexCoord0.xy;

    // get the position of the vertex relative to the modelViewProjectionMatrix
    v_position = ftransform();

    // this is the resulting vertex position
    gl_Position = v_position;
}

and then adding

        ofScale(width*0.5, height*0.5, 1);

Before the billboard drawing ( as the mesh is otherwise just drawn at -1 to 1 pixel in size )

    shader.begin();
        shader.setUniformTexture("u_tex0", tex_danny, 0);
        shader.setUniformTexture("u_lut", tex_lut, 1);
        shader.setUniformTexture("u_moss", tex_moss, 2);
        shader.setUniformTexture("u_noise", tex_noise, 3);
        shader.setUniformTexture("u_sprite", tex_sprite, 4);
        shader.setUniform2f("u_spriteResolution", tex_sprite.getWidth(), tex_sprite.getHeight());

        shader.setUniform2f("u_resolution", width, height);
        shader.setUniform2f("u_mouse", (float)ofGetMouseX(), (float)ofGetMouseY());
        shader.setUniform1f("u_time", ofGetElapsedTimef());

        ofScale(width*0.5, height*0.5, 1); //<-- needed so we don't draw a tiny 2pixel x 2pixel square in the screen center
        billboard.draw();
    shader.end();
patriciogonzalezvivo commented 1 year ago

YAY! That's awesome! do you mind submitting those as a PR? curious how they will run on my end