openGPMP
Open Source Mathematics Package
Macros | Functions | Variables
torus_animated.c File Reference
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>

Go to the source code of this file.

Macros

#define pi   3.142857
 

Functions

void reshape (GLint w, GLint h)
 
void display ()
 
void timer (int v)
 
void mouse (int button, int state, int x, int y)
 
void init ()
 
int main (int argc, char **argv)
 

Variables

static bool spinning = true
 
static const int FPS = 20
 
static GLfloat current_rot = 0.0
 

Macro Definition Documentation

◆ pi

#define pi   3.142857

Definition at line 40 of file torus_animated.c.

Function Documentation

◆ display()

void display ( void  )

Definition at line 70 of file torus_animated.c.

70  {
71  // setting every pixel in frame buffer to a clear color
72  glClear(GL_COLOR_BUFFER_BIT);
73  glMatrixMode(GL_MODELVIEW);
74  glLoadIdentity();
75 
76  glColor3f(1.0, 1.0, 1.0);
77  glRotatef(current_rot, 0.0, 0.0, 1.0);
78  glutWireTorus(0.5, 3, 15, 30);
79 
80  // being drawing
81 
82  glBegin(GL_POLYGON);
83  glRotatef(current_rot, 1.0, 0.0, 1.0);
84  glColor3f(1, 0, 0);
85  glVertex3f(0, 0, 0);
86  glVertex3f(10, 0, 0);
87  glColor3f(0, 1, 0);
88  glVertex3f(0, 0, 0);
89  glVertex3f(0, 10, 0);
90  glColor3f(0, 0, 1);
91  glVertex3f(0, 0, 0);
92  glVertex3f(0, 0, 10);
93  glEnd();
94 
95  glRotatef(current_rot, 1.0, 0.0, 1.0);
96 
97  // flush the draw cmd to display image immediately
98  glFlush();
99  glutSwapBuffers();
100 }
static GLfloat current_rot

References current_rot.

Referenced by main().

◆ init()

void init ( )

Definition at line 130 of file torus_animated.c.

130  {
131  glClearColor(0.0, 0.0, 0.0, 1.0);
132  glColor3f(1.0, 1.0, 1.0);
133 
134  glMatrixMode(GL_PROJECTION);
135  glLoadIdentity();
136  gluPerspective(60.0, 4.0 / 3.0, 1, 40);
137 
138  glMatrixMode(GL_MODELVIEW);
139  glLoadIdentity();
140  gluLookAt(4, 6, 5, 0, 0, 0, 0, 1, 0);
141 }

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 143 of file torus_animated.c.

143  {
144  glutInit(&argc, argv);
145  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
146 
147  // Position window at (80,80)-(480,380) and give it a title.
148  glutInitWindowPosition(80, 80);
149  glutInitWindowSize(1000, 800);
150  glutCreateWindow("A Torus on a Plane");
151 
152  glutReshapeFunc(reshape);
153 
154  // Tell GLUT that whenever the main window needs to be repainted
155  // that it should call the function display().
156  glutDisplayFunc(display);
157  glutTimerFunc(100, timer, 0);
158  glutMouseFunc(mouse);
159 
160  init();
161 
162  glutMainLoop();
163 }
void init()
void display()
void reshape(GLint w, GLint h)
void timer(int v)
void mouse(int button, int state, int x, int y)

References display(), init(), mouse(), reshape(), and timer().

◆ mouse()

void mouse ( int  button,
int  state,
int  x,
int  y 
)

Definition at line 122 of file torus_animated.c.

122  {
123  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
124  spinning = true;
125  } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
126  spinning = false;
127  }
128 }
static bool spinning

References spinning.

Referenced by main().

◆ reshape()

void reshape ( GLint  w,
GLint  h 
)

Definition at line 51 of file torus_animated.c.

51  {
52  glViewport(0, 0, w, h);
53  GLfloat aspect = (GLfloat)w / (GLfloat)h;
54  glMatrixMode(GL_PROJECTION);
55  glLoadIdentity();
56 
57  if (w <= h) {
58  // width is smaller, go from -50 .. 50 in width
59  glOrtho(-50.0, 50.0, -50.0 / aspect, 50.0 / aspect, -1.0, 1.0);
60  }
61 
62  else {
63  // height is smaller, go from -50 .. 50 in height
64  // glOrtho(-50.0*aspect, 50.0*aspect, -50.0, 50.0, -1.0, 1.0);
65 
66  glOrtho(-10.0 * aspect, 10.0 * aspect, -10.0, 10.0, -1.0, 1.0);
67  }
68 }

Referenced by main().

◆ timer()

void timer ( int  v)

Definition at line 105 of file torus_animated.c.

105  {
106  if (spinning) {
107  current_rot += 1.0;
108 
109  if (current_rot > 360.0) {
110  current_rot -= 360.0;
111  }
112  glutPostRedisplay();
113  }
114 
115  glutTimerFunc(1000 / FPS, timer, v);
116 }
static const int FPS

References current_rot, FPS, and spinning.

Referenced by main().

Variable Documentation

◆ current_rot

GLfloat current_rot = 0.0
static

Definition at line 46 of file torus_animated.c.

Referenced by display(), and timer().

◆ FPS

const int FPS = 20
static

Definition at line 43 of file torus_animated.c.

Referenced by timer().

◆ spinning

bool spinning = true
static

Definition at line 42 of file torus_animated.c.

Referenced by mouse(), and timer().