A__
A__

Reputation: 1751

Shaders not working as expected

Using OpenGL 3.2 / GLSL 150 with OpenFrameworks v0.8.3

I'm trying to implement shaders into my programs. My program successfully loads the correct frag and vert files, but I get this glitched visual and error:

enter image description here

[ error ] ofShader: setupShaderFromSource(): GL_FRAGMENT_SHADER shader failed to compile
[ error ] ofShader: GL_FRAGMENT_SHADER shader reports:
ERROR: 0:39: Use of undeclared identifier 'gl_FragColor'

I read this SO answer explaining that gl_FragColor is not supported in GLSL 300, but I'm (pretty sure I'm) not using that version. Regardless, when I change gl_FragColor with an outputColor var, my screen just appears black with no error.

Why isn't my shader appearing as expected? I have a feeling it is either my .vert file / a fundamental misunderstanding of how shapes are drawn from within shaders, or versioning problems.

My simplified program:

.h

#pragma once

#include "ofMain.h" //includes all openGL libs/reqs
#include "GL/glew.h"
#include "ofxGLSLSandbox.h" //addon lib for runtime shader editing capability

class ofApp : public ofBaseApp{

public:
    void setup();
    void draw();

    ofxGLSLSandbox *glslSandbox; //an object from the addon lib
};

.cpp

#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
    // create new ofxGLSLSandbox instance
    glslSandbox = new ofxGLSLSandbox();

    // setup shader width and height
    glslSandbox->setResolution(800, 480);

    // load fragment shader file
    glslSandbox->loadFile("shader"); //shorthand for loading both .frag and .vert as they are both named "shader.frag" and "shader.vert" and placed in the correct dir
}
//--------------------------------------------------------------
void ofApp::draw(){
    glslSandbox->draw();
}

.vert (just meant to be a pass-through... if that makes sense)

#version 150

uniform mat4 modelViewProjectionMatrix;
in vec4 position;

void main(){
    gl_Position = modelViewProjectionMatrix * position;
}

.frag (see 3rd interactive code block down this page for intended result)

#version 150

#ifdef GL_ES
    precision mediump float;
#endif

out vec4 outputColor;

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

float circle(in vec2 _st, in float _radius){
    vec2 dist = _st-vec2(0.5);
    return 1.-smoothstep(_radius-(_radius*0.01),
                         _radius+(_radius*0.01),
                         dot(dist,dist)*4.0);
}

void main(){
    vec2 st = gl_FragCoord.xy/u_resolution.xy;
    vec3 color = vec3(circle(st,0.9));
    outputColor = vec4( color, 1.0 );
}

Upvotes: 1

Views: 468

Answers (1)

Dietrich Epp
Dietrich Epp

Reputation: 213827

I don't see the uniform variables getting set anywhere, which means they take the default values. This means that your matrix is all zeroes.

Upvotes: 0

Related Questions