OpenGL causing issues when opening window

51 Views Asked by At

My code is not opening the window properly its just showing me blank page

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

I was expecting a red triangle to be shown on the screen however all I see is a blank white screen visual studio shows no error and the problem isnt with my shader file or any other file because it works with similar code its just this format that its causing me a headache

1

There are 1 best solutions below

2
Quimby On

You never swap the framebuffers, add glfwSwapBuffers(window); after renderShape call.