Giter Site home page Giter Site logo

d-k-e / opengl-window-tutorial Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 641 KB

Simple tutorial for building an opengl window with glfw, and glad on linux command line

License: GNU General Public License v3.0

CMake 0.49% C 20.85% Objective-C 2.20% C++ 73.29% Makefile 3.17%
opengl opengl-tutorial cmake-examples linux command-line glfw glad

opengl-window-tutorial's Introduction

opengl-window-tutorial

Simple tutorial for building an opengl window with glfw, and glad on linux command line.

Why ?

Well, it took me 2 weeks to figure out how to compile a goddamn window that's why.

Most of the tutorials, books (at least the popular ones) I have seen, show you either how to do it by using an ide, or come with a repository and a complex CMakeLists.txt file that magically builds examples.

If you are new into C/C++ world and the build/compile/run operations like myself, you will soon find yourself trying IDEs advertised in the tutorial X.

I did that, and it didn't work out well in the end. I realized that I was getting more and more hesitant about build/compile process, which made me question whether I wanted to really pursue learning OpenGL, whether I should look for alternatives, etc.

But in the end, I noticed that at its base, it is a very simple process.

So I prepared this tutorial for you.

Just follow the steps and you shall have your first window. Make sure to actually read every step instead of copy pasting mindlessly. I lost a lot of time looking for magical lines.

Step 1: Project Layout

Here is my layout. It should work for simple applications, but might fail to deal with complexities of a large application.

Here is the logic behind the layout:

  • root: project's root dir. Contains the CMakeLists.txt
    • /bin: contains the final executable
    • /libs: contains dynamic and static libs that will be linked to your project
    • /include: contains the header files, like *.h , *.hpp
    • /build: contains build files that would be generated by cmake
    • /src: contains source files that implements/uses those that were specified in your header files

Step 2: Cmake

This is by far the only challenging aspect of building the application for a beginner.

Here is the contents of the projects CMakeLists.txt, I shall explain what they do in the comments:

cmake_minimum_required (VERSION 2.8.3) # this line specifies
# the minimum version of cmake that is required to build this project
project("Hello Window OpenGL")
# specifies the name of the project
# if your name includes spaces it should be in quotes

find_package(OpenGL REQUIRED)
# If you don't know where your opengl library is
# then this would find the location of your opengl library

include_directories(
    # my headers
    "./include/"
    )
    # this includes the header files in your directory to your projects
    # meaning that this helps you to write with statements like
    # #include <GLFW/glfw3.h>
    # #include <glad/glad.h>
    # assuming that ./include directory also contains
    # other subdirectories like GLFW/ and glad/

set( GLFW_SHARED_LIB
    "${PROJECT_SOURCE_DIR}/libs/glfw/src/libglfw.so")
    # this sets a variable to the path of the dynamic library of glfw
    # Meaning that the functions of GLFW would be available at runtime
    # not at compile time.
    # If you have undefined reference errors try compiling
    # GLFW with static library option.
    # Notice that libs/ contain a sub directory glfw/
    # I simply copy pasted the src/ directory that resulted from
    # the building process of glfw to project_root/lib/glfw/

set ( ALL_LIBS
    ${OpenGL}
    ${GLFW_SHARED_LIB}
    "-ldl"
    )
    # This line simply assings a new variable to the group of
    # libraries that we need to link to
    # -ldl is necessary I don't know why.
    #

add_executable(myWin
    "src/glad.c"
    "src/window.cpp"
    )
# This line generates the executable, the access point to your program
# basically it creates a file called myWin, that can be run as ./myWin
# Notice that in order to create the executable we need to add the glad.c
# as well
target_link_libraries(myWin ${ALL_LIBS})
# this line links the dependency libraries to our executable
# With out this line we won't be able to use <glad/glad.h> <GLFW/glfw3.h>
# etc
install(TARGETS myWin DESTINATION "${PROJECT_SOURCE_DIR}/bin/")
# this line gives an install location for the executable
# if the make command is invoked as make install

The above file should be easy to read and understand.

Step 3: Build

Let's try it:

  • ~/projectRoot$ rm -rf build/: Deletes the build directory
  • ~/projectRoot$ mkdir build: Creates a build directory
  • cd build: Enters into the build directory
  • cmake ..: Builds the contents of CMakeLists.txt of the parent directory to the current directory
  • make install: Creates the file myWin and puts it into the projectRoot/bin directory
  • cd projectRoot/bin: Enters into the bin directory
  • ./myWin: Launches the executable

If everything went alright, you should see a black window with Learn OpenGL as title

opengl-window-tutorial's People

Contributors

d-k-e avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.