OpenGL Getting Started Tutorial 2

Source: Internet
Author: User

Explain the csdn writing the code end of the article


This course is about drawing simple geometries, before actually drawing, let's familiarize ourselves with some concepts.

Point, line, and polygon
We know the concept of a bit, line, and polygon in math (specifically, geometry), but these concepts are different in computers.
Math point, only position, no size. However, in a computer, no matter how the computational accuracy is improved, it is not always possible to represent an infinitely small
Point. On the other hand, no matter how accurate a graphics output device (for example, a display) is, you cannot output an infinitely small point at all. General situation
, the point in OpenGL is drawn as a single pixel (the concept of a pixel, please search by itself), although it may be small enough, but it does not
is infinitely small. On the same pixel, OpenGL can draw many coordinates with only slightly different points, but the specific color of the pixel will depend on
Implementation of OpenGL. Of course, excessive attention to detail is a dead-eye, we can not spend too much energy to study "How many points
Draw to the same pixel.
In the same way, the math line has no width, but OpenGL's line is wide. Also, OpenGL's line must be a finite length
degrees, not as infinite as the mathematical concept. It can be argued that OpenGL's "straight line" concept is similar to the mathematically "line segment", which can be
Two endpoints to determine.
A polygon is a closed area formed by the end-to-end of multiple segments. OpenGL stipulates that a polygon must be a "convex polygon" (its
Righteousness: Any two points within the polygon are determined by the segments within the polygon, which can also be deduced that the convex polygon cannot be hollow. Multilateral
Shape can be determined by the end of its edge, which can be called a vertex. (Note: If the polygons used are not convex polygons, the last output
The effect is undefined--opengl for efficiency, which relaxes the check, which can cause display errors. To avoid this error, try to use
Triangles, because triangles are convex polygons)
As you can imagine, the dots, lines, and polygons can be combined into various geometries. Even, you can think of an arc as a lot of short
Straight segments that are short enough to be less than the width of one pixel in length. So arcs and circles can also be represented.
The By connecting small polygons in different planes, we can also form a "surface".
Ii. specifying vertices in OpenGL
As can be known from the above discussion, "point" is the basis of everything.
How do I specify a point? OpenGL provides a series of functions. They all start with Glvertex, followed by a number and a letter or two.
For example:
glvertex2d
glvertex2f
glvertex3f
Glvertex3fv
Wait a minute.
Numbers indicate the number of parameters, 2 means two parameters, 3 means three, 4 means four (I know a little wordy ~).
The letter represents the type of the parameter, s represents a 16-bit integer (this type is defined as Glshort in OpenGL).
I represents a 32-bit integer (this type is defined as glint and Glsizei in OpenGL).
F represents a 32-bit floating-point number (this type is defined as Glfloat and Glclampf in OpenGL),
D represents a 64-bit floating-point number (this type is defined as gldouble and GLCLAMPD in OpenGL).
V represents the way in which several parameters are passed using pointers, as shown in the following example.
These functions are identical except for the type and number of arguments. For example, the following five pieces of code are functionally equivalent:
(i) Glvertex2i (1, 3);
(ii) glvertex2f (1.0f, 3.0f);
(iii) glvertex3f (1.0f, 3.0f, 0.0f);
(d) glvertex4f (1.0f, 3.0f, 0.0f, 1.0f);
(v) glfloat vertexarr3[] = {1.0f, 3.0f, 0.0f};
GLVERTEX3FV (VERTEXARR3);
Later we will use glvertex* to represent this series of functions.
Note: Many of OpenGL's functions are in this form, with an identical prefix followed by a parameter description tag, which will follow the learning
Learning in depth and more experience.
Third, start drawing
Assuming that I have specified a number of vertices now, how does OpenGL know what I want to do with these vertices? is a painting of a
Come out, or even a line? Or form a polygon? Or do something else?
To solve this problem, OpenGL requires that the command that specifies the vertex must be included after the Glbegin function, before the Glend function (otherwise
The specified vertex is ignored). And by Glbegin to indicate how these points are used.
For example I write:
Glbegin (gl_points);
GLVERTEX2F (0.0f, 0.0f);
GLVERTEX2F (0.5f, 0.0f);
Glend ();
The two points will be drawn separately. If Gl_points is replaced with Gl_lines, then two points are considered to be two sides of a line
Point, OpenGL will draw a straight line.
We can also specify more vertices, and then draw more complex graphics.
On the other hand, Glbegin supported the way in addition to gl_points and Gl_lines, and Gl_line_strip,gl_line_loop,

Gl_triangles,gl_triangle_strip,gl_triangle_fan, etc., the approximate effect of each method is as follows:


Disclaimer: The picture is from www.opengl.org, which is the drawing of the book "OpenGL Programming Guide", due to the old version of the book (first edition,
1994) is already circulating in the network, I hope not to touch the copyright issue.
I am not prepared to make a glbegin in various ways. You can try to change the way of Glbegin and vertex position by yourself, creating
Some interesting patterns.
Program code:
void Mydisplay (void)
{
Glclear (Gl_color_buffer_bit);
Glbegin (/* Fill in your desired pattern here */);
/* Use the glvertex* series function here */
/* Specify the vertex position you want */
Glend ();
Glflush ();
}
Change the code to the way you like it, then replace the Mydisplay function in the first lesson with it and run it after compiling.
Two examples
Example one, draw a circle
/*
Positive quadrilateral, positive Pentagon, positive hexagon, ... until n-sided, and when n is larger, the shape becomes closer to the circle.
When n is large to a certain extent, the human eye will not be able to distinguish it from the real circle.
At this time we have successfully drawn a "circle"
(Note: There are many ways to draw a circle, which is relatively simple, but less efficient)
Try modifying the value of the const int n below to see how the output changes when different values such as n=3,4,5,8,10,15,20,30,50
Change Gl_polygon to Gl_line_loop, gl_points and other ways to observe the output
*/
#include <math.h>
const int n = 20;
Const Glfloat R = 0.5f;
Const Glfloat Pi = 3.1415926536f;
void Mydisplay (void)
{
int i;
Glclear (Gl_color_buffer_bit);
Glbegin (Gl_polygon);
for (i=0; i<n; ++i)
GLVERTEX2F (R*cos (2*pi/n*i), R*sin (2*pi/n*i));
Glend ();
Glflush ();
}
Example two, draw a pentagram
/*
The five vertex distribution positions of the pentagram are as follows:
A
E B
D C
First, the distance a from the center of the pentagram to the vertex is calculated according to the cosine theorem column equation.
(assuming the side length of the pentagram corresponds to the positive pentagon. 0)
A = 1/(2-2*cos (72*pi/180));
Then, based on the definition of sine and cosine, calculates the x-coordinate bx and y-coordinate by of B, and the y-coordinate of C
(assuming the center of the pentagram at the origin of the coordinates)
BX = a * cos (* pi/180);
by = a * sin (* pi/180);
cy =-A * cos (* pi/180);
The coordinates of the five points can be simply represented by the above four quantities and some constants.
*/
#include <math.h>
Const Glfloat Pi = 3.1415926536f;
void Mydisplay (void)
{
Glfloat A = 1/(2-2*cos (72*pi/180));
Glfloat bx = a * cos (* pi/180);
Glfloat by = a * sin (* pi/180);
glfloat cy =-A * cos (* pi/180);
Glfloat
Pointa[2] = {0, a},
Pointb[2] = {bx, by},
Pointc[2] = {0.5, cy},
Pointd[2] = { -0.5, cy},
Pointe[2] = {-bx, by};
Glclear (Gl_color_buffer_bit);
According to the order of A->c->e->b->d->a, you can draw the pentagram
Glbegin (Gl_line_loop);
GLVERTEX2FV (Pointa);
GLVERTEX2FV (POINTC);
GLVERTEX2FV (PointE);
GLVERTEX2FV (POINTB);
GLVERTEX2FV (POINTD);
Glend ();
Glflush ();
}
Example three, draw a graph of sine function
/*
Since OpenGL default coordinate values can only be from 1 to 1, (can be modified, but the method is left for later speaking)
So we set a factor factor, and we reduce all the coordinate values,
So we can draw more sine cycles.
Try modifying the value of the factor to observe the change
*/

Summary # include <math.h>
Const glfloat factor = 0.1f;
void Mydisplay (void)
{
Glfloat x;
Glclear (Gl_color_buffer_bit);
Glbegin (Gl_lines);
GLVERTEX2F ( -1.0f, 0.0f);
GLVERTEX2F (1.0f, 0.0f); Above two points can be drawn X axis
GLVERTEX2F (0.0f, -1.0f);
GLVERTEX2F (0.0f, 1.0f); Two points above can draw Y axis
Glend ();
Glbegin (Gl_line_strip);
for (x=-1.0f/factor; x<1.0f/factor; x+=0.01f)
{
glvertex2f (X*factor, sin (x) *factor);
}
Glend ();
Glflush ();
}
This lesson describes the concepts of points, lines, and polygons, and how to use OpenGL to describe points and use points to describe the geometry.
Everyone can play their own imagination, draw a variety of geometric figures, of course, you can also use Gl_line_strip to place a lot of similar points
Then, make up the function image. If you are interested, you can also find some more beautiful image functions, do it yourself, with OpenGL to
Draw it.

OpenGL Getting Started Tutorial 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.