读取GLSL文件时获取垃圾字符 [英] Getting garbage chars when reading GLSL files

查看:606
本文介绍了读取GLSL文件时获取垃圾字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题。从文件加载我的着色器。着色器程序,当试图编译时,抛出这些错误的顶点和片段着色器:



顶点信息



0(12):错误C0000:语法错误,意外$未定义在令牌



片段信息



0(10):错误C0000:语法错误,未知的$ undefined在令牌



检查加载的文件内容我可以看到各种垃圾文本附加在开始和结束的着色器文件。喜欢这一个:



<$ p $在vec4位置中,p> #version 330

布局(位置= 0)
layout(location = 1)in vec4 color;

平滑出vec4 theColor;

void main()
{
gl_Position = position;
theColor = color;
}ýýýý««««««««þîþîþîþ

加载着色器的方法如下:

  void ShaderLoader :: loadShaders(char * vertexShaderFile,char * fragmentShaderFile){


vs = loadFile(vertexShaderFile,vlen);
fs = loadFile(fragmentShaderFile,flen);

}
char * ShaderLoader :: loadFile(char * fname,GLint& fSize){
ifstream :: pos_type size;
char * memblock;
string text;

//基于cplusplus.com中的示例读取文件
ifstream文件(fname,ios :: in | ios :: binary | ios :: ate);
if(file.is_open())
{
size = file.tellg()
fSize =(GLuint)size;
memblock = new char [size];
file.seekg(0,ios :: beg);
file.read(memblock,size);
file.close();
cout<< file< fname<< loaded<< endl;
text.assign(memblock)
}
else
{
cout<< 无法打开文件< fname<< endl;
exit(1);
}
return memblock;
}

我试图从UTF-8顶部ANSI改变编码,在视觉工作室外编辑,但问题仍然存在。任何帮助将非常感谢。

解决方案

必须做的是分配一个字节的内存,您可以在其中放置一个空('\0'):

 。 .. 
memblock = new char [1 + fSize];
file.seekg(0,ios :: beg);
file.read(memblock,size);
file.close();
memblock [size] ='\0';
...



edit



我改变了我的代码在数组中使用 fSize ,而不是 size ,因为它是一个GLint只是整数的typedef。此外,我尝试这种修复在我的机器,它的工作原理,只要我能告诉 - 没有垃圾在开始,没有在结束。


I run into the following problem.I load my shaders from files.The shader program ,when trying to compile, throws these errors for the vertex and fragment shaders:

Vertex info

0(12) : error C0000: syntax error, unexpected $undefined at token ""

Fragment info

0(10) : error C0000: syntax error, unexpected $undefined at token ""

When inspecting the loaded content of the files I can see all kinds of garbage text is attached at the beginnings and the ends of the shader files.Like this one:

#version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color;

smooth out vec4 theColor;

void main()
{
gl_Position = position;
theColor = color;
}ýýýý««««««««þîþîþîþ

The methods loading the shaders look as follows:

void ShaderLoader::loadShaders(char * vertexShaderFile,char *fragmentShaderFile){


vs = loadFile(vertexShaderFile,vlen);
    fs = loadFile(fragmentShaderFile,flen);

}
char *ShaderLoader::loadFile(char *fname,GLint &fSize){
ifstream::pos_type size;
char * memblock;
string text;

// file read based on example in cplusplus.com tutorial
ifstream file (fname, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
fSize = (GLuint) size;
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "file " << fname << " loaded" << endl;
text.assign(memblock);
}
else
{
cout << "Unable to open file " << fname << endl;
exit(1);
}
return memblock;
}

I tried to change the encoding from UTF-8 top ANSI ,also tried to edit outside the visual studio but the problem still persists .Any help on this will be greatly appreciated.

解决方案

It looks like all you have to do is allocate one more byte of memory in which you can place a null ('\0'):

...
memblock = new char[1 + fSize]; 
file.seekg (0, ios::beg);  
file.read (memblock, size);  
file.close();  
memblock[size] = '\0';
...

edit

I changed my code to use fSize in the array rather than size, since it is a GLint, which is just a typedef over an integer. Also, I tried this fix on my machine, and it works as far as I can tell - no junk at the beginning, and none at the end.

这篇关于读取GLSL文件时获取垃圾字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