#include "culooks.h"
#include <vector>
-#include <GL/glut.h>
#include <iostream>
#include <math.h>
window *Win = new window;
int winsize[2];
- if (windowid == 0) {
- glutInit(argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
- }
-
Win->layout[0] = xcubes;
Win->layout[1] = ycubes;
Win->aspect = (double)xcubes/ycubes;
Windows.push_back( pair<int,culooks::window*>(0, Win) );
if (windowid == 0) {
- glXMakeCurrent(0,0,0);
- pthread_create(&glThreadId, 0, &drawing::glutThread, NULL);
+ comarg comArg;
+ comArg.argc = argc;
+ comArg.argv = argv;
+ pthread_create(&glThreadId, 0, &drawing::glutThread, &comArg);
}
mywid = windowid;
windowid++;
}
+void culooks::setLink (const int& cubeid, const int& posdir,
+ const float& red, const float& green, const float& blue, const float& alpha)
+{
+ Windows[mywid].second->cubes[cubeid].setLink(posdir, red, green, blue, alpha);
+}
+
+void culooks::setPlaq (const int& cubeid, const int& posdir,
+ const float& red, const float& green, const float& blue, const float& alpha)
+{
+ Windows[mywid].second->cubes[cubeid].setPlaq(posdir, red, green, blue, alpha);
+}
+
+void culooks::swapBuffers () {
+ for (int icube=0; icube<Windows[mywid].second->cubes.size(); icube++) {
+ Windows[mywid].second->cubes[icube].swapLinkBuffer();
+ Windows[mywid].second->cubes[icube].swapPlaqBuffer();
+ }
+ Windows[mywid].second->redisplay = true;
+}
+
+void culooks::hidePlaqs (const int& cubeid) {
+ Windows[mywid].second->cubes[cubeid].hidePlaqs();
+}
+
+void culooks::hidePlaqs () {
+ for (int icube=0; icube<Windows[mywid].second->cubes.size(); icube++)
+ Windows[mywid].second->cubes[icube].hidePlaqs();
+}
+
+void culooks::hideLinks (const int& cubeid) {
+ Windows[mywid].second->cubes[cubeid].hideLinks();
+}
+
+void culooks::hideLinks () {
+ for (int icube=0; icube<Windows[mywid].second->cubes.size(); icube++)
+ Windows[mywid].second->cubes[icube].hideLinks();
+}
+
+void culooks::setBgColor(const float& red, const float& green, const float& blue, const float& alpha)
+{
+ Windows[mywid].second->bgcolor[0] = red;
+ Windows[mywid].second->bgcolor[1] = green;
+ Windows[mywid].second->bgcolor[2] = blue;
+ Windows[mywid].second->bgcolor[3] = alpha;
+}
public:
culooks (const char* name, const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv);
+ void setLink (const int& cubeid, const int& posdir,
+ const float& red, const float& green, const float& blue, const float& alpha);
+ void setPlaq (const int& cubeid, const int& posdir,
+ const float& red, const float& green, const float& blue, const float& alpha);
+ void swapBuffers();
+ void hidePlaqs(const int& cubeid);
+ void hidePlaqs();
+ void hideLinks(const int& cubeid);
+ void hideLinks();
+ void setBgColor(const float& red, const float& green, const float& blue, const float& alpha);
private:
+
+ struct comarg {
+ int *argc;
+ char **argv;
+ };
class cube {
public:
- cube(int l);
+ cube (int l);
void draw();
int id;
float az, alt;
float zoom;
+ void setLink (const int& posdir,
+ const float& red, const float& green, const float& blue, const float& alpha);
+ void setPlaq (const int& posdir,
+ const float& red, const float& green, const float& blue, const float& alpha);
+ void swapLinkBuffer();
+ void swapPlaqBuffer();
+ void hidePlaqs();
+ void hideLinks();
private:
void drawAll();
void drawBox();
- static int allid;
+ static void drawFrame();
float *plaq;
float *link;
+ float *plaqbuf[2];
+ float *linkbuf[2];
int l;
+ int plaqbuf_active;
+ int linkbuf_active;
+ static int allid;
};
struct window {
int gwinid;
bool initialized;
string name;
+ float bgcolor[4];
+ bool redisplay;
};
class drawing
static void reshapeFunc(int w, int h);
static void displayFunc();
static void initWindow(int winid);
- static void idleFunc_master();
+ static void idleFunc();
public:
static void* glutThread(void *leer);
};
int culooks::cube::allid = 0;
+void culooks::cube::setLink (const int& posdir,
+ const float& red, const float& green, const float& blue, const float& alpha) {
+ linkbuf[(linkbuf_active+1)%2][ posdir*4 ] = red;
+ linkbuf[(linkbuf_active+1)%2][ posdir*4 + 1 ] = green;
+ linkbuf[(linkbuf_active+1)%2][ posdir*4 + 2 ] = blue;
+ linkbuf[(linkbuf_active+1)%2][ posdir*4 + 3 ] = alpha;
+}
+
+void culooks::cube::setPlaq (const int& posdir,
+ const float& red, const float& green, const float& blue, const float& alpha) {
+ plaqbuf[(plaqbuf_active+1)%2][ posdir*4 ] = red;
+ plaqbuf[(plaqbuf_active+1)%2][ posdir*4 + 1 ] = green;
+ plaqbuf[(plaqbuf_active+1)%2][ posdir*4 + 2 ] = blue;
+ plaqbuf[(plaqbuf_active+1)%2][ posdir*4 + 3 ] = alpha;
+}
+
+void culooks::cube::hidePlaqs()
+{
+ for (int ibuf=0; ibuf<2; ibuf++)
+ for (int i=0; i<l*l*l*3; i++) {
+ plaqbuf[ibuf][i*4+0]=0;
+ plaqbuf[ibuf][i*4+1]=0;
+ plaqbuf[ibuf][i*4+2]=0;
+ plaqbuf[ibuf][i*4+3]=0;
+ }
+}
+
+void culooks::cube::hideLinks()
+{
+ for (int ibuf=0; ibuf<2; ibuf++)
+ for (int i=0; i<l*l*l*3; i++) {
+ linkbuf[ibuf][i*4+0]=0;
+ linkbuf[ibuf][i*4+1]=0;
+ linkbuf[ibuf][i*4+2]=0;
+ linkbuf[ibuf][i*4+3]=0;
+ }
+}
+
+void culooks::cube::swapLinkBuffer()
+{
+ linkbuf_active = (linkbuf_active+1)%2;
+ link = linkbuf[linkbuf_active];
+}
+
+void culooks::cube::swapPlaqBuffer()
+{
+ plaqbuf_active = (plaqbuf_active+1)%2;
+ plaq = plaqbuf[plaqbuf_active];
+}
+
void culooks::cube::draw()
{
glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
+ glPushMatrix();
glScalef(zoom, zoom, zoom);
glRotatef(az, 0, 1, 0);
glRotatef(alt, 1, 0, 0);
-
drawAll();
+ glPopMatrix();
+ /* draw frame */
+ glPushMatrix();
+ glScalef(zoom*1.01, zoom*1.01, zoom*1.01);
+ glRotatef(az, 0, 1, 0);
+ glRotatef(alt, 1, 0, 0);
+ drawFrame();
glPopMatrix();
}
culooks::cube::cube(int _l)
{
l = _l;
- link = new float[l*l*l*3 * 4];
- plaq = new float[l*l*l*3 * 4];
+ plaqbuf[0] = new float[l*l*l*3 * 4];
+ plaqbuf[1] = new float[l*l*l*3 * 4];
+ linkbuf[0] = new float[l*l*l*3 * 4];
+ linkbuf[1] = new float[l*l*l*3 * 4];
+
+ linkbuf_active = 0;
+ plaqbuf_active = 0;
+
+ link = linkbuf[0];
+ plaq = plaqbuf[0];
az = 30;
alt = -20;
zoom = 0.6;
id = allid;
allid++;
- for (int i=0; i<l*l*l*3; i++) {
- link[i*4+0]=1; link[i*4+3]=0.6;
- plaq[i*4+1]=1; plaq[i*4+3]=0.2;
- }
+ for (int ibuf=0; ibuf<2; ibuf++)
+ for (int i=0; i<l*l*l*3; i++) {
+ linkbuf[ibuf][i*4+0]=1; linkbuf[ibuf][i*4+3]=0.6;
+ plaqbuf[ibuf][i*4+1]=1; plaqbuf[ibuf][i*4+3]=0.2;
+ }
+}
+
+void culooks::cube::drawFrame()
+{
+ glBegin(GL_LINES);
+
+ glColor4f(0, 0, 0, 1);
+
+ glVertex3f(-1.0f, 1.0f, -1.0f);
+ glVertex3f( 1.0f, 1.0f, -1.0f);
+ glVertex3f(-1.0f, -1.0f, -1.0f);
+ glVertex3f( 1.0f, -1.0f, -1.0f);
+ glVertex3f(-1.0f, 1.0f, 1.0f);
+ glVertex3f( 1.0f, 1.0f, 1.0f);
+ glVertex3f(-1.0f, -1.0f, 1.0f);
+ glVertex3f( 1.0f, -1.0f, 1.0f);
+
+ glVertex3f( 1.0f, 1.0f, -1.0f);
+ glVertex3f( 1.0f, -1.0f, -1.0f);
+ glVertex3f( -1.0f, 1.0f, -1.0f);
+ glVertex3f( -1.0f, -1.0f, -1.0f);
+ glVertex3f( 1.0f, 1.0f, 1.0f);
+ glVertex3f( 1.0f, -1.0f, 1.0f);
+ glVertex3f( -1.0f, 1.0f, 1.0f);
+ glVertex3f( -1.0f, -1.0f, 1.0f);
+
+ glVertex3f( -1.0f, -1.0f, 1.0f);
+ glVertex3f( -1.0f, -1.0f, -1.0f);
+ glVertex3f( -1.0f, 1.0f, 1.0f);
+ glVertex3f( -1.0f, 1.0f, -1.0f);
+ glVertex3f( 1.0f, -1.0f, 1.0f);
+ glVertex3f( 1.0f, -1.0f, -1.0f);
+ glVertex3f( 1.0f, 1.0f, 1.0f);
+ glVertex3f( 1.0f, 1.0f, -1.0f);
+
+ glEnd();
}
void culooks::drawing::displayFunc()
{
culooks::window *Win = getWin();
-
- glClearColor(0,0,0,0);
+
+ glClearColor(Win->bgcolor[0], Win->bgcolor[1], Win->bgcolor[2], Win->bgcolor[3]);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
void culooks::drawing::initWindow(int winid)
{
+
+ glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(culooks::Windows[winid].second->w, culooks::Windows[winid].second->h);
glutInitWindowPosition(winid*100,winid*100);
glutCreateWindow( ("culooks / " + culooks::Windows[winid].second->name).c_str() );
-
+
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable(GL_BLEND);
+ glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
+ glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
+ glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
+
glutDisplayFunc(&drawing::displayFunc);
glutReshapeFunc(&drawing::reshapeFunc);
glutMotionFunc(&drawing::motionFunc);
glutMouseFunc(&drawing::mouseFunc);
-
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glEnable( GL_BLEND );
+ glutIdleFunc(&drawing::idleFunc);
culooks::Windows[winid].first = glutGetWindow();
-
culooks::Windows[winid].second->initialized = true;
+ culooks::Windows[winid].second->redisplay = true;
}
-void culooks::drawing::idleFunc_master()
+void culooks::drawing::idleFunc()
{
- for (int iwin=0; iwin < culooks::Windows.size(); iwin++)
+ int activeWindow = glutGetWindow();
+
+ for (int iwin=0; iwin < culooks::Windows.size(); iwin++) {
if (!culooks::Windows[iwin].second->initialized)
initWindow(iwin);
+ if (culooks::Windows[iwin].second->redisplay) {
+ glutSetWindow(culooks::Windows[iwin].first);
+ glutPostRedisplay();
+ culooks::Windows[iwin].second->redisplay = false;
+ }
+ }
+
+ glutSetWindow(activeWindow);
}
-void* culooks::drawing::glutThread(void *leer)
+void* culooks::drawing::glutThread(void *_comArg)
{
+ comarg *comArg = (comarg*)_comArg;
+
+ glutInit(comArg->argc, comArg->argv);
initWindow(0);
- glutIdleFunc(&idleFunc_master);
glutMainLoop();
}