OpenGL 学习之旅(二)

来源:互联网 发布:索尼运动耳机知乎 编辑:程序博客网 时间:2024/06/10 10:26

  • What is OpenGL
    • Objects in OpenGL
  • Creating a window
    • GLFW
      • Building GLFW
      • Add GLFW
      • Test GLFW
    • GLAD
      • Generate GLAD
      • Add GLAD
      • Test GLAD
  • Hello Window
    • Init GLFW
    • Init GLAD
    • Viewport
      • Coordinates in OpenGL
    • Render Loop
      • input
      • render
      • glfw operation
  • Hello Triangle
    • Graphics pipeline
    • 2D coordinates and 2D pixel
    • Shaders
    • How the pipeline operates
      • Vertex Data
      • Kind of render type
      • Fragment
    • What we should do
    • Vertex input
      • Vertex Buffer Objects VBO


What is OpenGL?

OpenGL is a large state machine. We change it state by using functions and tell it how to draw.

Objects in OpenGL

An object in OpenGL is a collection of options that represents a subset of OpenGL’s state.


Creating a window

We need to create an OpenGL context and an application window to draw in.

We have to craete a window, define a context and handle user input all by ourselves.

Some of more popu;ar libraries are GLUT, SDL, SFML, and GLFW, which already provide the functionality.

GLFW

Building GLFW

We need CMake.

CMake Download

Use CMake to compile GLFW as binaries.
Add GLFW’s static library to Xcode.

Add GLFW

GLFW with Xcode

Test GLFW

In the main file, the code as follow should be right.

#define GLFW_INCLUDE_GLCOREARB#include <GLFW/glfw3.h>

GLAD

We alse need GLAD.

Generate GLAD

GLAD - Web Service

Make sure the API-gl is what you need, the profile is Core. Then click Generate to produce the resulting library.

Add GLAD

After generating GLAD, you will get a zip file including two folders. Add these folders to your project and set something (for instance, header file path).

Test GLAD

In the main file, the code as follow should be right.

#include "glad/glad.h"

Hello Window

Init GLFW

Init GLAD

Viewport

Coordinates in OpenGL

Range (-1, 1) to width and height.

Cross coordinates

Render Loop

input

render

glfw operation


Hello Triangle

The large part of OpenGL’s work is about transforming all 3D coordinates to 2D pixels that fit on your screen.
The process pf transforming 4D coordinates to 2D coordinates is managed by tje graphics pipeline of OpenGL.

Graphics pipeline

It can be divided tow large parts:
- transform your 3D coordinates into 2D coordinates
- transform the 2D coordinate into actual colored pixels

2D coordinates and 2D pixel

2D coordinates is a very precise representation of where a point is in 2D space.
2D pixel is an approximation of that point limited by the resolution of your screen/window.

Shaders

Quickly process your data within the graphics pipeline by running small programs on the GPU.

Written in the OpenGL Shading Language(GLSL)

How the pipeline operates

Vertex Data

A collection of vertices.

Kind of render type

OpenGL requires you to hint what kind of render typed you want to form with the data.

Fragment

A fragment in OpenGL is all the data required for OpenGL to render a single pixel.

Purpose: To calculate the final color of a pixel.

What we should do?

We are required to define at least a vertex and fragment shader of our own.

Vertex input

OpenGL only process 3D coordinates when they’re in a specific range between -1.0 and 1.0 on all 3 axes(x, y and z).

Vertex Buffer Objects (VBO)

Vertex buffer objects can store a large number of vertices in the GPU’s memory.

The advantage of using those buffer objects is that we can send large batches of data all at once to the graphics card without having to send data a vertex a time.

原创粉丝点击