gpt4 book ai didi

c++ - GLSL 着色器不工作。没有编译错误,并且使用了 glUseProgram

转载 作者:行者123 更新时间:2023-11-30 02:23:40 24 4
gpt4 key购买 nike

我想出了渲染矩形的代码,但着色器不起作用。它仍然是空白的白色。

这里我将包含重要的代码

主要内容:

float verts[] = {
-.5f, -.5f, .0f,
-.5f, .5f, .0f,
.5f, .5f, .0f,
.5f, .5f, .0f,
.5f, -.5f, .0f,
-.5f, -.5f, .0f
};

Shader shader("basicVert.glsl", "basicFrag.glsl");

GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

shader.enable();

Shader.cpp(类函数)

Shader::Shader(const string vpath, const string fpath) {
Shader();
current_vpath = vpath;
current_fpath = fpath;
shaderID = init();
}

Shader::Shader(const char *vpath, const char *fpath) {
Shader(string(vpath), string(fpath));
}

Shader::~Shader() {
shaderID = NULL;
glDeleteProgram(shaderID);
}

void Shader::enable() {
glUseProgram(shaderID);
}

GLuint Shader::makeVertextShader(const char* source) {
GLuint vertShaderID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShaderID, 1, &source, NULL);
glCompileShader(vertShaderID);
GLint r;
glGetShaderiv(vertShaderID, GL_COMPILE_STATUS, &r);
if (r == GL_FALSE) {
GLint l;
glGetShaderiv(vertShaderID, GL_INFO_LOG_LENGTH, &l);
cout << l << endl;
char *bfer = new char[l];
glGetShaderInfoLog(vertShaderID, l, &l, bfer);
cerr << "Failed to compile VERTEXT SHADER! FILE NAME: " <<
current_vpath << endl;
cerr << bfer << endl;
glDeleteShader(vertShaderID);
delete[] bfer;
return NULL;
}
return vertShaderID;
}

GLuint Shader::makeFragmentShader(const char* source) {
GLuint fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShaderID, 1, &source, NULL);
glCompileShader(fragShaderID);
GLint r;
glGetShaderiv(fragShaderID, GL_COMPILE_STATUS, &r);
if (r == GL_FALSE) {
GLint l;
glGetShaderiv(fragShaderID, GL_INFO_LOG_LENGTH, &l);
char *bfer = new char[l];
glGetShaderInfoLog(fragShaderID, l, &l, bfer);
cerr << "Failed to compile FRAGMENT SHADER! FILE NAME: " <<
current_fpath << endl;
cerr << bfer << endl;
glDeleteShader(fragShaderID);
delete[] bfer;
return NULL;
}
return fragShaderID;
}

GLuint Shader::init() {
GLuint program = glCreateProgram();
const string vs = readFile(current_vpath);
const string vf = readFile(current_fpath);

const char *vertexsrc = vs.c_str();
const char *fragmentsrc = vf.c_str();

GLuint vertShaderID = this->makeVertextShader(vertexsrc);
GLuint fragShaderID = this->makeFragmentShader(fragmentsrc);

glAttachShader(program, vertShaderID);
glAttachShader(program, fragShaderID);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vertShaderID);
glDeleteShader(fragShaderID);
return program;
}

GLSL 顶点着色器

#version 330 core

layout(location = 0) in vec3 position;

void main(){

gl_Position = position;

}

GLSL 片段着色器

#version 330 core

layout(location = 0) out vec4 color;

void main(){

color = vec4(1.0, 0.0, 1.0, 1.0);
gl_FragColor = color;

}

最佳答案

gl_FragColor 现代版本的 GLSL 不再支持

所以它将在顶点着色器中,

layout(location = 0) in vec4 position;

void main()
{
gl_Position = position;
}

在全系统中,

layout(location = 0) out vec4 color;

void main()
{
color = vec4(1.0, 0.0, 1.0, 1.0);
//gl_FragColor is no longer supported in modern versions of GLSL
}

关于c++ - GLSL 着色器不工作。没有编译错误,并且使用了 glUseProgram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45851180/

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