0

Ok Im new to shaders and I am using glew... okay here is my code

#include <iostream>
#include <gl/glew.h>
#include <gl/glut.h>

float angle = 0.0f;

//global shader handle
GLhandleARB  g_shader;

char * LoadShader(char *file)
{
    FILE *f;
    fopen_s(&f,file, "r");

    if( f == NULL)
        return NULL;

    fseek(f, 0, SEEK_END);
    int size = ftell(f);
    fseek(f,0, SEEK_SET);
    if( size == 0 )
        return NULL;
    char *l_file = new char[size + 1];
    fread(l_file, 1, size, f);

    while( size-- > 0)
    {
        if(l_file[size] == (char)10)
        {
            l_file[size] = '\0';
            break;
        }
    }
    return l_file;
}

void renderScene(void) 
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef( 0.0f, 0.0f, -5.0f);
    glUseProgramObjectARB( g_shader );

    float vertices[] = { -1, 0, 0,  1, 0, 0,  0, 1, 0 };
    float colors [] =  { 1, 0, 0,  1, 0, 0,  0, 1, 0 };

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vertices);

    glEnableClientState(GL_COLOR_ARRAY);
    glColorPointer( 3, GL_FLOAT, 0, colors);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
    glUseProgramObjectARB(0);
    glutSwapBuffers();
    glutPostRedisplay();
    angle++;
}
void changeSize(int w, int h) {

    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if(h == 0)
        h = 1;

    float ratio = 1.0* w / h;

    // Reset the coordinate system before modifying
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Set the viewport to be the entire window
    glViewport(0, 0, w, h);

    // Set the correct perspective.
    gluPerspective(45,ratio,1,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0,0.0,5.0, 
              0.0,0.0,-1.0,
              0.0f,1.0f,0.0f);


}
bool setShaders( char *vertShaderFile, char *pixelShaderFile)
{
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
      /* Problem: glewInit failed, something is seriously wrong. */
      fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    }
    fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));

       /*runtime error occurs here*/
    g_shader = glCreateProgramObjectARB();
    GLhandleARB f = NULL; 
    f = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);    
    GLhandleARB v = NULL; 
    v = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);

    //loading and compiling vertex shader
    char *vs = LoadShader( vertShaderFile );
    char *fs = LoadShader( pixelShaderFile );

    if( fs == NULL )
    {
        std::cout<<"Fragment Shader invalid file"<<std::endl;
        return false;
    }
    if( vs == NULL )
    {
        std::cout<<"Vertex Shader invalid file"<<std::endl;
        return false;
    }




    const char *vv = vs;
    const char *ff = fs;

    glShaderSourceARB(v, 1, &vv, NULL);
    glShaderSourceARB(f, 1, &ff, NULL);

    delete [] vs;
    delete [] fs;

    glCompileShader( v );
    glCompileShader( f );


    glAttachObjectARB(g_shader, v);
    glAttachObjectARB(g_shader, f);

    glLinkProgramARB( g_shader );
    int link = 0;
    char error[1024];
    glGetObjectParameterivARB(g_shader, GL_OBJECT_LINK_STATUS_ARB, &link);
    if( !link )
    {
        glGetInfoLogARB(g_shader, sizeof(error), NULL, error);
        printf("Error: Shader Linkage error: \n%s\n", error);
        return false;
    }
    return true;
}
bool init()
{
    glClearColor( 0.0f, 0.0f, 0.0f, 1.0f);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);

    return true;
}

void main(int argc, char **argv) 
{
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("maze");
    glutDisplayFunc(renderScene);

    glutIdleFunc(renderScene);
    glutReshapeFunc(changeSize);
    glEnable(GL_DEPTH_TEST);
    init();

    if( !setShaders("../maze/vert.glsl","../maze/frag.glsl"))
        return;
    glutMainLoop();
}
//end code

Okay so please do not ask about the shader code cause it is set up properly. I am getting a runtime error saying:

Unhandled exception at 0x00000000 in prog.exe: 0xC0000005: Access violation reading location 0x00000000.

this happens whenever I am trying to call the glew function glCreateProgramObjectARB... Not sure what is going on and any help would be great

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by PsychoLogic
0

I realise this is an old thread, but anyone else with the same problem (as I just had), the solution is you need to run glewInit() before any of its exposed functionality can be used. In the above example, I would run it in the init() function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.