Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: how to

  1. #1
    Intern Newbie
    Join Date
    May 2017
    Posts
    39

    how to

    Ive got a decent amt of c++ knowledge but lately I am being humbled by trying to get n example out of a Redbook to work. Its 8.13 and I've typed it in exactly -- yet the compiler complains like there is no tomorrow. Even inserting the #version 330 core causes an error. The settings for the project are the same had they been a typical C++ glut or freeglut problem prior to the Redbook examples. I've searched the net for 8.13 and I am not seeing it. I am get kneecapped at the outset here: can anyone help?

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,124
    Quote Originally Posted by technologist View Post
    ...example out of a Redbook ... Its 8.13 ... I've searched the net for 8.13 and I am not seeing it.
    Which version of the OpenGL Programming Guide are you referring to? 9th? 8th?

    And what's the name of the example? "Source Code for an Antialiased Checkerboard Fragment Shader"? If so, there's quite a few hits for that on the net. Seach for the name of the example rather than the number.

  3. #3
    Intern Newbie
    Join Date
    May 2017
    Posts
    39

    still how to

    Quote Originally Posted by Dark Photon View Post
    Which version of the OpenGL Programming Guide are you referring to? 9th? 8th?

    And what's the name of the example? "Source Code for an Antialiased Checkerboard Fragment Shader"? If so, there's quite a few hits for that on the net. Search for the name of the example rather than the number.
    Thx for the reply. I have found much information but none that will help me compile the bottom code. I've tried .c thinking it was a .cpp issue/.c issue but no success at all. Why can't I compile this? 4.3 8th edition, paperback.

    Code glsl:
    #version 330 core
     
    uniform vec3 Color0; uniform vec3Color1; uniform vec3AvgColor; uniform floatFrequency;
    in vec2 TexCoord;
    outvec4 FragColor;
    void main()
    {
        vec3 color;
     
        //Determine the width of the projection of one pixel into
        //s-tspace
        vec2 fw = fwidth(TexCoord);
     
        // Determine the amount of fuzziness vec2 fuzz = fw* Frequency* 2.0;
        float fuzzMax = max(fuzz.s, fuzz.t);
     
        // Determine the position in the checkerboard pattern vec2 checkPos = fract(TexCoord* Frequency);
        if (fuzzMax < 0.5)
        {
            //If the filter width is small enough,
            //compute the pattern color
            vec2 p = smoothstep(vec2(0.5), fuzz +vec2(0.5), checkPos) + (1.0 - smoothstep(vec2(0.0), fuzz, checkPos));
            color = mix(Color0, Color1,
                        p.x * p.y + (1.0 - p.x)* (1.0 - p.y));
            // Fade in the average color when we get close to the limit color=mix(color,AvgColor,smoothstep(0.125,0.5,fuzzMax));
        }
        else
        {
            // Otherwise, use only the average color color = AvgColor;
        }
        FragColor = vec4(color, 1.0);
    }
    Last edited by Dark Photon; 06-16-2017 at 04:55 AM.

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2017
    Posts
    9
    What is the compiler complaining exactly?

    Quote Originally Posted by technologist View Post
    outvec4 FragColor;
    You need to put a space between "out" and "vec4". Also, there are some comments that you put that are mixed with some code, including variables declarations. Is the file in that way too or only here? It shouldn't be like that. For example,

    Code :
    // Determine the amount of fuzziness vec2 fuzz = fw* Frequency* 2.0;

    shoud be

    Code :
    // Determine the amount of fuzziness
    vec2 fuzz = fw* Frequency* 2.0;

  5. #5
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,124
    technologist, YardenJ2R's put you on the right track. For some reason, your shader is missing some space and EOL characters. With the changes he's suggested plus a few more, this compiles:

    Code glsl:
    #version 330 core
     
    uniform vec3 Color0;
    uniform vec3 Color1;
    uniform vec3 AvgColor;
    uniform float Frequency;
    in vec2 TexCoord;
    out vec4 FragColor;
     
    void main()
    {
        vec3 color;
     
        //Determine the width of the projection of one pixel into
        //s-tspace
        vec2 fw = fwidth(TexCoord);
     
        // Determine the amount of fuzziness
        vec2 fuzz = fw* Frequency* 2.0;
        float fuzzMax = max(fuzz.s, fuzz.t);
     
        // Determine the position in the checkerboard pattern
        vec2 checkPos = fract(TexCoord* Frequency);
        if (fuzzMax < 0.5)
        {
            //If the filter width is small enough,
            //compute the pattern color
            vec2 p = smoothstep(vec2(0.5), fuzz +vec2(0.5), checkPos) + (1.0 - smoothstep(vec2(0.0), fuzz, checkPos));
            color = mix(Color0, Color1,
                        p.x * p.y + (1.0 - p.x)* (1.0 - p.y));
            // Fade in the average color when we get close to the limit color=mix(color,AvgColor,smoothstep(0.125,0.5,fuzzMax));
        }
        else
        {
            // Otherwise, use only the average color color = AvgColor;
        }
        FragColor = vec4(color, 1.0);
    }

    Did a quick compile test here with cgc.

  6. #6
    Intern Newbie
    Join Date
    May 2017
    Posts
    39
    Ok, so I think we have established that the code is good. Mine still isn't compiling (posted below). Mybe a build setting, I don't know.

    ..\src\main.cpp:7:2: error: invalid preprocessing directive #version
    #version 330 core
    ^
    In file included from <command-line>:0:0:
    C:\soft_glut_binary\glut-3.7.6-bin\glut.h:50:24: error: redeclaration of C++ built-in type 'wchar_t' [-fpermissive]
    typedef unsigned short wchar_t;
    ^
    ..\src\main.cpp:9:1: error: 'uniform' does not name a type
    uniform vec3 Color0;
    ^
    ..\src\main.cpp:10:1: error: 'uniform' does not name a type
    uniform vec3 Color1;
    ^
    ..\src\main.cpp:11:1: error: 'uniform' does not name a type
    uniform vec3 AvgColor;
    ^
    ..\src\main.cpp:12:1: error: 'uniform' does not name a type
    uniform float Frequency;
    ^
    ..\src\main.cpp:13:1: error: 'in' does not name a type
    in vec2 TexCoord;
    ^
    ..\src\main.cpp:14:1: error: 'out' does not name a type
    out vec4 FragColor;
    ^
    ..\src\main.cpp:16:11: error: '::main' must return 'int'
    void main()
    ^
    ..\src\main.cpp: In function 'int main()':
    ..\src\main.cpp:18:5: error: 'vec3' was not declared in this scope
    vec3 color;
    ^
    ..\src\main.cpp:22:5: error: 'vec2' was not declared in this scope
    vec2 fw = fwidth(TexCoord);
    ^
    ..\src\main.cpp:25:10: error: expected ';' before 'fuzz'
    vec2 fuzz = fw* Frequency* 2.0;
    ^
    ..\src\main.cpp:26:25: error: 'fuzz' was not declared in this scope
    float fuzzMax = max(fuzz.s, fuzz.t);
    ^
    ..\src\main.cpp:26:39: error: 'max' was not declared in this scope
    float fuzzMax = max(fuzz.s, fuzz.t);
    ^
    ..\src\main.cpp:29:10: error: expected ';' before 'checkPos'
    vec2 checkPos = fract(TexCoord* Frequency);
    ^
    ..\src\main.cpp:34:14: error: expected ';' before 'p'
    vec2 p = smoothstep(vec2(0.5), fuzz +vec2(0.5), checkPos) + (1.0 - smoothstep(vec2(0.0), fuzz, checkPos));
    ^
    ..\src\main.cpp:35:9: error: 'color' was not declared in this scope
    color = mix(Color0, Color1,
    ^
    ..\src\main.cpp:35:21: error: 'Color0' was not declared in this scope
    color = mix(Color0, Color1,
    ^
    ..\src\main.cpp:35:29: error: 'Color1' was not declared in this scope
    color = mix(Color0, Color1,
    ^
    ..\src\main.cpp:36:21: error: 'p' was not declared in this scope
    p.x * p.y + (1.0 - p.x)* (1.0 - p.y));
    ^
    ..\src\main.cpp:36:57: error: 'mix' was not declared in this scope
    p.x * p.y + (1.0 - p.x)* (1.0 - p.y));
    ^
    ..\src\main.cpp:43:5: error: 'FragColor' was not declared in this scope
    FragColor = vec4(color, 1.0);
    ^
    ..\src\main.cpp:43:22: error: 'color' was not declared in this scope
    FragColor = vec4(color, 1.0);
    ^
    ..\src\main.cpp:43:32: error: 'vec4' was not declared in this scope
    FragColor = vec4(color, 1.0);
    ^
    In file included from <command-line>:0:0:
    C:\soft_glut_binary\glut-3.7.6-bin\glut.h: At global scope:
    C:\soft_glut_binary\glut-3.7.6-bin\glut.h:486:22: warning: 'void glutInit_ATEXIT_HACK(int*, char**)' defined but not used [-Wunused-function]
    static void APIENTRY glutInit_ATEXIT_HACK(int *argcp, char **argv) { __glutInitWithExit(argcp, argv, exit); }
    ^
    C:\soft_glut_binary\glut-3.7.6-bin\glut.h:503:21: warning: 'int glutCreateWindow_ATEXIT_HACK(const char*)' defined but not used [-Wunused-function]
    static int APIENTRY glutCreateWindow_ATEXIT_HACK(const char *title) { return __glutCreateWindowWithExit(title, exit); }
    ^
    C:\soft_glut_binary\glut-3.7.6-bin\glut.h:549:21: warning: 'int glutCreateMenu_ATEXIT_HACK(void (__attribute__((__cdecl__)) *)(int))' defined but not used [-Wunused-function]
    static int APIENTRY glutCreateMenu_ATEXIT_HACK(void (GLUTCALLBACK *func)(int)) { return __glutCreateMenuWithExit(func, exit); }
    ^

    10:22:19 Build Finished (took 137ms)

  7. #7
    Intern Contributor
    Join Date
    Jan 2017
    Posts
    54
    Technologist, I'm a newbie like you. I'm sure someone who knows what they're talking about will reply to this thread, but I'll post this so you can get started a little sooner.

    Somebody will let us both know if I'm full of crap.

    OpenGL's shader programs are written in GLSL, not C.

    It's similar to C. I'm assuming that's to make it familiar for C and C++ programmers.

    This tutorial introduces the concept, and should get you going. The next lesson introduces a nice utility that handles a shader's code in its own individual file.

    Spoiler: The vertex and fragment shaders aren't compiled by the IDE you're using to build your C++ application and they don't run on your CPU. They're built, error checked and linked by some functions your video card vendor installed on your computer along with their driver. The shaders live and run on your GPU. I believe they're the programs running in parallel on those thousands of processors you have on your GPU.

    It sounds to me like you would benefit from going through at least the early parts of the tutorial series.
    Last edited by larryl; 06-16-2017 at 03:38 PM.

  8. #8
    Junior Member Newbie
    Join Date
    Jun 2017
    Posts
    5
    Quote Originally Posted by technologist View Post
    Ok, so I think we have established that the code is good. Mine still isn't compiling (posted below). Mybe a build setting, I don't know.10:22:19 Build Finished (took 137ms)
    If I'm not wrong then the C++ compiler is considering your Shader code as C++ code, and hence reporting errors. So, I suspect some error in handling the Shader source file.
    Could you post your c/c++ code you are using to compile this shader, and also are you reading this Shader code from some file or some string inside your program?

  9. #9
    Intern Newbie
    Join Date
    May 2017
    Posts
    39
    Quote Originally Posted by WhiteSword View Post
    If I'm not wrong then the C++ compiler is considering your Shader code as C++ code, and hence reporting errors. So, I suspect some error in handling the Shader source file.
    Could you post your c/c++ code you are using to compile this shader, and also are you reading this Shader code from some file or some string inside your program?
    Could be. I will post whatever code (link below) I had at the top before Larry's response. Larry was right, as well as some other folks: this code is processed by GLSL:

    I also literally bumped on this by accident: https://learnopengl.com/#!Getting-started/Shaders

    It appears we have to learn a new total language. I am experiencing a lot of confusion abt my next move. Is this the right and only place to commit learning openGl: I don't know.

    Right now I am also following GLFW tutorials and both are requiring I learn GLSL...could be cool

    But have now come to a crossroads. I think for sure I need to back up and look at the viability of what's needed toprogram.

    http://www.studfiles.ru/preview/5125756/page:93/

    Which leaves me to the basic question is which resource most appropriate to learn OpenGl. Is there a language whose spoiler attribute isn't huge. I have performance demands.

    Where do I "place my bets" so to speak to get the correct openGL education out of this.
    Last edited by technologist; 06-16-2017 at 11:41 AM.

  10. #10
    Intern Newbie
    Join Date
    May 2017
    Posts
    39
    Update:

    This section, OpenGL Setup for GLSL, assumes you’ve got a pair of shaders, a vertex shader and a fragment shader, and you want to use them in an OpenGL application. If you’re not ready yet to write your own shaders there are plenty of places to get shaders from the internet. Try the site from the Orange Book. The tools for shader development, namely Shader Designer or Render Monkey, all have a lot of shader examples.
    So I am not sure I am ready to tackle vertex and fragment shaders, YET, or should I just swallow the bitter pill and get to it?

    Each shader is like a C module, and it must be compiled separately, as in C. The set of compiled shaders, is then linked into a program, exactly as in C
    So where do I go from here->

    where does basic OpenGl taper off, and shaders and fragments begin? For example would I necessarily need shaders and fragments with simple geometrical shapes being translated and rotated in space?
    Last edited by technologist; 06-16-2017 at 12:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •