Click here to Skip to main content
15,885,365 members
Articles / Multimedia / OpenGL

Create a Noise Image with GLSL Shader

Rate me:
Please Sign up or sign in to vote.
4.55/5 (11 votes)
6 Jul 2010CPOL2 min read 49.8K   2.3K   19  
A simple shader program to create noise effect in an image
#include "StdAfx.h"
#include "GLExtension.h"
#include "GL\GL.h"

// Macro for getting function pointers of Extension functions.
#define GET_PROC_ADDRESS(fname, failedFunName)\
{\
    Obj_##fname = (FPTR_##fname)wglGetProcAddress( #fname ); \
    if( 0 == Obj_##fname ) \
    { \
        failedFunName = new TCHAR[strlen(#fname)+1]; \
        wcscpy( failedFunName, L#fname ); \
        return false; \
    } \
}\

GLExtension* GLExtension::m_pGLExtension = 0;

GLExtension* GLExtension::GetInstance()
{
    if( 0 == GLExtension::m_pGLExtension )
    {
        GLExtension::m_pGLExtension = new GLExtension();
    }
    return GLExtension::m_pGLExtension;
}

// This initialization function should be called before calling any member functions in this class.
// Here create the function pointer for all extension functions.
// pFailedFunction Return NULL is all wglGetProcAddress are success.
// Else return the name of failed function. Application should delete this buffer.
bool GLExtension::GetWglProcAddress( TCHAR*& pFailedFunction )
{
    pFailedFunction = 0;
    // name of failed function
    GET_PROC_ADDRESS(glCreateProgramObjectARB,pFailedFunction);
    GET_PROC_ADDRESS( glCreateProgramObjectARB ,pFailedFunction);
    GET_PROC_ADDRESS( glCreateShaderObjectARB ,pFailedFunction); 
    GET_PROC_ADDRESS( glShaderSourceARB ,pFailedFunction); 
    GET_PROC_ADDRESS( glCompileShader ,pFailedFunction); 
    GET_PROC_ADDRESS( glAttachObjectARB ,pFailedFunction); 
    GET_PROC_ADDRESS( glLinkProgramARB ,pFailedFunction); 
    GET_PROC_ADDRESS( glUseProgramObjectARB ,pFailedFunction); 
    GET_PROC_ADDRESS( glDeleteObjectARB ,pFailedFunction); 
    GET_PROC_ADDRESS( glGetInfoLogARB ,pFailedFunction); 
    GET_PROC_ADDRESS( glGetUniformLocationARB ,pFailedFunction); 
    GET_PROC_ADDRESS( glGetAttribLocationARB ,pFailedFunction); 
    GET_PROC_ADDRESS( glUniform1iARB ,pFailedFunction); 
    GET_PROC_ADDRESS( glUniform1fARB ,pFailedFunction);
    GET_PROC_ADDRESS( glActiveTexture ,pFailedFunction);
    return true;
}

GLExtension::GLExtension(void)
{

}

GLExtension::~GLExtension(void)
{
}

GLhandleARB GLExtension::glCreateProgramObjectARB()
{
    if( Obj_glCreateProgramObjectARB )
    {
        return(GLhandleARB)(*Obj_glCreateProgramObjectARB)();
    }
    return (GLhandleARB)0;
}
GLhandleARB GLExtension::glCreateShaderObjectARB(GLenum shaderType)
{
    if( Obj_glCreateShaderObjectARB )
    {
        GLhandleARB hHandle = (*Obj_glCreateShaderObjectARB)(shaderType);
        return hHandle;
    }
    return (GLhandleARB)0;
}
void GLExtension::glShaderSourceARB( GLhandleARB shader, GLuint number_strings,
                                     const GLcharARB** strings, GLint *length)
{
    if( Obj_glShaderSourceARB )
    {
        (*Obj_glShaderSourceARB)(shader, 1, strings, length);
    }
}

void GLExtension::glCompileShader( GLhandleARB shader )
{
    if( Obj_glCompileShader )
    {
        (*Obj_glCompileShader)( shader );
    }
}
void GLExtension::glAttachObjectARB(GLhandleARB program, GLhandleARB shader)
{
    if( Obj_glAttachObjectARB )
    {
        (*Obj_glAttachObjectARB)(program,shader);
    }
}
void GLExtension::glLinkProgramARB(GLhandleARB program)
{
    if( Obj_glLinkProgramARB )
    {
        (*Obj_glLinkProgramARB)(program);
    }
}
void GLExtension::glUseProgramObjectARB(GLhandleARB program)
{
    if( Obj_glUseProgramObjectARB )
    {
        (*Obj_glUseProgramObjectARB)(program);
    }
}
void GLExtension::glDeleteObjectARB(GLhandleARB object)
{
    if( Obj_glDeleteObjectARB )
    {
        (*Obj_glDeleteObjectARB)(object);
    }
}
void GLExtension::glGetInfoLogARB(GLhandleARB object, GLsizei maxLenght,
                                  GLsizei *length, GLbyte*infoLog)
{
    if(Obj_glGetInfoLogARB)
    {
        (*Obj_glGetInfoLogARB)(object,maxLenght, length, infoLog);
    }
}
GLint GLExtension::glGetUniformLocationARB(GLhandleARB program, const GLbyte* name)
{
    if( Obj_glGetUniformLocationARB )
    {
        return (*Obj_glGetUniformLocationARB)(program,name);
    }
    return 0;
}
GLint GLExtension::glGetAttribLocationARB(GLhandleARB program, const GLbyte* name)
{
    if( Obj_glGetAttribLocationARB )
    {
       return (*Obj_glGetAttribLocationARB)( program, name );
    }
    return 0;
}

void GLExtension::glUniform1iARB(GLuint index, int val)
{
    if( Obj_glUniform1iARB )
    {
        (*Obj_glUniform1iARB)( index, val );
    }
}

void GLExtension::glUniform1fARB(GLuint index, float fval)
{
    if( Obj_glUniform1fARB )
    {
        (*Obj_glUniform1fARB)( index, fval );
    }
}

void GLExtension::glActiveTexture(GLenum Texture)
{
    if( Obj_glActiveTexture )
    {
        (*Obj_glActiveTexture)( Texture );
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions