简体   繁体   中英

OpenGL Shader failed to work

I tried to use my own vertex shader and fragment shader in my OpenGL project. Both the program itself and the shader programs were compiled and linked successfully but neither the vertex shader nor the fragment shader worked. I managed to make some changes in the shader code but nothing happened, and the two shaders still refused to take part in the rendering process. Below's my code:

#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;
}

Here's the shader programs below, very simple indeed:( vertex shader:

#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);
}

fragment shader: #version 330

out vec4 FragColor;

void main()
{
    FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

You have some small mistake here:

glUseProgram(Shader_handle);

This actually should be Program_handle . The above code should just produce some GL error and have no effect. Since you are in a compatibility/legacy profile, you will draw with the fixed function pipeline, and attribute 0 will alias the vertex positions. The default color will be white, so that is why you see a white triangle.

As I already noted in the comments, you also never query the location for your uniform and assume it is zero. Since you are using only one uniform, this is somewhat likely - however, it is not guaranteed by the GL spec at all. You also should really add that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM