OpenGL 在打开窗口时导致问题

问题描述 投票:0回答:1

我的代码没有正确打开窗口,它只是向我显示空白页面

#include <iostream>
#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 <glm/gtx/matrix_interpolation.hpp>
#include <glm/gtc/constants.hpp>
#include "shader.h"

// Function to create a shader program
GLuint createShaderProgram(const char* vertexShaderPath, const char* fragmentShaderPath) {
    return initShader(vertexShaderPath, fragmentShaderPath);
}

// Function to create a VAO and VBO for a shape
GLuint createShapeVAO(const GLfloat* vertices, GLsizei vertexCount) {
    GLuint VAO, VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertexCount, vertices, GL_STATIC_DRAW);

    // Set up vertex attributes
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(1);

    glBindVertexArray(0);

    return VAO;
}

// Function to render a shape
void renderShape(GLuint shaderProgram, GLuint VAO, GLsizei vertexCount, const glm::mat4& transform) {
    glUseProgram(shaderProgram);
    GLint transformLoc = glGetUniformLocation(shaderProgram, "transform");
    glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));

    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, vertexCount);
    glBindVertexArray(0);
}

int main(void) {
    //++++create a glfw window+++++++++++++++++++++++++++++++++++++++
    GLFWwindow* window;

    if (!glfwInit()) //Initialize the library
        return -1;

    window = glfwCreateWindow(640, 640, "OpenGL Window", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);//Make the window's context current

    //++++Initialize GLEW to setup the OpenGL Function pointers+++++++
    glewExperimental = GL_TRUE;
    glewInit();

    //++++Define the viewport dimensions++++++++++++++++++++++++++++
    glViewport(0, 0, 640, 640);

    GLuint shaderProgram = createShaderProgram("vert.glsl", "frag.glsl");

    // Define vertices for the triangle and square
    GLfloat vertices_DarkPinktriangle[] = {
        -0.4f, 0.4f, 0.0f, 170.0f / 255.0f, 51.0f / 255.0f, 106.0f / 255.0f, // Left
        0.4f, 0.4f, 0.0f, 170.0f / 255.0f, 51.0f / 255.0f, 106.0f / 255.0f, // Right
        0.0f, 0.0f, 0.0f, 170.0f / 255.0f, 51.0f / 255.0f, 106.0f / 255.0f, // Top
    };

    // Create VAOs for the shapes
    GLuint VAO_DarkPinktriangle = createShapeVAO(vertices_DarkPinktriangle, 3);
    //GLuint VAO_Greensquare = createShapeVAO(vertices_Greensquare, 4);

    // Initialize transformation matrices
    glm::mat4 transform_DarkPinktriangle = glm::mat4(1.0f);
    //glm::mat4 transform_Greensquare = glm::mat4(1.0f);

    // Main rendering loop
    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Render the triangle
        transform_DarkPinktriangle = glm::translate(transform_DarkPinktriangle, glm::vec3(0.0f, 0.0f, 0.0f));
        renderShape(shaderProgram, VAO_DarkPinktriangle, 3, transform_DarkPinktriangle);
    }

    // Cleanup
    glDeleteVertexArrays(1, &VAO_DarkPinktriangle);
    //glDeleteVertexArrays(1, &VAO_Greensquare);
    glDeleteProgram(shaderProgram);

    glfwTerminate();
    return 0;
}

我原以为屏幕上会显示一个红色三角形,但我看到的只是一个空白的白色屏幕,视觉工作室没有显示任何错误,问题不在于我的着色器文件或任何其他文件,因为它使用类似的代码,只是这种格式这让我头疼

c++ opengl graphics buffer shader
1个回答
0
投票

您永远不会交换帧缓冲区,请在循环末尾添加

glfwSwapBuffers(window);

© www.soinside.com 2019 - 2024. All rights reserved.