openGPMP
Open Source Mathematics Package
Macros | Functions | Variables
openGL_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 8 of file openGL_torus_animated.c.

Function Documentation

◆ display()

void display ( void  )

Definition at line 38 of file openGL_torus_animated.c.

38  {
39  // setting every pixel in frame buffer to a clear color
40  glClear(GL_COLOR_BUFFER_BIT);
41  glMatrixMode(GL_MODELVIEW);
42  glLoadIdentity();
43 
44  glColor3f(1.0, 1.0, 1.0);
45  glRotatef(current_rot, 0.0, 0.0, 1.0);
46  glutWireTorus(0.5, 3, 15, 30);
47 
48  // being drawing
49 
50  glBegin(GL_POLYGON);
51  glRotatef(current_rot, 1.0, 0.0, 1.0);
52  glColor3f(1, 0, 0);
53  glVertex3f(0, 0, 0);
54  glVertex3f(10, 0, 0);
55  glColor3f(0, 1, 0);
56  glVertex3f(0, 0, 0);
57  glVertex3f(0, 10, 0);
58  glColor3f(0, 0, 1);
59  glVertex3f(0, 0, 0);
60  glVertex3f(0, 0, 10);
61  glEnd();
62 
63  glRotatef(current_rot, 1.0, 0.0, 1.0);
64 
65  // flush the draw cmd to display image immediately
66  glFlush();
67  glutSwapBuffers();
68 }
static GLfloat current_rot

References current_rot.

Referenced by main().

◆ init()

void init ( )

Definition at line 98 of file openGL_torus_animated.c.

98  {
99  glClearColor(0.0, 0.0, 0.0, 1.0);
100  glColor3f(1.0, 1.0, 1.0);
101 
102  glMatrixMode(GL_PROJECTION);
103  glLoadIdentity();
104  gluPerspective(60.0, 4.0 / 3.0, 1, 40);
105 
106  glMatrixMode(GL_MODELVIEW);
107  glLoadIdentity();
108  gluLookAt(4, 6, 5, 0, 0, 0, 0, 1, 0);
109 }

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 111 of file openGL_torus_animated.c.

111  {
112  glutInit(&argc, argv);
113  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
114 
115  // Position window at (80,80)-(480,380) and give it a title.
116  glutInitWindowPosition(80, 80);
117  glutInitWindowSize(1000, 800);
118  glutCreateWindow("A Torus on a Plane");
119 
120  glutReshapeFunc(reshape);
121 
122  // Tell GLUT that whenever the main window needs to be repainted
123  // that it should call the function display().
124  glutDisplayFunc(display);
125  glutTimerFunc(100, timer, 0);
126  glutMouseFunc(mouse);
127 
128  init();
129 
130  glutMainLoop();
131 }
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 90 of file openGL_torus_animated.c.

90  {
91  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
92  spinning = true;
93  } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
94  spinning = false;
95  }
96 }
static bool spinning

References spinning.

Referenced by main().

◆ reshape()

void reshape ( GLint  w,
GLint  h 
)

Definition at line 19 of file openGL_torus_animated.c.

19  {
20  glViewport(0, 0, w, h);
21  GLfloat aspect = (GLfloat)w / (GLfloat)h;
22  glMatrixMode(GL_PROJECTION);
23  glLoadIdentity();
24 
25  if (w <= h) {
26  // width is smaller, go from -50 .. 50 in width
27  glOrtho(-50.0, 50.0, -50.0 / aspect, 50.0 / aspect, -1.0, 1.0);
28  }
29 
30  else {
31  // height is smaller, go from -50 .. 50 in height
32  // glOrtho(-50.0*aspect, 50.0*aspect, -50.0, 50.0, -1.0, 1.0);
33 
34  glOrtho(-10.0 * aspect, 10.0 * aspect, -10.0, 10.0, -1.0, 1.0);
35  }
36 }

Referenced by main().

◆ timer()

void timer ( int  v)

Definition at line 73 of file openGL_torus_animated.c.

73  {
74  if (spinning) {
75  current_rot += 1.0;
76 
77  if (current_rot > 360.0) {
78  current_rot -= 360.0;
79  }
80  glutPostRedisplay();
81  }
82 
83  glutTimerFunc(1000 / FPS, timer, v);
84 }
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 14 of file openGL_torus_animated.c.

Referenced by display(), and timer().

◆ FPS

const int FPS = 20
static

Definition at line 11 of file openGL_torus_animated.c.

Referenced by timer().

◆ spinning

bool spinning = true
static

Definition at line 10 of file openGL_torus_animated.c.

Referenced by mouse(), and timer().