Initialize an OpenGL application: display a single color
This commit is contained in:
commit
d7044f7352
22
.gitignore
vendored
Normal file
22
.gitignore
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
# CMake build directories
|
||||
build/
|
||||
cmake-build-*/
|
||||
out/
|
||||
|
||||
# CMake generated files
|
||||
CMakeFiles/
|
||||
CMakeCache.txt
|
||||
cmake_install.cmake
|
||||
Makefile
|
||||
*.vcxproj*
|
||||
*.sln
|
||||
|
||||
# IDE specific files
|
||||
.idea/
|
||||
.vscode/
|
||||
*.user
|
||||
|
||||
# Other temporary files
|
||||
*.log
|
||||
*.tmp
|
||||
*.swp
|
||||
16
CMakeLists.txt
Normal file
16
CMakeLists.txt
Normal file
@ -0,0 +1,16 @@
|
||||
cmake_minimum_required(VERSION 3.12)
|
||||
project(OpenGLTest CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLEW REQUIRED)
|
||||
find_package(glfw3 REQUIRED)
|
||||
|
||||
add_executable(opengl_test src/main.cpp)
|
||||
|
||||
target_link_libraries(opengl_test
|
||||
OpenGL::GL
|
||||
GLEW::GLEW
|
||||
glfw
|
||||
)
|
||||
85
src/main.cpp
Normal file
85
src/main.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
#include <stdio.h>
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
// Window dimensions
|
||||
const GLint WIDTH = 800, HEIGHT = 600;
|
||||
|
||||
|
||||
int main() {
|
||||
// Initialize GLFW
|
||||
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
|
||||
|
||||
if (!glfwInit()) {
|
||||
printf("GLFW initialization failed!");
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// Setup GLFW window properties
|
||||
// OpenGL version
|
||||
// @Reminder Look into making it work on Wayland
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
|
||||
// Core profile means it will not be backwards compatible
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Test Window", NULL, NULL);
|
||||
|
||||
if (!window) {
|
||||
const char *desc;
|
||||
int code = glfwGetError(&desc);
|
||||
printf("GLFW window creation failed!");
|
||||
printf("GLFW error code: %d, description: %s\n", code, desc ? desc : "No description given");
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Get Buffer size information
|
||||
int buffer_width, buffer_height;
|
||||
glfwGetFramebufferSize(window, &buffer_width, &buffer_height);
|
||||
|
||||
// Set context for GLEW to use
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
|
||||
glGetError();
|
||||
|
||||
// Allow modern extension features
|
||||
glewExperimental = GL_TRUE;
|
||||
|
||||
GLenum err = glewInit();
|
||||
if (err != GLEW_OK && err != GLEW_ERROR_NO_GLX_DISPLAY) {
|
||||
printf("OpenGL version: %s\n", glGetString(GL_VERSION));
|
||||
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
|
||||
printf("glewInit() returned: %d\n", err);
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Setup viewport size
|
||||
glViewport(0, 0, buffer_width, buffer_height);
|
||||
|
||||
// Loop until window is closed
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// Get and handle user input events
|
||||
glfwPollEvents();
|
||||
|
||||
// Clear window
|
||||
glClearColor(1.0f, 0.9f, 0.4f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
|
||||
// We usually have two buffers, the first is hidden and that's the one
|
||||
// we draw to. The second is the one we display, so when we're done drawing
|
||||
// we call this glfwSwapBuffers() to swap the buffers to show the one we've
|
||||
// been drawing to so that it now shows. We then can start drawing on the
|
||||
// other buffer.
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user