[BACK]Return to glray3.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / cfep

Annotation of OpenXM/src/cfep/glray3.c, Revision 1.1.1.1

1.1       takayama    1: /*
                      2:  * This program is under the GNU GPL.
                      3:  * Use at your own risk.
                      4:  *
                      5:  * written by David Bucciarelli (tech.hmw@plus.it)
                      6:  *            Humanware s.r.l.
                      7:  * MesaDemo. Mesa-5.1/progs/demos/ray.c. Modified by Nobuki Takayama. 2006-02-22.
                      8:  */
                      9:
                     10: #include <stdio.h>
                     11: #include <stdlib.h>
                     12: #include <string.h>
                     13: #include <math.h>
                     14: #include "myenv.h"
                     15:
                     16: #ifdef COCOA
                     17: #import <OpenGL/OpenGL.h>
                     18: #import <OpenGL/gl.h>
                     19: #import <OpenGL/glu.h>
                     20: #else
                     21: #include <GL/glut.h>
                     22: #endif
                     23:
                     24: #include "mygl.h"
                     25:
                     26: static int WIDTH = 640;
                     27: static int HEIGHT = 480;
                     28:
                     29: static GLint T0 = 0;
                     30: static GLint Frames = 0;
                     31:
                     32: #define BASESIZE 7.5f
                     33: #define SPHERE_RADIUS 0.75f
                     34:
                     35: #define TEX_CHECK_WIDTH 256
                     36: #define TEX_CHECK_HEIGHT 256
                     37: #define TEX_CHECK_SLOT_SIZE (TEX_CHECK_HEIGHT/16)
                     38: #define TEX_CHECK_NUMSLOT (TEX_CHECK_HEIGHT/TEX_CHECK_SLOT_SIZE)
                     39:
                     40: #define TEX_REFLECT_WIDTH 256
                     41: #define TEX_REFLECT_HEIGHT 256
                     42: #define TEX_REFLECT_SLOT_SIZE (TEX_REFLECT_HEIGHT/16)
                     43: #define TEX_REFLECT_NUMSLOT (TEX_REFLECT_HEIGHT/TEX_REFLECT_SLOT_SIZE)
                     44:
                     45: #ifndef M_PI
                     46: #define M_PI 3.1415926535
                     47: #endif
                     48:
                     49: #define EPSILON 0.0001
                     50:
                     51: #define clamp255(a)  ( (a)<(0.0f) ? (0.0f) : ((a)>(255.0f) ? (255.0f) : (a)) )
                     52:
                     53: #define fabs(x) ((x)<0.0f?-(x):(x))
                     54:
                     55: #define vequ(a,b) { (a)[0]=(b)[0]; (a)[1]=(b)[1]; (a)[2]=(b)[2]; }
                     56: #define vsub(a,b,c) { (a)[0]=(b)[0]-(c)[0]; (a)[1]=(b)[1]-(c)[1]; (a)[2]=(b)[2]-(c)[2]; }
                     57: #define        dprod(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2])
                     58: #define vnormalize(a,b) { \
                     59:   register float m_norm; \
                     60:   m_norm=sqrt((double)dprod((a),(a))); \
                     61:   (a)[0] /=m_norm; \
                     62:   (a)[1] /=m_norm; \
                     63:   (a)[2] /=m_norm; }
                     64:
                     65: static GLubyte checkmap[TEX_CHECK_HEIGHT][TEX_CHECK_WIDTH][3];
                     66: static GLuint checkid;
                     67: static int checkmap_currentslot = 0;
                     68:
                     69: static GLubyte reflectmap[TEX_REFLECT_HEIGHT][TEX_REFLECT_WIDTH][3];
                     70: static GLuint reflectid;
                     71: static int reflectmap_currentslot = 0;
                     72:
                     73: static GLuint lightdlist;
                     74: static GLuint objdlist;
                     75:
                     76: static float lightpos[3] = { 2.1, 2.1, 2.8 };
                     77: static float objpos[3] = { 0.0, 0.0, 1.0 };
                     78:
                     79: static float sphere_pos[TEX_CHECK_HEIGHT][TEX_REFLECT_WIDTH][3];
                     80:
                     81: static int win = 0;
                     82:
                     83: static float fogcolor[4] = { 0.05, 0.05, 0.05, 1.0 };
                     84:
                     85: static float obs[3] = { 7.0, 0.0, 2.0 };
                     86: static float dir[3];
                     87: static float v = 0.0;
                     88: static float alpha = -90.0;
                     89: static float beta = 90.0;
                     90:
                     91: static int fog = 1;
                     92: static int bfcull = 1;
                     93: static int poutline = 0;
                     94: static int help = 1;
                     95: static int showcheckmap = 1;
                     96: static int showreflectmap = 1;
                     97: static int joyavailable = 0;
                     98: static int joyactive = 0;
                     99:
                    100: static void
                    101: calcposobs(void)
                    102: {
                    103:    dir[0] = sin(alpha * M_PI / 180.0);
                    104:    dir[1] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0);
                    105:    dir[2] = cos(beta * M_PI / 180.0);
                    106:
                    107:    if (dir[0] < 1.0e-5 && dir[0] > -1.0e-5)
                    108:       dir[0] = 0;
                    109:    if (dir[1] < 1.0e-5 && dir[1] > -1.0e-5)
                    110:       dir[1] = 0;
                    111:    if (dir[2] < 1.0e-5 && dir[2] > -1.0e-5)
                    112:       dir[2] = 0;
                    113:
                    114:    obs[0] += v * dir[0];
                    115:    obs[1] += v * dir[1];
                    116:    obs[2] += v * dir[2];
                    117: }
                    118:
                    119:
                    120:
                    121: static void
                    122: reshape(int w, int h)
                    123: {
                    124:    WIDTH = w;
                    125:    HEIGHT = h;
                    126:    glViewport(0, 0, w, h);
                    127:    glMatrixMode(GL_PROJECTION);
                    128:    glLoadIdentity();
                    129:    gluPerspective(45.0, w / (float) h, 0.8, 40.0);
                    130:    glMatrixMode(GL_MODELVIEW);
                    131:    glLoadIdentity();
                    132: }
                    133:
                    134:
                    135:
                    136: static GLboolean
                    137: seelight(float p[3], float dir[3])
                    138: {
                    139:    float c[3], b, a, d, t, dist[3];
                    140:
                    141:    vsub(c, p, objpos);
                    142:    b = -dprod(c, dir);
                    143:    a = dprod(c, c) - SPHERE_RADIUS * SPHERE_RADIUS;
                    144:
                    145:    if ((d = b * b - a) < 0.0 || (b < 0.0 && a > 0.0))
                    146:       return GL_FALSE;
                    147:
                    148:    d = sqrt(d);
                    149:
                    150:    t = b - d;
                    151:
                    152:    if (t < EPSILON) {
                    153:       t = b + d;
                    154:       if (t < EPSILON)
                    155:         return GL_FALSE;
                    156:    }
                    157:
                    158:    vsub(dist, lightpos, p);
                    159:    if (dprod(dist, dist) < t * t)
                    160:       return GL_FALSE;
                    161:
                    162:    return GL_TRUE;
                    163: }
                    164:
                    165: static int
                    166: colorcheckmap(float ppos[3], float c[3])
                    167: {
                    168:    static float norm[3] = { 0.0f, 0.0f, 1.0f };
                    169:    float ldir[3], vdir[3], h[3], dfact, kfact, r, g, b;
                    170:    int x, y;
                    171:
                    172:    x = (int) ((ppos[0] + BASESIZE / 2) * (10.0f / BASESIZE));
                    173:    if ((x < 0) || (x > 10))
                    174:       return GL_FALSE;
                    175:
                    176:    y = (int) ((ppos[1] + BASESIZE / 2) * (10.0f / BASESIZE));
                    177:    if ((y < 0) || (y > 10))
                    178:       return GL_FALSE;
                    179:
                    180:    r = 255.0f;
                    181:    if (y & 1) {
                    182:       if (x & 1)
                    183:         g = 255.0f;
                    184:       else
                    185:         g = 0.0f;
                    186:    }
                    187:    else {
                    188:       if (x & 1)
                    189:         g = 0.0f;
                    190:       else
                    191:         g = 255.0f;
                    192:    }
                    193:    b = 0.0f;
                    194:
                    195:    vsub(ldir, lightpos, ppos);
                    196:    vnormalize(ldir, ldir);
                    197:
                    198:    if (seelight(ppos, ldir)) {
                    199:       c[0] = r * 0.05f;
                    200:       c[1] = g * 0.05f;
                    201:       c[2] = b * 0.05f;
                    202:
                    203:       return GL_TRUE;
                    204:    }
                    205:
                    206:    dfact = dprod(ldir, norm);
                    207:    if (dfact < 0.0f)
                    208:       dfact = 0.0f;
                    209:
                    210:    vsub(vdir, obs, ppos);
                    211:    vnormalize(vdir, vdir);
                    212:    h[0] = 0.5f * (vdir[0] + ldir[0]);
                    213:    h[1] = 0.5f * (vdir[1] + ldir[1]);
                    214:    h[2] = 0.5f * (vdir[2] + ldir[2]);
                    215:    kfact = dprod(h, norm);
                    216:    kfact = pow(kfact, 6.0) * 7.0 * 255.0;
                    217:
                    218:    r = r * dfact + kfact;
                    219:    g = g * dfact + kfact;
                    220:    b = b * dfact + kfact;
                    221:
                    222:    c[0] = clamp255(r);
                    223:    c[1] = clamp255(g);
                    224:    c[2] = clamp255(b);
                    225:
                    226:    return GL_TRUE;
                    227: }
                    228:
                    229: static void
                    230: updatecheckmap(int slot)
                    231: {
                    232:    float c[3], ppos[3];
                    233:    int x, y;
                    234:
                    235:    glBindTexture(GL_TEXTURE_2D, checkid);
                    236:
                    237:    ppos[2] = 0.0f;
                    238:    for (y = slot * TEX_CHECK_SLOT_SIZE; y < (slot + 1) * TEX_CHECK_SLOT_SIZE;
                    239:        y++) {
                    240:       ppos[1] = (y / (float) TEX_CHECK_HEIGHT) * BASESIZE - BASESIZE / 2;
                    241:
                    242:       for (x = 0; x < TEX_CHECK_WIDTH; x++) {
                    243:         ppos[0] = (x / (float) TEX_CHECK_WIDTH) * BASESIZE - BASESIZE / 2;
                    244:
                    245:         colorcheckmap(ppos, c);
                    246:         checkmap[y][x][0] = (GLubyte) c[0];
                    247:         checkmap[y][x][1] = (GLubyte) c[1];
                    248:         checkmap[y][x][2] = (GLubyte) c[2];
                    249:       }
                    250:    }
                    251:
                    252:    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, slot * TEX_CHECK_SLOT_SIZE,
                    253:                   TEX_CHECK_WIDTH, TEX_CHECK_SLOT_SIZE, GL_RGB,
                    254:                   GL_UNSIGNED_BYTE,
                    255:                   &checkmap[slot * TEX_CHECK_SLOT_SIZE][0][0]);
                    256:
                    257: }
                    258:
                    259: static void
                    260: updatereflectmap(int slot)
                    261: {
                    262:    float rf, r, g, b, t, dfact, kfact, rdir[3];
                    263:    float rcol[3], ppos[3], norm[3], ldir[3], h[3], vdir[3], planepos[3];
                    264:    int x, y;
                    265:
                    266:    glBindTexture(GL_TEXTURE_2D, reflectid);
                    267:
                    268:    for (y = slot * TEX_REFLECT_SLOT_SIZE;
                    269:        y < (slot + 1) * TEX_REFLECT_SLOT_SIZE; y++)
                    270:       for (x = 0; x < TEX_REFLECT_WIDTH; x++) {
                    271:         ppos[0] = sphere_pos[y][x][0] + objpos[0];
                    272:         ppos[1] = sphere_pos[y][x][1] + objpos[1];
                    273:         ppos[2] = sphere_pos[y][x][2] + objpos[2];
                    274:
                    275:         vsub(norm, ppos, objpos);
                    276:         vnormalize(norm, norm);
                    277:
                    278:         vsub(ldir, lightpos, ppos);
                    279:         vnormalize(ldir, ldir);
                    280:         vsub(vdir, obs, ppos);
                    281:         vnormalize(vdir, vdir);
                    282:
                    283:         rf = 2.0f * dprod(norm, vdir);
                    284:         if (rf > EPSILON) {
                    285:            rdir[0] = rf * norm[0] - vdir[0];
                    286:            rdir[1] = rf * norm[1] - vdir[1];
                    287:            rdir[2] = rf * norm[2] - vdir[2];
                    288:
                    289:            t = -objpos[2] / rdir[2];
                    290:
                    291:            if (t > EPSILON) {
                    292:               planepos[0] = objpos[0] + t * rdir[0];
                    293:               planepos[1] = objpos[1] + t * rdir[1];
                    294:               planepos[2] = 0.0f;
                    295:
                    296:               if (!colorcheckmap(planepos, rcol))
                    297:                  rcol[0] = rcol[1] = rcol[2] = 0.0f;
                    298:            }
                    299:            else
                    300:               rcol[0] = rcol[1] = rcol[2] = 0.0f;
                    301:         }
                    302:         else
                    303:            rcol[0] = rcol[1] = rcol[2] = 0.0f;
                    304:
                    305:         dfact = 0.1f * dprod(ldir, norm);
                    306:
                    307:         if (dfact < 0.0f) {
                    308:            dfact = 0.0f;
                    309:            kfact = 0.0f;
                    310:         }
                    311:         else {
                    312:            h[0] = 0.5f * (vdir[0] + ldir[0]);
                    313:            h[1] = 0.5f * (vdir[1] + ldir[1]);
                    314:            h[2] = 0.5f * (vdir[2] + ldir[2]);
                    315:            kfact = dprod(h, norm);
                    316:             kfact = pow(kfact, 4.0);
                    317:             if (kfact < 1.0e-10)
                    318:                kfact = 0.0;
                    319:          }
                    320:
                    321:         r = dfact + kfact;
                    322:         g = dfact + kfact;
                    323:         b = dfact + kfact;
                    324:
                    325:         r *= 255.0f;
                    326:         g *= 255.0f;
                    327:         b *= 255.0f;
                    328:
                    329:         r += rcol[0];
                    330:         g += rcol[1];
                    331:         b += rcol[2];
                    332:
                    333:         r = clamp255(r);
                    334:         g = clamp255(g);
                    335:         b = clamp255(b);
                    336:
                    337:         reflectmap[y][x][0] = (GLubyte) r;
                    338:         reflectmap[y][x][1] = (GLubyte) g;
                    339:         reflectmap[y][x][2] = (GLubyte) b;
                    340:       }
                    341:
                    342:    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, slot * TEX_REFLECT_SLOT_SIZE,
                    343:                   TEX_REFLECT_WIDTH, TEX_REFLECT_SLOT_SIZE, GL_RGB,
                    344:                   GL_UNSIGNED_BYTE,
                    345:                   &reflectmap[slot * TEX_REFLECT_SLOT_SIZE][0][0]);
                    346: }
                    347:
                    348: static void
                    349: drawbase(void)
                    350: {
                    351:    glColor3f(0.0, 0.0, 0.0);
                    352:    glBindTexture(GL_TEXTURE_2D, checkid);
                    353:    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
                    354:
                    355:    glBegin(GL_QUADS);
                    356:    glTexCoord2f(0.0f, 0.0f);
                    357:    glVertex3f(-BASESIZE / 2.0f, -BASESIZE / 2.0f, 0.0f);
                    358:
                    359:    glTexCoord2f(1.0f, 0.0f);
                    360:    glVertex3f(BASESIZE / 2.0f, -BASESIZE / 2.0f, 0.0f);
                    361:
                    362:    glTexCoord2f(1.0f, 1.0f);
                    363:    glVertex3f(BASESIZE / 2.0f, BASESIZE / 2.0f, 0.0f);
                    364:
                    365:    glTexCoord2f(0.0f, 1.0f);
                    366:    glVertex3f(-BASESIZE / 2.0f, BASESIZE / 2.0f, 0.0f);
                    367:
                    368:    glEnd();
                    369: }
                    370:
                    371: static void
                    372: drawobj(void)
                    373: {
                    374:    glColor3f(0.0, 0.0, 0.0);
                    375:    glBindTexture(GL_TEXTURE_2D, reflectid);
                    376:    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
                    377:
                    378:    glPushMatrix();
                    379:    glTranslatef(objpos[0], objpos[1], objpos[2]);
                    380:    glCallList(objdlist);
                    381:    glPopMatrix();
                    382: }
                    383:
                    384:
                    385: static void
                    386: updatemaps(void)
                    387: {
                    388:    updatecheckmap(checkmap_currentslot);
                    389:    checkmap_currentslot = (checkmap_currentslot + 1) % TEX_CHECK_NUMSLOT;
                    390:
                    391:    updatereflectmap(reflectmap_currentslot);
                    392:    reflectmap_currentslot =
                    393:       (reflectmap_currentslot + 1) % TEX_REFLECT_NUMSLOT;
                    394: }
                    395:
                    396: static void
                    397: draw(void)
                    398: {
                    399:    static char frbuf[80] = "";
                    400:
                    401:
                    402:    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                    403:
                    404:    glEnable(GL_TEXTURE_2D);
                    405:    glEnable(GL_DEPTH_TEST);
                    406:    if (fog)
                    407:       glEnable(GL_FOG);
                    408:    else
                    409:       glDisable(GL_FOG);
                    410:
                    411:    glPushMatrix();
                    412:    calcposobs();
                    413:
                    414:    gluLookAt(obs[0], obs[1], obs[2],
                    415:             obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2],
                    416:             0.0, 0.0, 1.0);
                    417:
                    418:    drawbase();
                    419:    drawobj();
                    420:
                    421:    glColor3f(1.0, 1.0, 1.0);
                    422:    glDisable(GL_TEXTURE_2D);
                    423:
                    424:    glPushMatrix();
                    425:    glTranslatef(lightpos[0], lightpos[1], lightpos[2]);
                    426:    glCallList(lightdlist);
                    427:    glPopMatrix();
                    428:
                    429:    glPopMatrix();
                    430:
                    431:    glDisable(GL_DEPTH_TEST);
                    432:    glDisable(GL_FOG);
                    433:
                    434:    glMatrixMode(GL_PROJECTION);
                    435:    glPushMatrix();
                    436:    glLoadIdentity();
                    437:    glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0);
                    438:    glMatrixMode(GL_MODELVIEW);
                    439:
                    440:    glColor3f(0.0f, 0.3f, 1.0f);
                    441:
                    442:    if (showcheckmap) {
                    443:       glEnable(GL_TEXTURE_2D);
                    444:       glBindTexture(GL_TEXTURE_2D, checkid);
                    445:       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
                    446:
                    447:       glBegin(GL_QUADS);
                    448:       glTexCoord2f(1.0f, 0.0f);
                    449:       glVertex2i(10, 30);
                    450:       glTexCoord2f(1.0f, 1.0f);
                    451:       glVertex2i(10 + 90, 30);
                    452:       glTexCoord2f(0.0f, 1.0f);
                    453:       glVertex2i(10 + 90, 30 + 90);
                    454:       glTexCoord2f(0.0f, 0.0f);
                    455:       glVertex2i(10, 30 + 90);
                    456:       glEnd();
                    457:
                    458:       glDisable(GL_TEXTURE_2D);
                    459:       glBegin(GL_LINE_LOOP);
                    460:       glVertex2i(10, 30);
                    461:       glVertex2i(10 + 90, 30);
                    462:       glVertex2i(10 + 90, 30 + 90);
                    463:       glVertex2i(10, 30 + 90);
                    464:       glEnd();
                    465:       glRasterPos2i(105, 65);
                    466:    }
                    467:
                    468:    if (showreflectmap) {
                    469:       glEnable(GL_TEXTURE_2D);
                    470:       glBindTexture(GL_TEXTURE_2D, reflectid);
                    471:       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
                    472:
                    473:       glBegin(GL_QUADS);
                    474:       glTexCoord2f(1.0f, 0.0f);
                    475:       glVertex2i(540, 30);
                    476:       glTexCoord2f(1.0f, 1.0f);
                    477:       glVertex2i(540 + 90, 30);
                    478:       glTexCoord2f(0.0f, 1.0f);
                    479:       glVertex2i(540 + 90, 30 + 90);
                    480:       glTexCoord2f(0.0f, 0.0f);
                    481:       glVertex2i(540, 30 + 90);
                    482:       glEnd();
                    483:
                    484:       glDisable(GL_TEXTURE_2D);
                    485:       glBegin(GL_LINE_LOOP);
                    486:       glVertex2i(540, 30);
                    487:       glVertex2i(540 + 90, 30);
                    488:       glVertex2i(540 + 90, 30 + 90);
                    489:       glVertex2i(540, 30 + 90);
                    490:       glEnd();
                    491:       glRasterPos2i(360, 65);
                    492:    }
                    493:
                    494:    glDisable(GL_TEXTURE_2D);
                    495:
                    496:    glRasterPos2i(10, 10);
                    497:    glRasterPos2i(360, 470);
                    498:
                    499:
                    500:    glMatrixMode(GL_PROJECTION);
                    501:    glPopMatrix();
                    502:    glMatrixMode(GL_MODELVIEW);
                    503:
                    504: }
                    505:
                    506: static void
                    507: inittextures(void)
                    508: {
                    509:    int y;
                    510:
                    511:    glGenTextures(1, &checkid);
                    512:    glBindTexture(GL_TEXTURE_2D, checkid);
                    513:
                    514:    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
                    515:    glTexImage2D(GL_TEXTURE_2D, 0, 3, TEX_CHECK_WIDTH, TEX_CHECK_HEIGHT,
                    516:                0, GL_RGB, GL_UNSIGNED_BYTE, checkmap);
                    517:
                    518:    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
                    519:    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
                    520:
                    521:    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                    522:    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                    523:
                    524:    for (y = 0; y < TEX_CHECK_NUMSLOT; y++)
                    525:       updatecheckmap(y);
                    526:
                    527:
                    528:
                    529:    glGenTextures(1, &reflectid);
                    530:    glBindTexture(GL_TEXTURE_2D, reflectid);
                    531:
                    532:    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
                    533:    glTexImage2D(GL_TEXTURE_2D, 0, 3, TEX_REFLECT_WIDTH, TEX_REFLECT_HEIGHT,
                    534:                0, GL_RGB, GL_UNSIGNED_BYTE, reflectmap);
                    535:
                    536:    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
                    537:    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
                    538:
                    539:    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                    540:    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                    541:
                    542:    for (y = 0; y < TEX_REFLECT_NUMSLOT; y++)
                    543:       updatereflectmap(y);
                    544:
                    545:
                    546: }
                    547:
                    548: static void
                    549: initspherepos(void)
                    550: {
                    551:    float alpha, beta, sa, ca, sb, cb;
                    552:    int x, y;
                    553:
                    554:    for (y = 0; y < TEX_REFLECT_HEIGHT; y++) {
                    555:       beta = M_PI - y * (M_PI / TEX_REFLECT_HEIGHT);
                    556:
                    557:       for (x = 0; x < TEX_REFLECT_WIDTH; x++) {
                    558:         alpha = -x * (2.0f * M_PI / TEX_REFLECT_WIDTH);
                    559:
                    560:         sa = sin(alpha);
                    561:         ca = cos(alpha);
                    562:
                    563:         sb = sin(beta);
                    564:         cb = cos(beta);
                    565:
                    566:         sphere_pos[y][x][0] = SPHERE_RADIUS * sa * sb;
                    567:         sphere_pos[y][x][1] = SPHERE_RADIUS * ca * sb;
                    568:         sphere_pos[y][x][2] = SPHERE_RADIUS * cb;
                    569:       }
                    570:    }
                    571: }
                    572:
                    573: static void
                    574: initdlists(void)
                    575: {
                    576:    GLUquadricObj *obj;
                    577:
                    578:    obj = gluNewQuadric();
                    579:    lightdlist = glGenLists(1);
                    580:    glNewList(lightdlist, GL_COMPILE);
                    581:    gluQuadricDrawStyle(obj, GLU_FILL);
                    582:    gluQuadricNormals(obj, GLU_NONE);
                    583:    gluQuadricTexture(obj, GL_TRUE);
                    584:    gluSphere(obj, 0.25f, 6, 6);  // 0.25 is the radius.
                    585:    glEndList();
                    586:
                    587:    objdlist = glGenLists(1);
                    588:    glNewList(objdlist, GL_COMPILE);
                    589:    gluQuadricDrawStyle(obj, GLU_FILL);
                    590:    gluQuadricNormals(obj, GLU_NONE);
                    591:    gluQuadricTexture(obj, GL_TRUE);
                    592:    gluSphere(obj, SPHERE_RADIUS, 16, 16);  // main sphere radius is 0.75
                    593:    glEndList();
                    594:
                    595: }
                    596:
                    597: #ifdef MYTEST
                    598: main(int argc, char **argv) {
                    599:   /* glutInit(&argc, argv);
                    600:   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
                    601:   glutInitWindowSize(400,400);
                    602:   glutInitWindowPosition(100,100);
                    603:   glutCreateWindow("glicosahedron");
                    604:   */ // They does not work.
                    605:
                    606:    glutInitWindowPosition(0, 0);
                    607:    glutInitWindowSize(WIDTH, HEIGHT);
                    608:    // glutInit(&ac, av);
                    609:
                    610:    //glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
                    611:    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
                    612:
                    613:    if (!(win = glutCreateWindow("Ray"))) {
                    614:       fprintf(stderr, "Error, couldn't open window\n");
                    615:       return -1;
                    616:    }
                    617:
                    618:
                    619:   glib3_ray_init();
                    620:   glib3_ray_change_alpha(-90.0);
                    621:   glib3_ray_change_beta(90.0);
                    622:   glutReshapeFunc(reshape);
                    623:   glutDisplayFunc(glib3_ray);
                    624:   // glutIdleFunc(glib3_ray);
                    625:   glib3_ray();
                    626:   glutMainLoop();
                    627: }
                    628: #endif
                    629:
                    630: void glib3_ray_change_alpha(float e) { alpha = e; }
                    631: void glib3_ray_change_beta(float e) { beta = e; }
                    632:
                    633:
                    634: void glib3_ray_reshape(float w,float h) {
                    635:   reshape(w,h);
                    636: }
                    637: void glib3_ray() {
                    638:   // glib3_ray_change_alpha(-90.0+xnear*5);
                    639:   // glib3_ray_change_beta(90.0+ynear*3);
                    640:   draw();
                    641: }
                    642:
                    643: void glib3_ray_init() {
                    644:
                    645:
                    646:    reshape(WIDTH, HEIGHT);
                    647:
                    648:    glShadeModel(GL_FLAT);
                    649:    glEnable(GL_DEPTH_TEST);
                    650:    glDepthFunc(GL_LEQUAL);
                    651:    glEnable(GL_CULL_FACE);
                    652:    glEnable(GL_TEXTURE_2D);
                    653:    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                    654:
                    655:    glEnable(GL_FOG);
                    656:    glFogi(GL_FOG_MODE, GL_EXP2);
                    657:    glFogfv(GL_FOG_COLOR, fogcolor);
                    658:
                    659:    glFogf(GL_FOG_DENSITY, 0.01);
                    660:
                    661:    calcposobs();
                    662:
                    663:    initspherepos();
                    664:
                    665:    inittextures();
                    666:    initdlists();
                    667:
                    668:    glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]);
                    669: }
                    670:
                    671:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>