void glLorenz(int r, int s, int b, int w){ int i; /* Coordinates */ double x = 1; double y = 1; double z = 1; /* Time step */ double dt = 0.001; glVertex4d(x+x_trans,y+y_trans,z+z_trans,w); /* * Integrate 50,000 steps (50 time units with dt = 0.001) * Explicit Euler integration */ for (i=0;i<50000;i++) { glColor3f(0,(1-i/50000.0),i/50000.0); double dx = s*(y-x); double dy = x*(r-z)-y; double dz = x*y - b*z; x += dt*dx; y += dt*dy; z += dt*dz; glVertex4d(x+x_trans,y+y_trans,z+z_trans,w); } }
/* Function to generate numerical coordinates of * the Lorenz Attractor. * */ void draw_attractor() { int i; // iteration variable /* Initial conditions */ double x = 1.0; double y = 1.0; double z = 1.0; /* Time step */ double dt = 0.001; glPointSize(1); // Set size of vertices. glPushMatrix(); // Push the matrix onto the stack glScaled(0.045, 0.045, 0.045); // Scale the vertex placement /* * Integrate 50,000 steps (50 time units with dt = 0.001) * Solving with explicit Euler integration * */ for (i = 0; i < 50000; i++) { double dx = s*(y-x); double dy = x *(r-z)-y; double dz = x*y - b*z; x += dt*dx; y += dt*dy; z += dt*dz; /* Store the solution values for animation*/ xsolution[i] = x; ysolution[i] = y; zsolution[i] = z; // Draw the vertices glBegin(GL_POINTS); glVertex4d(x, y, z, w); glEnd(); } /* Pop transformation matrix off the stack */ glPopMatrix(); // Tell GLUT to update the screen glutPostRedisplay(); }
static void display(void) { if (!model) return; glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glRotated(spin, 0, 1, 0); glPolygonMode(GL_FRONT, GL_LINE); glPolygonMode(GL_BACK, GL_LINE); for (int i = 0; i < obj_n_faces(model); ++i) { glBegin(GL_POLYGON); for (int j = 0; j < obj_face(model, i).len; ++j) { geometric_vertex vert = obj_geometric_vertex(model, obj_face(model, i).points[j].iv); glVertex4d(vert.x, vert.y, vert.z, vert.w); } glEnd(); } glPopMatrix(); glutSwapBuffers(); }
/* Animation function to show trajectory of particle. * Shows the velocity of the particlle as well. * */ void animate_particle() { glPushMatrix(); glScaled(0.045, 0.045, 0.045); // Scale the particle to the plot double x = xsolution[num]; double y = ysolution[num]; double z = zsolution[num]; num++; /* Draw the particle */ glPointSize(5); glColor3d(0, 1, 1); glBegin(GL_POINTS); glVertex4d(x, y, z, w); glEnd(); /* glut function to add delay to callback for animation */ glutTimerFunc(1000, animate_particle, 0); glPopMatrix(); }
/*TODO: add bottom and top face triangles*/ void draw_cube_brute() { draw_triangle(vertices_cube_brute, 4,5,1,BLUE); draw_triangle(vertices_cube_brute, 0,4,1,BLUE); draw_triangle(vertices_cube_brute, 5,6,2,CYAN); draw_triangle(vertices_cube_brute, 1,5,2,CYAN); draw_triangle(vertices_cube_brute, 3,2,6,YELLOW); draw_triangle(vertices_cube_brute, 7,3,6,YELLOW); draw_triangle(vertices_cube_brute, 0,3,7,MAGENTA); draw_triangle(vertices_cube_brute, 4,0,7,MAGENTA); draw_triangle(vertices_cube_brute, 1,2,3,WHITE); draw_triangle(vertices_cube_brute, 7,6,5,WHITE); //manually calculate the normals for the cube if(draw_norms){ glBegin(GL_LINES); { glVertex4f(1,1,-1,1); glVertex4f(1+1/sqrt(3),1+1/sqrt(3),-1-1/sqrt(3),1); glVertex4f(1,-1,-1,1); glVertex4f(1+1/sqrt(3),-1-1/sqrt(3),-1-1/sqrt(3),1); glVertex4d(1,-1,1,1); glVertex4f(1+1/sqrt(3),-1-1/sqrt(3),1+1/sqrt(3),1); glVertex4d(-1,-1,1,1); glVertex4f(-1-1/sqrt(3),-1-1/sqrt(3),1+1/sqrt(3),1); glVertex4d(-1,1,1,1); glVertex4f(-1-1/sqrt(3),1+1/sqrt(3),1+1/sqrt(3),1); glVertex4d(-1,1,-1,1); glVertex4f(-1-1/sqrt(3),1+1/sqrt(3),-1-1/sqrt(3),1); glVertex4d(-1,-1,-1,1); glVertex4f(-1-1/sqrt(3),-1-1/sqrt(3),-1-1/sqrt(3),1); glVertex4d(1,1,1,1); glVertex4f(1+1/sqrt(3),1+1/sqrt(3),1+1/sqrt(3),1); } glEnd(); } }
/* * Display the scene */ void display() { /* Coordinates */ double x = 1; double y = 1; double z = 1; /* Time step */ double dt = 0.001; // Clear the image glClear(GL_COLOR_BUFFER_BIT); // Reset previous transforms glLoadIdentity(); // Set view angle glRotated(ph,1,0,0); glRotated(th,0,1,0); // Draw 10 pixel yellow points glColor3f(0.0,1.0,0.5); glPointSize(1); glScaled(0.045, 0.045, 0.045); // Scale the vertex placement int i; for (i=0; i < 50000; i++) { double dx = s*(y-x); double dy = x*(r-z)-y; double dz = x*y - b*z; x += dt*dx; y += dt*dy; z += dt*dz; glBegin(GL_POINTS); glVertex4d(x,y,z,w); glEnd(); } // Draw axes in white glColor3f(1,1,1); glBegin(GL_LINES); glVertex3d(0,0,0); glVertex3d(30,0,0); glVertex3d(0,0,0); glVertex3d(0,30,0); glVertex3d(0,0,0); glVertex3d(0,0,30); glEnd(); // Label axes glRasterPos3d(30,0,0); Print("X"); glRasterPos3d(0,30,0); Print("Y"); glRasterPos3d(0,0,30); Print("Z"); // Display parameters glWindowPos2i(10,10); Print("Louis BOUDDHOU: View Angle = %d,%d %s",th,ph, " 3D"); // Flush and swap glFlush(); glutSwapBuffers(); }
void OGL3DBase::InitLighting() { nextLight = 0; PC_Lighting& currLights = plot3Dbase.plotLighting; if (!currLights.useLighting) { // no lighting glDisable(GL_LIGHTING); glDisable(GL_NORMALIZE); glDisable(GL_LIGHT0); return; } SC_ColorSpec penColor = plot3Dbase.defaultPenSet->GetColor(currLights.ambientPen); // set basic ambient GLfloat ambientLight[4]; ambientLight[0] = (GLfloat) (penColor.RH * currLights.ambientIntensity); ambientLight[1] = (GLfloat) (penColor.GS * currLights.ambientIntensity); ambientLight[2] = (GLfloat) (penColor.BV * currLights.ambientIntensity); ambientLight[3] = (GLfloat) currLights.ambientLightAlpha; glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight); glEnable(GL_LIGHTING); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); if (!currLights.useDefaultLight) { glDisable(GL_LIGHT0); return; } // set up default light penColor = plot3Dbase.defaultPenSet->GetColor(currLights.defaultDiffuseLightPen); GLfloat diffuseLight[4]; diffuseLight[0] = (GLfloat) (penColor.RH * currLights.defaultDiffuseLightIntensity); diffuseLight[1] = (GLfloat) (penColor.GS * currLights.defaultDiffuseLightIntensity); diffuseLight[2] = (GLfloat) (penColor.BV * currLights.defaultDiffuseLightIntensity); diffuseLight[3] = (GLfloat) currLights.defaultDiffuseLightAlpha; glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); penColor = plot3Dbase.defaultPenSet->GetColor(currLights.defaultSpecularLightPen); GLfloat specularLight[4]; specularLight[0] = (GLfloat) (penColor.RH * currLights.defaultSpecularLightIntensity); specularLight[1] = (GLfloat) (penColor.GS * currLights.defaultSpecularLightIntensity); specularLight[2] = (GLfloat) (penColor.BV * currLights.defaultSpecularLightIntensity); specularLight[3] = (GLfloat) currLights.defaultSpecularLightAlpha; glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight); double elev, azimuth; if (currLights.defaultLightIsFixed) { elev = currLights.fixedElevation; azimuth = currLights.fixedAzimuth; } else { elev = currView.elevation + currLights.relativeElevation; azimuth = currView.azimuth + currLights.relativeAzimuth; } elev = Radians(elev); azimuth = Radians(azimuth - 180.0); GLfloat defaultPosition[4]; defaultPosition[0] = (GLfloat) (sin(azimuth) * cos(elev)); defaultPosition[1] = (GLfloat) (cos(azimuth) * cos(elev)); defaultPosition[2] = (GLfloat) sin(elev); defaultPosition[3] = (GLfloat) currLights.defaultLightW; glLightfv(GL_LIGHT0, GL_POSITION, defaultPosition); if (currLights.showDefaultLight) { double defX = defaultPosition[0]; double defY = defaultPosition[1]; double defZ = defaultPosition[2]; if (currLights.defaultLightW > 0.01) { defX /= currLights.defaultLightW; defY /= currLights.defaultLightW; defZ /= currLights.defaultLightW; } static const double dl = 0.025; glColor3d(1.0, 0.0, 0.0); glLineWidth(3); glBegin(GL_LINES); glVertex4d(0.0, 0.0, 0.0, 1.0); glVertex4d(GLdouble(defX ), GLdouble(defY), GLdouble(defZ), currLights.defaultLightW); glEnd(); glColor3d(0.0, 0.0, 0.0); glBegin(GL_LINE_LOOP); glVertex3d(defX - dl, defY- dl, defZ- dl); glVertex3d(defX - dl, defY+ dl, defZ- dl); glVertex3d(defX + dl, defY+ dl, defZ- dl); glVertex3d(defX + dl, defY- dl, defZ- dl); glEnd(); glBegin(GL_LINE_LOOP); glVertex3d(defX - dl, defY- dl, defZ+ dl); glVertex3d(defX - dl, defY+ dl, defZ+ dl); glVertex3d(defX + dl, defY+ dl, defZ+ dl); glVertex3d(defX + dl, defY- dl, defZ+ dl); glEnd(); glBegin(GL_LINES); glVertex3d(defX - dl, defY- dl, defZ- dl); glVertex3d(defX - dl, defY- dl, defZ+ dl); glVertex3d(defX - dl, defY+ dl, defZ- dl); glVertex3d(defX - dl, defY+ dl, defZ+ dl); glVertex3d(defX + dl, defY- dl, defZ- dl); glVertex3d(defX + dl, defY- dl, defZ+ dl); glVertex3d(defX + dl, defY+ dl, defZ- dl); glVertex3d(defX + dl, defY+ dl, defZ+ dl); glEnd(); } glEnable(GL_LIGHT0); GLfloat mat_specular[] = {0.2F, 0.2F, 0.2F, 1.0F}; GLfloat mat_shininess[] = {50.0F}; glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess); nextLight++; }
inline void gl_render() const { glVertex4d( x, y, z, w); }
template< > inline void glVertex4< LDOUBL > ( LDOUBL x, LDOUBL y, LDOUBL z, LDOUBL w ){ glVertex4d((double)x,(double)y,(double)z,(double)w); };
template< > inline void glVertex4< double > ( double x,double y,double z, double w ){ glVertex4d(x,y,z,w); };
/* * Display the scene */ void display() { // Clear the image glClear(GL_COLOR_BUFFER_BIT); // Reset previous transforms glLoadIdentity(); // Set view angle glRotated(ph,1,0,0); glRotated(th,0,1,0); // Draw 10 pixel yellow points glPointSize(2); glBegin(GL_LINE_STRIP); switch (mode) { // Two dimensions case 1: /* Coordinates */ x = 1.0; y = 1.0; z = 1.0; /* Time step */ dt = 0.001; /* * Integrate 50,000 steps (50 time units with dt = 0.001) * Explicit Euler integration */ for (i=0; i<50000; i++) { //Calculate differentials dx = s*(y-x); dy = x*(r-z)-y; dz = x*y - b*z; // Determine new variables for point x = (double)(x + dt*dx); y = (double)(y + dt*dy); z = (double)(z + dt*dz); /* Set Color and Create Point */ // Make dx positive and scale if (dx < 0) cx = (-1 * dx) / 200.00; else cx = dx / 200.00; // Make dy positive and scale if (dy < 0) cy = (-1 * dy) / 200.00; else cy = dy / 200.00; //Make dz positive and scale if (dx < 0) cz = (-1 * dz) / 200.00; else cz = dz / 200.00; glColor3f(cx, cy, cz); glVertex2d(x, y); } break; // Three dimensions - constant Z case 2: /* Coordinates */ x = 1.0; y = 1.0; z = 1.0; /* Time step */ dt = 0.001; /* * Integrate 50,000 steps (50 time units with dt = 0.001) * Explicit Euler integration */ for (i=0; i<50000; i++) { //Calculate differentials dx = s*(y-x); dy = x*(r-z)-y; dz = x*y - b*z; // Determine new variables for point x = (double)(x + dt*dx); y = (double)(y + dt*dy); z = (double)(z + dt*dz); /* Set Color and Create Point */ // Make dx positive and scale if (dx < 0) cx = (-1 * dx) / 200.00; else cx = dx / 200.00; // Make dy positive and scale if (dy < 0) cy = (-1 * dy) / 200.00; else cy = dy / 200.00; //Make dz positive and scale if (dx < 0) cz = (-1 * dz) / 200.00; else cz = dz / 200.00; glColor3f(cx, cy, cz); glVertex3d( x, y, def_z); } break; // Three dimensions - variable Z case 3: /* Coordinates */ x = 1.0; y = 1.0; z = 1.0; /* Time step */ dt = 0.001; /* * Integrate 50,000 steps (50 time units with dt = 0.001) * Explicit Euler integration */ for (i=0; i<50000; i++) { //Calculate differentials dx = s*(y-x); dy = x*(r-z)-y; dz = x*y - b*z; // Determine new variables for point x = (double)(x + dt*dx); y = (double)(y + dt*dy); z = (double)(z + dt*dz); /* Set Color and Create Point */ // Make dx positive and scale if (dx < 0) cx = (-1 * dx) / 200.00; else cx = dx / 200.00; // Make dy positive and scale if (dy < 0) cy = (-1 * dy) / 200.00; else cy = dy / 200.00; //Make dz positive and scale if (dx < 0) cz = (-1 * dz) / 200.00; else cz = dz / 200.00; glColor3f(cx, cy, cz); glVertex3d( x, y, z); } break; // Four dimensions case 4: /* Coordinates */ x = 1.0; y = 1.0; z = 1.0; /* Time step */ dt = 0.001; /* * Integrate 50,000 steps (50 time units with dt = 0.001) * Explicit Euler integration */ for (i=0; i<50000; i++) { //Calculate differentials dx = s*(y-x); dy = x*(r-z)-y; dz = x*y - b*z; // Determine new variables for point x = (double)(x + dt*dx); y = (double)(y + dt*dy); z = (double)(z + dt*dz); /* Set Color and Create Point */ // Make dx positive and scale if (dx < 0) cx = (-1 * dx) / 200.00; else cx = dx / 200.00; // Make dy positive and scale if (dy < 0) cy = (-1 * dy) / 200.00; else cy = dy / 200.00; //Make dz positive and scale if (dx < 0) cz = (-1 * dz) / 200.00; else cz = dz / 200.00; glColor3f(cx, cy, cz); glVertex4d(x, y, z, def_w); } break; } glEnd(); // Draw axes in white glColor3f(1,1,1); glBegin(GL_LINES); glVertex3d(0,0,0); glVertex3d(dim - 5,0,0); glVertex3d(0,0,0); glVertex3d(0,dim -5,0); glVertex3d(0,0,0); glVertex3d(0,0,dim -5); glEnd(); // Label axes glRasterPos3d(dim - 5,0,0); Print("X"); glRasterPos3d(0,dim - 5,0); Print("Y"); glRasterPos3d(0,0,dim -5); Print("Z"); // Display parameters glWindowPos2i(5,5); Print("View Angle=%d,%d %s",th,ph,text[mode]); if (mode==2) Print(" z=%.1f",z); else if (mode==4) Print(" w=%.1f",def_w); // Flush and swap glFlush(); glutSwapBuffers(); }
inline void glVertex(Vector3d const &v, double const &z) { glVertex4d(v.c[0], v.c[1], v.c[2], z); }
M(void, glVertex4d, jdouble x, jdouble y, jdouble z, jdouble w) { glVertex4d(x, y, z, w); }
/* ‘ункци¤ построени¤ объекта анимации. * ј–√”ћ≈Ќ“џ: * - указатель на "себ¤" - сам объект анимации: * ok2UNIT_CUBE *Unit; * - указатель на контекст анимации: * ok2ANIM *Ani; * ¬ќ«¬–јўј≈ћќ≈ «Ќј„≈Ќ»≈: Ќет. */ static VOID CubeUnitRender( ok2UNIT_CUBE *Unit, ok2ANIM *Ani ) { /* INT i, s = 5, N = 0; VEC p = {1, 0, 0}; static FLT Delta = 0.1; MATR WVP; Delta += Ani->JZ * Ani->GlobalDeltaTime; //Ani->MatrWorld = MatrRotateY(30); //Ani->MatrView = MatrLookAt(VecSet(15, 15, 15), VecSet(0, 0, 0), VecSet(0, 1, 0)); Ani->MatrView = MatrLookAt(PointTransform(VecSet(20, 20, Ani->JX * Delta + 20), MatrRotateX(80 * Ani->JY)), VecSet(0, 0, 0), VecSet(0, 1, 0)); Ani->MatrWorld = MatrMulMatr(Ani->MatrWorld, MatrRotateX(Ani->DeltaTime * 80)); for (i = 0; i < 12; i++) { Ani->MatrWorld = MatrMulMatr(MatrTranslate(0.0, 5.0, 0.0), Ani->MatrWorld); Ani->MatrWorld = MatrMulMatr(MatrRotate(30, 1.0, 0.0, 1.0), Ani->MatrWorld); WVP = MatrMulMatr(Ani->MatrWorld, MatrMulMatr(Ani->MatrView, Ani->MatrProjection)); glLoadMatrixf(WVP.A[0]); OK2_RndGObjDraw(&Unit->Cow, Ani->hDC); } //OK2_RndGObjDraw(&Unit->Cow, Ani->hDC); */ INT i; MATR WVP; static DBL time; /* оси и позици¤ наблюдател¤ */ Ani->MatrWorld = MatrIdenity(); Ani->MatrView = MatrLookAt( PointTransform(PointTransform(VecSet(25, 25, 25), MatrRotateY(Ani->JR * 180)), MatrRotateZ(Ani->JY * 180)), VecSet(0, 0, 0), VecSet(0, 1, 0)); WVP = MatrMulMatr(OK2_Anim.MatrWorld, MatrMulMatr(OK2_Anim.MatrView, OK2_Anim.MatrProjection)); glLoadMatrixf(WVP.A[0]); glLineWidth(3); glBegin(GL_LINES); glColor3d(1, 0.5, 0.5); glVertex3d(-3, 0, 0); glVertex4d(1, 0, 0, 0); glColor3d(0.5, 1, 0.5); glVertex3d(0, -3, 0); glVertex4d(0, 1, 0, 0); glColor3d(0.5, 0.5, 1); glVertex3d(0, 0, -3); glVertex4d(0, 0, 1, 0); glEnd(); glColorMask(1, 1, 1, 0); for (i = -3; i < 30; i++) { glBegin(GL_TRIANGLE_STRIP); glVertex3d(-0.1, -0.1, i); glVertex3d(-0.1, 0.1, i); glVertex3d( 0.1, -0.1, i); glVertex3d( 0.1, 0.1, i); glEnd(); } /* –исуем примитивы */ time += Ani->GlobalDeltaTime; if (time > 1) { time = 0; OK2_ShadProgClose(OK2_ShaderProg); OK2_ShaderProg = OK2_ShadProgInit("a.vert", "a.frag"); } glLineWidth(1); if (Ani->Keys['Q']) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //Ani->MatrWorld = MatrTranslate(0, 0, 0.30 * sin(Ani->Time)); Ani->MatrWorld = MatrMulMatr(Ani->MatrWorld, MatrRotateY(30.0 * Ani->Time)); /* for (i = 0; i < 12; i++) { Ani->MatrWorld = MatrMulMatr(MatrRotate(30.0, 0.0, 1.0, 0.0), Ani->MatrWorld); Ani->MatrWorld = MatrMulMatr(MatrTranslate(1.0, 0.0, 0.0), Ani->MatrWorld); OK2_GeomDraw(&Unit->Geom); } */ /* for (i = 0; i < 6; i++) DrawUnitInPosition(&Unit->Geom[i], Ani, Unit->Scale`[i], 10.0, 30.0); */ Ani->MatrWorld = MatrMulMatr(Ani->MatrWorld, MatrScale(0.001, 0.001, 0.001)); for (i = 0; i < 12; i++) { Ani->MatrWorld = MatrMulMatr(MatrRotate(30.0, 0.0, 1.0, 0.0), Ani->MatrWorld); Ani->MatrWorld = MatrMulMatr(MatrTranslate(20000.0, 0.0, 0.0), Ani->MatrWorld); //Ani->MatrWorld = MatrMulMatr(MatrRotate(30.0, 0.0, 1.0, 0.0), Ani->MatrWorld); OK2_GeomDraw(&Unit->Geom[3]); Ani->MatrWorld = MatrMulMatr(MatrTranslate(-20000.0, 0.0, 0.0), Ani->MatrWorld); } //Ani->MatrWorld = MatrMulMatr(Ani->MatrWorld, MatrScale(50, 50, 50)); } /* End of 'OK2_AnimUnitRender' function */
///////////////////////////////////////////////////////// // Render // void GEMglVertex4d :: render(GemState *state) { glVertex4d (x, y, z, w); }