UPDATE: I’m relatively sure now that this is a bug, so I’ve file it here
Hey, I’m reawakening this thread because I think I have a very similar issue :), though specifically with #versioned shaders. (e.g. 150).
If I load the shaders from file, it works fine. If I setup from source via stringify or R", it compiles & links fine, but I see nothing. I also dump the strings to the console to compare, and they look fine. I couldn’t track down the issue. (Actually it seems to be displaying something, maybe it’s related to the default uniforms like modelViewProjectionMatrix etc not being passed correctly or something?)
I did try:
- manually adding
shader.setUniformMatrix4f("modelViewProjectionMatrix", ofGetCurrentMatrix(OF_MATRIX_PROJECTION) * ofGetCurrentMatrix(OF_MATRIX_MODELVIEW));
but this made no difference at all to either shader.
- renaming the uniform
modelViewProjectionMatrix
to mvp
and setting it manually to ofGetCurrentMatrix(OF_MATRIX_PROJECTION) * ofGetCurrentMatrix(OF_MATRIX_MODELVIEW))
. In this case shader0 (loaded from file) still showed the correct content, but was offset by half (excluding the plane’s position. is this a bug in core or user error on my side?) However shader1 is still not showing anything.
This is openFrameworks 0.10.1 on ubuntu 18.04.1 with Qt Creator 4.6.2
// vert shader
#version 150
uniform mat4 modelViewProjectionMatrix;
in vec4 position;
in vec2 texcoord;
out vec2 varyingtexcoord;
void main(){
varyingtexcoord = texcoord;
gl_Position = modelViewProjectionMatrix * position;
}
// frag shader
#version 150
uniform sampler2DRect tex0;
in vec2 varyingtexcoord;
out vec4 outputColor;
void main()
{
outputColor = texture(tex0, varyingtexcoord);
}
// main.cpp
#include "ofMain.h"
#define STRINGIFY(A) string("#version 150\n")+#A
class ofApp : public ofBaseApp{
public:
ofShader shader0; // loaded from file <-- WORKS!
ofShader shader1; // setup from (identical) source <-- COMPILES, BUT NO OUTPUT!
ofVideoGrabber cam;
ofPlanePrimitive quad;
//--------------------------------------------------------------
// vertex shader source
#ifdef STRINGIFY
string vertSource = STRINGIFY(
uniform mat4 modelViewProjectionMatrix;
in vec4 position;
in vec2 texcoord;
out vec2 varyingtexcoord;
void main(){
varyingtexcoord = texcoord;
gl_Position = modelViewProjectionMatrix * position;
}
);
//--------------------------------------------------------------
// fragment shader source
string fragSource = STRINGIFY(
uniform sampler2DRect tex0;
in vec2 varyingtexcoord;
out vec4 outputColor;
void main()
{
outputColor = texture(tex0, varyingtexcoord);
}
);
#else
string vertSource = R"(
#version 150
uniform mat4 modelViewProjectionMatrix;
in vec4 position;
in vec2 texcoord;
out vec2 varyingtexcoord;
void main(){
varyingtexcoord = texcoord;
gl_Position = modelViewProjectionMatrix * position;
}
)";
//--------------------------------------------------------------
// fragment shader source
string fragSource = R"(
#version 150
uniform sampler2DRect tex0;
in vec2 varyingtexcoord;
out vec4 outputColor;
void main()
{
outputColor = texture(tex0, varyingtexcoord);
}
)";
#endif
//--------------------------------------------------------------
void setup(){
ofBackground(50);
cam.setup(320, 240);
quad.set(cam.getWidth(), -cam.getHeight(), 2, 2); // flip vertically
quad.setPosition(cam.getWidth()/2, cam.getHeight()/2, 0);
quad.mapTexCoords(1, 1, cam.getWidth(), cam.getHeight());
// setup shader0 from files
shader0.load("pass");
// setup shader1 from source
ofLogNotice() << "//=== vertSource \n" << vertSource;
ofLogNotice() << "//=== fragSource \n" << fragSource;
shader1.setupShaderFromSource(GL_VERTEX_SHADER, vertSource);
shader1.setupShaderFromSource(GL_FRAGMENT_SHADER, fragSource);
shader1.linkProgram();
}
//--------------------------------------------------------------
void drawShader(ofShader& s) {
s.begin();
s.setUniformTexture("tex0", cam.getTexture(), 0);
quad.draw();
s.end();
}
//--------------------------------------------------------------
void draw(){
cam.update();
drawShader(shader0);
ofPushMatrix();
ofTranslate(0, cam.getHeight());
drawShader(shader1);
ofPopMatrix();
}
};
int main( ){
ofGLFWWindowSettings settings;
settings.setGLVersion(3, 2);
settings.setSize(1024, 768);
ofCreateWindow(settings);
ofRunApp(new ofApp());
}