OpenGL Black Screen on M2 Macbook

Viewed 40

I have been trying to create a basic OpenGL program in C to display a triangle. Except all I am getting is a black screen.

main.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//#include <GL/glew.h>
#define GL_SILENCE_DEPRECATION
#include "win.h"
#include "shaders/cshader.h"

#include "glew.h"
#define GLFW_INCLUDE_GLCOREARB
#include "glfw3.h"
#include <OpenGL/gl3.h>

int main(void) {

    if( !glfwInit()) {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }
    // create window
    GLFWwindow* window = createWindow(1280,720);

    glewExperimental=GL_TRUE; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    static const GLfloat g_vertex_buffer_data[] = {
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    0.0f,  1.0f, 0.0f,
    };
    // This will identify our vertex buffer
    GLuint vertexbuffer;
    // Generate 1 buffer, put the resulting identifier in vertexbuffer
    glGenBuffers(1, &vertexbuffer);
    // The following commands will talk about our 'vertexbuffer' buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    // Give our vertices to OpenGL.
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

    
    Shader shader = ShaderInit();
    shader.Load(&shader,"/Users/***/Desktop/coding/gl_test/shaders/tri.vert","/Users/***/Desktop/coding/gl_test/shaders/tri.frag");
    glUseProgram(shader.program);
    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
    glfwMakeContextCurrent(window);
    do{
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(
        0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        (void*)0            // array buffer offset
        );
        // Draw the triangle !
        // Starting from vertex 0; 3 vertices total -> 1 triangle
        glUseProgram(shader.program);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        
        glDisableVertexAttribArray(0);
        // Swap buffers
        if (glGetError() != GL_NO_ERROR){
            printf("Err");
        }
        glfwSwapBuffers(window);
        glfwPollEvents();

    } // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
        glfwWindowShouldClose(window) == 0 );
        return 0;
    }

win.c

#include <stdio.h>
#include <stdlib.h>
#include "/Users/***/Downloads/glew-2.1.0/include/GL/glew.h"
#include "/opt/homebrew/Cellar/glfw/3.3.8/include/GLFW/glfw3.h"

GLFWwindow* createWindow(int WIDTH, int HEIGHT){
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // We want OpenGL 4.1
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL 

    // Open a window and create its OpenGL context
    GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
    window = glfwCreateWindow( WIDTH, HEIGHT, "graphic", NULL, NULL);
    if( window == NULL ){
        fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible.\n" );
        glfwTerminate();
        return NULL;
    }
    glfwMakeContextCurrent(window);// Initialize GLEW
    glfwPollEvents();
    return window;
}

The shader program is basic and it compiles and links correctly. I have a vertex and fragment shader that should make the triangle red. Even if it is broken, I should still be getting a colored screen because of glClear and glClearColor.

I have tried changing opengl versions as well as linking different files, but nothing shows up.

Here is my makefile:

LIBS=-L/opt/homebrew/lib -L/Users/***/Downloads/glew-2.1.0/lib -lGL -lglew
INC=-I/Users/***/Downloads/glew-2.1.0/include
main:
    gcc main.c win.c shaders/cshader.c `pkg-config --cflags glfw3` $(LIBS) $(INC) -g -Wall -framework Cocoa -framework OpenGL `pkg-config --libs glfw3` -o test

Everything appears to link and compile without errors. What should I do.

(I know OpenGL is deprecated on new MacOS versions, but lots of people say that at least basic functionality still works)

0 Answers
Related