Mac上的Homebrew GLFW / GLEW

时间:2018-02-07 18:47:44

标签: c++ macos opengl cmake glfw

到目前为止,我已经通过Homebrew在MacOs上安装了GLFW和GLEW。它们安装在以下目录中(usr / local / Cellar /)。

我从教程中获取了以下脚本,并添加了一些遗留的OpenGL,希望我可以测试所有链接和工作的OpenGL。 (我这样做的原因是因为我还在学习OpenGL,而且我还没有使用着色器等)。

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



}

所有东西都编译并运行。但是没有画出任何东西。我甚至可以改变背景颜色(使用glClear)。我的问题是,如果某些事情不起作用,我甚至不确定那是什么东西。

由于

1 个答案:

答案 0 :(得分:1)

您正在混合旧的固定管道方式和现代(使用着色器)方式。

旧版OpenGL中glBegin - glEnd之间的所有行

GLFW_OPENGL_FORWARD_COMPAT不应对OSX产生任何影响。无论如何,请记住它与GLFW_OPENGL_CORE_PROFILE不兼容。

我的建议是你在编写着色器代码(编译等),即使它是第一次很长很难。

BTW,Mac中不需要glew。 OSX提供gl函数,因此您无需检索它们的函数指针。但是使用它是无害的。