我试图在我的OpenGL项目中使用自己的顶点着色器和片段着色器。 程序本身和着色器程序都已成功编译和链接,但顶点着色器和片段着色器都不起作用。我设法在着色器代码中进行了一些更改但没有发生任何事情,并且两个着色器仍然拒绝参与渲染过程。 以下是我的代码:
#include<GL\glew.h>
#include<GL\freeglut.h>
#include<iostream>
#include<fstream>
#include<string>
#include<assert.h>
using namespace std;
static GLuint VBO_handle = 0;
static GLfloat val = 1.5f;
static GLuint Unif_handle = 0;
static GLuint Program_handle = 0;
static GLuint Shader_handle = 0;
static GLuint Shader_handle2 = 0;
void init()
{
glGenBuffers(1, &VBO_handle);
glBindBuffer(GL_ARRAY_BUFFER, VBO_handle);
GLfloat vertices[] = { 0.0f, 0.8f, 0.0f, -0.8f, -0.8f, 0.0f, 0.8f, -0.8f, 0.0f };
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
void Rendering();
void callback_register()
{
glutDisplayFunc(Rendering);
glutIdleFunc(Rendering);
}
void Rendering()
{
glClear(GL_COLOR_BUFFER_BIT);
glUniform1f(Unif_handle, val);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glutSwapBuffers();
}
void ShaderReading()
{
Program_handle = glCreateProgram();
Shader_handle = glCreateShader(GL_VERTEX_SHADER);
std::fstream fin("vertex.vert", std::ios::in);
std::string temp;
while (fin.good())
{
string templine;
getline(fin, templine);
temp += templine;
temp.push_back('\n');
}
fin.close();
const GLchar* p[1];
p[0] = temp.c_str();
GLint Lengths[1];
Lengths[0] = temp.size();
glShaderSource(Shader_handle, 1, p, Lengths);
glCompileShader(Shader_handle);
GLint success;
glGetShaderiv(Shader_handle, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar InfoLog[1024];
glGetShaderInfoLog(Shader_handle, 1024, NULL, InfoLog);
fprintf(stderr, "Error compiling shader type %d: '%s'\n", GL_VERTEX_SHADER, InfoLog);
exit(1);
}
glAttachShader(Program_handle, Shader_handle);
GLint Success = 0;
GLchar ErrorLog[1024] = { 0 };
Shader_handle2 = glCreateShader(GL_FRAGMENT_SHADER);
fin.open("fragment.frag", std::ios::in);
temp.clear();
while (fin.good())
{
string templine;
getline(fin, templine);
temp += templine;
temp.push_back('\n');
}
fin.close();
p[0] = temp.c_str();
Lengths[0] = temp.size();
glShaderSource(Shader_handle2, 1, p, Lengths);
glCompileShader(Shader_handle2);
glGetShaderiv(Shader_handle2, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar InfoLog[1024];
glGetShaderInfoLog(Shader_handle2, 1024, NULL, InfoLog);
fprintf(stderr, "Error compiling shader type %d: '%s'\n", GL_VERTEX_SHADER, InfoLog);
exit(1);
}
glAttachShader(Program_handle, Shader_handle2);
Success = 0;
glLinkProgram(Program_handle);
glGetProgramiv(Program_handle, GL_LINK_STATUS, &Success);
if (Success == 0) {
glGetProgramInfoLog(Program_handle, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
exit(1);
}
glValidateProgram(Program_handle);
glGetProgramiv(Program_handle, GL_VALIDATE_STATUS, &Success);
if (!Success) {
glGetProgramInfoLog(Program_handle, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog);
exit(1);
}
glUseProgram(Shader_handle);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("6666");
callback_register();
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
init();
ShaderReading();
glutMainLoop();
return 0;
}
以下是着色器程序,非常简单:( 顶点着色器:
#version 330
layout (location = 0) in vec3 Position;
uniform float gScale;
void main()
{
gl_Position = vec4(gScale*Position.x, Position.y, Position.z, 1.0);
}
片段着色器: #version 330
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
答案 0 :(得分:2)
这里有一些小错误:
glUseProgram(Shader_handle);
实际上应该是 Program_handle
。上面的代码应该只产生一些GL错误并且没有效果。由于您处于兼容性/遗留配置文件中,因此将使用固定功能管道进行绘制,属性0将为顶点位置添加别名。默认颜色为白色,这就是您看到白色三角形的原因。
正如我在评论中已经提到的那样,你也永远不会查询你的制服的位置,并假设它是零。由于您只使用一件制服,这有点可能 - 但是,GL规格根本无法保证。你也应该加上它。