GL_LINES的OpenGL片段着色器仅在着色器编译失败时呈现行

时间:2017-01-10 20:32:26

标签: c++ opengl shader fragment-shader

我已经找了两天问题的答案。也许我在寻找时非常糟糕,但似乎并没有找到答案。

我正在尝试使用OpenGL的glDrawArrays(GL_LINES,...,...)在屏幕上绘制线条。这是我的片段着色器的当前代码:

#version 330 core

in vec4 vertexColor;

out vec4 color;

void main( )
{
    //color = vec3(0.5f, 1.0f, 1.0f);
    color = vec4(0.5f, 1.0f, 1.0f, 0.5f);
    //color = vertexColor;
}

最初color = vertexColor线应该可以工作,但屏幕上没有任何内容。我想也许从顶点着色器发送的数据有问题所以我尝试在片段着色器中手动设置输出。我不小心把它变成了vec3而不是vec4,我的控制台窗口出现错误,告诉我着色器的编译失败了。但线条出现了!它们在正确的位置显示为白色/灰色静电线。当我纠正错误并将输出设置为vec4时,如图所示,这些行再次不可见,我没有编译错误。

我做错了什么让我的线条看不见?

编辑: 这是我第一次参加最小,完整,可验证的例子"。如果将项目与这些文件链接到GLEW,GLFW和GLM,则应在您的系统上运行。如果我不想上传整个节目,我道歉。这似乎有点大,但我不知道它可能是什么"完成"。

当片段着色器无法编译时,这个仅产生一条纯黑线,而不是制作两条白色静态线。编译成功时,它仍然没有任何内容。

Main.cpp的

#define GLEW_STATIC
#include <GL/glew.h>

#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include "ShaderMaker.h"
#include "TargetBox.h"


const GLint WIDTH = 800, HEIGHT = 600;

//Camera 
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
GLfloat yaw = -90.0f;
GLfloat pitch = 0.0f;

// Delta time
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;

int main() {

    cameraFront.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
    cameraFront.y = sin(glm::radians(pitch));
    cameraFront.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
    cameraFront = glm::normalize(cameraFront);

    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

    GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Debugger", nullptr, nullptr);

    int screenWidth, screenHeight;
    glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

    if (window == nullptr) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();

        return EXIT_FAILURE;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;

    if (GLEW_OK != glewInit()) {
        std::cout << "Failed to initialize GLEW" << std::endl;

        return EXIT_FAILURE;
    }

    glViewport(0, 0, screenWidth, screenHeight);

    glEnable(GL_DEPTH_TEST);



    TargetBox tb;

    GLuint modelLoc = tb.getUniform("model");
    GLuint viewLoc = tb.getUniform("view");
    GLuint projectionLoc = tb.getUniform("projection");



    glm::mat4 projection;
    projection = glm::perspective(glm::radians(45.0f), (float)screenWidth / screenHeight, 0.1f, 100.0f);


    while (!glfwWindowShouldClose(window)) {
        GLfloat currentFrame = glfwGetTime();
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;

        glfwPollEvents();

        glClearColor(0.5f, 0.7f, 0.9f, 1.0f); // Unnecessary, but adds some color to an otherwise blank window
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glm::mat4 view;
        view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);

        tb.useShader();

        glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));

        glBindVertexArray(tb.getVAO());
        glm::vec3 posMod = tb.getPos();
        glm::mat4 model;
        model = glm::translate(model, glm::vec3(posMod.x, posMod.y, posMod.z));
        glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

        glDrawArrays(GL_LINES, 0, 4);



        glBindVertexArray(0);

        glfwSwapBuffers(window);
    }

    glfwTerminate();

    return EXIT_SUCCESS;
}

ShaderMaker.h

#pragma once

#ifndef SHADERMAKER_H
#define SHADERMAKER_H

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

#include<GL/glew.h>

