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);
}