Skip to content

Instantly share code, notes, and snippets.

@carbolymer
Created October 24, 2012 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbolymer/3948702 to your computer and use it in GitHub Desktop.
Save carbolymer/3948702 to your computer and use it in GitHub Desktop.
/* trdemo.c */
/*
* Test/demonstration of tile rendering utility library.
* See tr.h for more info.
*
* Brian Paul
* April 1997
*/
#include <iostream>
#include <vector>
#include <cstdio>
#include <fstream>
#include <stdlib.h>
#include <sstream>
#include "GL/glut.h"
#include "tr.h"
#include "argon.h"
#include "tr.c"
using namespace std;
#define TILESIZE 100
#define NUMBALLS 125
static GLfloat BallPos[NUMBALLS][3];
static GLfloat BallSize = 0.06;
static GLfloat BallColor[4] = {0.8, 0.5, 1., 0.0};
static unsigned long int counter = 0;
vector<vect*> crystal;
static GLboolean Perspective = GL_TRUE;
static int WindowWidth, WindowHeight;
void loadData(vector<vect*> &crystal)
{
vect *ptr;
char buffer[256];
int nlCounter = 0, i,j;
double x,y,z, trash;
ifstream infile("avs.dat", ifstream::in);
while(infile.good())
{
ptr = new vect[NUMBALLS];
for(j = 0; j < NUMBALLS; ++j)
{
for(i=0; i < 256; ++i)
buffer[i] = '\0';
infile >> buffer;
if(buffer[0] == '\0')
continue;
std::stringstream(buffer) >> ptr[j].x;
infile >> ptr[j].y;
infile >> ptr[j].z;
infile >> trash >> trash >> trash; // momenta
//cout << j << ":\t" << ptr[j].x << "\t" << ptr[j].y << "\t" << ptr[j].z << endl;
}
//cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
crystal.push_back(ptr);
}
infile.close();
}
/* Draw my stuff */
static void DrawScene(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (i=0;i<NUMBALLS;i++) {
int t;
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, BallColor);
glPushMatrix();
glTranslatef(BallPos[i][0], BallPos[i][1], BallPos[i][2]);
t = 12 + (int) (BallSize * 12);
glutSolidSphere(BallSize, t, t);
glPopMatrix();
}
}
/* Do a demonstration of tiled rendering */
static void Display(void)
{
GLubyte *image;
int tile = 0;
TRcontext *tr;
int i;
/* allocate final image buffer */
image = (GLubyte*) malloc(WindowWidth * WindowHeight * 4 * sizeof(GLubyte));
if (!image) {
printf("Malloc failed!\n");
return;
}
/* Setup tiled rendering. Each tile is TILESIZE x TILESIZE pixels. */
tr = trNew();
trSetup(tr, WindowWidth, WindowHeight, (GLubyte *) image,
TILESIZE, TILESIZE);
if (Perspective)
trFrustum(tr, -1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
else
trOrtho(tr, -3.0, 3.0, -3.0, 3.0, -3.0, 3.0);
/* Draw tiles */
do {
trBeginTile(tr);
DrawScene();
tile++;
} while (trEndTile(tr));
printf("%d tiles drawn\n", tile);
trDelete(tr);
/* show final image, might otherwise write it to a file */
glDrawPixels(WindowWidth, WindowHeight, GL_RGBA, GL_UNSIGNED_BYTE, image);
glFlush();
free(image);
}
static void Reshape( int width, int height )
{
WindowWidth = width;
WindowHeight = height;
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
if (Perspective)
glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
else
glOrtho( -3.0, 3.0, -3.0, 3.0, -3.0, 3.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
if (Perspective)
glTranslatef( 0.0, 0.0, -15.0 );
}
static void Refresh(int foo)
{
if(foo == 0)
{
glutTimerFunc(300,Refresh,0);
Display();
}
if(counter < crystal.size() - 1)
++counter;
for (int i=0;i<NUMBALLS;i++) {
// range -3.5...3.5
BallPos[i][0] = crystal[counter][i].x*1.5;
BallPos[i][1] = crystal[counter][i].y*1.5;
BallPos[i][2] = crystal[counter][i].z*1.5;
}
}
/* ARGSUSED1 */
static void Key( unsigned char key, int x, int y )
{
switch (key) {
case 27:
exit(0);
break;
}
Refresh(0);
glutPostRedisplay();
}
static void Init( void )
{
static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
}
int main( int argc, char *argv[] )
{
loadData(crystal);
Refresh(1);
glutInit( &argc, argv );
glutInitWindowPosition(0, 0);
glutInitWindowSize( 1000, 1000 );
glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH );
glutCreateWindow( "tile rendering" );
Init();
glutReshapeFunc( Reshape );
glutKeyboardFunc( Key );
glutDisplayFunc( Display );
glutTimerFunc(300,Refresh,0);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment