gpt4 book ai didi

c++ - OpenGL 着色器加载失败

转载 作者:太空狗 更新时间:2023-10-29 20:29:51 24 4
gpt4 key购买 nike

我的着色器加载代码有问题。让我感到困惑的奇怪事情是它可能每 5 次起作用一次,但随后只能起作用。例如,它会加载碎片着色器,但随后纹理将无法正常工作(它会在几何体上绘制奇怪的纹理外观)。我认为问题出在加载代码上,所以这就是我的问题所在。谁能发现我在下面的代码中没有发现的错误?

char* vs, * fs;

vertexShaderHandle = glCreateShader(GL_VERTEX_SHADER);
fragmentShaderHandle = glCreateShader(GL_FRAGMENT_SHADER);

long sizeOfVShaderFile = getSizeOfFile(VERTEX_SHADER_FILE_NAME);
long sizeOfFShaderFile = getSizeOfFile(FRAGMENT_SHADER_FILE_NAME);

if(sizeOfVShaderFile == -1)
{
cerr << VERTEX_SHADER_FILE_NAME<<" is null! Exiting..." << endl;
return;
}
if(sizeOfFShaderFile == -1)
{
cerr << FRAGMENT_SHADER_FILE_NAME<<" is null! Exiting..." << endl;
return;
}

vs = readFile(VERTEX_SHADER_FILE_NAME);
fs = readFile(FRAGMENT_SHADER_FILE_NAME);

const char* vv = vs, *ff = fs;

glShaderSource(vertexShaderHandle , 1, &vv, NULL);

cout << "DEBUGGING SHADERS" << endl;
cout << "VERTEX SHADER: ";
printShaderInfoLog(vertexShaderHandle);
cout << endl;

glShaderSource(fragmentShaderHandle, 1, &ff, NULL);

cout << "FRAGMENT SHADER: ";
printShaderInfoLog(fragmentShaderHandle);
cout << endl;


glCompileShader(vertexShaderHandle);

cout << "VERTEX SHADER: ";
printShaderInfoLog(vertexShaderHandle);
cout << endl;

glCompileShader(fragmentShaderHandle);

cout << "FRAGMENT SHADER: ";
printShaderInfoLog(fragmentShaderHandle);
cout << endl;


programHandle = glCreateProgram();

cout << "DEBUGGING PROGRAM" << endl;

glAttachShader(programHandle, vertexShaderHandle);

printProgramInfoLog(programHandle);

glAttachShader(programHandle, fragmentShaderHandle);

printProgramInfoLog(programHandle);


glLinkProgram(programHandle);

printProgramInfoLog(programHandle);

glUseProgram(programHandle);

printProgramInfoLog(programHandle);


delete[] vs; delete[] fs;

这是 readFile 函数:

char* readFile(const char* path)
{
unsigned int fileSize = getSizeOfFile(path);

char* file_data = new char[fileSize];

ifstream input_stream;

input_stream.open(path, ios::binary);

input_stream.read(file_data, fileSize);

input_stream.close();
//this is deleted at the end of the shader code
return file_data;
}

以下所有消息均来自完全相同的可执行文件(未重建)。

这是第一个可能的错误消息:

BallGLWidget::initializeGL called
DEBUGGING SHADERS
VERTEX SHADER:
FRAGMENT SHADER:
VERTEX SHADER: ERROR: 0:17: '<' : syntax error syntax error


FRAGMENT SHADER:
DEBUGGING PROGRAM
ERROR: One or more attached shaders not successfully compiled

ERROR: One or more attached shaders not successfully compiled

glGetError enum value: GL_NO_ERROR

另一个可能的错误信息:

BallGLWidget::initializeGL called
DEBUGGING SHADERS
VERTEX SHADER:
FRAGMENT SHADER:
VERTEX SHADER: ERROR: 0:17: 'tt' : syntax error syntax error


FRAGMENT SHADER: ERROR: 0:33: '?' : syntax error syntax error


DEBUGGING PROGRAM
ERROR: One or more attached shaders not successfully compiled

ERROR: One or more attached shaders not successfully compiled

这是它运行时的输出(可能是 5 或 6 次中的 1 次)

BallGLWidget::initializeGL called
DEBUGGING SHADERS
VERTEX SHADER:
FRAGMENT SHADER:
VERTEX SHADER:
FRAGMENT SHADER:
DEBUGGING PROGRAM
Image format is GL_RGB
Checking textures...
glGetError enum value: GL_NO_ERROR

我严重怀疑它是着色器本身,因为它们有时会工作......而且报告的错误是垃圾。

如果有任何更多信息有帮助,我很乐意提供。

编辑:这是着色器

顶点着色器:

attribute vec2 a_v_position;
attribute vec2 a_tex_position;

varying vec2 tex_coord_output;


void main()
{
tex_coord_output = a_tex_position;

gl_Position = vec4(a_v_position, 0.0, 1.0);
}

片段着色器:

varying vec2 tex_coord_output;

uniform sampler2D ballsampler;


void main()
{
gl_FragColor = texture2D(ballsampler, tex_coord_output);
}

最佳答案

您的问题与 Getting garbage chars when reading GLSL files 重复这是我的 answer对它:


您使用的是 C++,所以我建议您利用它。我建议您不要读入自分配的 char 数组,而是读入 std::string:

#include <string>
#include <fstream>

std::string loadFileToString(char const * const fname)
{
std::ifstream ifile(fname);
std::string filetext;

while( ifile.good() ) {
std::string line;
std::getline(ifile, line);
filetext.append(line + "\n");
}

return filetext;
}

它会自动处理所有内存分配和适当的定界——关键字是 RAII:资源分配即初始化。稍后您可以上传着色器源代码,例如

void glcppShaderSource(GLuint shader, std::string const &shader_string)
{
GLchar const *shader_source = shader_string.c_str();
GLint const shader_length = shader_string.size();

glShaderSource(shader, 1, &shader_source, &shader_length);
}

void load_shader(GLuint shaderobject, char * const shadersourcefilename)
{
glcppShaderSource(shaderobject, loadFileToString(shadersourcefilename));
}

关于c++ - OpenGL 着色器加载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8768695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com