2

I'm following this tutorial: https://www.youtube.com/watch?v=0CQP8huwLCg

I'm using XCode on Mac instead of Windows VS. I've linked everything together. However, I have deprecated function calls. Also, my fseek(fp, 0, SEEK_END); is returning NULL for some reason. Can anyone tell me how I can fix the deprecation calls and where I can find the updated API? Second, why is fseek returning null? I have the vsh and fsh files in the same folder...

Here's the main.cpp code to use this on a Mac:

#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#endif
#include <stdio.h>
#include <iostream>
using namespace std;

static char* readFile(const char* filename) {
    // Open the file
    FILE* fp = fopen (filename, "r");
    // Move the file pointer to the end of the file and determing the length
    fseek(fp, 0, SEEK_END);
    long file_length = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    char* contents = new char[file_length+1];
    // zero out memory
    for (int i = 0; i < file_length+1; i++) {
        contents[i] = 0;
    }
    // Here's the actual read
    fread (contents, 1, file_length, fp);
    // This is how you denote the end of a string in C
    contents[file_length+1] = '\0';
    fclose(fp);
    return contents;
}

GLuint makeVertexShader(const char* shaderSource) {
    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource (vertexShaderID, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(vertexShaderID);
    return vertexShaderID;
}

GLuint makeFragmentShader(const char* shaderSource) {
    GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShaderID, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(fragmentShaderID);
    return fragmentShaderID;
}

GLuint makeShaderProgram (GLuint vertexShaderID, GLuint fragmentShaderID) {
    GLuint shaderID = glCreateProgram();
    glAttachShader(shaderID, vertexShaderID);
    glAttachShader(shaderID, fragmentShaderID);
    glLinkProgram(shaderID);
    return shaderID;
}

int main (int argc, char** argv) {
    // Standard stuff...
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Shaders");
    //glewInit(); //only use for Windows platform

    char* vertexShaderSourceCode = readFile("vertexShader.vsh");
    char* fragmentShaderSourceCode = readFile("fragmentShader.fsh");
    GLuint vertShaderID = makeVertexShader(vertexShaderSourceCode);
    GLuint fragShaderID = makeFragmentShader(fragmentShaderSourceCode);
    GLuint shaderProgramID = makeShaderProgram(vertShaderID, fragShaderID);
    glUseProgram(shaderProgramID);
    printf ("vertShaderID is %d\n", vertShaderID);
    printf ("fragShaderID is %d\n", fragShaderID);
    printf ("shaderProgramID is %d\n", shaderProgramID);
    glDeleteProgram(shaderProgramID);
    int temp;
    scanf ("%d", &temp);
    return 0;
}

Then, here is the vertexShader.vsh file:

in vec4 s_vPosition;

void main () {
    // Look, Ma!  I avoided any matrix multiplication!
    // The value of s_vPosition should be between -1.0 and +1.0 (to be visible on the screen)
    gl_Position = s_vPosition;
}

Then, the fragmentShader.fsh:

out vec4 s_vColor;

void main () {
    // No matter what, color the pixel red!
    fColor = vec4 (1.0, 0.0, 0.0, 1.0);
}
jww
  • 97,681
  • 90
  • 411
  • 885
  • 5
    `fseek` returns 0 on success `0 == NULL`, so I don't see what you are asking on that count. Not sure what is deprecated, can you provide some details? – Mats Petersson Mar 11 '15 at 21:25
  • it gives a bad access error – Vector Croc Mar 11 '15 at 23:38
  • 1
    Where, on what line, what function? – Mats Petersson Mar 11 '15 at 23:40
  • the fseek and ftell are there to get the size of the file, you can instead use a realloc in a read loop to expand the buffer or use `fstat` to get the file length (though that breaks down with files over 4 gig) – ratchet freak Mar 11 '15 at 23:58
  • GLUT is marked as deprecated in Mac OS. You can still use it, though. See my answer here for details: http://stackoverflow.com/questions/24095931/glut-deprecation-in-mac-osx-10-9-ide-qt-creator/24098402#24098402. – Reto Koradi Mar 12 '15 at 03:57
  • thanks, I know I can still use them, I'm just looking for alternatives – Vector Croc Mar 12 '15 at 19:46
  • @MatsPetersson error at every instance/line of fseek. also all these: //glutInit(&argc, argv); //glutInitDisplayMode(GLUT_RGBA); //glutInitWindowSize(800, 600); //glutCreateWindow("Shaders"); – Vector Croc Mar 12 '15 at 19:47

1 Answers1

1

fseek (...) is supposed to return 0 (NULL) on success, so that is perfectly understandable. If ftell (...) is returning a non-zero value, then everything is good.

"it gives a bad access error"

First of all, do not bother zeroing out the memory. The only zero that matters is the one you added above fclose (...). Not only is that process highly inefficient, but the line where you add your NULL terminator is also the source of your problems.

You have a simple off-by-one indexing situation.

You have allocated an array with file_length+1 bytes and then you try to add a NULL terminator using the array subscript [file_length+1].

That is equivalent to the following expression: *(contents + file_length + 1) = '\0', which writes the value '\0' 1 byte beyond the end of your allocated memory.

I blame the new [...] operator, you probably read the line new char[file_length+1] and forgot that array indices begin at 0.

To fix your problem, simply re-write your code this way:

// This is how you denote the end of a string in C
contents[file_length] = '\0';

Incidentally, if you choose to keep the loop that writes all zeros to your allocated memory, you do not even need the line mentioned above. I would strongly suggest you nix the loop and fix your array subscript though.


As for deprecated functions, nothing you have shown is deprecated. You have not shown enough code to make those shaders work, however. At the very least you need a single vertex attribute pointer. Wherever that is done is probably the source of your deprecated function errors.

Community
  • 1
  • 1
Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • `calloc (...)` can allocate and zero your array in a single line of code, by the way. You don't seem to have anything against using the C standard library in the rest of your code - you might consider doing that instead of writing a separate loop if this sort of thing pops up in the future. – Andon M. Coleman Mar 12 '15 at 23:45
  • thanks for the tip for being efficient. I'm actually following the tutorial, so that was the code given. The most important part I'm trying to learn is making the triangle appear. I have replaced his function with a function using fstream, so that is no longer a problem. The functions were deprecated in OSX 10.9, if you're on a Mac: – Vector Croc Mar 13 '15 at 00:16