Ubuntu + opengl programming (4) use double easing

Source: Internet
Author: User

Ubuntu + opengl programming (4) use double easing


-- Lihn1987)



Haha, I want to highlight the uploaded code with the syntax. But how can I do this?


Because the editor I used to edit and upload files to a blog is

Openoffice, and the code editing tool I use is

VIM

In VIM, I set syntax highlighting, so I

VIM uses a command

Tohtml

So it is converted.

Source code of the html Version, highlighted by syntax


Then, because

Openoffice is supported.

The html format is copied.

Copy html to pull it !!! Wow ~~~~~~


 

What we need to do this time is a dual-Cache proxy that will move,

2D,

Opengl, the red small blocks will keep moving.


:


Show Code:


 

# Include
<GL/glut. h>

# Include
<Stdlib. h>

# Include
<Stdio. h>

// Initial square position and size

GLfloat x = 0.0f
;
GLfloat y = 0.0f
;
GLfloat rsize = 25
;

// Step size in x and y directions ctions

// (Number of pixels to move each time)

GLfloat xstep = 1.0f
;
GLfloat ystep = 1.0f
;

// Keep track of windows changing width and height

GLfloat implements wwidth;
GLfloat running wheight;

//////////////////////////////////////// ///////////////////

// Called to draw scene

Void
RenderScene (void
)
{
// Clear the window with current clearing color

GlClear (GL_COLOR_BUFFER_BIT );

// Set current drawing color to red

// R G B

GlColor3f (1.0f
, 0.0f
,
0.0f
);

// Draw a filled rectangle with current color

Glrectf (X, Y, x + rsize, Y-rsize );

// Flush drawing commands and swap

Gluswapbuffers ();
}

//////////////////////////////////////// ///////////////////

// Called by glut library when idle (Window not
Being

// Resized or moved)

Void
Timerfunction (int
Value)
{
// Reverse direction when you reach left or right
Edge

If
(X> repeated wwidth-rsize | x <
-Required wwidth)
Xstep =-xstep;

// Reverse direction when you reach top or bottom
Edge

If
(Y> running wheight | Y <
-Windowheight + rsize)
Ystep =-ystep;

// Actually move the square

X + = xstep;
Y + = ystep;

// Check bounds. This is in case the window is
Made

// Smaller while the rectangle is bouncing and


// Rectangle suddenly finds itself outside the new

// Clipping volume

If
(X> (required wwidth-rsize + xstep ))
X = windowWidth-rsize-1
;
Else
If
(X <
-(Required wwidth + xstep ))
X =-descriwwidth-1
;

If
(Y> (windowheight + ystep ))
Y = windowHeight-1
;
Else
If
(Y <
-(Windowheight-rsize + ystep ))
Y =-running wheight + rsize-1
;

// Redraw the scene with new coordinates

Glupostredisplay ();
Glutimerfunc (33
, Timerfunction, 1
);
}

//////////////////////////////////////// ///////////////////

// Setup the rendering state

Void
Setuprc (void
)
{
// Set clear color to blue

Glclearcolor (0.0f
, 0.0f
,
1.0f
, 1.0f
);
}

//////////////////////////////////////// ///////////////////

// Called by glut library when the window has
Chanaged size

Void
Changesize (int
W, int
H)
{
GLfloat aspectRatio;

// Prevent a divide by zero

If
(H = 0
)
H = 1
;

// Set Viewport to window dimensions

GlViewport (0
, 0
,
W, h );

// Reset coordinate system

GlMatrixMode (GL_PROJECTION );
GlLoadIdentity ();

// Establish clipping volume (left, right, bottom,
Top, near, far)

AspectRatio = (GLfloat) w/(GLfloat) h;
If
(W <= h)
{
Required wwidth = 100
;
Required wheight = 100
/AspectRatio;
GlOrtho (-100.0)
, 100.0
,
-Average wheight, average wheight, 1.0
,-1.0
);
}
Else

{
Required wwidth = 100
* Aspectratio;
Required wheight = 100
;
Glortho (-faster wwidth, faster wwidth,-100.0
,
100.0
, 1.0
,-1.0
);
}

Glmatrixmode (gl_modelview );
Glloadidentity ();
}

//////////////////////////////////////// ///////////////////

// Main program entry point

Int
Main (int

Argc, char
* Argv [])
{
Gluinit (& argc, argv );
Fig );
Gluinitwindowsize (800
, 600
);
Glucreatewindow ("Bounce"
);
Gludisplayfunc (renderscene );
Glureshapefunc (changesize );
Glutimerfunc (33
, Timerfunction, 1
);

Setuprc ();

Glumainloop ();

Return
0
;
}

Most of the Code is compared with the previous one.
Is the same, for example
Changesize
Function,
Renderscene
Function A has many similarities, but first we will introduce the changes in most of these identical parts.

 

First

Gluinitdisplaymode (glu_double
| Glu_rgba );

This sentence

Previously we used

Gluinitdisplaymode (glu_single
| Glu_rgb
);

According to our understanding, the changed parameters
Glu_double
This means to use dual buffering instead of slave
Before the single buffer.

What are the meanings of these functions?
I think it is necessary to tell you how to check
Opengl official description of it.

Here
Http://www.opengl.org/sdk/docs/man/is
OpenGL 2.1 Reference Pages

 

 

Another function

RenderScene

This function is used to initialize the current interface.
In this scenario, when we use dual-cache, there are also some differences in the plot functions.

Used

GlFlush ()
;
Changed

Gluswapbuffers ()

The following is an explanation on the official website.


Gluswapbuffers
Swaps
The buffers ofCurrent window
If double buffered.

Usage

void glutSwapBuffers(void);

Description

Performs a buffer swap onLayer in use
ForCurrent
Window

. Specifically,Gluswapbuffers

Promotes the contents of the back buffer ofLayer in use
Of
TheCurrent window
To become the contents of the front buffer.
The contents of the back buffer then become undefined. The update
Typically takes place during the vertical retrace of the monitor, rather
Than immediately after fig is called.


An implicit glflush is done by FIG
Before it returns. Subsequent OpenGL commands can be issued immediately
After calling fig, but are not executed until the buffer
Exchange is completed.

If the layer in use is not double buffered, gluswapbuffers has no
Effect

Specifically

This function is valid only when dual buffering is used.
Number is used to display the content on the back of the current buffer.
Glflash
Refresh, but an implicit call is made before the function is returned.

 

In addition, we also want to see a response
Call Functions


Void
Glutimerfunc (unsigned int msecs, void (* Func) (int value), int value );

With the intuition of our programmers, we should be able to think that this function is a timer,

The function is
Msecs
Call in milliseconds
Func
Function, and
Func
Parameter value passed in by the function
Value

 

Let's look at the timer callback function.

TimerFunction

This function moves the red rectangle over time.

An unknown function is called.

Glupostredisplay
It actually tells the operating system that the region needs to be re-painted

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.