TAPs 0.7.7.3
TAPsShaderFns.hpp File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <iostream>
#include <cmath>
#include <GL/glew.h>
#include <GL/wglew.h>
#include "../Core/TAPsLib.hpp"
Include dependency graph for TAPsShaderFns.hpp:

Go to the source code of this file.

Defines

#define GLEW_STATIC   1
#define TAPs_USE_GLSL
#define PrintOpenGLError()   PrintOglError(__FILE__, __LINE__)

Enumerations

enum  EShaderType { EVertexShader, EFragmentShader }

Functions

int PrintOglError (char *file, int line)
static GLint GetUniLoc (GLhandleARB program, const GLcharARB *name)
static GLint GetUniLoc (GLhandleARB program, const GLcharARB *name, int location)
static void PrintInfoLog (GLhandleARB obj)
static int ShaderSize (char *fileName, EShaderType shaderType)
static int ReadShader (char *fileName, EShaderType shaderType, char *shaderText, int size)
int ReadShaderSource (char *fileName, GLcharARB **vertexShader, GLcharARB **fragmentShader)
int InstallShaders (const GLcharARB *PNTriVertex, const GLcharARB *PNTriFragment, GLhandleARB &PNTriProg)
void SetUniformVariables ()
void DrawPNTrianglePatch (int faceNo, int smoothness, float *b, float *n)
void DrawTriangleUsingHardware (int noOfFaces, int smoothness, float *b, float *n)
void DetermineExtensions (Real rVolume)

Variables

bool gbUseGPU = true
int gbSmoothness = 10
GLhandleARB ProgramObject = 0
GLhandleARB VertexShaderObject = 0
GLhandleARB FragmentShaderObject = 0

Define Documentation

#define GLEW_STATIC   1

Definition at line 53 of file TAPsShaderFns.hpp.

#define PrintOpenGLError ( )    PrintOglError(__FILE__, __LINE__)

Definition at line 81 of file TAPsShaderFns.hpp.

Referenced by GetUniLoc(), InstallShaders(), and PrintInfoLog().

#define TAPs_USE_GLSL

Definition at line 62 of file TAPsShaderFns.hpp.


Enumeration Type Documentation

Enumerator:
EVertexShader 
EFragmentShader 

Definition at line 69 of file TAPsShaderFns.hpp.


Function Documentation

void DetermineExtensions ( Real  rVolume)

Definition at line 535 of file TAPsShaderFns.hpp.

{
    if (WGLEW_ARB_pbuffer) {
        std::cout << "OK, we can use pbuffers.\n";
    }
    else {
        std::cout << "Sorry, pbuffers will not work on this platform.\n";
    }
}
void DrawPNTrianglePatch ( int  faceNo,
int  smoothness,
float *  b,
float *  n 
)

*

Definition at line 425 of file TAPsShaderFns.hpp.

References GetUniLoc().

                                                                         {
    //std::cout << "Face#" << faceNo << "\n";

    // b is a pointer to a cubic triangle Bezier cofficient (10 pts * 3 dimensions)
    // n is a pointer to a quadratic triangle Bezier normal ( 6 pts * 3 dimensions)

    // handles to (shader) program object
    GLhandleARB PNTriProg = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);

    int noOfVertices = smoothness*(smoothness+1)/2;
    float *vertex = new float[noOfVertices*3];
    float *normal = new float[noOfVertices*3];

    // Set Uniform Variables
    int bIdx = faceNo*30;
    int nIdx = faceNo*18;
    glUniform3fvARB(GetUniLoc(PNTriProg, "BezierCoeffi"), 10, b+bIdx);
    //std::cerr << "TEST1\n";
    glUniform3fvARB(GetUniLoc(PNTriProg, "BezierNormal"),  6, n+nIdx);
    //std::cerr << "TEST2\n";

    int index = 0;
    float uInc = static_cast<float>( 1.0 / (smoothness-1) );
    float vInc = static_cast<float>( 1.0 / (smoothness-1) );
    float u = 0.0f;
    float v = 0.0f;

    u = 0.0f;
    int row1 = 0, row2 = smoothness*3;
    for ( int r = 1; r < smoothness; ++r, row1+=3 ) {
        v = 0.0f;
        glBegin( GL_TRIANGLE_STRIP );
            for ( int i = 0; i < smoothness-r; ++i, row1+=3, row2+=3 ) {
                glNormal3f( u, v, normal[row1+2] );
                glVertex3f( u, v, vertex[row1+2] );
                u += uInc;
                glNormal3f( u, v, normal[row2+2] );
                glVertex3f( u, v, vertex[row2+2] );
                v += vInc;
                u -= uInc;
            }
            glNormal3f( u, v, normal[row1+2] );
            glVertex3f( u, v, vertex[row1+2] );
        glEnd();
        u += uInc;
    }
    delete [] vertex;
    delete [] normal;
}

