This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

textureFiltering

By: a guest on Oct 28th, 2013  |  syntax: C  |  size: 22.35 KB  |  views: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <stdlib.h>
  4.  
  5. #include <GL/glew.h>
  6. #include <GL/glfw.h>
  7.  
  8. //Declare vectors for gcc.
  9. typedef float v4f __attribute__ ((vector_size (16)));
  10. typedef float rgba __attribute__ ((vector_size (16)));
  11. typedef float v2f __attribute__ ((vector_size (8)));
  12.  
  13. //Used by the report function defined below.
  14. #define REPORT_TIMESTAMP
  15. #define DEBUG
  16. #define FAIL 1
  17. #define WARN 2
  18. #define INFO 4
  19. #define PASS 8
  20. #define REPORT_SHOW (FAIL|WARN|INFO|PASS)
  21. void report(unsigned char level, const char* format, ...){//A "fancy-smancy printf replacer" for debugging purposes.
  22.   if(REPORT_SHOW & level){
  23.     va_list args;
  24.     va_start (args, format);
  25. #ifdef REPORT_TIMESTAMP
  26.     fprintf(stderr, "\033[1;37m[%7.4f]",glfwGetTime());
  27. #endif
  28.     if(level & FAIL)//critical failure
  29.       fputs("\033[1;31m[FAIL]\033[0m", stderr);
  30.     else if(level & PASS)//something went right
  31.       fputs("\033[1;32m[PASS]\033[0m", stderr);
  32.     else if(level &WARN)//Watch out for this.
  33.       fputs("\033[0;31m[WARN]\033[0m", stderr);
  34.     else if(level & INFO) //Information.
  35.       fputs("\033[1;34m[INFO]\033[0m", stderr);
  36.     vfprintf(stderr, format, args);
  37.     fputs("\n",stderr);
  38.   va_end(args);
  39.   }
  40. }
  41. GLuint shaderFromFilename(char *File,GLenum shaderType){
  42.   GLenum glError;
  43.   GLuint shader = glCreateShader(shaderType);
  44.   glError = glGetError();
  45.   if(glError != GL_NO_ERROR){
  46.     report(FAIL, "Failed to create shader.");
  47.     return -1;
  48.   }
  49.   FILE * fp;
  50.   unsigned long fileSize;
  51.   GLchar* source;
  52.   fp = fopen(File, "r");
  53.   if(fp == NULL){
  54.     report(FAIL, "Couldn't open shader file!");
  55.     return 0;
  56.   }
  57.   fseek(fp, 0, SEEK_END);
  58.   fileSize = ftell(fp);
  59.   rewind(fp); //be kind, rewind.
  60.   source = malloc((fileSize+1)*sizeof(char));
  61.   if(source == NULL){
  62.     report(FAIL, "Couldn't allocate earth for shader!");
  63.     return 0;
  64.   }
  65.   if(fread(source,1,fileSize,fp) != fileSize){
  66.     report(FAIL, "Couldn't read the opend shader file!");
  67.     return 0;
  68.   }
  69.   source[fileSize] = '\0';
  70.   glShaderSource(shader,1,(const GLchar**)&source,NULL);
  71.   glCompileShader(shader);
  72.   return shader;
  73. }
  74. void reportShaderLog(GLuint shader){
  75.   GLsizei logLength;
  76.   glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
  77.   if(logLength != 1){
  78.     GLchar *shaderLog = malloc(logLength);
  79.     glGetShaderInfoLog(shader,logLength,NULL,shaderLog);
  80.     report(WARN, "Shader log:\n %s",shaderLog);
  81.     free(shaderLog);
  82.   }
  83. }
  84. GLuint createProgramFromFilenames(char *vertexShader, char *fragmentShader){//simple wrapper for vertex+frag shader program creation.
  85.   GLenum glError;
  86.   GLuint vs = shaderFromFilename(vertexShader,GL_VERTEX_SHADER);
  87.   glError = glGetError();
  88.   if(glError != GL_NO_ERROR){
  89.     report(FAIL, "Failed everything!" ,vertexShader, fragmentShader,glError);
  90.     return -1;
  91.   }
  92.   GLint isCompiled;
  93.   glGetShaderiv(vs,GL_COMPILE_STATUS,&isCompiled);
  94.   isCompiled == GL_TRUE? report(PASS,"Compiled %s", vertexShader) : report(FAIL, "Couldn't compile %s",vertexShader);
  95.   reportShaderLog(vs);
  96.   GLuint fs = shaderFromFilename(fragmentShader,GL_FRAGMENT_SHADER);
  97.   glGetShaderiv(fs,GL_COMPILE_STATUS,&isCompiled);
  98.   isCompiled == GL_TRUE? report(PASS,"Compiled %s", fragmentShader) : report(FAIL, "Couldn't compile %s",fragmentShader);
  99.   glError = glGetError();
  100.   if(glError != GL_NO_ERROR){
  101.     report(FAIL, "Failed to compile everything!" ,vertexShader, fragmentShader,glError);
  102.     return -1;
  103.   }
  104.   reportShaderLog(fs);
  105.   glError = glGetError();
  106.   if(glError != GL_NO_ERROR){
  107.     report(FAIL, "what!" ,vertexShader, fragmentShader,glError);
  108.     return -1;
  109.   }
  110.   GLuint shaderProgram = glCreateProgram();
  111.   glError = glGetError();
  112.   if(glError != GL_NO_ERROR){
  113.     report(FAIL, "Couldn't create program!" ,vertexShader, fragmentShader,glError);
  114.     return -1;
  115.   }
  116.   glAttachShader(shaderProgram, fs);
  117.   glAttachShader(shaderProgram,vs);
  118.   glError = glGetError();
  119.   if(glError != GL_NO_ERROR){
  120.     report(FAIL, "Couldn't prepare linking %s and %s, error:" ,vertexShader, fragmentShader,glError);
  121.     return -1;
  122.   }
  123.   glLinkProgram(shaderProgram);
  124.   glError = glGetError();
  125.   if(glError != GL_NO_ERROR){
  126.     report(FAIL, "Couldn't link %s and %s, error:" ,vertexShader, fragmentShader,glError);
  127.     return -1;
  128.   }
  129.   report(PASS, "Linked %s and %s", vertexShader, fragmentShader);
  130.   return shaderProgram;
  131. }
  132. void GLFWCALL My_Key_Callback(int key, int action){
  133.   if(key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  134.     exit(EXIT_SUCCESS);
  135. }
  136. int initialize(){
  137.   glewExperimental = GL_TRUE;
  138.   GLenum err;
  139.   if( glfwInit() != GL_TRUE){
  140.     report(FAIL, "Couldn't Initialize GLFW!");
  141.     return -1;
  142.   }
  143. //Start by initializing OpenGL,
  144.   glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR,3);
  145.   glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR,0);
  146.   //glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  147.   { //Scope block because we don't need this anywhere else and it's to little to warrant a function.
  148.     int major,minor,rev;
  149.     glfwGetVersion(&major,&minor,&rev);
  150.     report(INFO, "Using GLFW version %d.%d, revision %d.", major,minor,rev);
  151.   }
  152.   if( glfwOpenWindow( 600,600, 0,0,0,0,0,0, GLFW_WINDOW ) != GL_TRUE){
  153.     report(FAIL, "Couldn't create window!");
  154.     glfwTerminate();
  155.     return -2;
  156.   }
  157.   err = glGetError();
  158.   if(err != GL_NO_ERROR){
  159.     report(FAIL, "Error on opening window!");
  160.   }
  161.   //Initialize GLEW so we can use our extensions.
  162.   err = glewInit();
  163.   if(err !=GLEW_OK){
  164.     report(FAIL,"Couldn't initialze GLEW: %s", glewGetErrorString(err));
  165.     return -3;
  166.   }
  167.   report(INFO, "Using GLEW %s", glewGetString(GLEW_VERSION));
  168.   err = glGetError();
  169.   if(err != GL_NO_ERROR){
  170.     report(WARN, "Glew set GL_ERROR, should be fine though!");
  171.   }
  172.   //glfwSwapInterval(0); //vsync off
  173. //Report some information about the window if wanted./*
  174.   report(INFO,"Using OpenGL Version %d.%d.",glfwGetWindowParam(GLFW_OPENGL_VERSION_MAJOR), glfwGetWindowParam(GLFW_OPENGL_VERSION_MINOR));
  175.   report(INFO,"Using (%d,%d,%d)RGB bit-depth.",glfwGetWindowParam(GLFW_RED_BITS),glfwGetWindowParam(GLFW_GREEN_BITS),glfwGetWindowParam(GLFW_BLUE_BITS));
  176.   report(INFO,"Hardware acceleration: %s.", glfwGetWindowParam(GLFW_ACCELERATED) == GL_TRUE? "Supported": "Unsupported");
  177.   report(INFO,"Refresh Rate: %dHz.", glfwGetWindowParam(GLFW_REFRESH_RATE));
  178.   report(PASS, "Intialized.");
  179.   glfwSetKeyCallback(My_Key_Callback);
  180.   return 0;
  181. }
  182.  
  183. int main(int argc, char **argv)
  184. {
  185.   GLenum glError;
  186.   if(initialize()) //basic initialisation
  187.    exit(EXIT_FAILURE);
  188.   glError = glGetError();
  189.   if(glError != GL_NO_ERROR){
  190.     report(FAIL, "Failed to init!");
  191.     return -1;
  192.   }
  193. //create the shaders
  194.   GLuint shaderProgram = createProgramFromFilenames("./shaders/pointPath.vert", "./shaders/spritePoint.frag");
  195.   glUseProgram (shaderProgram);
  196.   if(glGetError() != GL_NO_ERROR)
  197.     report(FAIL, "An error occured while building the shader program!");
  198.   else
  199.     report(PASS, "Succesfully using shaders");
  200.  
  201. //Create and set first texture.
  202.   v2f path[] = {{-0.5,0.5},//A path "t -> (x,y)" along which we will place our sprites
  203.     {-0.5,-0.5},
  204.     {0.5,-0.5},
  205.     {0.5,0.5}};
  206.   glActiveTexture(GL_TEXTURE0);
  207.   if(glGetError() != GL_NO_ERROR)
  208.     report(FAIL, "Couldn't activate texture!");
  209.   GLuint pathTex;
  210.   glGenTextures(1, &pathTex); //create a new texture
  211.   glBindTexture(GL_TEXTURE_1D, pathTex); //start using it as a 1D texture
  212.   glTexImage1D(GL_TEXTURE_1D,0,GL_RG32F, sizeof(path)/sizeof(v2f),0,GL_RG,GL_FLOAT,path); //store the coordinates of path in the texture so that we can interpolate our path in the shader.
  213.   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0);//We need these two lines
  214.   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);//because we don't have mipmaps.
  215.   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//We want the path to be linearly filtered
  216.   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//Set the min filter just to be safe.
  217.   if(glGetError() != GL_NO_ERROR)
  218.     report(FAIL, "Something went wrong setting up pathTex");
  219.   GLint uniformLoc = glGetUniformLocation(shaderProgram, "pathTexture"); //locate the uniform
  220.   uniformLoc >= 0?glUniform1i(uniformLoc, 0):report(FAIL, "found uniform spriteTexture at %d", uniformLoc); //and set it to "0" because we're using GL_TEXTURE0
  221.   if(glGetError() != GL_NO_ERROR)
  222.     report(FAIL, "Couldn't set uniform!");
  223.   report(PASS, "Setup texture for path");
  224. //create and set second texture
  225.   rgba sprite[] = {{1.0,0,0,1},{0,1.0,0,1},{0,0,1.0,1},{1,1,0,1}}; //our sprite, a simple "red,green,blue,yellow" square.
  226.   glActiveTexture(GL_TEXTURE0+1); //use texUnit 1 now.
  227.   if(glGetError() != GL_NO_ERROR)
  228.     report(FAIL, "Couldn't activate texture!");
  229.   GLuint spriteTex;
  230.   glGenTextures(1, &spriteTex); //create a new texture
  231.   glBindTexture(GL_TEXTURE_2D, spriteTex); //start using it as a 2D texture
  232.   glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, 2,2,0,GL_RGBA,GL_FLOAT,sprite); //load the data again. 2x2 sprite.
  233.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);//We need these two lines
  234.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);//because we don't have mipmaps.
  235.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//We want our sprites to be filtered to
  236.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//the nearest pixel, not interpolated.
  237.   if(glGetError() != GL_NO_ERROR)
  238.     report(FAIL, "Something went wrong setting up pathTex");
  239.   uniformLoc = glGetUniformLocation(shaderProgram, "spriteTexture"); //locates and set the second sampler.
  240.   uniformLoc >= 0?glUniform1i(uniformLoc, 1):report(FAIL, "found uniform spriteTexture at %d", uniformLoc);
  241.   if(glGetError() != GL_NO_ERROR)
  242.     report(FAIL, "Couldn't set uniform!");
  243.   report(PASS, "Setup texture for sprite");
  244.  
  245. //Create our sprite locations
  246. #define Npoints 1024 //any number > the number of path points will do.
  247.   GLfloat points[Npoints]; //the location in "t-space" of our sprites, i.e. the texture coordinate for our path texture.
  248.   for(size_t i = 0; i < Npoints;i++)
  249.     points[i] = (float)i/(float)Npoints; //fill it from 0 to 1.
  250.   report(PASS, "generated points array.");
  251. //set-up a our buffers.
  252.   unsigned int vaSprites;
  253.   glGenVertexArrays(1,&vaSprites);
  254.   report(PASS, "Generated vertex array");
  255.   unsigned int vbSprites;
  256.   glGenBuffers(1, &vbSprites);
  257.   glBindBuffer(GL_ARRAY_BUFFER, vbSprites);
  258.   glBufferData (GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
  259.   if(glGetError() != GL_NO_ERROR)
  260.     report(FAIL, "Something went wrong generating the vbSprites!");
  261.   glBindVertexArray(vaSprites);
  262.   GLint inputAttrib = glGetAttribLocation(shaderProgram, "t");
  263.   glEnableVertexAttribArray(inputAttrib);
  264.   glVertexAttribPointer (inputAttrib, 1, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
  265.   if(glGetError() != GL_NO_ERROR)
  266.     report(FAIL, "Something went wrong generating the vaSprites!");
  267.   else
  268.     report(PASS, "Set-up vaSprites.");
  269.  
  270.   glEnable(GL_POINT_SPRITE); //We need this (on Nvidia atleast) for obvious reasons.
  271.   glPointSize(64);
  272.  
  273.  
  274.   while(1){ //our main loop,it's broken by hitting space.
  275.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  276.     glDrawArrays(GL_POINTS,0, Npoints);
  277.     glfwSwapBuffers();
  278.    }
  279.   return 0;
  280. }
  281.         #include <stdio.h>
  282. #include <stdarg.h>
  283. #include <stdlib.h>
  284.  
  285. #include <GL/glew.h>
  286. #include <GL/glfw.h>
  287.  
  288. //Declare vectors for gcc.
  289. typedef float v4f __attribute__ ((vector_size (16)));
  290. typedef float rgba __attribute__ ((vector_size (16)));
  291. typedef float v2f __attribute__ ((vector_size (8)));
  292.  
  293. //Used by the report function defined below.
  294. #define REPORT_TIMESTAMP
  295. #define DEBUG
  296. #define FAIL 1
  297. #define WARN 2
  298. #define INFO 4
  299. #define PASS 8
  300. #define REPORT_SHOW (FAIL|WARN|INFO|PASS)
  301. void report(unsigned char level, const char* format, ...){//A "fancy-smancy printf replacer" for debugging purposes.
  302.   if(REPORT_SHOW & level){
  303.     va_list args;
  304.     va_start (args, format);
  305. #ifdef REPORT_TIMESTAMP
  306.     fprintf(stderr, "\033[1;37m[%7.4f]",glfwGetTime());
  307. #endif
  308.     if(level & FAIL)//critical failure
  309.       fputs("\033[1;31m[FAIL]\033[0m", stderr);
  310.     else if(level & PASS)//something went right
  311.       fputs("\033[1;32m[PASS]\033[0m", stderr);
  312.     else if(level &WARN)//Watch out for this.
  313.       fputs("\033[0;31m[WARN]\033[0m", stderr);
  314.     else if(level & INFO) //Information.
  315.       fputs("\033[1;34m[INFO]\033[0m", stderr);
  316.     vfprintf(stderr, format, args);
  317.     fputs("\n",stderr);
  318.   va_end(args);
  319.   }
  320. }
  321. GLuint shaderFromFilename(char *File,GLenum shaderType){
  322.   GLenum glError;
  323.   GLuint shader = glCreateShader(shaderType);
  324.   glError = glGetError();
  325.   if(glError != GL_NO_ERROR){
  326.     report(FAIL, "Failed to create shader.");
  327.     return -1;
  328.   }
  329.   FILE * fp;
  330.   unsigned long fileSize;
  331.   GLchar* source;
  332.   fp = fopen(File, "r");
  333.   if(fp == NULL){
  334.     report(FAIL, "Couldn't open shader file!");
  335.     return 0;
  336.   }
  337.   fseek(fp, 0, SEEK_END);
  338.   fileSize = ftell(fp);
  339.   rewind(fp); //be kind, rewind.
  340.   source = malloc((fileSize+1)*sizeof(char));
  341.   if(source == NULL){
  342.     report(FAIL, "Couldn't allocate earth for shader!");
  343.     return 0;
  344.   }
  345.   if(fread(source,1,fileSize,fp) != fileSize){
  346.     report(FAIL, "Couldn't read the opend shader file!");
  347.     return 0;
  348.   }
  349.   source[fileSize] = '\0';
  350.   glShaderSource(shader,1,(const GLchar**)&source,NULL);
  351.   glCompileShader(shader);
  352.   return shader;
  353. }
  354. void reportShaderLog(GLuint shader){
  355.   GLsizei logLength;
  356.   glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
  357.   if(logLength != 1){
  358.     GLchar *shaderLog = malloc(logLength);
  359.     glGetShaderInfoLog(shader,logLength,NULL,shaderLog);
  360.     report(WARN, "Shader log:\n %s",shaderLog);
  361.     free(shaderLog);
  362.   }
  363. }
  364. GLuint createProgramFromFilenames(char *vertexShader, char *fragmentShader){//simple wrapper for vertex+frag shader program creation.
  365.   GLenum glError;
  366.   GLuint vs = shaderFromFilename(vertexShader,GL_VERTEX_SHADER);
  367.   glError = glGetError();
  368.   if(glError != GL_NO_ERROR){
  369.     report(FAIL, "Failed everything!" ,vertexShader, fragmentShader,glError);
  370.     return -1;
  371.   }
  372.   GLint isCompiled;
  373.   glGetShaderiv(vs,GL_COMPILE_STATUS,&isCompiled);
  374.   isCompiled == GL_TRUE? report(PASS,"Compiled %s", vertexShader) : report(FAIL, "Couldn't compile %s",vertexShader);
  375.   reportShaderLog(vs);
  376.   GLuint fs = shaderFromFilename(fragmentShader,GL_FRAGMENT_SHADER);
  377.   glGetShaderiv(fs,GL_COMPILE_STATUS,&isCompiled);
  378.   isCompiled == GL_TRUE? report(PASS,"Compiled %s", fragmentShader) : report(FAIL, "Couldn't compile %s",fragmentShader);
  379.   glError = glGetError();
  380.   if(glError != GL_NO_ERROR){
  381.     report(FAIL, "Failed to compile everything!" ,vertexShader, fragmentShader,glError);
  382.     return -1;
  383.   }
  384.   reportShaderLog(fs);
  385.   glError = glGetError();
  386.   if(glError != GL_NO_ERROR){
  387.     report(FAIL, "what!" ,vertexShader, fragmentShader,glError);
  388.     return -1;
  389.   }
  390.   GLuint shaderProgram = glCreateProgram();
  391.   glError = glGetError();
  392.   if(glError != GL_NO_ERROR){
  393.     report(FAIL, "Couldn't create program!" ,vertexShader, fragmentShader,glError);
  394.     return -1;
  395.   }
  396.   glAttachShader(shaderProgram, fs);
  397.   glAttachShader(shaderProgram,vs);
  398.   glError = glGetError();
  399.   if(glError != GL_NO_ERROR){
  400.     report(FAIL, "Couldn't prepare linking %s and %s, error:" ,vertexShader, fragmentShader,glError);
  401.     return -1;
  402.   }
  403.   glLinkProgram(shaderProgram);
  404.   glError = glGetError();
  405.   if(glError != GL_NO_ERROR){
  406.     report(FAIL, "Couldn't link %s and %s, error:" ,vertexShader, fragmentShader,glError);
  407.     return -1;
  408.   }
  409.   report(PASS, "Linked %s and %s", vertexShader, fragmentShader);
  410.   return shaderProgram;
  411. }
  412. void GLFWCALL My_Key_Callback(int key, int action){
  413.   if(key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  414.     exit(EXIT_SUCCESS);
  415. }
  416. int initialize(){
  417.   glewExperimental = GL_TRUE;
  418.   GLenum err;
  419.   if( glfwInit() != GL_TRUE){
  420.     report(FAIL, "Couldn't Initialize GLFW!");
  421.     return -1;
  422.   }
  423. //Start by initializing OpenGL,
  424.   glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR,3);
  425.   glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR,0);
  426.   //glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  427.   { //Scope block because we don't need this anywhere else and it's to little to warrant a function.
  428.     int major,minor,rev;
  429.     glfwGetVersion(&major,&minor,&rev);
  430.     report(INFO, "Using GLFW version %d.%d, revision %d.", major,minor,rev);
  431.   }
  432.   if( glfwOpenWindow( 600,600, 0,0,0,0,0,0, GLFW_WINDOW ) != GL_TRUE){
  433.     report(FAIL, "Couldn't create window!");
  434.     glfwTerminate();
  435.     return -2;
  436.   }
  437.   err = glGetError();
  438.   if(err != GL_NO_ERROR){
  439.     report(FAIL, "Error on opening window!");
  440.   }
  441.   //Initialize GLEW so we can use our extensions.
  442.   err = glewInit();
  443.   if(err !=GLEW_OK){
  444.     report(FAIL,"Couldn't initialze GLEW: %s", glewGetErrorString(err));
  445.     return -3;
  446.   }
  447.   report(INFO, "Using GLEW %s", glewGetString(GLEW_VERSION));
  448.   err = glGetError();
  449.   if(err != GL_NO_ERROR){
  450.     report(WARN, "Glew set GL_ERROR, should be fine though!");
  451.   }
  452.   //glfwSwapInterval(0); //vsync off
  453. //Report some information about the window if wanted./*
  454.   report(INFO,"Using OpenGL Version %d.%d.",glfwGetWindowParam(GLFW_OPENGL_VERSION_MAJOR), glfwGetWindowParam(GLFW_OPENGL_VERSION_MINOR));
  455.   report(INFO,"Using (%d,%d,%d)RGB bit-depth.",glfwGetWindowParam(GLFW_RED_BITS),glfwGetWindowParam(GLFW_GREEN_BITS),glfwGetWindowParam(GLFW_BLUE_BITS));
  456.   report(INFO,"Hardware acceleration: %s.", glfwGetWindowParam(GLFW_ACCELERATED) == GL_TRUE? "Supported": "Unsupported");
  457.   report(INFO,"Refresh Rate: %dHz.", glfwGetWindowParam(GLFW_REFRESH_RATE));
  458.   report(PASS, "Intialized.");
  459.   glfwSetKeyCallback(My_Key_Callback);
  460.   return 0;
  461. }
  462.  
  463. int main(int argc, char **argv)
  464. {
  465.   GLenum glError;
  466.   if(initialize()) //basic initialisation
  467.    exit(EXIT_FAILURE);
  468.   glError = glGetError();
  469.   if(glError != GL_NO_ERROR){
  470.     report(FAIL, "Failed to init!");
  471.     return -1;
  472.   }
  473. //create the shaders
  474.   GLuint shaderProgram = createProgramFromFilenames("./shaders/pointPath.vert", "./shaders/spritePoint.frag");
  475.   glUseProgram (shaderProgram);
  476.   if(glGetError() != GL_NO_ERROR)
  477.     report(FAIL, "An error occured while building the shader program!");
  478.   else
  479.     report(PASS, "Succesfully using shaders");
  480.  
  481. //Create and set first texture.
  482.   v2f path[] = {{-0.5,0.5},//A path "t -> (x,y)" along which we will place our sprites
  483.     {-0.5,-0.5},
  484.     {0.5,-0.5},
  485.     {0.5,0.5}};
  486.   glActiveTexture(GL_TEXTURE0);
  487.   if(glGetError() != GL_NO_ERROR)
  488.     report(FAIL, "Couldn't activate texture!");
  489.   GLuint pathTex;
  490.   glGenTextures(1, &pathTex); //create a new texture
  491.   glBindTexture(GL_TEXTURE_1D, pathTex); //start using it as a 1D texture
  492.   glTexImage1D(GL_TEXTURE_1D,0,GL_RG32F, sizeof(path)/sizeof(v2f),0,GL_RG,GL_FLOAT,path); //store the coordinates of path in the texture so that we can interpolate our path in the shader.
  493.   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0);//We need these two lines
  494.   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);//because we don't have mipmaps.
  495.   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//We want the path to be linearly filtered
  496.   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//Set the min filter just to be safe.
  497.   if(glGetError() != GL_NO_ERROR)
  498.     report(FAIL, "Something went wrong setting up pathTex");
  499.   GLint uniformLoc = glGetUniformLocation(shaderProgram, "pathTexture"); //locate the uniform
  500.   uniformLoc >= 0?glUniform1i(uniformLoc, 0):report(FAIL, "found uniform spriteTexture at %d", uniformLoc); //and set it to "0" because we're using GL_TEXTURE0
  501.   if(glGetError() != GL_NO_ERROR)
  502.     report(FAIL, "Couldn't set uniform!");
  503.   report(PASS, "Setup texture for path");
  504. //create and set second texture
  505.   rgba sprite[] = {{1.0,0,0,1},{0,1.0,0,1},{0,0,1.0,1},{1,1,0,1}}; //our sprite, a simple "red,green,blue,yellow" square.
  506.   glActiveTexture(GL_TEXTURE0+1); //use texUnit 1 now.
  507.   if(glGetError() != GL_NO_ERROR)
  508.     report(FAIL, "Couldn't activate texture!");
  509.   GLuint spriteTex;
  510.   glGenTextures(1, &spriteTex); //create a new texture
  511.   glBindTexture(GL_TEXTURE_2D, spriteTex); //start using it as a 2D texture
  512.   glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, 2,2,0,GL_RGBA,GL_FLOAT,sprite); //load the data again. 2x2 sprite.
  513.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);//We need these two lines
  514.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);//because we don't have mipmaps.
  515.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//We want our sprites to be filtered to
  516.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//the nearest pixel, not interpolated.
  517.   if(glGetError() != GL_NO_ERROR)
  518.     report(FAIL, "Something went wrong setting up pathTex");
  519.   uniformLoc = glGetUniformLocation(shaderProgram, "spriteTexture"); //locates and set the second sampler.
  520.   uniformLoc >= 0?glUniform1i(uniformLoc, 1):report(FAIL, "found uniform spriteTexture at %d", uniformLoc);
  521.   if(glGetError() != GL_NO_ERROR)
  522.     report(FAIL, "Couldn't set uniform!");
  523.   report(PASS, "Setup texture for sprite");
  524.  
  525. //Create our sprite locations
  526. #define Npoints 1024 //any number > the number of path points will do.
  527.   GLfloat points[Npoints]; //the location in "t-space" of our sprites, i.e. the texture coordinate for our path texture.
  528.   for(size_t i = 0; i < Npoints;i++)
  529.     points[i] = (float)i/(float)Npoints; //fill it from 0 to 1.
  530.   report(PASS, "generated points array.");
  531. //set-up a our buffers.
  532.   unsigned int vaSprites;
  533.   glGenVertexArrays(1,&vaSprites);
  534.   report(PASS, "Generated vertex array");
  535.   unsigned int vbSprites;
  536.   glGenBuffers(1, &vbSprites);
  537.   glBindBuffer(GL_ARRAY_BUFFER, vbSprites);
  538.   glBufferData (GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
  539.   if(glGetError() != GL_NO_ERROR)
  540.     report(FAIL, "Something went wrong generating the vbSprites!");
  541.   glBindVertexArray(vaSprites);
  542.   GLint inputAttrib = glGetAttribLocation(shaderProgram, "t");
  543.   glEnableVertexAttribArray(inputAttrib);
  544.   glVertexAttribPointer (inputAttrib, 1, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
  545.   if(glGetError() != GL_NO_ERROR)
  546.     report(FAIL, "Something went wrong generating the vaSprites!");
  547.   else
  548.     report(PASS, "Set-up vaSprites.");
  549.  
  550.   glEnable(GL_POINT_SPRITE); //We need this (on Nvidia atleast) for obvious reasons.
  551.   glPointSize(64);
  552.  
  553.  
  554.   while(1){ //our main loop,it's broken by hitting space.
  555.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  556.     glDrawArrays(GL_POINTS,0, Npoints);
  557.     glfwSwapBuffers();
  558.    }
  559.   return 0;
  560. }
clone this paste RAW Paste Data