code format
This commit is contained in:
parent
04fac81a8e
commit
6200685e52
95
glxgears.c
95
glxgears.c
@ -12,13 +12,11 @@
|
|||||||
|
|
||||||
/* Conversion to GLUT by Mark J. Kilgard */
|
/* Conversion to GLUT by Mark J. Kilgard */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "glut_wrap.h"
|
#include "glut_wrap.h"
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static GLint T0 = 0;
|
static GLint T0 = 0;
|
||||||
static GLint Frames = 0;
|
static GLint Frames = 0;
|
||||||
@ -28,7 +26,6 @@ static GLboolean Visible = GL_TRUE;
|
|||||||
static GLboolean Animate = GL_TRUE;
|
static GLboolean Animate = GL_TRUE;
|
||||||
static GLfloat viewDist = 40.0;
|
static GLfloat viewDist = 40.0;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
||||||
Draw a gear wheel. You'll probably want to call this function when
|
Draw a gear wheel. You'll probably want to call this function when
|
||||||
@ -42,10 +39,10 @@ static GLfloat viewDist = 40.0;
|
|||||||
|
|
||||||
**/
|
**/
|
||||||
|
|
||||||
static void
|
static void gear(
|
||||||
gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
|
GLfloat inner_radius, GLfloat outer_radius, GLfloat width, GLint teeth,
|
||||||
GLint teeth, GLfloat tooth_depth)
|
GLfloat tooth_depth
|
||||||
{
|
) {
|
||||||
GLint i;
|
GLint i;
|
||||||
GLfloat r0, r1, r2;
|
GLfloat r0, r1, r2;
|
||||||
GLfloat angle, da;
|
GLfloat angle, da;
|
||||||
@ -69,7 +66,9 @@ gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
|
|||||||
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
|
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
|
||||||
if (i < teeth) {
|
if (i < teeth) {
|
||||||
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
|
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
|
||||||
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
|
glVertex3f(
|
||||||
|
r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
glEnd();
|
glEnd();
|
||||||
@ -96,7 +95,9 @@ gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
|
|||||||
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
|
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
|
||||||
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
|
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
|
||||||
if (i < teeth) {
|
if (i < teeth) {
|
||||||
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
|
glVertex3f(
|
||||||
|
r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5
|
||||||
|
);
|
||||||
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
|
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,8 +109,12 @@ gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
|
|||||||
for (i = 0; i < teeth; i++) {
|
for (i = 0; i < teeth; i++) {
|
||||||
angle = i * 2.0 * M_PI / teeth;
|
angle = i * 2.0 * M_PI / teeth;
|
||||||
|
|
||||||
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
|
glVertex3f(
|
||||||
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
|
r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5
|
||||||
|
);
|
||||||
|
glVertex3f(
|
||||||
|
r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5
|
||||||
|
);
|
||||||
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
|
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
|
||||||
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
|
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
|
||||||
}
|
}
|
||||||
@ -132,12 +137,16 @@ gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
|
|||||||
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
|
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
|
||||||
glNormal3f(cos(angle), sin(angle), 0.0);
|
glNormal3f(cos(angle), sin(angle), 0.0);
|
||||||
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
|
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
|
||||||
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
|
glVertex3f(
|
||||||
|
r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5
|
||||||
|
);
|
||||||
u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
|
u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
|
||||||
v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
|
v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
|
||||||
glNormal3f(v, -u, 0.0);
|
glNormal3f(v, -u, 0.0);
|
||||||
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
|
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
|
||||||
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
|
glVertex3f(
|
||||||
|
r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5
|
||||||
|
);
|
||||||
glNormal3f(cos(angle), sin(angle), 0.0);
|
glNormal3f(cos(angle), sin(angle), 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,25 +166,20 @@ gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
|
|||||||
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
|
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
|
||||||
}
|
}
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
|
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
|
||||||
static GLint gear1, gear2, gear3;
|
static GLint gear1, gear2, gear3;
|
||||||
static GLfloat angle = 0.0;
|
static GLfloat angle = 0.0;
|
||||||
|
|
||||||
static void
|
static void cleanup(void) {
|
||||||
cleanup(void)
|
|
||||||
{
|
|
||||||
glDeleteLists(gear1, 1);
|
glDeleteLists(gear1, 1);
|
||||||
glDeleteLists(gear2, 1);
|
glDeleteLists(gear2, 1);
|
||||||
glDeleteLists(gear3, 1);
|
glDeleteLists(gear3, 1);
|
||||||
glutDestroyWindow(win);
|
glutDestroyWindow(win);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void draw(void) {
|
||||||
draw(void)
|
|
||||||
{
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
@ -227,14 +231,10 @@ draw(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void idle(void) {
|
||||||
static void
|
|
||||||
idle(void)
|
|
||||||
{
|
|
||||||
static double t0 = -1.;
|
static double t0 = -1.;
|
||||||
double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
|
double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
|
||||||
if (t0 < 0.0)
|
if (t0 < 0.0) t0 = t;
|
||||||
t0 = t;
|
|
||||||
dt = t - t0;
|
dt = t - t0;
|
||||||
t0 = t;
|
t0 = t;
|
||||||
|
|
||||||
@ -244,9 +244,7 @@ idle(void)
|
|||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void update_idle_func(void) {
|
||||||
update_idle_func(void)
|
|
||||||
{
|
|
||||||
if (Visible && Animate)
|
if (Visible && Animate)
|
||||||
glutIdleFunc(idle);
|
glutIdleFunc(idle);
|
||||||
else
|
else
|
||||||
@ -255,9 +253,7 @@ update_idle_func(void)
|
|||||||
|
|
||||||
/* change view angle, exit upon ESC */
|
/* change view angle, exit upon ESC */
|
||||||
/* ARGSUSED1 */
|
/* ARGSUSED1 */
|
||||||
static void
|
static void key(unsigned char k, int, int) {
|
||||||
key(unsigned char k, int x, int y)
|
|
||||||
{
|
|
||||||
switch (k) {
|
switch (k) {
|
||||||
case 'z':
|
case 'z':
|
||||||
view_rotz += 5.0;
|
view_rotz += 5.0;
|
||||||
@ -287,9 +283,7 @@ key(unsigned char k, int x, int y)
|
|||||||
|
|
||||||
/* change view angle */
|
/* change view angle */
|
||||||
/* ARGSUSED1 */
|
/* ARGSUSED1 */
|
||||||
static void
|
static void special(int k, int, int) {
|
||||||
special(int k, int x, int y)
|
|
||||||
{
|
|
||||||
switch (k) {
|
switch (k) {
|
||||||
case GLUT_KEY_UP:
|
case GLUT_KEY_UP:
|
||||||
view_rotx += 5.0;
|
view_rotx += 5.0;
|
||||||
@ -310,9 +304,7 @@ special(int k, int x, int y)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* new window size or exposure */
|
/* new window size or exposure */
|
||||||
static void
|
static void reshape(int width, int height) {
|
||||||
reshape(int width, int height)
|
|
||||||
{
|
|
||||||
GLfloat h = (GLfloat)height / (GLfloat)width;
|
GLfloat h = (GLfloat)height / (GLfloat)width;
|
||||||
|
|
||||||
glViewport(0, 0, (GLint)width, (GLint)height);
|
glViewport(0, 0, (GLint)width, (GLint)height);
|
||||||
@ -322,9 +314,7 @@ reshape(int width, int height)
|
|||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void init(int argc, char *argv[]) {
|
||||||
init(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
static GLfloat pos[4] = {5.0, 5.0, 10.0, 0.0};
|
static GLfloat pos[4] = {5.0, 5.0, 10.0, 0.0};
|
||||||
static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0};
|
static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0};
|
||||||
static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
|
static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
|
||||||
@ -365,28 +355,22 @@ init(int argc, char *argv[])
|
|||||||
printf("GL_VENDOR = %s\n", (char *)glGetString(GL_VENDOR));
|
printf("GL_VENDOR = %s\n", (char *)glGetString(GL_VENDOR));
|
||||||
printf("GL_EXTENSIONS = %s\n", (char *)glGetString(GL_EXTENSIONS));
|
printf("GL_EXTENSIONS = %s\n", (char *)glGetString(GL_EXTENSIONS));
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
} else if (strcmp(argv[i], "-exit") == 0) {
|
||||||
else if ( strcmp(argv[i], "-exit")==0) {
|
|
||||||
autoexit = 30;
|
autoexit = 30;
|
||||||
printf("Auto Exit after %i seconds.\n", autoexit);
|
printf("Auto Exit after %i seconds.\n", autoexit);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
} else if (strcmp(argv[i], "-noanim") == 0) {
|
||||||
else if (strcmp(argv[i], "-noanim") == 0) {
|
|
||||||
Animate = GL_FALSE;
|
Animate = GL_FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void visible(int vis) {
|
||||||
static void
|
|
||||||
visible(int vis)
|
|
||||||
{
|
|
||||||
Visible = vis;
|
Visible = vis;
|
||||||
update_idle_func();
|
update_idle_func();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[]) {
|
||||||
{
|
|
||||||
glutInitWindowSize(300, 300);
|
glutInitWindowSize(300, 300);
|
||||||
glutInit(&argc, argv);
|
glutInit(&argc, argv);
|
||||||
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
|
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
|
||||||
@ -403,4 +387,3 @@ int main(int argc, char *argv[])
|
|||||||
glutMainLoop();
|
glutMainLoop();
|
||||||
return 0; /* ANSI C requires main to return int. */
|
return 0; /* ANSI C requires main to return int. */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user