class ShaderMaker {
public:
    // The program ID
    GLuint Program;
    // Constructor reads and builds the shader
    ShaderMaker(const GLchar* vertexPath, const GLchar* fragmentPath) {
        // 1. Retrieve the vertex/fragment source code from filePath
        std::string vertexCode;
        std::string fragmentCode;
        std::ifstream vShaderFile;
        std::ifstream fShaderFile;
        // ensures ifstream objects can throw exceptions:
        vShaderFile.exceptions(std::ifstream::badbit);
        fShaderFile.exceptions(std::ifstream::badbit);
        try {
            // Open files
            vShaderFile.open(vertexPath);
            fShaderFile.open(fragmentPath);
            std::stringstream vShaderStream, fShaderStream;
            // Read file's buffer contents into streams
            vShaderStream << vShaderFile.rdbuf();
            fShaderStream << fShaderFile.rdbuf();
            // close file handlers
            vShaderFile.close();
            fShaderFile.close();
            // Convert stream into GLchar array
            vertexCode = vShaderStream.str();
            fragmentCode = fShaderStream.str();
        }
        catch (std::ifstream::failure e) {
            std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
        }
        const GLchar* vShaderCode = vertexCode.c_str();
        const GLchar* fShaderCode = fragmentCode.c_str();

        // 2. Compile shaders
        GLuint fragment;
        GLint success;
        GLchar infoLog[512];

        // Vertex shader
        GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertex, 1, &vShaderCode, NULL);
        glCompileShader(vertex);
        // Print compile errors if any
        glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
        if (!success) {
            glGetShaderInfoLog(vertex, 512, NULL, infoLog);
            std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
        }

        // Similar for fragment shader
        fragment = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragment, 1, &fShaderCode, NULL);
        glCompileShader(fragment);
        // Print compile errors if any
        glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
        if (!success) {
            glGetShaderInfoLog(fragment, 512, NULL, infoLog);
            std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
        }

        // Shader program
        this->Program = glCreateProgram();
        glAttachShader(this->Program, vertex);
        glAttachShader(this->Program, fragment);
        glLinkProgram(this->Program);
        // Print linking errors if any
        glGetShaderiv(this->Program, GL_LINK_STATUS, &success);
        if (!success) {
            glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
            std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
        }

        // Delete the shaders as they're linked into our program now and no longer necessary
        glDeleteShader(vertex);
        glDeleteShader(fragment);
    }
    // Use the program
    void Use() { glUseProgram(this->Program); }
    // Get a uniform
    GLint getUniform(const GLchar * name) {
        return glGetUniformLocation(this->Program, name);
    }
};

#endif

TargetBox.h

#pragma once

#ifndef TARGETBOX_H
#define TARGETBOX_H

#define GLEW_STATIC
#include <GL/glew.h>

#include<glm/glm.hpp>
#include <GLFW/glfw3.h>

#include "ShaderMaker.h"


class TargetBox
{
public:
    TargetBox();
    ~TargetBox();

    void useShader();

    GLuint getVAO();

    glm::vec3 getPos();
    void setPos(glm::vec3 p);

    GLuint getUniform(GLchar * u);
private:
    glm::vec3 pos;

    GLuint VAO, VBO;
    const GLchar *vertexShaderPath = ".\\VShaderLine.txt";
    const GLchar *fragmentShaderPath = ".\\FShaderLine.txt";

    ShaderMaker shader = ShaderMaker(vertexShaderPath, fragmentShaderPath);
};

#endif

TargetBox.cpp

#include "TargetBox.h"



TargetBox::TargetBox()
{
    glGenBuffers(1, &VBO);
    glGenVertexArrays(1, &VAO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    GLfloat vertices[] = {
        -2, 0, -5,  1.0f, 1.0f, 1.0f,
        2, 0, 5,    1.0f, 1.0f, 1.0f,
        2, 0, 5,    1.0f, 0.0f, 0.0f,
        2, 10, 5,   0.0f, 1.0f, 0.0f
    };

    size_t data_len = sizeof(vertices);
    glBufferData(GL_ARRAY_BUFFER, data_len, vertices, GL_STATIC_DRAW);

    // Position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid *)0);
    glEnableVertexAttribArray(0);
    //Color attribute
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(1);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);
}


TargetBox::~TargetBox()
{
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
}


void TargetBox::useShader() { shader.Use(); }

GLuint TargetBox::getVAO() { return VAO; }

glm::vec3 TargetBox::getPos() { return pos; }
void TargetBox::setPos(glm::vec3 p) { pos = p; }

GLuint TargetBox::getUniform(GLchar * u) { return shader.getUniform(u); }

VShaderLine.txt

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;

out vec4 vertexColor;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main( )
{
    // The order of multiplication is important?
    gl_Position = projection * view * model * vec4( position, 1.0 );
    vertexColor = vec4(color.xyz, 1.0f);
}

FShaderLine.txt

#version 330 core

in vec4 vertexColor;

out vec4 color;

void main( )
{
    color = vec3(0.5f, 1.0f, 1.0f);
    //color = vec4(0.5f, 1.0f, 1.0f, 0.5f);
    //color = vertexColor;
}

0 个答案:

没有答案