glsl UNEXPECTED NEW_IDENTIFIER,期待$ end

时间:2017-05-19 19:20:56

标签: opengl glsl shader opengl-4

我在编译片段着色器时正在尝试这个令人沮丧的错误。如果我尝试按原样编译着色器:

#version 450 core
in vec2 tex_coord;
in flat int vertex_id;
out vec4 color;
layout (binding = 20) uniform sampler2D buildingTex;
layout (binding = 21) uniform sampler2D groundTex;
const int cube_vertices = 36;
const int ground_vertices = 4;
void main(void)
{

  if(vertex_id < cube_vertices)
    {
      float scaleF = .5f;
      vec2 scale = vec2(scaleF,scaleF);
      color = texture(buildingTex,tex_coord*scale);
    }
  if(vertex_id >= cube_vertices)
    {
      color = texture(groundTex,tex_coord);
    }


}
该公司抱怨:glsl意外的NEW_IDENTIFIER,期待$ end。

但是,如果我向我的片段着色器添加另一个片段着色器,我在另一个文件中,然后我对其所有行进行注释,如下所示:

// #version 450 core

// in vec2 tex_coord;
// in flat int vertex_id;
// out vec4 color;

// layout (binding = 20) uniform sampler2D buildingTex;
// layout (binding = 21) uniform sampler2D groundTex;

// int cube_vertices;
// int ground_vertices;


// void main(void)
// {

//   if(vertex_id < cube_vertices)
//     {
//       float scaleF = .5f;
//       vec2 scale = vec2(scaleF,scaleF);
//       color = texture(buildingTex,tex_coord*scale);
//       //color = vec4(1.0,1.0,1.0,1.0);
//     }
//   if(vertex_id >= cube_vertices)
//     {
//       color = texture(groundTex,tex_coord);
//       //color = vec4(1.0,0.0,0.0,1.0);
//     }




#version 450 core
in vec2 tex_coord;
in flat int vertex_id;
out vec4 color;
layout (binding = 20) uniform sampler2D buildingTex;
layout (binding = 21) uniform sampler2D groundTex;
const int cube_vertices = 36;
const int ground_vertices = 4;
void main(void)
{

  if(vertex_id < cube_vertices)
    {
      float scaleF = .5f;
      vec2 scale = vec2(scaleF,scaleF);
      color = texture(buildingTex,tex_coord*scale);
    }
  if(vertex_id >= cube_vertices)
    {
      color = texture(groundTex,tex_coord);
    }


}

在这种情况下,着色器被编译! 我试图删除旧代码的注释行,似乎我可以删除其中的一些,而其他我无法触摸,以编译着色器。

此时着色器是:

// #version 450 core

// in vec2 tex_coord;
// in flat int vertex_id;
// out vec4 color;

// layout (binding = 20) uniform sampler2D buildingTex;
// layout (binding = 21) uniform sampler2D groundTex;

// int cube_vertices;
//   if(vertex_id < cube_vertices)


#version 450 core
in vec2 tex_coord;
in flat int vertex_id;
out vec4 color;
layout (binding = 20) uniform sampler2D buildingTex;
layout (binding = 21) uniform sampler2D groundTex;
const int cube_vertices = 36;
const int ground_vertices = 4;
void main(void)
{

  if(vertex_id < cube_vertices)
    {
      float scaleF = .5f;
      vec2 scale = vec2(scaleF,scaleF);
      color = texture(buildingTex,tex_coord*scale);
    }
  if(vertex_id >= cube_vertices)
    {
      color = texture(groundTex,tex_coord);
    }


}

似乎我无法从评论代码中删除。

因为这似乎是我一生中遇到的最不合逻辑的问题,而且由于其他问题都有同样的错误警告,但是以一种与我的情况不符的方式解决了,我正在开启一个新的任务。 / p>

ps:我试图制作一个全新的着色器,但它没有用。

pps:我改变了emacs的主模式(通过Mx而不是修改emacs的init文件!)以便使用我在cpp文件中使用的普通快捷方式,在着色器文件中出现问题(。 vert,.frag ...),但是即使恢复默认模式并重新启动emacs,着色器也无法编译!

编辑:这是我用来加载着色器的方法

 const char* Shader::loadShader(std::string shaderPath)
  {
    std::ifstream temp_ss(shaderPath,std::ifstream::in);
    if(temp_ss)
      {
     char c;
    char *ss = new char[shader_size];

    unsigned i = 0 ;
    while(temp_ss.get(c))
      {

      ss[i++] = c;
      }

    std::cout<<std::ends;
   //ends adds a null, and then flushes the buffer
   //if i don't use it , often, the window does not show anything
   //I could use std::cout<<std::endl; in place of ends, 
   //but ends seem a more polite solution to me



//CHECK SHADER
    for(int i = 0; i < shader_size; i++)
      if(ss[i] == '\0')
        std::cout<<"shader.cpp::ERROR: NULL CHARACTER FOUND ON SHADER "<<
          shaderPath<<std::endl;

    return ss;

      }
    else
      std::clog<<"shader.cpp::loadShader() : ERROR ::: no shaders found at the path : "<<shaderPath<<std::endl;



  }

1 个答案:

答案 0 :(得分:1)

来自OP:

TO SUMMARIZE:着色器文件必须为空终止,我修改了将着色器源加载到缓冲区的函数,如下所示:

const char* Shader::loadShader(std::string shaderPath)
  {
    std::ifstream temp_ss(shaderPath,std::ifstream::in);
    if(temp_ss)
      {
     char c;
    char *ss = new char[shader_size];

    unsigned i = 0 ;
    while(temp_ss.get(c))
      {

      ss[i++] = c;
      }

    //CHECK SHADER
        bool nullTerminated = false;
    for(int i = 0; i < shader_size; i++)
      if(ss[i] == '\0')
        nullTerminated = true;

    if(!nullTerminated)
      ss[shader_size++] = '\0';

    return ss;

      }
    else
      std::clog<<"shader.cpp::loadShader() : ERROR ::: no shaders found at the path : "<<shaderPath<<std::endl;



  }

并且片段着色器编译!