About OpenGL vsync (OpenGL vertical synchronization)

Source: Internet
Author: User

 

1) wgl_ext_swap_control extension is relatively simple to implement vertical synchronization:

 

// In wglext. h

# Ifndef wgl_ext_swap_control
# Define wgl_ext_swap_control 1
# Ifdef wgl_wglext_prototypes
Extern bool winapi wglswapintervalext (INT );
Extern int winapi wglgetswapintervalext (void );
# Endif/* wgl_wglext_prototypes */
Typedef bool (winapi * pfnwglswapintervalextproc) (INT interval );
Typedef int (winapi * pfnwglgetswapintervalextproc) (void );
# Endif

// Our source code

# Include <Gl/wglext. h>

Pfnwglswapintervalextproc wglswapintervalext;
Pfnwglgetswapintervalextproc wglgetswapintervalext;

 

// After OpenGL render context being created

Wglswapintervalext = (pfnwglswapintervalextproc) wglgetprocaddress ("wglswapintervalext ");
Wglgetswapintervalext = (pfnwglgetswapintervalextproc) wglgetprocaddress ("wglgetswapintervalext ");

Wglswapintervalext (1 );
Int vsync_count = wglgetswapintervalext (); // if the number of vsync_count and wglswapintervalext is different, vsync is ineffective.

// In OpenGL extensions

Http://www.opengl.org/registry/specs/EXT/wgl_swap_control.txt

 

Overview

This extension allows an application to specify a minimum Periodicity
Of Color buffer swaps, measured in video frame periods.

This extension allows OpenGL applicationsProgramSpecify the minimum cycle of a flipped color cache, which is measured by the video refresh cycle.

A video frame period is the time required by the monitor to display
Full Frame of video data. In the case of an interlaced monitor,
This is typically the time required to display both the even and odd
Fields of a frame of video data.

The video refresh cycle is the time required for the video monitor to display the data of a complete frame of the image. For the monitor with the line scan method, this time is the total time of the odd and even fields.

In addition, it is said on the Internet that all n cards support this extension, while a card and 3dlabs do not support this extension. I have not verified this statement, but wgl_ext_swap_control does belong to "vendor extension ", instead of "ARB extension ".

Difficult-to-secure video cards do not support this function. Another safer method is to use DirectDraw... however, we don't seem to like the MS DirectX series for OpenGL, but this is indeed a good method:

1) Use DirectDraw for Vertical synchronization:

# Include <ddraw. h>
# Pragma comment (Lib, "ddraw. lib ")

 

// Before create OpenGL render Context

Hresult hR = directdrawcreateex (null, (void **) & m_pdd, iid_idirectdraw7, null );

Ddsurfacedesc2 ddsd;
Ddscaps2 ddscaps;

M_pdd-> setcooperativelevel (m_hwnd, ddscl_normal );
 
Memset (& ddsd, 0, sizeof (ddsd); // set all fields of struct to 0 and set. dwsize
Ddsd. dwsize = sizeof (ddsd); // sizeof the variable-these two steps required for most ddraw structs
Ddsd. dwflags = ddsd_caps; // set flags for variables we're using...
Ddsd. ddscaps. dwcaps = ddscaps_primarysurface; // set the variables we said we wowould in dwflags
HR = m_pdd-> createsurface (& ddsd, & m_pddsprimary, null );

// On Draw
M_pdd-> waitforverticalblank (ddwaitvb_blockbegin, null );
Swapbuffers (m_hdc );

 

// On Destroy

If (m_pddsprimary)
M_pddsprimary-> release ();
M_pddsprimary = NULL;

If (m_pdd)
M_pdd-> release ();
M_pdd = NULL;

 

This method not only supports vertical synchronization, but also allows OpenGL full screen programs to exclusively occupy the screen.

 

 

 

 

 

 

 

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.