【发布时间】:2017-05-19 19:20:56
【问题描述】:
我在编译片段着色器时正在试验这个令人沮丧的错误。如果我尝试按原样编译着色器:
#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 UNEXPECTED 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);
}
}
看来我不能再从注释代码中删除了。
由于这似乎是我一生中遇到的最不合逻辑的问题,并且由于其他问题具有相同的错误警告,但以与我的情况不符的方式解决,我正在开启一个新任务。
ps:我尝试制作一个全新的着色器,但没有成功。
pps:为了使用我在 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;
}
【问题讨论】:
-
我们需要查看您用于将该文本发送到着色器的代码。有错误的可能性很大。
-
有时复制和粘贴带有一些不需要的字符,大部分是不可见的(它们代表什么)。但是编译器会检测到它们。
-
编译器通常会告诉你错误所在的行。
-
@NicolBolas 也许可以,但是它在顶点着色器上工作得很好,那么为什么要在片段着色器上出错呢?
-
@Ripi2 总是在文件的末尾,在最后一个大括号之后有很多行
标签: opengl glsl shader opengl-4