home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
egltk22.zip
/
wave.c
< prev
next >
Wrap
Text File
|
1998-02-21
|
18KB
|
519 lines
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Sample Escape GL Custom Screen Saver
// Copyright (c) 1997, Snow Storm Software
// All Rights Reserved
//
// This sample is based on the WAVE.C sample provided by Silicon Graphics Inc.
/*
* Copyright (c) 1993-1997, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc. not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission.
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
*
* US Government Users Restricted Rights
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement.
* Unpublished-- rights reserved under the copyright laws of the
* United States. Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
*
* OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
*/
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "custom.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "tk.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// general instructions:
// - copy this file to another name eg. cool.c
// - modify all functions examples to provide the processing you desire
// - add your project to the 'TARGETS =' part of 'MAKEFILE.' eg. 'TARGETS = sample.egl wave.egl cool.egl'
// - make sure the 'TOP =' part of 'MAKEFILE' correctly identifies the location of your opengl base directory
// eg. 'TOP = \opengl'
// - build your project using 'nmake'
// - copy your new screen saver module 'cool.egl' to any Escape GL sub-directory
// - reload Escape GL
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// global variables
// define any variables you require
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define PI 3.14159265358979323846
#define GETCOORD(frame, x, y) (&(theMesh.coords[frame*theMesh.numCoords+(x)+(y)*(theMesh.widthX+1)]))
#define GETFACET(frame, x, y) (&(theMesh.facets[frame*theMesh.numFacets+(x)+(y)*theMesh.widthX]))
GLenum rgb, doubleBuffer, directRender;
GLint colorIndexes1[3];
GLint colorIndexes2[3];
GLenum clearMask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
GLenum smooth = GL_FALSE;
GLenum lighting = GL_TRUE;
GLenum depth = GL_TRUE;
GLenum stepMode = GL_FALSE;
GLenum spinMode = GL_FALSE;
GLint contouring = 0;
GLint widthX, widthY;
GLint checkerSize;
float height;
//GLint frames, curFrame = 0, nextFrame = 0;
GLint frames, nextFrame = 0;
GLfloat curFrame = 0;
struct facet {
float color[3];
float normal[3];
};
struct coord {
float vertex[3];
float normal[3];
};
static struct mesh {
GLint widthX, widthY;
GLint numFacets;
GLint numCoords;
GLint frames;
struct coord *coords;
struct facet *facets;
} theMesh;
GLubyte contourTexture1[] = {
255, 255, 255, 255,
255, 255, 255, 255,
255, 255, 255, 255,
127, 127, 127, 127,
};
GLubyte contourTexture2[] = {
255, 255, 255, 255,
255, 127, 127, 127,
255, 127, 127, 127,
255, 127, 127, 127,
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// eglDesc
// defines the name of the module as it will appear in the drop down module list
// this name must be unique and not match any existing modules, nor custom modules
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char * eglDesc()
{
return( "Wave Sample" );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// eglIdle
// animates and displays the module
// the parameter 'step' simply defines the time in seconds between time steps, use this to control the animation speed
// the size of the step is based on the speed of the target system and the complexity of the module, slower modules
// will take larger steps
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void eglIdle( GLfloat s )
{
struct coord *coord;
struct facet *facet;
float *lastColor;
float *thisColor;
GLint i, j, curFrameIdx;
glClear(clearMask);
if (nextFrame || !stepMode) {
// curFrame++;
curFrame += s * 15.;
}
// if (curFrame >= theMesh.frames) {
// curFrame = 0;
// }
curFrameIdx = curFrame;
curFrameIdx %= theMesh.frames;
if ((nextFrame || !stepMode) && spinMode) {
// glRotatef(5.0, 0.0, 0.0, 1.0);
glRotatef( s * 180., 0.0, 0.0, 1.0);
}
nextFrame = 0;
for (i = 0; i < theMesh.widthX; i++) {
glBegin(GL_QUAD_STRIP);
lastColor = NULL;
for (j = 0; j < theMesh.widthY; j++) {
facet = GETFACET(curFrameIdx, i, j);
if (!smooth && lighting) {
glNormal3fv(facet->normal);
}
if (lighting) {
if (rgb) {
thisColor = facet->color;
glColor3fv(facet->color);
} else {
thisColor = facet->color;
glMaterialfv(GL_FRONT_AND_BACK, GL_COLOR_INDEXES,
facet->color);
}
} else {
if (rgb) {
thisColor = facet->color;
glColor3fv(facet->color);
} else {
thisColor = facet->color;
glIndexf(facet->color[1]);
}
}
if (!lastColor || (thisColor[0] != lastColor[0] && smooth)) {
if (lastColor) {
glEnd();
glBegin(GL_QUAD_STRIP);
}
coord = GETCOORD(curFrameIdx, i, j);
if (smooth && lighting) {
glNormal3fv(coord->normal);
}
glVertex3fv(coord->vertex);
coord = GETCOORD(curFrameIdx, i+1, j);
if (smooth && lighting) {
glNormal3fv(coord->normal);
}
glVertex3fv(coord->vertex);
}
coord = GETCOORD(curFrameIdx, i, j+1);
if (smooth && lighting) {
glNormal3fv(coord->normal);
}
glVertex3fv(coord->vertex);
coord = GETCOORD(curFrameIdx, i+1, j+1);
if (smooth && lighting) {
glNormal3fv(coord->normal);
}
glVertex3fv(coord->vertex);
lastColor = thisColor;
}
glEnd();
}
}
static void SetColorMap(void)
{
static float green[3] = {0.2, 1.0, 0.2};
static float red[3] = {1.0, 0.2, 0.2};
float *color, percent;
GLint *indexes, entries, i, j;
long buf[4];
entries = tkGetColorMapSize();
entries = 256;
colorIndexes1[0] = 1;
colorIndexes1[1] = 1 + (GLint)((entries - 1) * 0.3);
colorIndexes1[2] = (GLint)((entries - 1) * 0.5);
colorIndexes2[0] = 1 + (GLint)((entries - 1) * 0.5);
colorIndexes2[1] = 1 + (GLint)((entries - 1) * 0.8);
colorIndexes2[2] = entries - 1;
for (i = 0; i < 2; i++) {
switch (i) {
case 0:
color = green;
indexes = colorIndexes1;
break;
case 1:
color = red;
indexes = colorIndexes2;
break;
}
for (j = indexes[0]; j < indexes[1]; j++) {
percent = 0.2 + 0.8 * (j - indexes[0]) /
(float)(indexes[1] - indexes[0]);
tkSetOneColor(j, percent*color[0], percent*color[1],
percent*color[2]);
}
for (j=indexes[1]; j<=indexes[2]; j++) {
percent = (j - indexes[1]) / (float)(indexes[2] - indexes[1]);
tkSetOneColor(j, percent*(1-color[0])+color[0],
percent*(1-color[1])+color[1],
percent*(1-color[2])+color[2]);
}
}
}
static void InitMesh(void)
{
struct coord *coord;
struct facet *facet;
float dp1[3], dp2[3];
float *pt1, *pt2, *pt3;
float angle, d, x, y;
GLint numFacets, numCoords, frameNum, i, j;
theMesh.widthX = widthX;
theMesh.widthY = widthY;
theMesh.frames = frames;
numFacets = widthX * widthY;
numCoords = (widthX + 1) * (widthY + 1);
theMesh.numCoords = numCoords;
theMesh.numFacets = numFacets;
theMesh.coords = (struct coord *)malloc(frames*numCoords*
sizeof(struct coord));
theMesh.facets = (struct facet *)malloc(frames*numFacets*
sizeof(struct facet));
if (theMesh.coords == NULL || theMesh.facets == NULL) {
printf("Out of memory.\n");
return;
}
for (frameNum = 0; frameNum < frames; frameNum++) {
for (i = 0; i <= widthX; i++) {
x = i / (float)widthX;
for (j = 0; j <= widthY; j++) {
y = j / (float)widthY;
d = sqrt(x*x+y*y);
if (d == 0.0) {
d = 0.0001;
}
angle = 2 * PI * d + (2 * PI / frames * frameNum);
coord = GETCOORD(frameNum, i, j);
coord->vertex[0] = x - 0.5;
coord->vertex[1] = y - 0.5;
coord->vertex[2] = (height - height * d) * cos(angle);
coord->normal[0] = -(height / d) * x * ((1 - d) * 2 * PI *
sin(angle) + cos(angle));
coord->normal[1] = -(height / d) * y * ((1 - d) * 2 * PI *
sin(angle) + cos(angle));
coord->normal[2] = -1;
d = 1.0 / sqrt(coord->normal[0]*coord->normal[0]+
coord->normal[1]*coord->normal[1]+1);
coord->normal[0] *= d;
coord->normal[1] *= d;
coord->normal[2] *= d;
}
}
for (i = 0; i < widthX; i++) {
for (j = 0; j < widthY; j++) {
facet = GETFACET(frameNum, i, j);
if (((i/checkerSize)%2)^(j/checkerSize)%2) {
if (rgb) {
facet->color[0] = 1.0;
facet->color[1] = 0.2;
facet->color[2] = 0.2;
} else {
facet->color[0] = colorIndexes1[0];
facet->color[1] = colorIndexes1[1];
facet->color[2] = colorIndexes1[2];
}
} else {
if (rgb) {
facet->color[0] = 0.2;
facet->color[1] = 1.0;
facet->color[2] = 0.2;
} else {
facet->color[0] = colorIndexes2[0];
facet->color[1] = colorIndexes2[1];
facet->color[2] = colorIndexes2[2];
}
}
pt1 = GETCOORD(frameNum, i, j)->vertex;
pt2 = GETCOORD(frameNum, i, j+1)->vertex;
pt3 = GETCOORD(frameNum, i+1, j+1)->vertex;
dp1[0] = pt2[0] - pt1[0];
dp1[1] = pt2[1] - pt1[1];
dp1[2] = pt2[2] - pt1[2];
dp2[0] = pt3[0] - pt2[0];
dp2[1] = pt3[1] - pt2[1];
dp2[2] = pt3[2] - pt2[2];
facet->normal[0] = dp1[1] * dp2[2] - dp1[2] * dp2[1];
facet->normal[1] = dp1[2] * dp2[0] - dp1[0] * dp2[2];
facet->normal[2] = dp1[0] * dp2[1] - dp1[1] * dp2[0];
d = 1.0 / sqrt(facet->normal[0]*facet->normal[0]+
facet->normal[1]*facet->normal[1]+
facet->normal[2]*facet->normal[2]);
facet->normal[0] *= d;
facet->normal[1] *= d;
facet->normal[2] *= d;
}
}
}
}
static void InitMaterials(void)
{
static float ambient[] = {0.1, 0.1, 0.1, 1.0};
static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
static float position[] = {90.0, 90.0, 300.0, 0.0};
static float front_mat_shininess[] = {60.0};
static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
static float back_mat_shininess[] = {60.0};
static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
static float lmodel_twoside[] = {GL_TRUE};
glMatrixMode(GL_PROJECTION);
gluPerspective(450., 1.0, 0.5, 10.0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
if (rgb) {
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
}
if (rgb) {
glEnable(GL_COLOR_MATERIAL);
} else {
SetColorMap();
}
}
static void InitTexture(void)
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// eglInit
// initializes the opengl module you wish to display and is called once before the model is animated
// this function is called once to setup the module
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void eglInit( GLint quality )
{
rgb = GL_TRUE;
frames = 10;
widthX = 10;
widthY = 10;
checkerSize = 2;
height = 0.2;
spinMode = GL_TRUE;
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
smooth = GL_TRUE;
glFrontFace(GL_CW);
glEnable(GL_DEPTH_TEST);
InitMaterials();
InitTexture();
InitMesh();
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.4, -1.8);
glScalef(2.0, 2.0, 2.0);
glRotatef(-35.0, 1.0, 0.0, 0.0);
glRotatef(35.0, 0.0, 0.0, 1.0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// eglReshape
// resizes the window
// use this function to define the viewport and projection transformations based on the window size
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void eglReshape( GLint width, GLint height )
{
glViewport(0, 0, (GLint)width, (GLint)height);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// eglDestroy
// destroy display lists, objects etc. when the module has ended
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void eglDestroy()
{
if ( theMesh.coords ) free( theMesh.coords );
if ( theMesh.facets ) free( theMesh.facets );
theMesh.coords = 0;
theMesh.facets = 0;
}