Code Search for Developers
 
 
  

vpt.cxx from gzz at Krugle


Show vpt.cxx syntax highlighted

/*
vpt.cxx
 *    
 *    Copyright (c) 2003, Tuomas J. Lukka
 *    
 *    This file is part of Gzz.
 *    
 *    Gzz is free software; you can redistribute it and/or modify it under
 *    the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *    
 *    Gzz is distributed in the hope that it will be useful, but WITHOUT
 *    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
 *    Public License for more details.
 *    
 *    You should have received a copy of the GNU Lesser General
 *    Public License along with Gzz; if not, write to the Free
 *    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *    
 *    
 */
/*
 * Written by Tuomas J. Lukka
 */

#include <unistd.h>
#include <stdlib.h>
#include <GzzGL.hxx>

using namespace Gzz::AbstractWin;
using namespace Gzz;
using namespace Gummi;
using namespace Gummi::Font;


Window *w;

double t = 0;

#define NPG 2
#define ATEXSIZE 1024
typedef Tex::Raster<Tex::Format::Alpha> AlphaRaster;
typedef Tex::Raster<Tex::Format::RGBA> RGBARaster;

TextRenderer *textrenderer;


void unitquad() {
    glBegin(GL_QUAD_STRIP);
    /*
    glTexCoord2f(0, 0);
    glVertex2f(0,0);
    glTexCoord2f(0, 1);
    glVertex2f(0,1);
    glTexCoord2f(1, 0);
    glVertex2f(1,0);
    glTexCoord2f(1, 1);
    glVertex2f(1,1);
    */
    glVertex2f(0,0);
    glVertex2f(0,1);
    glVertex2f(1,0);
    glVertex2f(1,1);
    glEnd();
}

void sepquads() {
    glBegin(GL_QUADS);
    for(double x=-1; x<2; x+=0.1)
	for(double y=-1; y<2; y+=0.1) {
	    glVertex2f(x,y);
	    glVertex2f(x+0.08,y);
	    glVertex2f(x+0.08,y+0.08);
	    glVertex2f(x,y+0.08);
	}

    glEnd();
}
Tex::Id bgid ;

/** The texture id of the stencil texture.
 */
Tex::Id stencilid ;
Tex::Id viewportid ;

void bg() {
    glEnable(GL_TEXTURE_2D);
    bgid.bind();
    double ed = 100;
    glBegin(GL_QUAD_STRIP);
    glTexCoord2f(-ed, -ed);
    glVertex2f(-ed,-ed);
    glTexCoord2f(-ed, ed+1);
    glVertex2f(-ed,ed+1);
    glTexCoord2f(ed+1, -ed);
    glVertex2f(ed+1,-ed);
    glTexCoord2f(ed+1, ed+1);
    glVertex2f(ed+1,ed+1);
    glEnd();
}

string s1("Testistringi");
string s2("A much longer string that will go out");
void texts() {
    glPushMatrix();
    glScalef(0.01, 0.01, 0.01);
    Gummi::Distort::Direct v;
    glColor4f(0, 0, 0, 1);
    Gummi::Font::renderIter(*textrenderer, s1.begin(), s1.end(), 0, 50, v);
    Gummi::Font::renderIter(*textrenderer, s2.begin(), s2.end(), -100, 100, v);
    glPopMatrix();
}

    void repaint() {
	w->setCurrent();
	glClearColor(0.2, 0.4, 0.2, 1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	GLERR;

	glActiveTextureARB(GL_TEXTURE0_ARB);
	glEnable(GL_TEXTURE_2D);
	glActiveTextureARB(GL_TEXTURE1_ARB);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);

	GLERR;

        glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0.1);

        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	int size[4];
	w->getSize(size);
	setStandardCoordinates(Vec(size[2],size[3]));


	glColor4f(1, 1, 1, 1);

	for(int i=0; i<NPG; i++) {

	    double x = 400 + 400*sin(M_PI * (t + i));
	    double y = 300 - (100+100*i)*sin(M_PI * (2*t + i));

	    double scale = 600 + 300 * sin(M_PI * (t + i + 1));

	    // double angle = 30 * i * sin(3.1*t);
	    double angle = 0;


	    if(i == 1) glColor4f(1, 1, 0, 1);
	    if(i == 2) glColor4f(0, 1, 0, 1);
	    if(i == 3) glColor4f(0, 0, 1, 1);

	    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
	    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

	    GLERR;

	    double rad = angle / 180 * M_PI;

	    float vec[2][4] = {
		{ cos(rad) / scale, sin(rad) / scale, 0, 0 },
		    { -sin(rad) / scale, cos(rad) / scale, 0, 0 }
	    };
	    vec[0][3] = -(vec[0][0] * x + vec[0][1] * y);
	    vec[1][3] = -(vec[1][0] * x + vec[1][1] * y);

	    glTexGenfv(GL_S, GL_EYE_PLANE, vec[0]);
	    glTexGenfv(GL_T, GL_EYE_PLANE, vec[1]);

	    glEnable(GL_TEXTURE_GEN_S);
	    glEnable(GL_TEXTURE_GEN_T);
	    GLERR;


	    glPushMatrix();
		GLERR;
		glTranslatef(x, y, 0);
		glRotatef(angle, 0, 0, 1);
		glScalef(scale, scale, scale);

		// Now, the inside is the unit quad.
		// clip to it now to save a lot of overhead...

		double clip[4][4] = {
		    { 1, 0, 0, 0 },
		    { 0, 1, 0, 0 },
		    { -1, 0, 0, 1 },
		    { 0, -1, 0, 1 }
		};
		glClipPlane(GL_CLIP_PLANE0, clip[0]);
		glClipPlane(GL_CLIP_PLANE1, clip[1]);
		glClipPlane(GL_CLIP_PLANE2, clip[2]);
		glClipPlane(GL_CLIP_PLANE3, clip[3]);

		glEnable(GL_CLIP_PLANE0);
		glEnable(GL_CLIP_PLANE1);
		glEnable(GL_CLIP_PLANE2);
		glEnable(GL_CLIP_PLANE3);

		glPushMatrix();

		// Then, scale and move viewport...
		glTranslatef(0.3 * sin(M_PI * 1.42 * t),
			    0.1 * (1+sin(M_PI * 1.51 * t)), 0);
		double iscale = 0.2 + 0.1 * sin(M_PI * 1.5 * (t + 0.1 * t * i));
		glScalef(iscale, iscale, 1);


		stencilid.bind();
		GLERR;
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		GLERR;
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		GLERR;
		// glCallList(1);
		glActiveTextureARB(GL_TEXTURE0_ARB);


		bg();
		texts();

		glPopMatrix();

		glDisable(GL_TEXTURE_2D);
		glActiveTextureARB(GL_TEXTURE1_ARB);
		viewportid.bind();
		glColor4f(1,1,1,1);
		unitquad();



	    glPopMatrix();
	    GLERR;
	}

	w->swapBuffers();

	t += 0.01;
	w->repaint();

	GLERR;
    }

int power = 2;
bool roundcorners = true;

double dith(double v) {
    v += 0.2 * drand48() - 0.1;
    if(v < 0) return 0;
    if(v > 1) return 1;
    return v;
}

double transverse, dotlight;

double orad = 1;
double irad = 0.95;
double height = orad - irad;
// 0 = inside, 1 = border, 2 = outside
int region(double x, double y, bool lit = false) {
    if(!roundcorners) 
	return (x < -0.9) || (y < -0.9) || (x > 0.9) || (y > 0.9);
    double x8 = x*x;
    x8 = x8*x8;
    x8 = x8*x8;
    double y8 = y*y;
    y8 = y8*y8;
    y8 = y8*y8;

    double rad = sqrt(sqrt(sqrt(x8 + y8)));
    transverse = (rad - irad ) / (orad - irad);

    if (lit && transverse > 0 && transverse < 1) {
      double rad_x = rad * x8 / (x * (x8 + y8));
      double rad_y = rad * y8 / (y * (x8 + y8));

      double h = sqrt(transverse * (1-transverse)) * height;
      double dh = (.5 - transverse) / h / (orad - irad) * height * height;

      //cout << transverse << ":" << h << " " << dh << "\n";
      //cout << rad_x << "," << rad_y << "\n";

      double nx = -rad_x * dh;
      double ny = -rad_y * dh;
      double nz = 1;

      
      double m = 1 / sqrt(nx * nx + ny * ny + nz * nz);
      nz *= m;
      ny *= m;
      nz *= m;

      //const double light[3] = { -.5, .5, 1/sqrt(2) };
      const double light[3] = { -.5/sqrt(2+sqrt(2)), 
				.5/sqrt(2+sqrt(2)), 
				(1+1/sqrt(2))/sqrt(2+sqrt(2)) };
      
      dotlight = light[0] * nx - light[1] * ny + light[2] * nz;
      if (dotlight < 0) dotlight = 0;
      //cout << " --> " << dotlight << "\n";

    } else {
      dotlight = 0;
    }

    if(transverse < 0) transverse = 0;
    if(transverse > 1) transverse = 1;
    return (rad > irad) + (rad > orad);

}


void regentex() {
    AlphaRaster rb(ATEXSIZE,ATEXSIZE);
    RGBARaster shadow(ATEXSIZE, ATEXSIZE);

    for(int x=0; x<ATEXSIZE; x++) {
	for(int y=0; y<ATEXSIZE; y++) {
	    double dx = x/(ATEXSIZE+1.0) * 2 - 1;
	    double dy = y/(ATEXSIZE+1.0) * 2 - 1;


	    int salpha = 127 * (region(dx-0.05, dy-0.05) != 0);

	    int alpha = 255 * (region(dx, dy) == 0);
	    rb(x, y) = alpha; 

	    salpha = salpha >? 255 * (region(dx, dy) == 1);
	    salpha = salpha * (region(dx, dy, true) != 2);

	    double highlightstrength = 4*transverse*(1-transverse);


	    double r, g, b;
#if 0
	    if(highlightstrength < 0.25) {
		r = g = b = 0;
	    } else if(highlightstrength < 0.98) {
		b = (highlightstrength) * 0.5 + 0.5;
		r = highlightstrength * 0.5;
		g = highlightstrength * 0.5;
	    } else {
		b = 0.4;
		r = 1; g = 1;
	    } 
#else
	    if (transverse <= 0 || transverse >= 1) {
	      r = g = b = 0;
	    } else {
	      r = g = 0; b = .5;

	      double specular = dotlight;
	      specular *= specular;
	      specular *= specular;
	      specular *= specular;
	      
	      r += specular * .5;
	      g += specular * 1;
	      b += specular * .5;
	    }
#endif

	    int shadr = (int)(255 * r);
	    int shadg = (int)(255 * g);
	    int shadb = (int)(255 * b);

	    shadow(x, y) = (salpha << 24) + (shadb << 16) + (shadg << 8) + (shadr << 0);
	}
    }

    if(!stencilid) stencilid.alloc();
    stencilid.bind();
    rb.call_gluBuild2DMipmaps();

    if(!viewportid) viewportid.alloc();
    viewportid.bind();
    shadow.call_gluBuild2DMipmaps();

#define BGSIZE 128
    RGBARaster im(BGSIZE,BGSIZE);

    for(int x=0; x<BGSIZE; x++) {
	for(int y=0; y<BGSIZE; y++) {
	    double col = 0.5 + 0.2 * 
		sin(x*M_PI*8/BGSIZE) * 
		sin(y*M_PI*8/BGSIZE);
	    int icol = int(255 * dith(col));
	    im(x,y) = icol + (icol << 8) + (icol << 16) + (255 << 24);
	}
    }

    if(!bgid) bgid.alloc();

    bgid.bind();

    im.call_gluBuild2DMipmaps();

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

}


class EH : public Eventhandler {
public:
    virtual void keystroke(const char *str) {
	string s(str);
	if(s=="Up") 
	    power++;
	if(s=="Down") 
	    power--;
	if(s=="r") 
	    roundcorners = !roundcorners;
	regentex();
    }
};



int main() {
    WindowSystem *ws = WindowSystem::getInstance();
    w  = ws->openWindow(0, 0, 1000, 1000);
    w->setCurrent();
    sleep(1);



    regentex();

    


    Gummi::Font::Font *gf0 = ws->loadFont("../../../gzz/gfx/gl/a010013l.pfb", 60);
      Gummi::Font::Font *gf = new Gummi::Font::Font_Bordered(
		    gf0, 20);
      textrenderer = new TextRenderer(gf);




    glNewList(1, GL_COMPILE);
    sepquads();
    glEndList();

    w->setEventHandler(new EH());


    cout << "Main loop\n";
    while(true) {
	ws->eventLoop(false);
	repaint();
    }
    cout << "Main exiting\n";
}




See more files for this project here

gzz

An implementation of Ted Nelson's ZZstructure. ZZstructure is a new type of programming platform for structured data.

Project homepage: http://savannah.nongnu.org/projects/gzz
Programming language(s): C++,Java,Python
License: lgpl21

  Makefile
  goal.jpeg
  vpt.cxx