无法在GLFW中使用OpenGL显示三角形

时间:2014-11-02 00:55:10

标签: c++ opengl shader glfw

我一直关注Nico Cvitak在YouTube上的Modern Opengl教程,以帮助我从SDL过渡到OpenGl / GLFW;这是我无法上班的课程的链接:[https://www.youtube.com/watch?v=2K_R4C1UP-c][1]

这是我的代码:

myVertexShader.vs

#version 150 core

in vec2 v_pos;

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

myFragmentShader.fs

#version 150 core

out vec4 fragData;

void main()
{
    fragData = vec4(1.0, 1.0, 1.0, 1.0);
}

的main.cpp

#define GLFW_INCLUDE_GLCOREARB

#include <iostream>
#include <GLFW/glfw3.h>
#include <OpenGL/OpenGL.h>
#include "ShaderUtils.h"

int main(int argc, const char * argv[])
{
    GLFWwindow * window;
    
    //Initialize the library
    if (!glfwInit())
        return -1;
    
    //Specify Hints
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    
    //Create a windowed mode window and its OpenGL context
    window = glfwCreateWindow(640, 480, "Mojicon", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return 1;
    }
    
    //Make the window's context current
    glfwMakeContextCurrent(window);
    std::cout << glGetString(GL_VERSION) << std::endl;
    
    glClearColor(0.0, 0.0, 0.0, 1.0);
    
    GLuint vertexShader = ShaderUtils::createShaderFromFile("myVertexShader.vs", GL_VERTEX_SHADER);
    GLuint fragmentShader = ShaderUtils::createShaderFromFile("myFragmentShader.fs", GL_FRAGMENT_SHADER);
    
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    
    glBindFragDataLocation(shaderProgram, 0, "fragData");
    
    glLinkProgram(shaderProgram);
    GLint linkStatus;
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkStatus);
    if (linkStatus != GL_TRUE)
    {
        cout << "Program Link Failed!" << endl;
        GLint infoLogLength;
        glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &infoLogLength);
        GLchar * infoLog = new GLchar[infoLogLength + 1];
        glGetShaderInfoLog(shaderProgram, infoLogLength + 1, NULL, infoLog);
        cout << infoLog << endl;
        delete infoLog;
        
        return 0;
    }
    
    glUseProgram(shaderProgram);
    
    //vertex buffer objects
    GLuint myVBO;
    glGenBuffers(1, &myVBO);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    
    GLfloat bufferData[] =
    {
        +0.0, +0.5,
        -0.5, -0.5,
        +0.5, -0.5,
    };
    
    glBufferData(GL_ARRAY_BUFFER, sizeof(bufferData), bufferData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, NULL);
    
    GLuint myVAO;
    glGenVertexArrays(1, &myVAO);
    glBindVertexArray(myVAO);
    
    GLint positionLoc = glGetAttribLocation(shaderProgram, "v_pos");
    
    glEnableVertexAttribArray(positionLoc);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    glVertexAttribPointer(positionLoc, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0);
    glBindBuffer(GL_ARRAY_BUFFER, NULL);
    glBindVertexArray(NULL);
    
    //Loop the window until closed
    while (!glfwWindowShouldClose(window))
    {
        //Render here
        GLint windowWidth, windowHeight;
        glfwGetWindowSize(window, &windowWidth, &windowHeight);
        glViewport(0, 0, windowWidth, windowHeight);
        glClear(GL_COLOR_BUFFER_BIT);
        
        glBindVertexArray(myVAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glBindVertexArray(NULL);
        
        //Poll for events
        glfwPollEvents();
        
        //Swap buffers
        glfwSwapBuffers(window);
    }
    
    glDeleteBuffers(1, &myVBO);
    glDeleteVertexArrays(1, &myVAO);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
    glDeleteProgram(shaderProgram);
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

ShaderUtil.h

#define GLFW_INCLUDE_GLCOREARB

#include <iostream>
#include <fstream>
#include <streambuf>
#include <GLFW/glfw3.h>

using namespace std;

class ShaderUtils
{
public:
    static GLuint createShaderFromFile(const GLchar * path, GLenum shaderType);
};

ShaderUtil.cpp

#include "ShaderUtils.h"

GLuint ShaderUtils::createShaderFromFile(const GLchar * path, GLenum shaderType)
{
    GLuint shaderID = glCreateShader(shaderType);
    
    ifstream fin;
    fin.open(path);
    if (!fin.is_open())
    {
        cout << "File Not Found '" << path << "'!" << endl;
        return -1;
    }
    string source((istreambuf_iterator<GLchar>(fin)), istreambuf_iterator<GLchar>());
    fin.close();
    
    const GLchar * shaderSource = source.c_str();
    glShaderSource(shaderID, 1, &shaderSource, NULL);
    
    glCompileShader(shaderID);
    
    GLint compileStatus;
    glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compileStatus);
    
    if (compileStatus != GL_TRUE)
    {
        cout << "Shader Failed To Compile: '" << path << "'!" << endl;
        GLint infoLogLength;
        glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
        GLchar * infoLog = new GLchar[infoLogLength + 1];
        glGetShaderInfoLog(shaderID, infoLogLength + 1, NULL, infoLog);
        cout << infoLog << endl;
        delete infoLog;
        
        return -1;
    }
    
    return 0;
}

我试图让这段代码与教程代码几乎完全相同,而且我不能为我的生活找到任何差异。帮我找到问题(没有错误)并向我解释。

1 个答案:

答案 0 :(得分:1)

您的createShaderFromFile()方法始终返回0:

GLuint ShaderUtils::createShaderFromFile(const GLchar * path, GLenum shaderType)
{
    ...
    return 0;
}

您将此返回值用作着色器ID:

GLuint vertexShader = ShaderUtils::createShaderFromFile(
        "myVertexShader.vs", GL_VERTEX_SHADER);
GLuint fragmentShader = ShaderUtils::createShaderFromFile(
        "myFragmentShader.fs", GL_FRAGMENT_SHADER);

这些值将显示为0.要使其工作,您需要从方法返回着色器ID:

GLuint ShaderUtils::createShaderFromFile(const GLchar * path, GLenum shaderType)
{
    ...
    return shaderID;
}

您应该可以通过逐步调试调试器中的代码来跟踪此类问题。