gpt4 book ai didi

c++ - 从文件加载后,顶点着色器和片段着色器均未编译

转载 作者:行者123 更新时间:2023-11-28 00:07:18 24 4
gpt4 key购买 nike

<分区>

我是 OpenGL 的新手,我试图编写一个 Shader 类,从文件加载着色器并编译它们。问题是,它们都没有编译。两者都显示如下错误消息:

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

我在这里搜索了很多相同问题的问题,但没有一个有效。代码如下:

着色器.h

#ifndef SHADER_H
#define SHADER_H

#include <GL/glew.h>

#include <string>
#include <fstream>
#include <sstream>
#include <iostream>

class Shader {
public:
Shader(const GLchar* path);
GLint Compile(GLenum type);
GLuint GetID() const;
const char* GetShaderCode() const;
void InfoLog() const;
private:
GLuint shaderId;
const char* shaderCode;
int shaderCodeLength;
char log[512];

};

#endif

Shader.cpp(问题所在的函数)

#include "Shader.h"

Shader::Shader(const GLchar* path) {
std::string shaderString;
std::ifstream shaderFile;
std::stringstream shaderStream;

shaderFile.exceptions(std::ifstream::badbit);

try {
shaderFile.open(path);
shaderStream << shaderFile.rdbuf();
shaderFile.close();
shaderString = shaderStream.str();

} catch (std::ifstream::failure e) {
std::cout << "Error while reading the file. (Does the file exist?)" << std::endl;

}


shaderCode = const_cast<const GLchar*>(shaderString.c_str());
shaderCodeLength = shaderString.length();
}

GLint Shader::Compile(GLenum type) {
//Compilates the shader and returns GL_TRUE if succesful or GL_FALSE otherwise.
GLint status;
shaderId = glCreateShader(type);
glShaderSource(shaderId, 1, &shaderCode, &shaderCodeLength);
glCompileShader(shaderId);
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status);
glGetShaderInfoLog(shaderId, 512, NULL, log);

return status;
}

shader.vert

#version 130

in vec2 position;
in vec3 color;
in vec2 texcoord;
out vec3 Color;
out vec2 Texcoord;
uniform mat4 trans;

void main() {
Color = color;
Texcoord = texcoord;
gl_Position = trans * vec4(position, 0.0, 1.0);
}

shader.frag

#version 130

in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D texKitten;
uniform sampler2D texPuppy;

void main() {
outColor = mix(texture(texKitten, Texcoord), texture(texPuppy, Texcoord), 0.5);

};

很多人说问题是着色器源代码有点像这样:#version 130in vec3 Color;...(但是当我在主代码上打印它时,它打印就像它在文件上一样)并且许多人说这是从文件加载着色器的正确方法。那么,这样做有什么问题呢?有没有更好的办法?

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