【问题标题】:Shaders not working as expected着色器未按预期工作
【发布时间】:2016-01-26 21:19:02
【问题描述】:

将 OpenGL 3.2 / GLSL 150 与 OpenFrameworks v0.8.3 结合使用

我正在尝试在我的程序中实现着色器。我的程序成功加载了正确的 frag 和 vert 文件,但我得到了这个错误的视觉和错误:

[ 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'

我读到这个SO answer 解释说gl_FragColor 在 GLSL 300 中不受支持,但我(很确定我)没有使用那个版本。无论如何,当我用 outputColor var 更改 gl_FragColor 时,我的屏幕只是显示为黑色,没有错误。

为什么我的着色器没有按预期显示?我有一种感觉,要么是我的 .vert 文件/对如何从着色器中绘制形状的根本误解,要么是版本控制问题。

我的简化程序:

.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(只是为了传递...如果有意义的话)

#version 150

uniform mat4 modelViewProjectionMatrix;
in vec4 position;

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

.frag(请参阅this page 下方的第三个交互式代码块了解预期结果)

#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 );
}

【问题讨论】:

  • 你的制服放在哪里?
  • 啊,我明白了。我必须设置变量以传递给着色器。谢谢。请随意回答,否则我会在弄清楚之后再做。

标签: c++ opengl glsl openframeworks


【解决方案1】:

我没有看到统一变量在任何地方设置,这意味着它们采用默认值。这意味着您的矩阵全为零。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-04
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    相关资源
    最近更新 更多