James
James

Reputation: 97

Homebrew GLFW/GLEW on Mac

I have so far installed GLFW and GLEW through Homebrew on MacOs. These are installed in the following directory (usr/local/Cellar/).

I have taken the following script from a tutorial, and added some legacy OpenGL, in the hope that I can test the OpenGL all linked and working. (The reason I am doing this is because I am still learning OpenGL, and I do not yet under shaders etc).

CMakeList.txt

cmake_minimum_required(VERSION 3.3)
project(Lib_Test)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa -framework OpenGL -framework IOKit")

set(SOURCE_FILES src/main.cpp CMakeLists.txt)

# add extra include directories
include_directories(/usr/local/include)

# add extra lib directories
link_directories(/usr/local/lib)

add_executable(Lib_Test main.cpp)
target_link_libraries(Lib_Test glfw)
target_link_libraries(Lib_Test glew)
find_package (GLM REQUIRED)
include_directories(include)

main.cpp

#include <stdio.h>


// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic.

#include <GL/glew.h>

// Include GLFW
#include <GLFW/glfw3.h>


// Include GLM

#include <glm/glm.hpp>

using namespace glm;


int main(){

// Initialise GLFW
if( !glfwInit() )
{
    fprintf( stderr, "Failed to initialize GLFW\n" );
    return -1;
}


glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
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( 800, 600, "My App", NULL, NULL);
if( window == NULL ){
    fprintf( stderr, "Failed to open GLFW window.\n" );
    glfwTerminate();
    return -1;
}

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


// Ensure we can capture keys being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

do{

    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
    glVertex2f(-0.5f, -0.5f);
    glVertex2f(0.0f, -0.5f);
    glVertex2f(0.5f, -0.5f);
    glEnd();

    // Swap buffers
    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 );



}

Everything compiles, and runs. But nothing is ever drawn. I can even change the background colour (using glClear). My problem is, if something isn't working, I'm not even sure what that something is.

Thanks

Upvotes: 0

Views: 4078

Answers (1)

Ripi2
Ripi2

Reputation: 7198

You are mixing old fixed-pipeline way and modern (with shaders) way.

All lines between and including glBegin - glEnd are only for old OpenGL.

GLFW_OPENGL_FORWARD_COMPAT should not have any effect on OSX. Anyhow, remember it's not compatible with GLFW_OPENGL_CORE_PROFILE.

My advise is that you write shaders stuff code (compilation, etc), even it's long and hard for the first time.

BTW, glew is not needed in Mac. OSX provides the gl-functions, so you don't need to retrive the function pointers to them. It's harmless to use it though.

Upvotes: 2

Related Questions