2
\$\begingroup\$

Hello this code seems so well done and yet I'm getting this weird access violation error when I use anything but 0 in glShaderSources 2nd argument :

glShaderSource( fragshader, 0, fragfilebuff, NULL );

If I use lenofstr or any other number then 0 it calls up an error like this :

error

I have no idea for the life of me why this is happpening I've updated my drivers , I've even tried messing around with glewInit(); too see if it's something to do with that but no luck what so ever.

This nvidia dll has some problem with my code and I don't know why?...

void LoadShaders(const char *fragshaderfn, const char *vertshaderfn)
{
    FILE *fragfile;
    const GLchar *fragfilebuff[256] = { 0 };
    fopen_s(&fragfile, fragshaderfn, "r");

    fread(fragfilebuff, sizeof(char), sizeof(char) * 256, fragfile);
    printf(fragfilebuff);

    GLsizei lenofstr = strlen(fragfilebuff);
    printf("this is the length -> %d", lenofstr);

    GLuint fragshader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(fragshader, 0,
        fragfilebuff, NULL);

    glCompileShader(fragshader);

};

Here's the complete source :

#include <SDL.h>
#include <GL/glew.h>
#include <SDL_opengl.h>
#include <stdio.h>
#include <conio.h>


#define SCREEN_HEIGHT 480
#define SCREEN_WIDTH 640


SDL_Renderer *mainrenderer;
SDL_Window *mainwindow;
SDL_Event event;
bool running = true;


void LoadShaders(const char *fragshaderfn, const char *vertshaderfn)
{
    FILE *fragfile;
    const GLchar *fragfilebuff[256] = { 0 };
    fopen_s(&fragfile, fragshaderfn, "r");

    fread(fragfilebuff, sizeof(char), sizeof(char) * 256, fragfile);
    printf(fragfilebuff);

    GLsizei lenofstr = strlen(fragfilebuff);
    printf("this is the length -> %d", lenofstr);

    GLuint fragshader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(fragshader, lenofstr,
        fragfilebuff, NULL);

    glCompileShader(fragshader);

};


int main(int argc , char **argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);

    mainwindow = SDL_CreateWindow(
        "C OpenGL Engine",
        300, 300, 640, 480,
        SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL );

    SDL_GLContext maincontext = SDL_GL_CreateContext(mainwindow);
    SDL_GL_MakeCurrent(mainwindow, maincontext);

    glewExperimental = GL_TRUE;
    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));

    if (SDL_GL_SetSwapInterval(1) < 0)
    {
        printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
    }

    LoadShaders("ffile.frag", "");

    glClearColor(0.0, 0.0, 0.0, 1.0);

    while (running == true)
    {
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_QUIT:
                    SDL_GL_DeleteContext(maincontext);
                    SDL_Quit();

                    return 0;
                break;
            }
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

        SDL_GL_SwapWindow(mainwindow);
    }

    SDL_GL_DeleteContext(maincontext);
    SDL_DestroyWindow(mainwindow);
    SDL_Quit();
    return 0;
}

All help would be appreciated I am using C , SDL 2.0 and OpenGL 3.3+ , MSVC 2015 , Win10 64bit

\$\endgroup\$
1
  • \$\begingroup\$ Easy: glShaderSource( fragshader, 1, &fragfilebuff, NULL ); if your string has a 0x00 at the end. Replace NULL with &fragfilelen if not. \$\endgroup\$
    – MickLH
    Commented Apr 14, 2016 at 0:21

1 Answer 1

4
\$\begingroup\$
void glShaderSource(GLuint shader,
    GLsizei count,
    const GLchar **string,
    const GLint *length);

Okay, I guess you didn't understand what these values mean. string is an "array"(pointer is more correct since a pointer is not an array) of "char const*" (I advice you to put all your code in one and only one char const *.

length is an "array" of int. Each values inside this "array" should be set at the length of the respective "char const*" in "string".

Count is just the number of char const* and int you have in both array above.

So, I'd go for that :

char const *code = getStringFromFile(path);
int length = strlen(code);
glShaderSource(shader, 1, &code, &length);

That should work :)

the getStringFromFile(path) could be done like this:

const char *getStringFromFile(char const *path) {
    FILE *file = fopen(path, "ab"); // open the file at the end and binary
    int length = ftell(file); // Ask the size of the file
    fseek(file, 0, SEEK_SET); // return to the beginning of the file
    char *buffer = calloc(length + 1, sizeof(char)); // alloc the buffer with the end 0
    fread(buffer, sizeof(char), length, file); // read the file
    fclose(file);
    return buffer;
}

This code may countain error, I didn't test it, but the idea is here

\$\endgroup\$
10
  • \$\begingroup\$ omg someone answered : D , I'll study this so much and see if I can make it work. I should say that I did know an array might be needed however wasn't entirely shore how strict it was about that so good for clarifying. \$\endgroup\$ Commented Apr 13, 2016 at 23:11
  • \$\begingroup\$ Good luck, if you did not understand something, please, let me know. Btw, by getStringFromFile, I mean a function which put all the file into one buffer and return it. (Don't forget to free it) \$\endgroup\$ Commented Apr 13, 2016 at 23:13
  • \$\begingroup\$ It is a c++ code I made, but maybe it could help you github.com/qnope/GLTools/blob/master/System/GL/Pipeline/… \$\endgroup\$ Commented Apr 13, 2016 at 23:15
  • \$\begingroup\$ I'll check it and yes if i do not understand something I'll defiantly without a shadow of a doubt be right here again : ). \$\endgroup\$ Commented Apr 13, 2016 at 23:15
  • \$\begingroup\$ Antonie Morrier I must say I'm confused as why this code works , I'm glad that it is by the way just wanted too know why exactly I needed too make a const char *getstringfromfile function and use that in glshadersource instead of just giving it the buffer, is this because OpenGL can't read the buffer and needs you to do it for it? \$\endgroup\$ Commented Apr 13, 2016 at 23:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .