OpenGL implements two-dimensional graphics and three-dimensional graphics

Source: Internet
Author: User

OpenGL is a powerful underlying graphics library whose commands were originally implemented using C language. OpenGL defines a graphical program interface, often used for processing three-dimensional images, powerful, easy to call, in the image processing is very popular.

One of the main uses of OpenGL is a toolkit for implementing graphics: GLUT.

GLUT (pronounced like the GLUT in gluttony) is the OpenGL Utility Toolkit, a window System independent t Oolkit for writing OpenGL programs. It implements a simple Windowing application programming Interface (API) for OpenGL.

GLUT is designed for constructing small to medium sized OpenGL programs. While GLUT are well-suited to learning OpenGL and developing simple OpenGL applications, GLUT are not a full-featured Toolki T so large applications requiring sophisticated user interfaces is better off using native window system toolkits like Mo Tif. GLUT is simple, easy, and small.

Glut may not be fully functional for large projects, it is designed for some small to medium-sized OpenGL projects.

1. The implementation of two-dimensional image is relatively simple, directly under the code:

#include <gl/glut.h>void Rectangle (void) {
The function of GL begins with the standard function of OpenGL//(using the value of the current buffer) to clear the specified buffer glclear (gl_color_buffer_bit);
Draw Rectangle//GLRECTF ( -0.5f, -0.5f, 0.5f, 0.5f);//Draw straight glbegin (gl_lines); glvertex2f (0.5f, 0.5f); glvertex2f ( -0.5f, -0.5f); Glend ();//refresh buffer to ensure that the drawing command can be executed Glflush ();} int main (int argc, char *argv[]) {//Initialize glut Libraryglutinit (&ARGC, argv);//Initialize the size of the window glutinitwindowsize (300, 300) ; Sets the position where the window appears//glutinitwindowposition (int x, int y),//INIT program Presentation mode Glutinitdisplaymode (GLUT_RGBA); Glutcreatewindow (" Project of OpenGL ")//win: Specifies the child window's parent window//glutcreatesubwindow (int win, int x, int y, int width, int height);//Set function callback for the current window for drawing Glutdisplayfunc (&rectangle);//glut event loop, otherwise no graphics effect (Flash over) glutmainloop (); return 0;}

Implementation results:

You can also draw a circle in the window:

#include <math.h>
Glfloat r = 0.5f; Glfloat PI = 3.141592653f;int pre = 30;glbegin (Gl_polygon);//Draw polygon for (int i = 0; i < pre; i++) {glvertex2f (R * COS (2 * PI*I/PRE), R * Sin (2 * pi*i/pre));} Glend ();

In fact, the circle is not a circle, but a polygon approximation. Gl_polygon is used to draw polygons, the number of edges reaches a certain level, and the shape shown is approximate to the circle.

  

2. Next, realize the three-dimensional image cube.

Three-dimensional transformation:

Model View Transformation (Gl_modelview): From the point of view of "relative movement", it is equivalent to change the position and direction of the observer and change the position and direction of the object itself.

Perspective projection Transformation (gl_projection): Defines a visual space in which objects outside the visual space are not drawn to the screen.

Viewport Transformations (Glviewport): Typically, the program default pixel fills the entire window, and the viewport transform sets the pixel's fill on the window.

Each time the object is transformed, it is necessary to set the matrix of the current operation as a matrix of some transformation, and the matrix must be converted to the unit matrix before the transformation can be manipulated.

Glmatrixmode (gl_projection);
Glloadidentity ();

Full code:

#include <GL/glut.h> #include <math.h>void setcube (void) {Glclearcolor (1.0, 1.0, 1.0, 0.0); Glclear (Gl_ Color_buffer_bit); glcolor3f (0, 0, 0);//Set Black glloadidentity ()//load Unit matrix Glulookat (6, 0, 2.5, 0, 0, 0, 1, 1, 0);// The first three parameters set the observer's observation position, the three parameters set the observer position, and the last three parameters set the observer's viewing direction gllinewidth (2.0f);//Set the pixel width of the edge, the default is 1.0fglutWireCube (2.0); Glflush ();} void Drawcube (void) {Glclearcolor (1.0, 1.0, 1.0, 0.0); Glclear (gl_color_buffer_bit); glloadidentity (); Glulookat (4, 0, 1.5, 0, 0, 0, 1, 1, 0);//Draw the face of the cube glcolor3f (0, 1, 0); Glbegin (gl_quads);//---1---glnormal3f (-1, 0, 0);//set point normal vector glvertex3f ( 0.5, 0.5, 0.5); glvertex3f (0.5, -0.5, 0.5); glvertex3f (0.5, -0.5, -0.5); glvertex3f (0.5, 0.5, -0.5);//---2---glnormal3f (- 1, 0, 0); glvertex3f ( -0.5, 0.5, 0.5); glvertex3f ( -0.5, -0.5, 0.5); glvertex3f ( -0.5, -0.5, -0.5); glvertex3f (-0.5, 0.5,-0.5) ;//---3---glnormal3f (0, 1, 0); glvertex3f (0.5, 0.5, 0.5); glvertex3f ( -0.5, 0.5, 0.5); glvertex3f (-0.5, 0.5,-0.5); glvertex3f (0.5, 0.5, -0.5);//---4---glnormal3f (0,-1, 0); glvertex3f (0.5, -0.5, 0.5); glvertex3f ( -0.5, -0.5, 0.5); glvertex3f ( -0.5, -0.5, -0.5); glvertex3f (0.5, -0.5, -0.5);//---5---glnormal3f (0, 0, 1); glvertex3f (0.5, 0.5, 0.5); glvertex3f ( -0.5, 0.5, 0.5), glvertex3f ( -0.5, -0.5, 0.5), glvertex3f (0.5, -0.5, 0.5);//---6--- glnormal3f (0, 0,-1); glvertex3f (0.5, 0.5, 0.5); glvertex3f ( -0.5, 0.5, 0.5); glvertex3f ( -0.5, -0.5, 0.5); glvertex3f (0.5,- 0.5, 0.5); Glend ()//drawglcolor3f (0, 0, 0); Gllinewidth (2.0f);//Draw the side Glbegin (gl_lines) of the cube,//---1---glvertex3f (0.5, 0.5, 0.5); glvertex3f ( -0.5, 0.5, 0.5); glvertex3f ( -0.5, 0.5, 0.5); glvertex3f ( -0.5, -0.5, 0.5); glvertex3f (-0.5,-0.5, 0.5) ; glvertex3f (0.5, -0.5, 0.5); glvertex3f (0.5, -0.5, 0.5), glvertex3f (0.5, 0.5, 0.5);//---2---glvertex3f (0.5, 0.5,-0.5); glvertex3f ( -0.5, 0.5, -0.5); glvertex3f ( -0.5, 0.5, -0.5); glvertex3f ( -0.5, -0.5, -0.5); glvertex3f (-0.5,-0.5,-0.5); glvertex3f (0.5, -0.5, -0.5); glvertex3f (0.5, -0.5, -0.5); glvertex3f (0.5, 0.5, -0.5);//---3---glvertex3f (0.5, 0.5, 0.5); glvertex3f (0.5, -0.5, 0.5); glvertex3f (0.5, -0.5, 0.5); glvertex3f (0.5,-0.5, -0.5); glvertex3f (0.5, -0.5, -0.5); glvertex3f (0.5, 0.5, -0.5); glvertex3f (0.5, 0.5, -0.5); glvertex3f (0.5, 0.5, 0.5) ;//---4---glvertex3f ( -0.5, 0.5, 0.5); glvertex3f ( -0.5, -0.5, 0.5); glvertex3f ( -0.5, -0.5, 0.5); glvertex3f (-0.5,-0.5,- 0.5); glvertex3f ( -0.5, -0.5, -0.5); glvertex3f ( -0.5, 0.5, -0.5); glvertex3f ( -0.5, 0.5, -0.5); glvertex3f (-0.5, 0.5, 0.5); Glend (); Glflush ();} void ThreeD (int w, int h) {glviewport (0, 0, (Glsizei) W, (Glsizei) h);//Resize view window size Glmatrixmode (gl_projection); Glloadidentity (); Glfrustum ( -1.0, 1.0, -1.0, 1.0, 2.0, 10.0); Glmatrixmode (Gl_modelview);} int main (int argc, char *argv[]) {//Initialize glut Libraryglutinit (&AMP;ARGC, argv);//Initialize the size of the window Glutinitwindowsize (500, 500) ; Sets the position where the window appears//glutinitwindowposition (int x, int y),//INIT program Presentation mode Glutinitdisplaymode (GLUT_RGBA); Glutcreatewindow (" Project of OpenGL ");
Set function callback for the current window for drawing Glutdisplayfunc (Drawcube),//function Glutreshapefunc (threed) When window changes, Glutmainloop (); return 0;}

You can call OpenGL's Glutwirecube (glfloat size) to draw a cubic square, such as a setcube function.

I want to implement a green cube with black edges, and the cube must be drawn, such as the Drawcube function.

Implementation results:

  

  

OpenGL implements two-dimensional graphics and three-dimensional graphics

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.