OpenGL Tutorial - 2 | Drawing some basic primitives | OpenGL in C++ with the GLUT library
OpenGL Tutorial - 2 | Drawing some basic primitives | OpenGL in C++ with the GLUT library - Notes
1. Summary
This tutorial introduces the fundamentals of drawing primitives in OpenGL using C++ and the GLUT library. It covers essential initialization steps like setting up the viewport and projection matrix, which are crucial before rendering any shapes. The video demonstrates drawing points, and triangles, and explains the significance of vertex order for polygon rendering.
2. Key Takeaways
* **Viewport:** Defines the rectangular area within the window where drawings are displayed. Use `glViewport()` to set it.
* **Reshape Callback (glutReshapeFunc):** A function called when the window is first created or resized. It's used to set the viewport and adjust the projection matrix.
* **Projection Matrix:** Specifies how 3D objects are projected onto the 2D screen. The tutorial uses a 2D orthographic projection with `glOrtho2D()`.
* **Matrix Modes:** OpenGL uses different matrices. The video covers modelview (for transformations) and projection. `glMatrixMode()` is used to switch between them.
* **GL Begin/End:** Enclose the drawing commands (vertices) between `glBegin()` and `glEnd()`.
* **Vertices (GL Vertex):** Define the points that make up primitives. `glVertex2f()` specifies a 2D point (x, y).
* **Primitive Types:** `GL_POINTS`, `GL_TRIANGLES`, `GL_QUADS`, and `GL_POLYGON` are shown for specifying the type of the primitive to draw with glBegin().
* **Vertex Order for Polygons:** The order in which you specify vertices affects how the polygon is drawn (front/back faces) and should always be anti-clockwise.
3. Detailed Notes
**I. Initialization and Setup**
* **Black Background:** The background color is set to black using `glClearColor(0.0f, 0.0f, 0.0f, 0.0f);`.
* **Essential steps before drawing**
* Viewport Initialization
* Projection Matrix Initialization
* **Reshape Callback:**
* A callback function named `reshape` is defined, which is registered using `glutReshapeFunc(reshape);`.
* This function is called:
* When the window is initially created.
* Whenever the window is resized, maximized, or minimized.
* The reshape callback receives the new width and height of the window as arguments.
**II. Viewport**
* **Definition:** The viewport is the rectangular area *within* the window where OpenGL will actually draw. It's the "clipping area."
* **`glViewport()` Function:**
* Used to specify the viewport.
* Takes four arguments:
* `x`, `y`: The bottom-left corner coordinates of the viewport relative to the bottom-left of the window. `0, 0` positions the viewport at the bottom-left.
* `width`, `height`: The width and height of the viewport in pixels.
* To make the viewport fill the entire window: set `glViewport(0, 0, width, height);`. Where width and height come from the reshape function arguments.
**III. Projection Matrix**
* **Matrix Modes:**
* OpenGL uses different matrices to control the rendering pipeline:
* `GL_MODELVIEW`: Used for object transformations (translation, rotation, scaling). This is the default matrix.
* `GL_PROJECTION`: Used for setting up the camera's perspective and determining how objects are projected onto the screen.
* **`glMatrixMode()` Function:**
* Used to switch between matrix modes.
* Called with an argument to specify the matrix mode that needs to be used.
* To switch to the projection matrix: `glMatrixMode(GL_PROJECTION);`
* **`glLoadIdentity()` Function:**
* Resets the current matrix to the identity matrix.
* Called after switching matrix modes to reset the projection matrix.
* `glLoadIdentity();`
* **`glOrtho2D()` Function (2D Orthographic Projection):**
* Sets up a 2D orthographic projection.
* Takes four arguments:
* `left`, `right`: The left and right boundaries of the viewing volume.
* `bottom`, `top`: The bottom and top boundaries of the viewing volume.
* Example of usage to set a cartesian plane with the origin at the center: `glOrtho2D(-10, 10, -10, 10);`
**IV. Drawing Primitives**
* **`glBegin()` and `glEnd()`:**
* Define a block of code where drawing primitives will be specified.
* `glBegin()` takes a single argument that specifies the *type* of primitive to draw.
* `glEnd()` marks the end of the primitive definition. OpenGL *needs* this to know that all the vertices for the drawing has been specified.
* **`glVertex2f()` Function:**
* Specifies a vertex point using `x` and `y` coordinates.
* **`GL_POINTS`:**
* Draws individual points.
* `glBegin(GL_POINTS);`
* `glVertex2f(x, y);`
* `glEnd();`
* **`glPointSize()` Function:**
* Change the size of the point.
* Takes an argument which is the size of the point in pixels.
* **`GL_TRIANGLES`:**
* Draws triangles. OpenGL interprets three vertices as a triangle.
* `glBegin(GL_TRIANGLES);`
* `glVertex2f(x1, y1);`
* `glVertex2f(x2, y2);`
* `glVertex2f(x3, y3);`
* `glEnd();`
* **`GL_QUADS`:**
* Draws quadrilaterals (four-sided polygons). OpenGL interprets four vertices as a quad.
* `glBegin(GL_QUADS);`
* `glVertex2f(x1, y1);`
* `glVertex2f(x2, y2);`
* `glVertex2f(x3, y3);`
* `glVertex2f(x4, y4);`
* `glEnd();`
* **`GL_POLYGON`:**
* Draws filled polygons (with any number of vertices).
* `glBegin(GL_POLYGON);`
* `glVertex2f(x1, y1);`
* `glVertex2f(x2, y2);`
* `glVertex2f(x3, y3);`
* ... (more vertices)
* `glEnd();`
* **Polygon Front/Back Faces and Vertex Order:**
* The order of vertices defines the front and back faces of a polygon.
* **Anti-clockwise order**: Defines the front face. (This is the *usual* convention).
* **Clockwise order**: Defines the back face.
Related Summaries
Why this video matters
This video provides valuable insights into the topic. Our AI summary attempts to capture the core message, but for the full nuance and context, we highly recommend watching the original video from the creator.
Disclaimer: This content is an AI-generated summary of a public YouTube video. The views and opinions expressed in the original video belong to the content creator. YouTube Note is not affiliated with the video creator or YouTube.

![[캡컷PC]0015-복합클립만들기분리된영상 하나로 만들기](https://img.youtube.com/vi/qtUfil0xjCs/mqdefault.jpg)
