Skip to content

Instantly share code, notes, and snippets.

@ipatch
Last active November 1, 2024 20:39
Show Gist options
  • Save ipatch/8517a5914d56c45b0ebc4dd4df5160c4 to your computer and use it in GitHub Desktop.
Save ipatch/8517a5914d56c45b0ebc4dd4df5160c4 to your computer and use it in GitHub Desktop.
hello world gl
  • use legacy token to push changes to this repo, ie. PAT token does not work with gists
cmake_minimum_required(VERSION 3.10)
# Set the project name
project(HelloWorldOpenGL)
# Include CMAKE_PREFIX_PATH for custom search paths
list(APPEND CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
if(APPLE)
# Set paths for Homebrew's mesa-glu
find_path(GLU_INCLUDE_DIR GL/glu.h PATH_SUFFIXES GL PATHS ${CMAKE_PREFIX_PATH})
find_library(GLU_LIBRARY GLU PATHS ${CMAKE_PREFIX_PATH})
# NO WORK!
# set(GLU_INCLUDE_DIR "${HOMEBREW_PREFIX}/opt/mesa-glu/include")
# set(GLU_LIBRARY "${HOMEBREW_PREFIX}/opt/mesa-glu/lib/libGLU.dylib")
else()
find_path(GLU_INCLUDE_DIR GL/glu.h PATH_SUFFIXES GL PATHS ${CMAKE_PREFIX_PATH})
find_library(GLU_LIBRARY GLU PATHS ${CMAKE_PREFIX_PATH})
# NO WORK! github ubuntu runner
# Set paths for Linux
# set(GLU_INCLUDE_DIR "${HOMEBREW_PREFIX}/opt/mesa-glu/include")
# set(GLU_LIBRARY "${HOMEBREW_PREFIX}/opt/mesa-glu/lib/libGLU.so")
endif()
# Check if GLU headers and library exist
if(EXISTS "${GLU_INCLUDE_DIR}/GL/glu.h" AND EXISTS "${GLU_LIBRARY}")
message(STATUS "Found GLU headers at: ${GLU_INCLUDE_DIR}")
message(STATUS "Found GLU library at: ${GLU_LIBRARY}")
else()
message(FATAL_ERROR "======================\n"
"GLU library not found.\n"
"Please check that mesa-glu is installed.\n"
"======================")
endif()
# if(APPLE)
# set(GLU_INCLUDE_DIR "${HOMEBREW_PREFIX}/opt/mesa-glu/include")
# set(GLU_LIBRARY "${HOMEBREW_PREFIX}/opt/mesa-glu/lib/libGLU.so")
# include(FindPackageMessage)
# if(OPENGL_GLU_FOUND)
# find_package_message(OPENGL_GLU
# "Found OpenGLU: ${OPENGL_glu_LIBRARY}"
# "[${OPENGL_glu_LIBRARY}][${OPENGL_INCLUDE_DIR}]")
# else(OPENGL_GLU_FOUND)
# message(FATAL_ERROR "======================\n"
# "GLU library not found.\n"
# "======================\n")
# endif(OPENGL_GLU_FOUND)
# find_package(OpenGL REQUIRED)
# set(GLU_INCLUDE_DIR "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework")
# set(GLU_LIBRARY "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries/libGLU.tbd")
# set(GLU_INCLUDE_DIR "${HOMEBREW_PREFIX}/opt/mesa-glu/include")
# set(GLU_LIBRARY "${HOMEBREW_PREFIX}/opt/mesa-glu/lib/libGLU.dylib")
# Set the include path for OpenGL's framework headers on macOS
# set(APPLE_OPENGL_INCLUDE_PATH "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Headers")
# set(GLU_LIBRARY "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries/libGLU.tbd")
# Confirm that the include path exists
# if(EXISTS "${APPLE_OPENGL_INCLUDE_PATH}")
# message(STATUS "Using Apple OpenGL include path: ${APPLE_OPENGL_INCLUDE_PATH}")
# include_directories("${APPLE_OPENGL_INCLUDE_PATH}")
# else()
# message(FATAL_ERROR "Apple OpenGL framework headers not found at ${APPLE_OPENGL_INCLUDE_PATH}")
# endif()
# find_package(GLU REQUIRED)
# else(UNIX)
# find_package(OpenGL REQUIRED)
# set(GLU_INCLUDE_DIR "${HOMEBREW_PREFIX}/opt/mesa-glu/include")
# set(GLU_LIBRARY "${HOMEBREW_PREFIX}/opt/mesa-glu/lib/libGLU.so")
# # Include the GLU directory
# if(EXISTS "${GLU_INCLUDE_DIR}")
# include_directories("${GLU_INCLUDE_DIR}")
# else()
# message(FATAL_ERROR "Could not find GLU headers at ${GLU_INCLUDE_DIR}")
# endif()
# # find_package(GLU REQUIRED)
# endif()
# check if GLU headers found, if not fail
# if(NOT OPENGL_GLU_INCLUDE_DIR)
# message(FATAL_ERROR "could not find GLU headers")
# else()
# message(STATUS "Found GLU headers: ${OPENGL_GLU_INCLUDE_DIR}")
# endif()
# Check if GLU lib found
if(NOT GLU_LIBRARY)
message(FATAL_ERROR "Could not find GLU library")
else()
message(STATUS "Found GLU library: ${GLU_LIBRARY}")
endif()
message(STATUS "------------------------------------------------------")
message(STATUS "ipatch foo 12 ")
message(STATUS "opengl_glu_library=${OPENGL_GLU_LIBRARY} ")
message(STATUS "------------------------------------------------------")
find_package(GLEW REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
# Add the executable
add_executable(hello_world_opengl hello_world_opengl.cpp)
# Link the libraries, link the headers followed by the libs
target_include_directories(hello_world_opengl PRIVATE ${OPENGL_INCLUDE_DIR} ${OPENGL_GLU_INCLUDE_DIR} ${GLFW_INCLUDE_DIRS})
# target_include_directories(hello_world_opengl PRIVATE ${OPENGL_INCLUDE_DIR} ${GLU_INCLUDE_DIR} ${GLFW_INCLUDE_DIRS})
target_link_libraries(hello_world_opengl PRIVATE ${OPENGL_LIBRARIES} ${OPENGL_GLU_LIBRARY} GLEW::GLEW ${GLFW_LIBRARIES})
# target_link_libraries(hello_world_opengl PRIVATE ${OPENGL_LIBRARIES} ${GLU_LIBRARY} GLEW::GLEW ${GLFW_LIBRARIES})
# target_include_directories(hello_world_opengl PRIVATE ${OPENGL_GLU_INCLUDE_DIR})
# target_link_libraries(hello_world_opengl PRIVATE ${OPENGL_GLU_LIBRARY} GLEW::GLEW ${GLFW_LIBRARIES})
# target_include_directories(hello_world_opengl PRIVATE ${GLFW_INCLUDE_DIRS})
# target_link_directories(hello_world_opengl PRIVATE ${GLFW_LIBRARY_DIRS})
# install the actual binary
install(TARGETS hello_world_opengl DESTINATION bin)
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <GL/glu.h>
#include <iostream>
// Error callback function for GLFW
void error_callback(int error, const char* description)
{
std::cerr << "Error: " << description << std::endl;
}
// Key callback function for GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
int main()
{
// Initialize the library
if (!glfwInit())
{
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
// Set the error callback
glfwSetErrorCallback(error_callback);
// Create a windowed mode window and its OpenGL context
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Make the window's context current
glfwMakeContextCurrent(window);
// Initialize GLEW
GLenum err = glewInit();
if (GLEW_OK != err)
{
std::cerr << "Error: " << glewGetErrorString(err) << std::endl;
glfwTerminate();
return -1;
}
// Set the key callback
glfwSetKeyCallback(window, key_callback);
// Loop until the user closes the window
while (!glfwWindowShouldClose(window))
{
// Render here
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up the model-view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Apply transformations
gluLookAt(0.0, 0.0, 5.0, // Eye position
0.0, 0.0, 0.0, // Center position
0.0, 1.0, 0.0); // Up vector
// Render a simple triangle
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glEnd();
// Swap front and back buffers
glfwSwapBuffers(window);
// Poll for and process events
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment