Last active
May 30, 2024 17:09
-
-
Save ipatch/8517a5914d56c45b0ebc4dd4df5160c4 to your computer and use it in GitHub Desktop.
hello world gl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.10) | |
# Set the project name | |
project(HelloWorldOpenGL) | |
# Find the required packages | |
find_package(OpenGL REQUIRED) | |
find_package(GLU REQUIRED) | |
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.c) | |
# Link the libraries | |
target_link_libraries(hello_world_opengl PRIVATE OpenGL::GLU GLEW::GLEW ${GLFW_LIBRARIES}) | |
target_include_directories(hello_world_opengl PRIVATE ${GLFW_INCLUDE_DIRS}) | |
target_link_directories(hello_world_opengl PRIVATE ${GLFW_LIBRARY_DIRS}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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