Help sending ofTexture to shader

Hi
I’m running into a basic problem of sending ofTexture with custom data to shader. I modified the simple quad example. I expected a noisy visual but instead get a black screen. Any idea whats wrong? I’m using of_v0.11.2_linux64gcc6_release

ofApp.h

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{
        public:
                void setup();
                void update();
                void draw();
                
                void keyPressed(int key);
                void keyReleased(int key);
                void mouseMoved(int x, int y); 
                void mouseDragged(int x, int y, int button);
                void mousePressed(int x, int y, int button);
                void mouseReleased(int x, int y, int button);
                void windowResized(int w, int h); 
                void dragEvent(ofDragInfo dragInfo);
                void gotMessage(ofMessage msg);

    
        ofShader shader;
        ofTexture noise;
};

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    
#ifdef TARGET_OPENGLES
        shader.load("shadersES2/shader");
#else
        if(ofIsGLProgrammableRenderer()){
                shader.load("shadersGL3/shader");
        }else{
                shader.load("shadersGL2/shader");
        }
#endif

        ofDisableArbTex();
        noise.allocate(64, 64, GL_LUMINANCE);
}

//--------------------------------------------------------------
void ofApp::update(){
    //  
    unsigned char data[64*64] = {0};
    for(int i =0; i < 64 *64; i++) {
        data[i] = rand() % 256;
    }   
    noise.loadData(data, 64, 64, GL_LUMINANCE);
}

//--------------------------------------------------------------
void ofApp::draw(){
    ofSetColor(255);
    
    noise.bind();
    shader.begin();

    shader.setUniform2f("u_resolution", ofGetWidth(), ofGetHeight());
    
    ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight());
    
    noise.unbind();
    shader.end();
}

shader.frag

version 150

uniform sampler2D u_tex;
uniform vec2 u_resolution;
out vec4 outputColor;

void main()
{

    vec2 st = gl_FragCoord.xy/u_resolution.xy;

    vec3 color = texture(u_tex, st).rgb;

        outputColor = vec4(color, 1.0);
}

This first line in the fragment shader should be:

#version 150

Best,
Paul

Also don’t forget to modify main.cpp to use a different gl version. I usually do this:

int main( ){
    ofGLFWWindowSettings settings;
    settings.setGLVersion(3,2); // #version 150 in the shader
    settings.setSize(1024, 768);
    ofCreateWindow(settings);

    ofRunApp(new ofApp());
}

Ah yes, it is indeed version #150, that is a typo. And main.cpp has

//========================================================================                                                                                                                                                                                              │·····················································
int main( ){                                                                                                                                                                                                                                                            │·····················································
                                                                                                                                                                                                                                                                        │·····················································
#ifdef OF_TARGET_OPENGLES                                                                                                                                                                                                                                               │·····················································
        ofGLESWindowSettings settings;                                                                                                                                                                                                                                  │·····················································
        settings.glesVersion=2;                                                                                                                                                                                                                                         │·····················································
#else                                                                                                                                                                                                                                                                   │·····················································
        ofGLWindowSettings settings;                                                                                                                                                                                                                                    │·····················································
        settings.setGLVersion(3,2);                                                                                                                                                                                                                                     │·····················································
#endif                                                                                                                                                                                                                                                                  │·····················································
        ofCreateWindow(settings);                                                                                                                                                                                                                                       │·····················································
                                                                                                                                                                                                                                                                        │·····················································
        // this kicks off the running of my app                                                                                                                                                                                                                         │·····················································
        // can be OF_WINDOW or OF_FULLSCREEN                                                                                                                                                                                                                            │·····················································
        // pass in width and height too:                                                                                                                                                                                                                                │·····················································
        ofRunApp(new ofApp());                                                                                                                                                                                                                                          │·····················································
                                                                                                                                                                                                                                                                        │·····················································
}                                                                                                                                                                                                                                                                       │·····················································
~

any further ideas?

OK glad you have a main.cpp that calls for a programmable pipeline.

oF has a default name of tex0 (zero not “oh”) for the texture (sampler2DRect ?). Calling .bind() or .draw() on various objects will result in them arriving in the shader with the name of tex0. So, you can either call ofTexture::bind() and change the name of the texture in the shader to tex0, or you can send the texture with a specific name with ofShader::setUniformTexture(). Drawing the rectangle sends some texcoord into the shader, which also happens if ofTexture()::draw() is called (instead of .bind()).

Also, the shader code above uses normalized coordinates (0.0 - 1.0). So, if you get a screen that is a solid color (the color at (0, 0)), the texture does not have normalized coordinates. So just try using gl_FragCoord.xy in the call to texture(). Calling ofDisableArbTex() has something to do with using sampler2D (no arbitrary rectangles) and (probably) normalized coordinates.

Also I remember reading this forum thread about GL_LUMINANCE so you may need to pick a different type of format for the ofTexture.