Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/freeglut.h>
- #include <GL/glext.h>
- #include <stdio.h>
- #include <math.h>
- GLuint w = 512;//Window Width
- GLuint h = 512;//Window Height
- GLuint lineTexture;
- PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = NULL;
- PFNGLBLENDEQUATIONSEPARATEPROC glBlendEquationSeparate = NULL;
- float invSqrt(float x){//Quake fast inverse square root function
- union{
- float x;
- int i;
- } u;
- u.x = x;
- u.i = 0x5f3759df - (u.i >> 1);
- return u.x * (1.5f - 0.5f*x * u.x * u.x);
- }
- void line(float x1, float y1, float x2, float y2, float r){
- // Calculate the offset for A, B, C, D
- // D________________C
- // | |
- // p1______________p2
- // | |
- // A________________B
- float dx = x1-x2;
- float dy = y1-y2;
- r *= invSqrt(dx*dx + dy*dy);
- dx *= r;
- dy *= r;
- glBindTexture(GL_TEXTURE_2D, lineTexture);
- glBegin(GL_QUADS);
- glTexCoord2i(0, 0); glVertex2f(x1-dy, y1+dx);
- glTexCoord2i(1, 0); glVertex2f(x2-dy, y2+dx);
- glTexCoord2i(1, 1); glVertex2f(x2+dy, y2-dx);
- glTexCoord2i(0, 1); glVertex2f(x1+dy, y1-dx);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- void reshape(int width, int height){
- glViewport(0, 0, w=width, h=height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, w, 0, h, -10, 10);
- glMatrixMode(GL_MODELVIEW);
- }
- void display(){
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glColor4f(1.0, 1.0, 1.0, 0.5);
- glEnable(GL_TEXTURE_2D);
- //Have fun playing around with these functions
- //glEnable(GL_ALPHA_TEST);
- //glAlphaFunc(GL_NOTEQUAL, 0);
- //glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
- //glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
- //glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- line(100, 100, w-100, h-100, 10);
- line(100, h-100, w-100, 100, 10);
- glFlush();
- glutSwapBuffers();
- }
- int main(int argc, char **argv){
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
- glutInitWindowSize(w, h);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("Blend");
- glutReshapeFunc(reshape);
- glutDisplayFunc(display);
- glBlendFuncSeparate = (PFNGLBLENDFUNCSEPARATEPROC) wglGetProcAddress("glBlendFuncSeparate");
- glBlendEquationSeparate = (PFNGLBLENDEQUATIONSEPARATEPROC)wglGetProcAddress("glBlendEquationSeparate");
- //Generate line texture
- GLuint *data = new GLuint[w*h];
- int r = h/2;
- int r2 = r*r;
- for (int y=0; y<h; y++){
- int dy = y-r;
- GLuint alpha = (0xFF-0xFF*dy*dy/r2)<<24; //Alpha is based on (squared distance) / (squared radius)
- GLuint color = 0xFFFFFF|alpha; //Format: ABGR
- int i = y*w;
- int n = i+w;
- while (i<n) data[i++] = color;//Color for all (?, y) values is the same
- }
- glGenTextures(1, &lineTexture);
- glBindTexture(GL_TEXTURE_2D, lineTexture);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLubyte*)data);
- glBindTexture(GL_TEXTURE_2D, 0);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement