zombie_lad
zombie_lad

Reputation: 33

Get 0:1(1): error: syntax error, unexpected $end when trying to compile shader

I'm making a simple program to render a triangle. The program reads a vertex shader and a fragment shader from a file and compiles them. However one of the shaders - but never both of them - will not compile.

This is the source code of my main file:

//glew
#define GLEW_STATIC
#include <GL/glew.h>
//GLFW
#include <GLFW/glfw3.h>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>

//Read shaders from file
const GLchar* readfile(std::string filename){
  std::ifstream file (filename, std::ifstream::in);
  std::stringstream buffer;
  buffer << file.rdbuf();
  std::string dataString = buffer.str();
  const char *dataCString = dataString.c_str();
  const GLchar *data = const_cast<GLchar*>(dataCString);
  file.close();
  return data;
}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode){
  //when user presses the escape key WindowShouldClose is set to true closing the application
  if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){
    glfwSetWindowShouldClose(window, GL_TRUE);
  }
}

void render(){
  glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  glClear(GL_COLOR_BUFFER_BIT);
  glDrawArrays(GL_TRIANGLES, 0,3);
}

int main(){
  //initialize glfw with necessary information
  glfwInit();
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

  glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

  //create a window with glfw
  GLFWwindow* window = glfwCreateWindow(800, 600, "opengl window", nullptr, nullptr);
  glfwMakeContextCurrent(window);

  //set required key callback functions
  glfwSetKeyCallback(window, key_callback);

  //initialize glew
  glewExperimental = GL_TRUE;
  glewInit();

  //specify size of rendering window
  glViewport(0, 0, 800, 600);

  GLuint vao;
  glGenVertexArrays(1, &vao);
  glBindVertexArray(vao);

  float vertices[]={
    0.0f, 0.5f, //Vertex 1 (X, Y)
    0.5f, -0.5f, //Vertex 2 (X, Y)
    -0.5f, -0.5f //Vertex 3 (X, Y)
  };

  GLuint vbo;
  glGenBuffers(1, &vbo); //Generate 1 buffer
  glBindBuffer(GL_ARRAY_BUFFER, vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

  const GLchar* vertexSource = readfile("vertexShader.glsl");
  const GLchar* fragmentSource = readfile("fragmentShader.glsl");

  //Compile vertex shader 
  GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  glShaderSource(vertexShader, 1, &vertexSource, NULL);
  glCompileShader(vertexShader);

  //Check vertex shader compiled sucessfully
  GLint vertexStatus;
  glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexStatus);
  std::cout << "Vertex shader compiled: " << vertexStatus << std::endl;
  char buffer[1024];
  glGetShaderInfoLog(vertexShader, 1024, NULL, buffer);
  std::cout << buffer << std::endl;

  //Compile fragment shader
  GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
  glCompileShader(fragmentShader);

  //Check fragment shader compiled sucessfully
  GLint fragmentStatus;
  glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentStatus);
  std::cout << "Fragment shader compiled: " <<  fragmentStatus << std::endl;
  glGetShaderInfoLog(fragmentShader, 1024, NULL, buffer);
  std::cout << buffer << std::endl;

  //Compile shaders into program
  GLuint shaderProgram = glCreateProgram();
  glAttachShader(shaderProgram, vertexShader);
  glAttachShader(shaderProgram, fragmentShader);
  glBindFragDataLocation(shaderProgram, 0, "outColor");

  glLinkProgram(shaderProgram);
  glUseProgram(shaderProgram);

  GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
  glEnableVertexAttribArray(posAttrib);
  glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);

  //game loop
  while(!glfwWindowShouldClose(window)){
    //check and call events
    glfwPollEvents();

    //rendering goes here
    render();

    //swap the buffers
    glfwSwapBuffers(window);
  }

  glfwTerminate(); 
  return 0;
}

This is fragmentShader.glsl:

#version 150 core

out vec4 outColor;

void main()
{
  outColor = vec4(1.0, 1.0, 1.0, 1.0);
}

This is vertexShader.glsl

#version 150 core

in vec2 position;

void main()
{
  gl_Position = vec4(position, 0.0, 1.0);
}

I get this error when one of the shaders doesn't compile and I'm 99.99% that there's no garbage characters on my first line that could be causing this.

0:1(1): error: syntax error, unexpected $end

Upvotes: 2

Views: 7572

Answers (2)

bent
bent

Reputation: 1

I had a similar issue. I was reading in shaders from txt files before compiling them. Examples around the net have you storing shaders as a char*[]. This can cause both the error listed here, and "preprocessor error: Illegal non-directive after #" due to white space and null-termination shenanigans.

Changing &vertexSource to vertexSource will make the compiler correctly read it as one long string without null terminators.

Upvotes: 0

derhass
derhass

Reputation: 45362

Your dataString has only local lifetime in readfile(), so the pointer you are returning is invalid as soon as the function is left.

Upvotes: 6

Related Questions