OpenGL 4.0 Shading Language Cookbook - need help

edited August 2014 in GLSL / Shaders

I started with OpenGL 4.0 Shading Language Cookbook cause it's a well recommended book. Only I can't even get the first examples to work... oO. And for the purpose of learning I really want to do it in processing. I tried to adapt it to processing.

Those are the errors I get:

java.lang.RuntimeException: java.lang.RuntimeException: Cannot compile vertex shader:
ERROR: 0:1: Invalid qualifiers 'in' in global variable context
ERROR: 0:2: Invalid qualifiers 'in' in global variable context
ERROR: 0:4: Invalid qualifiers 'out' in global variable context
ERROR: 0:7: Use of undeclared identifier 'Color'
ERROR: 0:7: Use of undeclared identifier 'VertexColor'
ERROR: 0:8: Use of undeclared identifier 'VertexPosition'

BasicFrag.glsl

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

#define PROCESSING_COLOR_SHADER

in vec3 Color;

out vec4 FragColor;

void main() {
    FragColor = vec4(Color, 1.0);
}

basicVert.glsl

in vec3 VertexPosition;
in vec3 VertexColor;

out vec3 Color;

void main() {
    Color = VertexColor;
    gl_Position = vec4( VertexPosition, 1.0);
}

About the processing code, I have no clue at all but I would even be happy if the sketch can run.

PShader basic;

void setup() {
  size(640, 360, P3D);
  noStroke();
  basic = loadShader("basicFrag.glsl", "basicVert.glsl");
}

void draw() {
  shader(basic);
  background(0); 

  beginShape(QUADS);

  fill(0, 1, 1); vertex(-1,  1,  1);
  fill(1, 1, 1); vertex( 1,  1,  1);
  fill(1, 0, 1); vertex( 1, -1,  1);
  fill(0, 0, 1); vertex(-1, -1,  1);
  endShape();

}

I hope someone can help.

Answers

  • I thought Processing 2 series used OpenGL 2 or something.
    I even heard that Processing 1 series used OpenGL 1!

  • I did that tutorial but it's not really helpfull.

    OpenGL 2.0 is only 10 years old :)

  • edited August 2014

    @GoToLoop: By default Processing 2.0+ follows the OpenGL ES 2.0 specifications to ensure it will run on both desktop and mobile platforms. However you can get the underlying JOGL objects and access all the GL functions for profiles 3.0+, for example to use geometry and tessellation shaders.

    @clankill3r: Perhaps you are dismissing this tutorial too quickly. If you want to use shaders in Processing, this is pretty much the basic knowledge you should have. So I believe there really is some valuable information in there. Having said that, I think more basic shader example would be helpful and it is in fact already an issue on GitHub, here. Also note that, compared with only a year ago, there seem to be more Processing shader examples around (especially on GitHub).

    Now the specifics, there are a few problems with your code:

    • Do not suddenly start using normalized values in the fill() and vertex() calls of your Processing code. Just because you use a shader doesn't mean your 'regular' Processing drawing style should be adapted. I'm pretty sure you are able to draw a quad covering the screen right? So then do that. Use the color values and the vertex values that you usually would if you hadn't called a custom shader.
    • Use the default Processing variables for inside shaders. For example, in Processing, the vertex and color attributes hold the position and color of the input vertex, respectively. This explains your undeclared identifier errors.
    • Use the correct syntax for GLSL shader, for example gl_FragColor.
    • As said, Processing by default uses the ES 2.0 specification in order to be cross-compatible with mobile devices. This explains the use of varying. If you want to use in/out you can simply define a higher shader version in the GLSL files. Then it will just use that version, as long as your graphics card supports it! Note that this obviously means that people/devices with older graphics cards will NOT be able to run the code. More specifically, for in/out of shared variables you need at least #version 150. This explains your invalid qualifiers errors. For some examples in your book, you might even need #version 400 etc.

    Below is the fixed code...

    Processing Code

    PShader basic;
    
    void setup() {
      size(640, 360, P2D);
      noStroke();
      basic = loadShader("basicFrag.glsl", "basicVert.glsl");
    }
    
    void draw() {
      shader(basic);
      background(255);
      beginShape(QUADS);
      fill(0, 0, 255); vertex(0,  0);
      fill(0, 255, 255); vertex(width, 0);
      fill(255, 0, 255); vertex(width, height);
      fill(255, 0, 0);  vertex(0, height);
      endShape();
    }
    

    basicVert.glsl

    #version 150
    
    #define PROCESSING_COLOR_SHADER
    
    uniform mat4 transform;
    
    in vec4 vertex;
    in vec4 color;
    
    out vec4 vertColor;
    
    void main() {
      gl_Position = transform * vertex;    
      vertColor = color;
    }
    

    basicFrag.glsl

    #version 150
    
    #ifdef GL_ES
    precision mediump float;
    precision mediump int;
    #endif
    
    in vec4 vertColor;
    
    void main() {
      gl_FragColor = vertColor;
    }
    
  • How I wish Processing's own pre-processor had #define & #ifdef... :-<
    This way, we could write 1 source code which would work in multi-modes! $-)

Sign In or Register to comment.