Skip to main content
added 1923 characters in body
Source Link
PB020
  • 55
  • 7

Solution: The error was in my GLSL shader code, not the code I was using to compile the shaders. That being said, I also made changes to properly check for compilation and linking errors based on @Rabbid76 and @David Sullivan's responses.

Shader* createShader(char* vertexfile, char* fragmentfile) { const char* vertexcode = loadshader(vertexfile); unsigned int vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex, 1, &vertexcode, NULL); glCompileShader(vertex); { char log[1024]; int success; glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertex, 1024, NULL, log); printf("Vertex Shader Failed to Compile!\n> OpenGL Error: %s\n", log); } } const char* fragmentcode = loadshader(fragmentfile); unsigned int fragment = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment, 1, &fragmentcode, NULL); glCompileShader(fragment); { char log[1024]; int success; glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragment, 1024, NULL, log); printf("Fragment Shader Failed to Compile!\n> OpenGL Error: %s\n", log); } } unsigned int data = glCreateProgram(); glAttachShader(data, vertex); glAttachShader(data, fragment); glLinkProgram(data); { char log[1024]; int success; glGetProgramiv(data, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(data, 1024, NULL, log); printf("Shader Failed to Link!\n> OpenGL Error: %s", log); } } glDeleteShader(vertex); glDeleteShader(fragment); free((void*) vertexcode); free((void*) fragmentcode); Shader* shader = (Shader*) malloc(sizeof(Shader)); shader -> data = data; return shader; } 

Solution: The error was in my GLSL shader code, not the code I was using to compile the shaders. That being said, I also made changes to properly check for compilation and linking errors based on @Rabbid76 and @David Sullivan's responses.

Shader* createShader(char* vertexfile, char* fragmentfile) { const char* vertexcode = loadshader(vertexfile); unsigned int vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex, 1, &vertexcode, NULL); glCompileShader(vertex); { char log[1024]; int success; glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertex, 1024, NULL, log); printf("Vertex Shader Failed to Compile!\n> OpenGL Error: %s\n", log); } } const char* fragmentcode = loadshader(fragmentfile); unsigned int fragment = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment, 1, &fragmentcode, NULL); glCompileShader(fragment); { char log[1024]; int success; glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragment, 1024, NULL, log); printf("Fragment Shader Failed to Compile!\n> OpenGL Error: %s\n", log); } } unsigned int data = glCreateProgram(); glAttachShader(data, vertex); glAttachShader(data, fragment); glLinkProgram(data); { char log[1024]; int success; glGetProgramiv(data, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(data, 1024, NULL, log); printf("Shader Failed to Link!\n> OpenGL Error: %s", log); } } glDeleteShader(vertex); glDeleteShader(fragment); free((void*) vertexcode); free((void*) fragmentcode); Shader* shader = (Shader*) malloc(sizeof(Shader)); shader -> data = data; return shader; } 
added 577 characters in body
Source Link
PB020
  • 55
  • 7
#define GLEW_STATIC #define NO_SDL_GLEXT #include <GL/glew.h> #include <SDL2/SDL.h> #include <SDL2/SDL_opengl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFFSIZE 1024 #define MAXSIZE 1048576 typedef struct Shader { unsigned int data; } Shader; char* loadshader(char*); Shader* createShader(char* vertexfile, char* fragmentfile) { char msg[512]; char log[512]; int success;  const char* vertexcode = loadshader(vertexfile); unsigned int vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex, 1, &vertexcode, NULL); glCompileShader(vertex); {   char log[512]; int success; glGetProgramiv(vertex, GL_LINK_STATUSGL_COMPILE_STATUS, &success); if (!success) {   printf("Vertex File Name: %s\n", vertexfile); printf("Vertex Code: %d\n", vertexcode);  printf("Vertex Value: %d\n", vertex);  glGetProgramInfoLog(vertex, 512, NULL, log);   printf(strcat(strcpy(msg, "Vertex Shader Failed to Compile!\n> OpenGL Error: "),%s\n\n log)); - - - - }- \n\n", log); } const char* fragmentcode = loadshader(fragmentfile); unsigned int fragment = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment, 1, &fragmentcode, NULL); glCompileShader(fragment); { char log[512]; int success; glGetProgramiv(fragment, GL_LINK_STATUSGL_COMPILE_STATUS, &success); if (!success) { printf("\n\nFragment"Fragment File Name: %s\n", fragmentfile); printf("Fragment Code: %d\n", fragmentcode);  printf("Fragment Value: %d\n", fragment);  glGetProgramInfoLog(fragment, 512, NULL, log);   printf(strcat(strcpy(msg, "Fragment Shader Failed to LinkCompile!\n> OpenGL Error"),Error: log)); %s\n\n - - - }- - \n\n", log); } unsigned int data = glCreateProgram(); glAttachShader(data, vertex); glAttachShader(data, fragment); glLinkProgram(data); {   char log[512]; int success; glGetProgramiv(data, GL_LINK_STATUS, &success); if (!success) {   printf("Program Value: %d\n", data); glGetProgramInfoLog(data, 512, NULL, log);   printf(strcat(strcpy(msg, "\n\nShader Program"Shader Failed to Link!\n> OpenGL Error: ")%s", log)); } } glDeleteShader(vertex); glDeleteShader(fragment); free((void*) vertexcode); free((void*) fragmentcode); Shader* shader = (Shader*) malloc(sizeof(Shader)); shader -> data = data; return shader; } int mainWinMain(int argc, char const *argv[]) { glewInitSDL_Init(SDL_INIT_VIDEO); Shader*SDL_Window* shaderwindow = createShaderSDL_CreateWindow("data/test.vs""Basic Window", "data/test.fs"660, 240, 600, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); printf("%s\n",SDL_GLContext shadercontext ?= "ShaderSDL_GL_CreateContext(window);  Successfully Created!"glewInit();  : "FailedShader* toshader Create= Shader!"createShader("data/test.vs", "data/test.fs"); return 0; } char* loadshader(char* filename) { FILE* file = fopen(filename, "r"); if (!file) return NULL; int flag = 0; char* shader = (char*) malloc(MAXSIZE); char* buffer = (char*) malloc(BUFFSIZE); while (fgets(buffer, BUFFSIZE, file) != NULL) strcat(shader, buffer); free(buffer); fclose(file); printf("\"%s\" Content:\n%s\n", filename, shader); return shader; } 
"data/test.vs" Content: #version 330 core layout (location = 0) in vec3 POSITION; void main() { gl_Position = vec4(POSITION, 1.0); } Vertex File Name: data/test.vs Vertex Code: 81465408-1521078208 Vertex Value: 1 Vertex Shader Failed to Compile! > OpenGL Error: ░☻░╗x¥j☻ - - - - - "data/test.fs" Content: #version 330 core out vec4 COLOR; void main() { COLOR = vec4((1.0f, 0.5f, 0.2f, 1.0f); } Fragment File Name: data/test.fs Fragment Code: 82587712-1486147520 Fragment Value: 2 Fragment Shader Failed to Compile! > OpenGL Error: ░☻░╗x¥j☻ [process exited- with- code- 4294967295- - Program Value: 3 Shader Failed to Link! > OpenGL Error: Fragment shader(0xffffffffs)] were not successfully compiled before glLinkProgram() was called. Link failed. 
#define GLEW_STATIC #include <GL/glew.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFFSIZE 1024 #define MAXSIZE 1048576 typedef struct Shader { unsigned int data; } Shader; char* loadshader(char*); Shader* createShader(char* vertexfile, char* fragmentfile) { char msg[512]; char log[512]; int success;  const char* vertexcode = loadshader(vertexfile); unsigned int vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex, 1, &vertexcode, NULL); glCompileShader(vertex); { glGetProgramiv(vertex, GL_LINK_STATUS, &success); if (!success) {   printf("Vertex File Name: %s\n", vertexfile); printf("Vertex Code: %d\n", vertexcode);  printf("Vertex Value: %d\n", vertex); glGetProgramInfoLog(vertex, 512, NULL, log);   printf(strcat(strcpy(msg, "Vertex Shader Failed to Compile!\n> OpenGL Error: "), log));  } } const char* fragmentcode = loadshader(fragmentfile); unsigned int fragment = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment, 1, &fragmentcode, NULL); glCompileShader(fragment); { glGetProgramiv(fragment, GL_LINK_STATUS, &success); if (!success) { printf("\n\nFragment File Name: %s\n", fragmentfile); printf("Fragment Code: %d\n", fragmentcode);  printf("Fragment Value: %d\n", fragment); glGetProgramInfoLog(fragment, 512, NULL, log);   printf(strcat(strcpy(msg, "Fragment Shader Failed to Link!\n> OpenGL Error"), log));  } } unsigned int data = glCreateProgram(); glAttachShader(data, vertex); glAttachShader(data, fragment); glLinkProgram(data); { glGetProgramiv(data, GL_LINK_STATUS, &success); if (!success) {   printf("Program Value: %d\n", data); glGetProgramInfoLog(data, 512, NULL, log);   printf(strcat(strcpy(msg, "\n\nShader Program Failed to Link!\n> OpenGL Error: "), log)); } } glDeleteShader(vertex); glDeleteShader(fragment); free((void*) vertexcode); free((void*) fragmentcode); Shader* shader = (Shader*) malloc(sizeof(Shader)); shader -> data = data; return shader; } int main(int argc, char const *argv[]) { glewInit(); Shader* shader = createShader("data/test.vs", "data/test.fs"); printf("%s\n", shader ? "Shader Successfully Created!" : "Failed to Create Shader!"); return 0; } char* loadshader(char* filename) { FILE* file = fopen(filename, "r"); if (!file) return NULL; int flag = 0; char* shader = (char*) malloc(MAXSIZE); char* buffer = (char*) malloc(BUFFSIZE); while (fgets(buffer, BUFFSIZE, file) != NULL) strcat(shader, buffer); free(buffer); fclose(file); return shader; } 
Vertex File Name: data/test.vs Vertex Code: 81465408 Vertex Value: 1 Vertex Shader Failed to Compile! > OpenGL Error: ░☻ Fragment File Name: data/test.fs Fragment Code: 82587712 Fragment Value: 2 Fragment Shader Failed to Compile! > OpenGL Error: ░☻ [process exited with code 4294967295 (0xffffffff)] 
#define GLEW_STATIC #define NO_SDL_GLEXT #include <GL/glew.h> #include <SDL2/SDL.h> #include <SDL2/SDL_opengl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFFSIZE 1024 #define MAXSIZE 1048576 typedef struct Shader { unsigned int data; } Shader; char* loadshader(char*); Shader* createShader(char* vertexfile, char* fragmentfile) { const char* vertexcode = loadshader(vertexfile); unsigned int vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex, 1, &vertexcode, NULL); glCompileShader(vertex); {   char log[512]; int success; glGetProgramiv(vertex, GL_COMPILE_STATUS, &success); printf("Vertex File Name: %s\n", vertexfile); printf("Vertex Code: %d\n", vertexcode); printf("Vertex Value: %d\n", vertex);  glGetProgramInfoLog(vertex, 512, NULL, log); printf("Vertex Shader Failed to Compile!\n> OpenGL Error: %s\n\n - - - - - \n\n", log); } const char* fragmentcode = loadshader(fragmentfile); unsigned int fragment = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment, 1, &fragmentcode, NULL); glCompileShader(fragment); { char log[512]; int success; glGetProgramiv(fragment, GL_COMPILE_STATUS, &success); printf("Fragment File Name: %s\n", fragmentfile); printf("Fragment Code: %d\n", fragmentcode); printf("Fragment Value: %d\n", fragment);  glGetProgramInfoLog(fragment, 512, NULL, log); printf("Fragment Shader Failed to Compile!\n> OpenGL Error: %s\n\n - - - - - \n\n", log); } unsigned int data = glCreateProgram(); glAttachShader(data, vertex); glAttachShader(data, fragment); glLinkProgram(data); {   char log[512]; int success; glGetProgramiv(data, GL_LINK_STATUS, &success); printf("Program Value: %d\n", data); glGetProgramInfoLog(data, 512, NULL, log); printf("Shader Failed to Link!\n> OpenGL Error: %s", log); } glDeleteShader(vertex); glDeleteShader(fragment); free((void*) vertexcode); free((void*) fragmentcode); Shader* shader = (Shader*) malloc(sizeof(Shader)); shader -> data = data; return shader; } int WinMain(int argc, char const *argv[]) { SDL_Init(SDL_INIT_VIDEO); SDL_Window* window = SDL_CreateWindow("Basic Window", 660, 240, 600, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); SDL_GLContext context = SDL_GL_CreateContext(window);  glewInit();  Shader* shader = createShader("data/test.vs", "data/test.fs"); return 0; } char* loadshader(char* filename) { FILE* file = fopen(filename, "r"); if (!file) return NULL; int flag = 0; char* shader = (char*) malloc(MAXSIZE); char* buffer = (char*) malloc(BUFFSIZE); while (fgets(buffer, BUFFSIZE, file) != NULL) strcat(shader, buffer); free(buffer); fclose(file); printf("\"%s\" Content:\n%s\n", filename, shader); return shader; } 
"data/test.vs" Content: #version 330 core layout (location = 0) in vec3 POSITION; void main() { gl_Position = vec4(POSITION, 1.0); } Vertex File Name: data/test.vs Vertex Code: -1521078208 Vertex Value: 1 Vertex Shader Failed to Compile! > OpenGL Error: ░╗x¥j☻ - - - - - "data/test.fs" Content: #version 330 core out vec4 COLOR; void main() { COLOR = vec4((1.0f, 0.5f, 0.2f, 1.0f); } Fragment File Name: data/test.fs Fragment Code: -1486147520 Fragment Value: 2 Fragment Shader Failed to Compile! > OpenGL Error: ░╗x¥j☻ - - - - - Program Value: 3 Shader Failed to Link! > OpenGL Error: Fragment shader(s) were not successfully compiled before glLinkProgram() was called. Link failed. 
Source Link
PB020
  • 55
  • 7

OpenGL Shader Compilation and Linking Error

I have a method that creates and returns a shader program from two given strings (vertex and fragment shader filenames). Initially, it was working perfectly, with the compilation and linking being successful, before randomly failing and giving me an error message of unicode emoji's.

Example Code:

#define GLEW_STATIC #include <GL/glew.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFFSIZE 1024 #define MAXSIZE 1048576 typedef struct Shader { unsigned int data; } Shader; char* loadshader(char*); Shader* createShader(char* vertexfile, char* fragmentfile) { char msg[512]; char log[512]; int success; const char* vertexcode = loadshader(vertexfile); unsigned int vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex, 1, &vertexcode, NULL); glCompileShader(vertex); { glGetProgramiv(vertex, GL_LINK_STATUS, &success); if (!success) { printf("Vertex File Name: %s\n", vertexfile); printf("Vertex Code: %d\n", vertexcode); printf("Vertex Value: %d\n", vertex); glGetProgramInfoLog(vertex, 512, NULL, log); printf(strcat(strcpy(msg, "Vertex Shader Failed to Compile!\n> OpenGL Error: "), log)); } } const char* fragmentcode = loadshader(fragmentfile); unsigned int fragment = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment, 1, &fragmentcode, NULL); glCompileShader(fragment); { glGetProgramiv(fragment, GL_LINK_STATUS, &success); if (!success) { printf("\n\nFragment File Name: %s\n", fragmentfile); printf("Fragment Code: %d\n", fragmentcode); printf("Fragment Value: %d\n", fragment); glGetProgramInfoLog(fragment, 512, NULL, log); printf(strcat(strcpy(msg, "Fragment Shader Failed to Link!\n> OpenGL Error"), log)); } } unsigned int data = glCreateProgram(); glAttachShader(data, vertex); glAttachShader(data, fragment); glLinkProgram(data); { glGetProgramiv(data, GL_LINK_STATUS, &success); if (!success) { printf("Program Value: %d\n", data); glGetProgramInfoLog(data, 512, NULL, log); printf(strcat(strcpy(msg, "\n\nShader Program Failed to Link!\n> OpenGL Error: "), log)); } } glDeleteShader(vertex); glDeleteShader(fragment); free((void*) vertexcode); free((void*) fragmentcode); Shader* shader = (Shader*) malloc(sizeof(Shader)); shader -> data = data; return shader; } int main(int argc, char const *argv[]) { glewInit(); Shader* shader = createShader("data/test.vs", "data/test.fs"); printf("%s\n", shader ? "Shader Successfully Created!" : "Failed to Create Shader!"); return 0; } char* loadshader(char* filename) { FILE* file = fopen(filename, "r"); if (!file) return NULL; int flag = 0; char* shader = (char*) malloc(MAXSIZE); char* buffer = (char*) malloc(BUFFSIZE); while (fgets(buffer, BUFFSIZE, file) != NULL) strcat(shader, buffer); free(buffer); fclose(file); return shader; } 

The output of the print statements are as follows:

Vertex File Name: data/test.vs Vertex Code: 81465408 Vertex Value: 1 Vertex Shader Failed to Compile! > OpenGL Error: ░☻ Fragment File Name: data/test.fs Fragment Code: 82587712 Fragment Value: 2 Fragment Shader Failed to Compile! > OpenGL Error: ░☻ [process exited with code 4294967295 (0xffffffff)]