0% found this document useful (0 votes)
17 views7 pages

CH 6 CG

Chapter 6

Uploaded by

abiysol95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

CH 6 CG

Chapter 6

Uploaded by

abiysol95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CHAPTER SIX: STATE MANAGEMENT AND DRAWING GEOMETRIC OBJECTS

State management and drawing geometric objects are essential components of OpenGL
programming. They allow developers to control how graphics are rendered and how various
states affect the rendering process.

6.1. Basic State Management in OpenGL

State management involves controlling various settings that influence rendering. Key aspects
include:

 Clearing Buffers: Before drawing, you typically clear the window to a specific color. This
is done using glClearColor() to set the clear color and glClear() to clear the buffers. For
example:

 Enabling/Disabling States: You can enable or disable various features such as lighting, texturing,
and blending using glEnable() and glDisable(). For instance, to enable lighting:

In OpenGL, managing the rendering state is crucial for controlling how graphics are displayed.
You can enable or disable various features such as lighting, texturing, and blending using the
functions glEnable() and glDisable(). Here’s a brief overview of how to use these functions
effectively:

1. Lighting: To enable lighting in your scene, you would use:

This enables the use of textures on your geometric objects, allowing for more complex visual effects.

3. Blending: To enable blending, which is useful for transparency effects, you would use:
This stops OpenGL from applying lighting calculations to your objects.

6.2. Drawing Geometric Objects


OpenGL allows you to draw various geometric primitives, including points, lines, and polygons.
Here’s how you can draw these objects:

 Drawing Points: You can control the size of points using glPointSize(). For example:

Drawing Lines: Lines can be drawn with varying widths using glLineWidth(). Here’s an example of
drawing a line:

Drawing Polygons: Polygons can be filled or outlined. To draw a filled polygon, you can use:
Example: Drawing a Triangle

Here’s a complete example that combines state management and drawing geometric objects,
specifically a triangle:

In this example, we set the clear color to black, clear the color buffer, and then draw a triangle with
vertices of different colors. The glFlush() function ensures that all OpenGL commands are executed.
Code Breakdown

1. Clearing the Buffer:


glClear(GL_COLOR_BUFFER_BIT);

This line clears the color buffer, preparing the window for new drawing commands.

2. Setting Line Width and Color:

glLineWidth(2.0); // Set line width to 2 pixels


glColor3f(1.0, 0.0, 0.0); // Set color to red

Here, you set the width of the lines to 2 pixels and the color to red. It's important to set
these states before drawing, as OpenGL uses the current state settings when rendering.

3. Drawing Lines:
o Using GL_LINES:

glBegin(GL_LINES);
glVertex2f(-0.5, -0.5); // Start point
glVertex2f(0.5, 0.5); // End point
glEnd();

This block draws a single line between the specified vertices.

o Using GL_LINE_STRIP:

glBegin(GL_LINE_STRIP);
glVertex2f(-0.5, 0.5);
glVertex2f(0.0, 0.0);
glVertex2f(0.5, 0.5);
glEnd();

This block creates a connected series of lines, effectively drawing a continuous


line through the specified points.

4. Flushing the Commands:

glFlush();

This command ensures that all OpenGL commands are executed as quickly as possible.

To draw polygons in OpenGL, you can choose to fill them or outline them based on your
requirements. Here’s how you can do both:

Drawing Filled Polygons

To draw a filled polygon, you typically use the GL_POLYGON primitive. However, it's important
to note that GL_POLYGON is only valid for convex polygons. Here’s a basic example of how to
draw a filled polygon:

glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5); // Vertex 1
glVertex2f(0.5, -0.5); // Vertex 2
glVertex2f(0.5, 0.5); // Vertex 3
glVertex2f(-0.5, 0.5); // Vertex 4
glEnd();

Drawing Outlined Polygons

To draw the outline of a polygon, you can use GL_LINE_LOOP. This will connect the vertices in a
loop, effectively outlining the shape. Here’s how you can do it:

glBegin(GL_LINE_LOOP);
glVertex2f(-0.5, -0.5); // Vertex 1
glVertex2f(0.5, -0.5); // Vertex 2
glVertex2f(0.5, 0.5); // Vertex 3
glVertex2f(-0.5, 0.5); // Vertex 4
glEnd();

Combining Filled and Outlined Polygons

If you want to draw a filled polygon with an outline, you can first draw the filled polygon and
then draw the outline on top of it. Here’s an example:

// Draw filled polygon


glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 0.0); // Yellow fill
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, -0.5);
glVertex2f(0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();

// Draw outline
glColor3f(0.0, 0.0, 0.0); // Black outline
glBegin(GL_LINE_LOOP);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, -0.5);
glVertex2f(0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();

You might also like