Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 782383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:22:42+00:00 2026-05-14T20:22:42+00:00

I just wanted to try out some shaders on a flat image. Turns out

  • 0

I just wanted to try out some shaders on a flat image. Turns out that writing a C program, which just takes a picture as a texture and applies, let’s say a gaussian blur, as a fragment shader on it is not that easy: You have to initialize OpenGL which are like 100 lines of code, then understanding the GLBuffers, etc.. Also to communicate with the windowing system one has to use GLUT which is another framework..

Turns out that Nvidia’s Fx composer is nice to play with shaders.. But I still would like to have a simple C or C++ program which just applies a given fragment shader to an image and displays the result. Does anybody have an example or is there a framework?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:22:43+00:00Added an answer on May 14, 2026 at 8:22 pm

    First of all, I’d avoid using glut — it’s buggy, hasn’t been updated in roughly a decade, and its design doesn’t really fit very well with what most people want today (e.g., though you can use it for animations, it’s really intended primarily to produce a static display). I pointed out a number of alternatives to glut in a previous answer.

    That (mostly) leaves the code to compile, link, and use shaders. I’ve written a small class I find handy for this purpose:

    class shader_prog {
        GLuint vertex_shader, fragment_shader, prog;
    
        template <int N>
        GLuint compile(GLuint type, char const *(&source)[N]) {
            GLuint shader = glCreateShader(type);
            glShaderSource(shader, N, source, NULL);
            glCompileShader(shader);
            GLint compiled;
            glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
            if (!compiled) {
                GLint length;
                glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
                std::string log(length, ' ');
                glGetShaderInfoLog(shader, length, &length, &log[0]);
                throw std::logic_error(log);
                return false;
            }
            return shader;
        }
    public:
        template <int N, int M>
        shader_prog(GLchar const *(&v_source)[N], GLchar const *(&f_source)[M]) {
            vertex_shader = compile(GL_VERTEX_SHADER, v_source);
            fragment_shader = compile(GL_FRAGMENT_SHADER, f_source);
            prog = glCreateProgram();
            glAttachShader(prog, vertex_shader);
            glAttachShader(prog, fragment_shader);
            glLinkProgram(prog);
        }
    
        operator GLuint() { return prog; }
        void operator()() { glUseProgram(prog); }
    
        ~shader_prog() {
            glDeleteProgram(prog);
            glDeleteShader(vertex_shader);
            glDeleteShader(fragment_shader);
        }
    };
    

    For a simple demo, a couple of “pass-through” shaders (just imitate the fixed-functionality pipeline):

    const GLchar *vertex_shader[] = {
        "void main(void) {\n",
        "    gl_Position = ftransform();\n",
        "    gl_FrontColor = gl_Color;\n",
        "}"
    };
    
    const GLchar *color_shader[] = {
        "void main() {\n",
        "    gl_FragColor = gl_Color;\n",
        "}"
    };
    

    Which you’d use something like:

    void draw() { 
        // compile and link the specified shaders:
        static shader_prog prog(vertex_shader, color_shader);
    
        // Use the compiled shaders:    
        prog(); 
    
        // Draw something:
        glBegin(GL_TRIANGLES);
            glColor3f(0.0f, 0.0f, 1.0f);
            glVertex3f(-1.0f, 0.0f, -1.0f);
            glColor3f(0.0f, 1.0f, 0.0f);
            glVertex3f(1.0f, 0.0f, -1.0f);
            glColor3f(1.0f, 0.0f, 0.0f);
            glVertex3d(0.0, -1.0, -1.0);
        glEnd();
    }
    

    If you’re going to use, for example, a number of different fragment shaders in the course of drawing your scene, you simply define a static object for each, then execute prog1();, prog2();, etc., just prior drawing the objects you want shaded with each shader. E.g.,

    void draw() { 
        static shader_prog wall_shader("wall_vertex", "wall_frag");
        static shader_prog skin_shader("skin_vertex", "skin_frag");
    
        wall_shader();
        draw_walls();
    
        skin_shader();
        draw_skin();
    }
    

    Edit: As @rotoglup quite correctly points out, this use of static variables delays destruction until after the OpenGL context has been destroyed, so when the destructors attempt to use glDeleteProgram/glDeleteShader, results are unpredictable (at best).

    While this may be excusable in a demo program, it’s decidedly undesirable in real use. At the same time, you generally do not want to re-compile your shader(s) every time you enter the function(s) that use them.

    To avoid both problems, you generally want to create your shader objects as members of a class instance whose lifetime is, in turn, tied to the lifetime of whatever it’s going to shade:

    class some_character_type { 
        shader_prog skin_shader;
    public:
        // ...
    };
    

    This will compile/link the shader program once when you create a character of that type, and destroy it when you destroy that character.

    Of course, in a few cases, this isn’t exactly desirable either. Just for example, consider a 3D version of the ancient “kill lots of targets” games like Galaga or Centipede. For games like this, you’re creating and destroying lots of essentially identical targets relatively quickly. Given a large number of essentially identical targets, you probably want to use something like a shared_ptr<shader_prog> to create a single instance of the shader that’s shared between all the instances of a particular target type. Given that you re-use the same target types many times, you may want to go a bit further even than that, so you maintain the same shaders through the entire game, not just when a particular type of target is being shown.

    In any case, we’re getting a bit off-track here. The point is that compiling and linking shaders is a fairly expensive process, so you normally want to manage their lifetime to avoid creating and destroying them a lot more often than truly necessary (though that’s not to say that it’s critical to create them all at the beginning of the game and only destroy them at the end, either).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.