Glsl shader (C language)

Source: Internet
Author: User

basic.vert
#version 400layout(location =0) in vec3 VertexPosition;layout(location =1) in vec3 VertexColor;out vec3 Color;uniform mat4 Matrix;void main(){    Color= VertexColor;    gl_Position= Matrix *vec4(VertexPosition,1.0);}
Basic. frag
#version 400    in vec3 Color;    out vec4 FragColor;    void main()  {      FragColor = vec4(Color,1.0);  }  


Main Program main. c
# Include
  
   
# Include
   
    
# Include
    
     
# Include "readtext. h" # ifdef _ APPLE __# include
     
      
# Else # define FREEGLUT_STATIC # include
      
       
# Endif # pragma comment (lib ,".. /readtext. lib ") # pragma comment (lib ,".. /glew32.lib ") GLuint vShader, fShader, programHandle; int infologLength = 0; int charsWritten = 0; char * infoLog; // float positionData array [] = {-1.0f, -1.0f, 0.0f, 1.0f,-1.0f, 0.0f, 0.0f, 1.0f, 0.0f}; // color array float colorData [] = {0.2f, 0.8f, 0.0f, 0.4f, 0.6f, 0.0f, 0.1f, 0.9f, 1.0f}; float matrix [16]; matrix [16] = {0.5, 0.0,-0.86 6, 0.0, 0.0, 1.0, 0.0, 0.0, 0.866, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0 }; /// // transformation matrix ///////////////////// //// // cos (angle ), 0,-sin (angle), // column vector // 0, 1, 0, // sin (angle), 0, cos (angle) // GLint matrixLocation; GLuint vaoHandle; // vertex array object void initShader () {char * renderer, * vender, * version, * glslVersion; char * vs, * fs; char * vv, * ff; // check version renderer = glGe TString (GL_RENDERER); vender = glGetString (GL_VENDOR); version = glGetString (GL_VERSION); glslVersion = glGetString (GL_SHADING_LANGUAGE_VERSION); printf ("% s \ n", renderer ); printf ("% s \ n", vender); printf ("% s \ n", version); printf ("% s \ n", glslVersion ); // The Shadow processes vShader = glCreateShader (GL_VERTEX_SHADER); vs = readText (".. /basic. vert "); printf (" % s \ n ", vs); if (vShader = 0) {printf (" fail to cre At vertex shader "); exit (1) ;}// associate source code with the shader vv = vs; glShaderSource (vShader, 1, & vv, NULL ); glCompileShader (vShader); free (vs); // check whether the compilation is successful. glGetShaderiv (vShader, GL_INFO_LOG_LENGTH, & infologLength); if (infologLength> 0) {infoLog = (char *) malloc (infologLength); glGetShaderInfoLog (vShader, infologLength, & charsWritten, infoLog); printf ("% s \ n", infoLog); free (infoLog );} /////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //// // FShader = glCreateShader (GL_FRAGMENT_SHADER ); if (fShader = 0) {printf ("fail to creat shader \ n"); exit (1) ;}fs = readText (".. /basic. frag "); printf (" % s \ n ", fs); ff = fs; glShaderSource (fShader, 1, & ff, NULL); glComp IleShader (fShader); free (fs); // check whether the compilation is successful glGetShaderiv (fShader, GL_INFO_LOG_LENGTH, & infologLength); if (infologLength> 0) {infoLog = (char *) malloc (infologLength); glGetShaderInfoLog (fShader, infologLength, & charsWritten, infoLog); printf ("% s \ n", infoLog); free (infoLog );} //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //// //////////////////////////////////////// ///////// Create the program object programHandle = glCreateProgram (); glAttachShader (programHandle, vShader); glAttachShader (programHandle, fShader); glLinkProgram (programHandle); glGetProgramiv (programHandle, handler, & infologLength); if) {infoLog = (char *) malloc (infologLength); glGetProgramInfoLog (programHandle, infologLength, & charsWritten, I NfoLog); printf ("% s \ n", infoLog); free (infoLog);} glUseProgram (programHandle);} void initVBO () {// Create and populate the buffer objects GLuint vboHandles [2]; GLuint positionBufferHandle, colorBufferHandle; glGenBuffers (2, vboHandles); positionBufferHandle = vboHandles [0]; colorBufferHandle = vboHandles [1]; // bind VBO for glBindBuffer (GL_ARRAY_BUFFER, positionBufferHandle); // load data to VBO glBufferData (GL _ ARRAY_BUFFER, 12 * sizeof (float), positionData, buffers); // bind a VBO to use buffers (GL_ARRAY_BUFFER, colorBufferHandle); // load data to VBO glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), colorData, sums); sums (1, & vaoHandle); glBindVertexArray (vaoHandle); glablevertexattribarray (0); // vertex coordinate glablevertexattribarray (1 ); // vertex color // before calling glVertexAttribPointer, You need to bind glBindBuffer (GL_AR RAY_BUFFER, positionBufferHandle); random (0, 3, GL_FLOAT, GL_FALSE, 0, NULL); glBindBuffer (GL_ARRAY_BUFFER, colorBufferHandle); random (1, 3, GL_FLOAT, GL_FALSE, 0, NULL);} void init () {// initialize the glew extension library glewInit (); initShader (); initVBO (); glClearColor (0.0, 0.0, 0.0, 0.0 ); // glShadeModel (GL_SMOOTH);} void display () {glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_TEST); // draw 1 Triangles (using common methods) // glBegin (GL_TRIANGLES); // glColor3f (0.0f, 1.0f, 0.0f); // glVertex3f (0.0f, 1.0f, 0.0f ); // glColor3f (0.0f, 1.0f, 0.0f); // glVertex3f (-1.0f,-1.0f, 0.0f); // glColor3f (0.0f, 0.0f, 1.0f ); // glVertex3f (1.0f,-1.0f, 0.0f); // glEnd (); // use VAO and VBO to draw glBindVertexArray (vaoHandle); glDrawArrays (GL_TRIANGLES ); glBindVertexArray (0); // transfer matrix. MatrixLocation = glGetUniformLocation (programHandle, "Matrix"); glUniformMatrix4fv (matrixLocation, 1, GL_TRUE, matrix); glswapbuffers (); glupostredisplay ();} int main (int argc, char ** argv) {gluinit (& argc, argv); gluinitdisplaymode (glu_double | glu_rgb); gluinitwindowsize (600,600); gluinitwindowposition (100,100); glucreatewindow ("GLSL Test: draw a triangle "); init (); fig (display); fig (); return 0 ;}
      
     
    
   
  
The library used can be downloaded on the my resources page!

Related Article

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.