Using shaders in OpenGL

Source: Internet
Author: User



using the GLSL shader step in OpenGL
The GLSL is suitable for both vertex shaders and fragment shaders.
to use a shader object:

1. Create a Shader object:
Gluint glcreateshader (glenum type);
Creates a shader object, the type value must be Gl_vertex_shader or Gl_fragment_shader. Error returns 0
2. The source of the shader is associated with the shader object:
Glshadersource (Gluint shader, Glsizei count, const glchar** string, const glint* length);
3, compile the source of the shader object:
void Glcompileshader (Gluint shader);
Glgetshaderiv (Gl_cimpile_status); Query compilation results
Glgetshaderinfolog (Gluint shader, Glsizei bufSize, Glsizei *length, char* infoLog);
4. Create an empty shader program:
Gluint Glcreateprogram (); If error, return 0;
5. Connect the shader object to the shader program:
Glattachshader (gluint program, gluint Shader); Gldetachshader (program, shader);
6. Link the objects connected to the shader program into an executable program:
Voi Gllinkprogram (gluint program);
GLGETPROGRAMIV (Gl_link_status); Query link results, Gl_true represent success
Glgetprograminfolog (Gluint shader, Glsizei bufSize, Glsizei *length, char* infoLog);
7. Start the vertex or fragment shader program:
void Gluseprogram (Gluint program); Using the program's object handle as a parameter
eg

Gluint Shader,program; Gluint compiled, linked;const glchar* shadersrc[] = {"void main () {" "Gl_position = Gl_modelviewprojectionmatrix *gl_vertex; "}"}; Shader = Glcreateshader (Gl_vertex_shader); Glshadersource (shader, 1, shadersrc, NULL); Glcompileshader (shader);    Glgetshaderinfolog (shader, Gl_compile_status, &compiled); if (!compiled) {glint length;    glchar* log;    Glgetshaderiv (shader, Gl_info_log_length, &length);    Log = (glchar*) malloc (length);    Glgetshaderinfolog (shader, length, &length, log);    fprintf (stderr, "compile log = '%s ' \ n", log); Free (log);} Program =glcreateprogram (); Glattachshader (program, shader); Gllinkprogram (program); GLGETPROGRAMIV (program, Gl_link _status, &linked); if (linker) {Gluseprogram (program);}    else{glint length;    glchar* log;    GLGETPROGRAMIV (program, Gl_info_log_length, &length);    Log = (glchar*) malloc (length);    Glgetprograminfolog (program, length, &length, log); fprintf (stderr, "link log = '%s ')\ n ", log); Free (log);}

To delete a shader:

void Gldeleteshader (Gluint shader); If it is being used, it is marked as deleted and deleted after use.
Determine if the shader program name is valid:
Glboolean Glisprogram (gluint program); Gl_true
Verify that a shader can be executed in the current OpenGL state:
void Glvalidateprogram (Gluint program);

If validation passes, Gl_validate_status is set to Gl_true, calling GLGETPROGRAMIV () to query the value of Gl_validate_status.



Using shaders in OpenGL

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.