Linux OpenGL Practice Chapter -3 Framebuffer

Source: Internet
Author: User

Glew description

Glew (OpenGL Extension Wrangler) is another auxiliary library of OpenGL that encapsulates the process of getting function addresses from the OpenGL library, and also includes some OpenGL programming methods that can be used across platforms.

This practice is to use the data cache to draw two triangles, focusing on the creation of the cache and data entry. After the data is entered, it can be divided into non-index-based drawing and index-based drawing according to the data usage method, which are Gldrawarray and gldrayelements respectively.

First, clear the OpenGL cache usage steps:

    1. Glgenbuffer
    2. Glbindbuffer
    3. Glbufferdata
    4. Glvertexattribpointer
    5. Glbindvertexarray
    6. Gldrawarrays

To put it simply: Create the cache first, then enter the cached data, and then draw. Here is a description of the relevant API:

Create cache

Glgenbuffer (glsizei N, gluint* buffers);//create n Cache object, save in buffers array

Entering or outputting data to the cache

Glbindbuffer (glenum target, gluint buffer); Bind cache to target specified cache binding point

Glbufferdata (glenum target, glsizeiptr size, const glvoid* data, glenum usage); Allocates a size storage space for the cache object bound to target, and if data is not NULL, the allocated storage space is initialized with data. Usage indicates that the data in the cache may have some specific uses.

Glbuffersubdata (glenum target, glsizeiptr offset, glsizeiptr size, const glvoid* data);//Replace some of the data in the cache with new data

Clear Data

Glclearbufferdata (glenum target, Glenum internalformat, glenum format, glenum type, const glvoid* data); Using data to populate the cache storage space, format and type specify data formats and types that need to be converted to Internalformat before populating

Glclearsubbufferdata (glenum target, glenum Internalformat, glsizeiptr offset, glsizeiptr size, glenum format, Glenum type , const glvoid* data); As above, the difference is that this method only fills the area specified by offset and size;

Set vertex properties

Glvertexattribpointer (gluint index, glint size, glenum type, Glboolean normalized, Glsizei stride, const glvoid* pointer);

Draw

Gldrawarrays (glenum mode, glint first, glsizei count);//Direct read vertex data drawing

Gldrawelements (glenum mode, Glsizei count, glenum type, const glvoid* indices); Draw based on index (that is, all vertices are in an array, using an index to refer to the vertex data)

The following is an example of drawing two triangles based on the API described above.

#include <GL/glew.h> #include <GL/freeglut.h> #include <stdio.h> #include "ShaderHelper.h" #define Buffer_offset (n) ((glvoid*) n) enum Vao_ids {triangles,numvaos};enum buffers_ids {ArrayBuffer, numbuffers};enum Attrib_ IDs {vposition=0}; Gluint Vaos[numvaos]; Gluint buffers[numbuffers];const gluint numvertices=6;void init (); void display (); int main (int argc,char* argv[]) {g        Lutinit (&AMP;ARGC,ARGV);        Glutinitcontextversion (3,3);        Glutinitcontextprofile (Glut_core_profile); Glutinitdisplaymode (Glut_rgb |        Glut_single);        Glutinitwindowposition (0,0);        Glutinitwindowsize (300,300); Glutcreatewindow ("Frame Buffer");/* Const glubyte* name = glgetstring (Gl_vendor); Returns the name of the manufacturer responsible for the current OpenGL implementation of const glubyte* Biaoshifu = glgetstring (gl_renderer); Returns a renderer identifier, usually a hard > piece Platform const glubyte* openglversion =glgetstring (gl_version); Returns the version number of the current OpenGL implementation Const glubyte* gluversion= glugetstring (glu_version); Returns the current Glu ToolPak version of the PRINTF ("OpenGL implementation Vendor Name:%s\n", name);        printf ("Renderer identifier:%s\n", Biaoshifu);         printf ("Oopengl implementation version number:%s\n", openglversion);         printf ("Oglu Tool Library version:%s\n", gluversion); */glewexperimental=gl_true;         Glewinit ();         Init ();          Glutdisplayfunc (display);         Glutmainloop (); return 0;         } void Init () {glclearcolor (0.0,0.0,0.0,1.0);         Glmatrixmode (gl_projection);         Glortho ( -5,5,-5,5,5,15);         Glmatrixmode (Gl_modelview);          Glulookat (0,0,10,0,0,0,0,1,0);         Glgenvertexarrays (Numvaos,vaos);          Glbindvertexarray (Vaos[triangles]); Glfloat vertices[numvertices][2]={{ -0.90,-0.90}, {0.85,-0.90}, {-0.90,          0.85}, {0.90,-0.85}, {0.90, 0.90}, {-0.85, 0.90},};         Glgenbuffers (numbuffers,buffers);         Glbindbuffer (Gl_array_buffer,buffers[arraybuffer]); Glbufferdata (Gl_array_buffer,sizeof (vertices), vertices,gl_static_draw); Shaderinfo shaders[] = {{Gl_vertex_shader, "Triangles.vert"}, {Gl_fragment_shader, "Triangl        Es.frag "}, {gl_none,null},};        Gluint program = loadshaders (shaders);        Gluseprogram (program);        Glvertexattribpointer (Vposition,2,gl_float,gl_false,0,buffer_offset (0)); Glenablevertexattribarray (vposition);}        void display () {glclear (gl_color_buffer_bit);        Glbindvertexarray (Vaos[triangles]);        Gldrawarrays (gl_triangles,0,numvertices); Glflush ();}

Note: The code is written according to Red Book, some auxiliary code (such as Loadshader) is not posted, please refer to www.opengl-readbook.com for details.

The final effect is as follows:

Linux OpenGL Practice Chapter -3 Framebuffer

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.