在窗口中看不到三角形

时间:2018-11-23 06:57:09

标签: c++ opengl graphics

大家好,我是opengl的新手(仅2-3天)。我正在学习一个教程,我写的完整代码与讲师相同,但结果却不尽相同。 通过运行此代码,您应该使三角形在屏幕中向左和向右移动。但是我唯一得到的是黑屏。注意:编译此代码时没有错误。

#include<iostream>
#include<string.h>
#include<cmath>
#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>


const GLint WIDTH = 800, HEIGHT = 600; 
const float toRadians = 3.141592 / 180;
GLuint VAO, VBO, shader, uniformModel;
bool direction = true;
float triOffset = 0.0f, triMaxOffset = 0.7f, triIncrement = 0.0005f;

static const char* vShader = "                                      \n\
#version 330                                                    \n\
                                                                \n\
layout(location=0) in vec3 pos;                                 \n\
uniform mat4 model;                                         \n\
void main() {                                                   \n\
    gl_Position = model*vec4(0.4*pos.x,0.4*pos.y,pos.z, 1);     \n\
}                                                               \n\
";

static const char* fShader = "                                      \n\
#version 330                                                    \n\
                                                                \n\
out vec4 color;                                                 \n\
                                                                \n\
void main() {                                                   \n\
    color = vec4(1,0.0,0.0, 1.0);                                   \n\
}                                                               \n\
";

void createTriangle() {

    GLfloat vertices[] = {
        -1.0f,-1.0f,0.0f,
        1.0f,-1.0f,0.0f,
        0.0f,1.0,0.0f
    };

    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);
}

void AddShader(GLuint theProgram, const char* shaderCode, GLenum shaderType)         
{
    GLuint theShader = glCreateShader(shaderType);
    const GLchar* theCode[1];
    theCode[0] = shaderCode;
    GLint codeLength[1];
    codeLength[0] = strlen(shaderCode);

    glShaderSource(theShader, 1, theCode, codeLength);
    glCompileShader(theShader);

    GLint result = 0;
    GLchar eLog[1024] = { 0 };

    glGetShaderiv(theShader, GL_COMPILE_STATUS, &result);
    if (!result) {
        glGetShaderInfoLog(shader, sizeof(eLog), NULL, eLog);
        std::cout << "Error Compiling " << shaderType << " Shader " << eLog <<           
        std::endl;
        return;
    }
    glAttachShader(theProgram, theShader);
}

void CompileShader() {
    shader = glCreateProgram();

    if (!shader) {
        std::cout << "Error In the shader" << std::endl;
        return;
    }
    AddShader(shader, vShader, GL_VERTEX_SHADER);
    AddShader(shader, fShader, GL_FRAGMENT_SHADER);

    GLint result = 0;
    GLchar eLog[1024] = { 0 };

    glLinkProgram(shader);
    glGetProgramiv(shader, GL_LINK_STATUS, &result);
    if (!result) {
        glGetProgramInfoLog(shader, sizeof(eLog), NULL, eLog);
        std::cout << "Error Linking Program " << eLog << std::endl;
        return;
    }

    glValidateProgram(shader);
    glGetProgramiv(shader, GL_VALIDATE_STATUS, &result);
    if (!result) {
        glGetProgramInfoLog(shader, sizeof(eLog), NULL, eLog);
        std::cout << "Error Validating Program " << eLog << std::endl;
        return;
    }
    uniformModel = glGetUniformLocation(shader, "model");
}

int main()
{
    if (!glfwInit()) {
        std::cout << "Window Initialization Failed" << std::endl;
        glfwTerminate();
        return 1;
    }

    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_FALSE);

    GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGl",nullptr,nullptr);
    if (nullptr == window) {
      std::cout << "Failed To Create GLFW Window" << std::endl;
      glfwTerminate();
      return EXIT_FAILURE;
    }

    int bufferWidth, bufferHeight;
    glfwGetFramebufferSize(window, &bufferWidth, &bufferHeight);

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
        std::cout << "Failed To Initialize GLEW" << std::endl;
        glfwDestroyWindow(window);
        glfwTerminate();
        return EXIT_FAILURE;
    }

    glViewport(0, 0, bufferWidth, bufferHeight);
    createTriangle();
    CompileShader();

    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents(); 

        if (direction) {
            triOffset += triIncrement;
        }
        else {
            triOffset -= triIncrement;
        }

        if (abs(triOffset) >= triMaxOffset) {
            direction = !direction;
        }

        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(shader);

        glm::mat4 model; 
        model = glm::translate(model, glm::vec3(triOffset, 0.0f, 0.0f));
        model = glm::rotate(model, 45 * toRadians, glm::vec3(0, 0, 1));
        glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));

        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glBindVertexArray(0);
        glUseProgram(0);
        glfwSwapBuffers(window);
    }

    glfwTerminate();
    return EXIT_SUCCESS; 
}

0 个答案:

没有答案