Here is the call graph for this function:

void DrawTriangleUsingHardware ( int  noOfFaces,
int  smoothness,
float *  b,
float *  n 
)

*

Definition at line 479 of file TAPsShaderFns.hpp.

References GetUniLoc().

Referenced by OpenGLPNTriangleVolPresModel< T >::DrawGL(), and OpenGLPNTriangleModel< T >::DrawGL().

                                                                                  {
    
    int noOfVertices = smoothness*(smoothness+1)/2;
    float *vertex = new float[noOfVertices*3];
    float *normal = new float[noOfVertices*3];
    for ( int faceNo = 0; faceNo < noOfFaces; ++faceNo ) {
        //std::cout << "Face#" << faceNo << "\n";

        // b is a pointer to a cubic triangle Bezier cofficient (10 pts * 3 dimensions)
        // n is a pointer to a quadratic triangle Bezier normal ( 6 pts * 3 dimensions)

        // handles to (shader) program object
        GLhandleARB PNTriProg = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);

        // Set Uniform Variables
        int bIdx = faceNo*30;
        int nIdx = faceNo*18;
        glUniform3fvARB(GetUniLoc(PNTriProg, "BezierCoeffi"), 10, b+bIdx);
        //std::cerr << "TEST1\n";
        glUniform3fvARB(GetUniLoc(PNTriProg, "BezierNormal"),  6, n+nIdx);
        //std::cerr << "TEST2\n";

        int index = 0;
        float uInc = static_cast<float>( 1.0 / (smoothness-1) );
        float vInc = static_cast<float>( 1.0 / (smoothness-1) );
        float u = 0.0f;
        float v = 0.0f;

        u = 0.0f;
        int row1 = 0, row2 = smoothness*3;
        for ( int r = 1; r < smoothness; ++r, row1+=3 ) {
            v = 0.0f;
            glBegin( GL_TRIANGLE_STRIP );
                for ( int i = 0; i < smoothness-r; ++i, row1+=3, row2+=3 ) {
                    glNormal3f( u, v, normal[row1+2] );
                    glVertex3f( u, v, vertex[row1+2] );
                    u += uInc;
                    glNormal3f( u, v, normal[row2+2] );
                    glVertex3f( u, v, vertex[row2+2] );
                    v += vInc;
                    u -= uInc;
                }
                glNormal3f( u, v, normal[row1+2] );
                glVertex3f( u, v, vertex[row1+2] );
            glEnd();
            u += uInc;
        }
    }
    delete [] vertex;
    delete [] normal;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static GLint GetUniLoc ( GLhandleARB  program,
const GLcharARB *  name 
) [static]

Definition at line 96 of file TAPsShaderFns.hpp.

References PrintOpenGLError.

Referenced by DrawPNTrianglePatch(), DrawTriangleUsingHardware(), InstallShaders(), and SetUniformVariables().

{
    GLint loc;

    loc = glGetUniformLocationARB(program, name);

    if (loc == -1)
        printf("No such uniform named \"%s\"\n", name);

    PrintOpenGLError();  // Check for OpenGL errors
    return loc;
}

Here is the caller graph for this function:

static GLint GetUniLoc ( GLhandleARB  program,
const GLcharARB *  name,
int  location 
) [static]

Definition at line 113 of file TAPsShaderFns.hpp.

References PrintOpenGLError.

{
    GLint loc;

    loc = glGetUniformLocationARB(program, name);

    if (loc == -1)
        printf("No such uniform named \"%s\"\n", name);

    if (location != 0) {
        std::cout << "Loc: " << location << "\n";
    }
    PrintOpenGLError();  // Check for OpenGL errors
    return loc;
}
int InstallShaders ( const GLcharARB *  PNTriVertex,
const GLcharARB *  PNTriFragment,
GLhandleARB &  PNTriProg 
)

Definition at line 322 of file TAPsShaderFns.hpp.

References GetUniLoc(), PrintInfoLog(), and PrintOpenGLError.

{
    GLhandleARB PNTriVS, PNTriFS;//, PNTriProg;   // handles to objects
    GLint       vertCompiled, fragCompiled;    // status values
    GLint       linked;

    // Create a vertex shader object and a fragment shader object

    PNTriVS = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
    PNTriFS = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);

    // Load source code strings into shaders

    glShaderSourceARB(PNTriVS, 1, &PNTriVertex, NULL);
    glShaderSourceARB(PNTriFS, 1, &PNTriFragment, NULL);

    // Compile the PNTri vertex shader, and print out
    // the compiler log file.

    glCompileShaderARB(PNTriVS);
    PrintOpenGLError();  // Check for OpenGL errors
    glGetObjectParameterivARB(PNTriVS,
                GL_OBJECT_COMPILE_STATUS_ARB, &vertCompiled);
    PrintInfoLog(PNTriVS);

    // Compile the PNTri vertex shader, and print out
    // the compiler log file.

    glCompileShaderARB(PNTriFS);
    PrintOpenGLError();  // Check for OpenGL errors
    glGetObjectParameterivARB(PNTriFS,
                GL_OBJECT_COMPILE_STATUS_ARB, &fragCompiled);
    PrintInfoLog(PNTriFS);

    if (!vertCompiled || !fragCompiled)
        return 0;

    // Create a program object and attach the two compiled shaders

    PNTriProg = glCreateProgramObjectARB();
    glAttachObjectARB(PNTriProg, PNTriVS);
    glAttachObjectARB(PNTriProg, PNTriFS);

    // Link the program object and print out the info log

    glLinkProgramARB(PNTriProg);
    PrintOpenGLError();  // Check for OpenGL errors
    glGetObjectParameterivARB(PNTriProg,
                GL_OBJECT_LINK_STATUS_ARB, &linked);
    PrintInfoLog(PNTriProg);

    if (!linked)
        return 0;

    // Install program object as part of current state
    glUseProgramObjectARB(PNTriProg);

    // Set up initial uniform values
    glUniform3fARB(GetUniLoc(PNTriProg, "BrickColor"), 1.0f, 0.3f, 0.2f);
    glUniform3fARB(GetUniLoc(PNTriProg, "MortarColor"), 0.85f, 0.86f, 0.84f);
    //glUniform2fARB(GetUniLoc(PNTriProg, "BrickSize"), 0.30f, 0.15f);
    glUniform2fARB(GetUniLoc(PNTriProg, "BrickSize"), 1.80f, 0.90f);
    glUniform2fARB(GetUniLoc(PNTriProg, "BrickPct"), 0.90f, 0.85f);
    glUniform3fARB(GetUniLoc(PNTriProg, "LightPosition"), 0.0f, 2.0f, 4.0f);
    glUniform1fARB(GetUniLoc(PNTriProg, "SpecMag"), 16.0f);

    return 1;
}

Here is the call graph for this function:

static void PrintInfoLog ( GLhandleARB  obj) [static]

Definition at line 132 of file TAPsShaderFns.hpp.

References PrintOpenGLError.

Referenced by InstallShaders().

{
    int infologLength = 0;
    int charsWritten  = 0;
    GLcharARB *infoLog;

    PrintOpenGLError();  // Check for OpenGL errors

    glGetObjectParameterivARB(obj, GL_OBJECT_INFO_LOG_LENGTH_ARB,
                                         &infologLength);
    PrintOpenGLError();  // Check for OpenGL errors

    if (infologLength > 0)
    {
        infoLog = (GLcharARB*)malloc(infologLength);
        if (infoLog == NULL)
        {
            printf("ERROR: Could not allocate InfoLog buffer\n");
            exit(1);
        }
        glGetInfoLogARB(obj, infologLength, &charsWritten, infoLog);
        printf("InfoLog:\n%s\n\n", infoLog);
        free(infoLog);
    }
    PrintOpenGLError();  // Check for OpenGL errors
}

Here is the caller graph for this function:

int PrintOglError ( char *  file,
int  line 
)

Definition at line 551 of file TAPsShaderFns.hpp.

{
    //
    // Returns 1 if an OpenGL error occurred, 0 otherwise.
    //
    GLenum glErr;
    int    retCode = 0;

    glErr = glGetError();
    while (glErr != GL_NO_ERROR)
    {
        printf("glError in file %s @ line %d: %s\n", file, line, gluErrorString(glErr));
        retCode = 1;
        glErr = glGetError();
    }
    return retCode;
}
static int ReadShader ( char *  fileName,
EShaderType  shaderType,
char *  shaderText,
int  size 
) [static]

Definition at line 218 of file TAPsShaderFns.hpp.

References EFragmentShader, and EVertexShader.

Referenced by ReadShaderSource().

{
    //
    // Reads a shader from the supplied file and returns the shader in the
    // arrays passed in. Returns 1 if successful, 0 if an error occurred.
    // The parameter size is an upper limit of the amount of bytes to read.
    // It is ok for it to be too big.
    //
    FILE *fh;
    char name[100];
    int count;

    strcpy(name, fileName);

    switch (shaderType) 
    {
        case EVertexShader:
            strcat(name, ".vert");
            break;
        case EFragmentShader:
            strcat(name, ".frag");
            break;
        default:
            printf("ERROR: unknown shader file type\n");
            exit(1);
            break;
    }

    //
    // Open the file
    //
    fh = fopen(name, "r");
    if (!fh)
        return -1;

    //
    // Get the shader from a file.
    //
    fseek(fh, 0, SEEK_SET);
    count = static_cast<int>( fread(shaderText, 1, size, fh) );
    shaderText[count] = '\0';

    if (ferror(fh))
        count = 0;

    fclose(fh);
    return count;
}

Here is the caller graph for this function:

int ReadShaderSource ( char *  fileName,
GLcharARB **  vertexShader,
GLcharARB **  fragmentShader 
)

Definition at line 270 of file TAPsShaderFns.hpp.

References EFragmentShader, EVertexShader, ReadShader(), and ShaderSize().

{
    int vSize, fSize;

    //
    // Allocate memory to hold the source of our shaders.
    //
    vSize = ShaderSize(fileName, EVertexShader);
    fSize = ShaderSize(fileName, EFragmentShader);
    std::cout << "vSize = " << vSize << std::endl;
    std::cout << "fSize = " << fSize << std::endl;
    //char c;
    //std::cout << "Press any key to continue." << std::endl;
    //std::cin >> c;

    if (vSize == -1)
    {
        printf("Cannot determine size of the shader %s.vert\n", fileName);
        exit(1);
        return 0;
    }
    if (fSize == -1)
    {
        printf("Cannot determine size of the shader %s.frag\n", fileName);
        exit(1);
        return 0;
    }

    *vertexShader = (GLcharARB *) malloc(vSize);
    *fragmentShader = (GLcharARB *) malloc(fSize);

    //
    // Read the source code
    //
    if (!ReadShader(fileName, EVertexShader, *vertexShader, vSize))
    {
        printf("Cannot read the file %s.vert\n", fileName);
        return 0;
    }

    if (!ReadShader(fileName, EFragmentShader, *fragmentShader, fSize))
    {
        printf("Cannot read the file %s.frag\n", fileName);
        return 0;
    }

    return 1;
}

Here is the call graph for this function:

void SetUniformVariables ( )

Definition at line 397 of file TAPsShaderFns.hpp.

References GetUniLoc().

                           {
    static float brickSize[] = { 1.80f, 0.90f };
    static int count = 1;
    // handles to (shader) program object
    GLhandleARB PNTriProg = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);

    glUniform2fARB(GetUniLoc(PNTriProg, "BrickSize"), brickSize[0], brickSize[1]);
    
    float inc = 0.000025f;
    if (count <= 10000) {
        brickSize[0] += inc;
        brickSize[1] += inc;
    }
    else if (count <= 20000) {
        brickSize[0] -= inc;
        brickSize[1] -= inc;
    }
    else {
        count = 0;
    }
    ++count;
    //std::cout << count << std::endl;
}

Here is the call graph for this function:

static int ShaderSize ( char *  fileName,
EShaderType  shaderType 
) [static]

Definition at line 162 of file TAPsShaderFns.hpp.

References EFragmentShader, and EVertexShader.

Referenced by ReadShaderSource().

{
    //
    // Returns the size in bytes of the shader fileName.
    // If an error occurred, it returns -1.
    //
    // File name convention:
    //
    // <fileName>.vert
    // <fileName>.frag
    //
    int fd;
    char name[128];
    int count = -1;

    strcpy(name, fileName);

    switch (shaderType)
    {
        case EVertexShader:
            strcat(name, ".vert");
            break;
        case EFragmentShader:
            strcat(name, ".frag");
            break;
        default:
            printf("ERROR: unknown shader file type\n");
            exit(1);
            break;
    }

    //
    // Open the file, seek to the end to find its length
    //
#ifdef WIN32 /*[*/
    fd = _open(name, _O_RDONLY);
    if (fd != -1)
    {
        count = _lseek(fd, 0, SEEK_END) + 1;
        _close(fd);
    }
#else /*][*/
    fd = open(name, O_RDONLY);
    if (fd != -1)
    {
        count = lseek(fd, 0, SEEK_END) + 1;
        close(fd);
    }
#endif /*]*/

    return count;
}

Here is the caller graph for this function:


Variable Documentation

GLhandleARB FragmentShaderObject = 0

Definition at line 91 of file TAPsShaderFns.hpp.

int gbSmoothness = 10

Definition at line 88 of file TAPsShaderFns.hpp.

GLhandleARB ProgramObject = 0

Definition at line 89 of file TAPsShaderFns.hpp.

GLhandleARB VertexShaderObject = 0

Definition at line 90 of file TAPsShaderFns.hpp.

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines