PDA

View Full Version : Shader Compilation Error



74oshua
12-08-2017, 12:57 PM
Hello everyone! This is my first post, so tell me if I'm doing anything wrong or if I need to provide more information.

I am pretty well-versed in C, and I have decided to learn OpenGL. However, I am following this tutorial: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ and I need some help. I'm trying to compile the shaders that were given in the tutorial, but I keep running into this error when I print the error logs for the shader compiler:


ERROR: 0:7: '' : illegal non-ASCII character (0xff)
ERROR: 0:7: '' : illegal non-ASCII character (0xff)
ERROR: 0:7: '' : illegal non-ASCII character (0xff)
ERROR: 0:7: '' : illegal non-ASCII character (0xff)
ERROR: 0:7: '' : illegal non-ASCII character (0xff)
ERROR: 0:7: '' : illegal non-ASCII character (0xff)


ERROR: 0:6: '' : illegal non-ASCII character (0xff)
ERROR: 0:6: '' : illegal non-ASCII character (0xff)
ERROR: 0:6: '' : illegal non-ASCII character (0xff)
ERROR: 0:6: '' : illegal non-ASCII character (0xff)
ERROR: 0:6: '' : illegal non-ASCII character (0xff)


Attached vertex shader is not compiled.

It appears that the compiler seems to think that there is a character with the value 0xFF at the end of the file, which there isn't.

Here is my code:

SOURCE FILES

main.c:


#include "window.h"
#include "shaders.h"
#include "prerequisites.h"

int error(char * message);

//static symbols
const float triangleData[] =
{
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
enum states
{
RUNNING, EXIT
} program;

int main(int argc, char **argv)
{
//initiate glfw
if (!glfwInit())
{
return error("Could not intitiate GLFW");
}

//create a window
GLFWwindow * window = 0;
window = JCreateWindow("Window", 1000, 720);
if (window == 0)
{
return error("Could not create window");
}

//initiate glew
if (glewInit() != GLEW_OK)
{
return error("Could not intitiate GLEW");
}

//generate and bind the vao
GLuint VAO = 100;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
if (VAO == -1)
{
return error("Could not create VAO");
}

//generate and bind the vbo
GLuint VBO = 100;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);

//add the triangle to the vbo
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleData), triangleData, GL_STATIC_DRAW);

//create and use a shader program
int shaderProgram = JLinkShaderProgram("../vertex.glsl", "../fragment.glsl");

program = RUNNING;

//main loop
while (program == RUNNING)
{
if (JRefresh(window))
{
program = EXIT;
}

//draw the triangle
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glUseProgram(shaderProgram);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
}

return 0;
}

int error(char * message)
{
fprintf(stderr, message);
return -1;
}

shaders.c:


#include "shaders.h"

int JLoadShader(char * src, char * shaderPath)
{
FILE * file = fopen(shaderPath, "r");

//get the file size
fseek(file, 0, SEEK_END);
int size = ftell(file);
rewind(file);

//read file into memory
fread(src, size, 1, file);

fclose(file);

return size;
}

int JCompileShader(GLchar * shaderPath, char shaderType)
{
GLuint shader = 0;
if (shaderType == 'v')
{
shader = glCreateShader(GL_VERTEX_SHADER);
}
else if (shaderType == 'f')
{
shader = glCreateShader(GL_FRAGMENT_SHADER);
}

char src[10000];
memset(&src, 0, sizeof(src));
JLoadShader(src, shaderPath);

const GLchar * srcPtr = (const GLchar *)src;

glShaderSource(shader, 1, &srcPtr , NULL);
glCompileShader(shader);

//error checking
GLint Result = GL_FALSE;
int InfoLogLength;
glGetShaderiv(shader, GL_COMPILE_STATUS, &Result);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &InfoLogLength);
if ( InfoLogLength > 0 )
{
char VertexShaderErrorMessage[InfoLogLength+1];
glGetShaderInfoLog(shader, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
printf("%s\n", &VertexShaderErrorMessage[0]);
}

return shader;
}

int JLinkShaderProgram(char * vertexShader, char * fragmentShader)
{
int program = glCreateProgram();
int vShader = 0;
int fShader = 0;

//compile the shaders and link the program
vShader = JCompileShader(vertexShader, 'v');
fShader = JCompileShader(fragmentShader, 'f');
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);

// Check the program
int InfoLogLength;
GLint Result = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &Result);
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &InfoLogLength);
if ( InfoLogLength > 0 ){
char ProgramErrorMessage[InfoLogLength+1];
glGetProgramInfoLog(program, InfoLogLength, NULL, &ProgramErrorMessage[0]);
printf("%s\n", &ProgramErrorMessage[0]);
}

return program;
}

window.c:


#include "window.h"

GLFWwindow * JCreateWindow(char * windowName, int width, int height)
{
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // We want OpenGL 4.4
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL

// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
window = glfwCreateWindow( width, height, windowName, NULL, NULL);
if( window == NULL ){
return 0;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental=1; // Needed in core profile

return window;
}

char JRefresh(GLFWwindow * window)
{
//swap buffers
glfwSwapBuffers(window);

//check if the window should close
glfwPollEvents();
if (glfwWindowShouldClose(window))
{
return 1;
}
else
{
return 0;
}
}

HEADER FILES

prerequisites.h:


#include <stdio.h>
#include <stdlib.h>

#define GLEW_STATIC

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <GL/freeglut.h>

shaders.h:


#include "prerequisites.h"
#include <string.h>

int JLoadShader(char * src, char * shaderPath);
int JCompileShader(GLchar * shaderPath, char shaderType);
int JLinkShaderProgram(char * vertexShader, char * fragmentShader);
unsigned long JGetFileSize(FILE * file);

window.h:


#include "prerequisites.h"

GLFWwindow * JCreateWindow(char * windowName, int width, int height);
char JRefresh(GLFWwindow * window);

I am using CodeLite 11.0.0 as my IDE

Alfonse Reinheart
12-08-2017, 04:22 PM
Your "shader.c" seems to be an exact copy of "main.c". Probably a copy-and-paste error.

74oshua
12-08-2017, 04:50 PM
OK, I figured it out! It turns out my function that loaded the shader into a char array didn't include formatted characters (\n, ect...). I rewrote my JLoadShader() function to properly format files. New function:


int JLoadShader(char * src, char * shaderPath)
{
FILE * file = fopen(shaderPath, "r");

//get the file size
fseek(file, 0, SEEK_END);
int size = ftell(file);
rewind(file);

//read file into memory
fread(src, size, 1, file);

fclose(file);

return size;
}