【发布时间】:2023-03-08 22:57:01
【问题描述】:
到目前为止,我已经通过 Homebrew 在 MacO 上安装了 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)。我的问题是,如果某些东西不起作用,我什至不确定那是什么。
谢谢
【问题讨论】:
-
在核心配置文件中,无法使用 glBegin/glEnd 进行绘制。
标签: c++ macos opengl cmake glfw