Advertisement
Guest User

Blend

a guest
Jul 12th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. #include <GL/freeglut.h>
  2. #include <GL/glext.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. GLuint w = 512;//Window Width
  7. GLuint h = 512;//Window Height
  8. GLuint lineTexture;
  9.  
  10. PFNGLBLENDFUNCSEPARATEPROC     glBlendFuncSeparate     = NULL;
  11. PFNGLBLENDEQUATIONSEPARATEPROC glBlendEquationSeparate = NULL;
  12.  
  13. float invSqrt(float x){//Quake fast inverse square root function
  14.     union{
  15.         float x;
  16.         int i;
  17.     } u;
  18.     u.x = x;
  19.     u.i = 0x5f3759df - (u.i >> 1);
  20.     return u.x * (1.5f - 0.5f*x * u.x * u.x);
  21. }
  22.  
  23. void line(float x1, float y1, float x2, float y2, float r){
  24.  
  25.     //  Calculate the offset for A, B, C, D
  26.  
  27.     //  D________________C
  28.     //  |                |
  29.     //  p1______________p2
  30.     //  |                |
  31.     //  A________________B
  32.  
  33.     float dx = x1-x2;
  34.     float dy = y1-y2;
  35.     r  *= invSqrt(dx*dx + dy*dy);
  36.     dx *= r;
  37.     dy *= r;
  38.  
  39.     glBindTexture(GL_TEXTURE_2D, lineTexture);
  40.     glBegin(GL_QUADS);
  41.     glTexCoord2i(0, 0); glVertex2f(x1-dy, y1+dx);
  42.     glTexCoord2i(1, 0); glVertex2f(x2-dy, y2+dx);
  43.     glTexCoord2i(1, 1); glVertex2f(x2+dy, y2-dx);
  44.     glTexCoord2i(0, 1); glVertex2f(x1+dy, y1-dx);
  45.     glEnd();
  46.     glBindTexture(GL_TEXTURE_2D, 0);
  47. }
  48.  
  49. void reshape(int width, int height){
  50.     glViewport(0, 0, w=width, h=height);
  51.     glMatrixMode(GL_PROJECTION);
  52.     glLoadIdentity();
  53.     glOrtho(0, w, 0, h, -10, 10);
  54.     glMatrixMode(GL_MODELVIEW);
  55. }
  56.  
  57. void display(){
  58.     glClearColor(0.0, 0.0, 0.0, 0.0);
  59.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  60.     glLoadIdentity();
  61.  
  62.     glColor4f(1.0, 1.0, 1.0, 0.5);
  63.     glEnable(GL_TEXTURE_2D);
  64.  
  65.     //Have fun playing around with these functions
  66.  
  67.     //glEnable(GL_ALPHA_TEST);
  68.     //glAlphaFunc(GL_NOTEQUAL, 0);
  69.     //glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  70.     //glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
  71.     //glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
  72.     glEnable(GL_BLEND);
  73.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  74.  
  75.  
  76.     line(100,   100, w-100, h-100, 10);
  77.     line(100, h-100, w-100,   100, 10);
  78.  
  79.     glFlush();
  80.     glutSwapBuffers();
  81. }
  82.  
  83. int main(int argc, char **argv){
  84.     glutInit(&argc, argv);
  85.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  86.     glutInitWindowSize(w, h);
  87.     glutInitWindowPosition(100, 100);
  88.     glutCreateWindow("Blend");
  89.     glutReshapeFunc(reshape);
  90.     glutDisplayFunc(display);
  91.  
  92.     glBlendFuncSeparate     = (PFNGLBLENDFUNCSEPARATEPROC)    wglGetProcAddress("glBlendFuncSeparate");
  93.     glBlendEquationSeparate = (PFNGLBLENDEQUATIONSEPARATEPROC)wglGetProcAddress("glBlendEquationSeparate");
  94.  
  95.     //Generate line texture
  96.     GLuint *data = new GLuint[w*h];
  97.     int r = h/2;
  98.     int r2 = r*r;
  99.     for (int y=0; y<h; y++){
  100.         int dy = y-r;
  101.         GLuint alpha = (0xFF-0xFF*dy*dy/r2)<<24; //Alpha is based on (squared distance) / (squared radius)
  102.         GLuint color = 0xFFFFFF|alpha;           //Format: ABGR
  103.         int i = y*w;
  104.         int n = i+w;
  105.         while (i<n) data[i++] = color;//Color for all (?, y) values is the same
  106.     }
  107.  
  108.     glGenTextures(1, &lineTexture);
  109.     glBindTexture(GL_TEXTURE_2D, lineTexture);
  110.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  111.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  112.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  113.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  114.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLubyte*)data);
  115.     glBindTexture(GL_TEXTURE_2D, 0);
  116.  
  117.     glutMainLoop();
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement