📄 visual.cpp
字号:
#include <windows.h> // - Windows API and system calls
#include <stdio.h> // - Just for some ASCII messages
#include <gl/gl.h> // - The OpenGL API
#include <gl/glaux.h> // - A windows library extension API
#include "glut.h" // - An interface and windows
// management library
#include "visual.h" // Header file for our OpenGL functions
#include <math.h>
/* ------------------------------------------------------------------------- */
/* ezgraphGL.c -- OpenGL graphics routines.
*
* Copyright (C) 1992, 1994, 1997, 1998 Dwight Barkley
*
* RCS Information
* ---------------------------
* $Revision: 3.1.1.2 $
* $Date: 2002/08/01 08:17:14 $
* ------------------------------------------------------------------------- */
#include "ezspiral.h"
#include "ezgraphGL.h"
/* -------------------------------------------------------------------------
* This file contains all graphics manipulation routines.
* The important things to know about this file are:
*
* (1) X11 is used to open the graphics window and handle the events
* (i.e. key presses and pointer motions within the window). InitX() is a
* long routine that opens the window; Event_check() contains the event loop
* that looks for window events; QuitX() quits X obviously. You should be
* able to switch over to a higher-level method with only minor
* modifications except for these routines.
*
* (2) After the window is open, OpenGL is used to handle all the rendering.
* myReshape() must be called before anything can be plotted. To understand
* this function see the OpenGL manual.
*
* The routines near the top of this file handle the interactive graphics
* through OpenGL calls. Note: to add a new interactive feature, add to the
* event loop in Event_check() and add a corresponding routine to perform the
* desired task.
*
* (3) Note on the graphics modes.
*
* There are 2 modes the program can be in:
* MODE_SIMULATING Simulation progresses through time (it cannot be
* rotated while in this mode)
* MODE_VIEWING Simulation is paused.
* ------------------------------------------------------------------------- */
/*
* Global variables for this file only
* ----------------------------------- */
static int field; /* Field being viewed */
static int show_tip; /* Tip draw flag */
static int ezmode; /* Mode flag */
static Real half_width,
half_height;
static Real plot_length[2]; /* Lengths of the simulation volume in
graphics coordinates (see
Draw_ini()). */
/*
* Private functions
* ----------------- */
static int setColor (int i, int j);
//==================================================================================
void Render()
{
/* Main plotting routine */
int i,j;
Real x1, x2, y1, y2, rect_h=plot_length[0]/(NX-1);
if(GRAPHICS) {
/* Clear the color buffer and draw a blue rectangle the size of
the simulation area */
glClear(GL_COLOR_BUFFER_BIT);
GLCOLOR3(0.,0.,1.);
GLRECT(-half_width, -half_height, half_width, half_height);
}
if(field != NO_FIELD) {
y1 = -half_height;
y2 = y1 + rect_h;
for(j=1;j<NY;j++) {
x1 = -half_width;
x2 = x1 + rect_h;
for(i=1;i<NX;i++) {
if(setColor(i,j)) GLRECT(x1,y1,x2,y2);
x1 += rect_h;
x2 += rect_h;
}
y1 += rect_h;
y2 += rect_h;
}
}
// if( write_tip || show_tip ) Find_tips();
// if( show_tip ) Draw_tips();
if(GRAPHICS)
glutSwapBuffers();
}
//-----------------------------------------------------------
void Idle()
{
glutPostRedisplay();
}
//-----------------------------------------------------------
void Resize(int w, int h)
{
/* half_width and half_height define the area viewed in GL. In general if
* these are large, then simulation area will appear small, and vice versa.
* PLOT_SIZE in ezgraphGL.h allows adjustment of this without changing any
* of the code below. */
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (-PLOT_SIZE*half_width, PLOT_SIZE*half_width,
-PLOT_SIZE*half_height, PLOT_SIZE*half_height, -20., 20.);
glMatrixMode (GL_MODELVIEW);
glViewport (0, 0, w, h);
}
//-----------------------------------------------------------
void Setup(int initial_field)
{
///////////////////////////////////////////////////////////////////////////////
/* Initialize everything necessary for plotting. */
field = initial_field;
show_tip = FALSE;
/* The lengths of the simulation area in graphics coordinates are set. I
* choose to have the largest plot_length=1. Thus the simulation area lies
* inside the unit square in graphics coordinates. */
{
int nmax = max(NX,NY);
plot_length[0] = (NX-1.)/(nmax-1.);
plot_length[1] = (NY-1.)/(nmax-1.);
}
half_width = 0.5*plot_length[0];
half_height = 0.5*plot_length[1];
/* At this point everything has been initialized for finding tips
* without graphics. Can return after setting a few things. Setting field
* to NO_FIELD and ezmode to MODE_SIMULATING will give only minimum OpenGL
* calls. */
if( !GRAPHICS ) {
field = NO_FIELD;
ezmode = MODE_SIMULATING;
return;
}
/* Set the shade model to Flat. The default Smooth (GL_SMOOTH)
* can take too long on some systems and I see no difference. */
glShadeModel(GL_FLAT);
/* Makes little difference. */
glEnable(GL_DITHER);
/* Set the background color. Here: red=green=blue=BACKGROUND. One can
* set any values one chooses so long as 0 <= red, green, blue <=1 */
glClearColor(BACKGROUND,BACKGROUND,BACKGROUND,0.0);
/* Set starting mode */
if(START_PAUSED) {
ezmode = MODE_VIEWING;
}
else {
ezmode = MODE_SIMULATING;
}
}
/* ========================================================================= */
static int setColor (int i, int j)
{
Real scaled_v, red, green, blue;
switch (field) {
case U_FIELD : /* Set the u color */
if (U(i,j) < 0.1) { /* quiescent state (u<0.1): no color set */
return(0);
}
else if(U(i,j) < 0.9) { /* Interface (0.1<u<0.9): Black */
GLCOLOR3(0.,0.,0.);
}
else { /* Excited state (u>0.9): Red */
GLCOLOR3(1.,0.,0.);
}
break;
case V_FIELD : /* Set the v_color */
/* Scale the v-field between 0 and 1. The maximum (VMAX) and minimum
* (VMIN) values used for scaling are somewhat ad hoc. The max and min
* functions are used to ensure 0 <= scaled_v < 1. */
#define VMAX (0.8*a_param)
#define VMIN 0.0
scaled_v = (V(i,j)-VMIN)/(VMAX-VMIN);
#if 0
scaled_v = max(0.,scaled_v);
scaled_v = min(1.,scaled_v);
#endif
red = 1.-scaled_v*scaled_v;
green = scaled_v*scaled_v/2.0;
blue = scaled_v*scaled_v;
GLCOLOR3(red, green, blue);
}
return(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -