/* table.txt Created by: Jonathan Kipling Knight, Prism Research Created on: 25 February 1999 An example of using NewtonGL in an NTK project. This file may be incorporated, whole or modified, into a NewtonGL project without fear of persecution or tar-and-feathering. This file must be included in an NTK project. It must be processed before the 'table constant is used in the project. This file must be preceded in the project by both Math.k and NewtonGL.k files which are included with this package. Before NewtonGL.k, the compile-time statement GLUnitUsed:=TRUE; must be put in a file therby allowing the protoGL Unit Reference to be defined. To use the 'table example, add the statement: AddStepView(table) in an appropriate place like :ViewSetupDone(). If this line is added in the ViewChildrenSetup() then the 'table view will end up being opened twice by the Newton OS. This program has been tested on a MP2100 using NewtonGL v0.09. It takes up about 87k and takes about 35 seconds to draw initially and 17 seconds to redraw. */ DefineGlobalConstant('table, { _proto: protoGL, // bring in the prototype /* if you receive an error when your program is first run, most likely you need to increase one of the next two variables. */ MAX_VERTICES: 25, // maximum number of vertices for any object MAX_OBJECTS: 350, // maximum number of objects eye: [0,0.5,0.7], // eye coordinates tableTop: {w:0.1,l:0.2,t:0.01}, // tabletop dimensions thigh: {w:0.02,l:0.08,t:0.02}, // upper table leg dimensions shin: {w:0.02,l:0.08,t:0.02}, // lower table leg dimensions checkSize: 0.1, // size of squares on floor floorX: 10, // # squares on floor in x direction floorY: 10, // # squares on floor in y direction tablePos: // positions of a corner of the table [ [-0.2,-0.2], [-0.2,-0.2], [-0.2,-0.2], [-0.2,-0.1], [-0.2,-0.1], [-0.2,-0.1], ], orients: // orientations of the table [ [0,0,0], [0,0,0], [10,20,0], [10,0,20], [20,10,10], [20,10,10], ], legAngles: // angles of the table legs [ [[0,0,0,0],[0,0,0,0]], [[0,0,0,10],[0,0,0,0]], [[0,0,0,20],[0,0,0,0]], [[0,0,0,30],[0,0,0,0]], [[0,0,0,40],[0,0,0,0]], [[0,0,0,50],[0,0,0,0]], ], PenDown: func(b1,b2,x,y) /* this function will be called every time pen touches the screen */ begin if |counter:prism|thetaThigh then // knee can't bend backwards thetaShin := thetaThigh; :glRotatef(thetaShin,1,0,0); // Put knee at origin :glTranslatef(shin.w*0.5,shin.t*0.5,-shin.l*0.5); :glScalef(shin.w,shin.t,shin.l); :glutSolidCube(1.0); :glPopMatrix(); end, DrawTop: func() begin :glPushMatrix(); :glTranslatef(tableTop.w*0.5,tableTop.l*0.5,tableTop.t*0.5); :glScalef(tableTop.w,tableTop.l,tableTop.t); :glutSolidCube(1.0); :glPopMatrix(); end, DrawTable: func(x,y,z,orient,altitude,tilt,thetaThighs,thetaShins) begin local x1,y1,d,phi; :glPushMatrix(); :glTranslatef(x,y,z); :glRotatef(orient,0,0,1); // z-axis rotation :glRotatef(tilt,0,1,0); // y-axis rotation :glRotatef(altitude,1,0,0); // x-axis rotation x1 := 0; y1 := thigh.t; :DrawLeg(x1,y1,thetaThighs[0],thetaShins[0]); x1 := tableTop.w-thigh.w; y1 := thigh.t; :DrawLeg(x1,y1,thetaThighs[1],thetaShins[1]); x1 := 0; y1 := tableTop.l; :DrawLeg(x1,y1,thetaThighs[2],thetaShins[2]); d := sqrt((tableTop.w-thigh.w)*(tableTop.w-thigh.w)+tableTop.l*tableTop.l); phi := atan2(tableTop.w-thigh.w,tableTop.l); x1 := d*sin(phi); y1 := d*cos(phi); :DrawLeg(x1,y1,thetaThighs[3],thetaShins[3]); :DrawTop(); :glPopMatrix(); end, DrawFloor: func(x,y) begin local i,j; for i:=0 to floorX-1 by 2 do begin for j:=0 to floorY-1 do begin :glRectf(x+(i+(j mod 2))*checkSize, y+j*checkSize, x+(i+1+(j mod 2))*checkSize, y+(j+1)*checkSize); end; end; end, Init: func(argc,argv) begin if argv then // change eye coordinates begin eye := Clone(eye); if Length(argv)>0 then eye[0] := argv[0]; if Length(argv)>1 then eye[1] := argv[1]; if Length(argv)>2 then eye[2] := argv[2]; end; // place light source #0 at 1,1,1 :glEnable(GL_LIGHT0); :glLight(GL_LIGHT0,GL_POSITION,[1.0,1.0,1.0,0.0]); // define material properties :glMaterial(GL_FRONT,GL_SPECULAR,[1,1,1,1]); :glMaterial(GL_FRONT,GL_SHININESS,50.0); // initialize counter DefGlobalVar('|counter:prism|,0); end, Redraw: func(w,h) /* This function sets up redrawing the window */ begin :glViewport(0,0,w,h); :glClearColor(1.0,0.5,1.0,1.0); :glMatrixMode(GL_PROJECTION); :glLoadIdentity(); :glFrustrum(-1.0,1.0,-1.0,1.0,1.5,20.0); :glMatrixMode(GL_MODELVIEW); :glLoadIdentity(); :gluLookAt(eye[0],eye[1],eye[2], 0.0,0.0,0.0, 0.0,1.0,0.0); end, main: func(int argc, argv) /* This function is the entry point to a NewtonGL program. It must be named main. */ begin :glutInit(argc,argv); // required line :glutInitWindowSize(200,200); // window is 200 by 200 pixels :glutInitWindowPosition(10,30); // at x=10, y=30 pixels :glutCreateWindow("Dancing Table"); // name the window :Init(argc,argv); // call your initializer /* a display function must be declared */ :glutDisplayFunc(Displayer); // declare the display function :glutReshapeFunc(Redraw); // declare the reshape function :glutMouseFunc(PenDown); // declare the pen function :glutMainLoop(); // required line, starts the drawing end, argv: NIL, argc: NIL, });