GLSL perspective matrix (c)

Source: Internet
Author: User

Perspective. vert

#version 330layout(location =0) in vec4 vertexPosition;layout(location =1) in vec4 vertexColor;smooth out vec4 theColor;uniform vec2 offset;uniform mat4 perspectiveMatrix;void main(){vec4 cameraPosition =vertexPosition +vec4(offset.x, offset.y, 0.0, 0.0);gl_Position =perspectiveMatrix *cameraPosition;theColor =vertexColor;}

Perspective. frag

#version 330smooth in vec4 theColor;out vec4 outputColor;void main(){outputColor =theColor;}

Perspective. c

//////////////////////////////////////// //// // View version 2: matrix implementation. // Call the library vmath // Author: blue silk cream ////////////////////////////////////// /// // # include
 
  
# Include
  
   
# Include
   
    
# Include "readtext. h" # include "vmath. h" # ifdef _ APPLE _ # include
    
     
# Else # define FREEGLUT_STATIC # include
     
      
# Endif # pragma comment (lib, "readtext. lib ") # pragma comment (lib," vmath. lib ") int I; GLuint vShader, fShader, programHandle; GLint vaoHandle, vboHandles [2], callback, colorBufferHandle; Matrix44f M; // projection matrix GLint handler; //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /// //////////////////////////////////////// //// // Int infoLogLength = 0; int charWritten = 0; char * infoLog; //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /// // float positionData [] = {0.25f, 0.25f,-1.25f, 1.0f, 0.25f,-0.25f,-1.25f, 1.0f,-0.25f, 0.25f,-1.25f, 1.0 F, 0.25f,-0.25f,-1.25f, 1.0f,-0.25f,-0.25f,-1.25f, 1.0f,-0.25f, 0.25f,-1.25f, 1.0f, 0.25f, 0.25f,-2.75f, 1.0f,-0.25f, 0.25f,-2.75f, 1.0f, 0.25f,-0.25f,-2.75f, 1.0f, 0.25f,-0.25f,-2.75f, 1.0f,-0.25f, 0.25f,-2.75f, 1.0f,-0.25f,-0.25f,-2.75f, 1.0f,-0.25f, 0.25f,-1.25f, 1.0f,-0.25f,-0.25f,-1.25f, 1.0f,-0.25f,-0.25f, -2.75f, 1.0f,-0.25f, 0.25f,-1.25f, 1.0f,-0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, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0 F, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f }; //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //// // void SetupShader () {char * render Er; char * vender; char * version; char * glslVersion; // char *, * fs; char * vv, * ff; // renderer = glGetString (GL_RENDERER ); vender = glGetString (GL_VENDOR); version = glGetString (GL_VERSION); glslVersion = glGetString (GL_SHADING_LANGUAGE_VERSION ); printf ("************************************* * *************** \ n "); printf ("graphic card: \ t % s \ n", renderer); printf ("graphic comp Any: \ t % s \ n ", vender); printf (" openGL version: \ t % s \ n ", version); printf (" GLSL version: \ t % s \ n ", glslVersion ); printf ("************************************* * *************** \ n "); //////////////////////////////////////// /// // vShader = glCreateShader (GL_VERTEX_SHADER ); vs = readText ("shader/perspective. vert "); // printf (" % s \ n ", vs); vv = vs; if (vShader = 0) {printf (" Error: fail to create Shader! "); Exit (1);} glShaderSource (vShader, 1, & vv, NULL); glCompileShader (vShader); free (vs); glGetShaderiv (vShader, GL_INFO_LOG_LENGTH, & infoLogLength); if (infoLogLength> 0) {infoLog = (char *) malloc (infoLogLength); glGetShaderInfoLog (vShader, infoLogLength, & charWritten, infoLog ); printf ("% s \ n", infoLog ); printf ("************************************* * ***************************** \ n "); free (infoLog );}///////////// //////////////////////////////////////// ///////////////// // FShader = glCreateShader (GL_FRAGMENT_SHADER ); if (fShader = 0) {printf ("can't create fragment shader! \ N "); exit (1);} fs = readText (" shader/perspective. frag "); // printf (" % s \ n ", fs); ff = fs; glShaderSource (fShader, 1, & ff, NULL); glCompileShader (fShader ); free (fs); glGetShaderiv (fShader, GL_INFO_LOG_LENGTH, & infoLogLength); if (infoLogLength> 0) {infoLog = (char *) malloc (infoLogLength); glGetShaderInfoLog (fShader, infoLogLength, & charWritten, infoLog); // charWritten is printf ("% s \ n", infoLog); printf ("**** **************************************** * ************ \ N "); free (infoLog );} //////////////////////////////////////// //////////////////////////////////////// ////// programHandle = glCreateProgram (); histogram (programHandle, vShader); glAttachShader (programHandle, fShader); glLinkProgram (programHandle); glUseProgram (programHandle); iterator (programHandle, iterator, & infoLogLength); if (inf OLogLength> 0) {infoLog = (char *) malloc (infoLogLength); glGetProgramInfoLog (programHandle, infoLogLength, & charWritten, infoLog); printf ("% s \ n", infoLog ); printf ("************************************* * ******************* \ n "); free (infoLog) ;}} void InitVBO () {glGenBuffers (2, vboHandles); positionBufferHandle = vboHandles [0]; colorBufferHandle = vboHandles [1]; //////////////////////////////////////// /// glBindB Uffer (random, positionBufferHandle); glBufferData (random, sizeof (positionData), positionData, random); random (random, colorBufferHandle); glBufferData (random, sizeof (colorData), colorData, GL_STATIC_DRAW ); //////////////////////////////////////// /glGenVertexArrays (1, & vaoHandle); glBindVertexArray (vaoHandle ); /// // EVertexAttribArray (0); Round (1); Round (GL_ARRAY_BUFFER, positionBufferHandle); Round (0, 4, GL_FLOAT, GL_FALSE, 0, NULL); Round (GL_ARRAY_BUFFER, colorBufferHandle ); glVertexAttribPointer (1, 4, GL_FLOAT, GL_FALSE, 0, NULL );} //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////////// // Void SetupRC () {glewInit (); SetupShader (); InitVBO (); glClearColor (1.0, 0.0, 0.0, 1.0 ); //////////////////////////////////////// glable (GL_CULL_FACE ); glCullFace (GL_BACK); glFrontFace (GL_CW );} //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /// // glGet ** Location () series functions must be used after glLinkProgram. Note! // Do not convert the floating point value in shaderzhong. //////////////////////////////////////// ///// Void RenderScene () {glClear (callback); glUseProgram (programHandle); offsetUniformLocation = glGetUniformLocation (programHandle, "offset"); response = glGetUniformLocation (programHandle, "Callback"); terminate (M,-1.0, 1.0, 1.0,-1.0, 1.0, 3.0); // calculation matrix MglUniform2f (offsetUniformLocation, 0.5, 0.5); // change it to 0.0, 0.0, only a square shape is displayed, and the perspective effect is invisible. Vertex (vertex, 1, GL_TRUE, M); glBindVertexArray (vaoHandle); glDrawArrays (GL_TRIANGLES, 0, 36); glBindVertexArray (0); fig ();} void keyBoard (unsigned char key, int x, int y) {if (key = fig) exit (0);} int main (int argc, char * argv []) {gluinit (& argc, argv); gluinitdisplaymode (FIG); gluinitwindowsize (800,600); gluinitwindowposition (0, 0); glucreatewindow ("fourth: perspective "); setupRC (); fig (RenderScene); fig (keyBoard); fig (); return 0 ;}
     
    
   
  
 

Vmath can be downloaded from my resources

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.