Skip to content

Commit fa4e514

Browse files
author
Eric Sartre
committed
add various samples
1 parent 0d64449 commit fa4e514

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

samples/planet.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include "geometry.h"
3+
#include "solid.h"
4+
5+
int main()
6+
{
7+
init_geometry();
8+
shape_t *planet = new_sphere(50, 20, 20);
9+
shape_t *sp1 = new_sphere(10, 16, 16);
10+
shape_t *sp2 = new_sphere(10, 16, 16);
11+
shape_t *t1 = new_taurus(30, 4, 50, 10);
12+
shape_t *t2 = new_taurus(30, 4, 40, 10);
13+
shape_translate(sp1, 80, 70, 40);
14+
shape_translate(sp2, -90, 10, -60);
15+
shape_scale(t1, 5, 1.2, 5);
16+
shape_scale(t2, 3.5, 1.2, 3.5);
17+
shape_rotate(t1, -20, 0, 0, 1);
18+
shape_rotate(t2, 20, 0, 0, 1);
19+
flushOBJ(stdout);
20+
free_shape(planet);
21+
free_shape(t1);
22+
free_shape(t2);
23+
finalize_geometry();
24+
return 0;
25+
}
26+

samples/revolution.c

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include <math.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <GL/glut.h>
5+
#include "geometry.h"
6+
#include "solid.h"
7+
8+
#define INITIAL_CAPACITY_POINTS 100
9+
#define DIVISION_NUMBER 60
10+
#define WINDOW_HEIGHT 600
11+
12+
static point_t *pt = NULL;
13+
static unsigned numpoints = 0;
14+
static unsigned capacity = INITIAL_CAPACITY_POINTS;
15+
static FILE *file_out = NULL;
16+
17+
void push_point(const double x, const double y)
18+
{
19+
if(numpoints >= capacity) {
20+
capacity *= 2;
21+
if((pt = realloc(pt, capacity * sizeof(point_t))) == NULL) {
22+
error_printf("out of memory\n");
23+
exit(EXIT_FAILURE);
24+
}
25+
}
26+
pt[numpoints].x = x;
27+
pt[numpoints].y = y;
28+
numpoints++;
29+
}
30+
31+
void discard_top()
32+
{
33+
if(numpoints == 0) return;
34+
numpoints--;
35+
}
36+
37+
void display(void)
38+
{
39+
glClear(GL_COLOR_BUFFER_BIT);
40+
// Draw points
41+
glLineWidth(3.0);
42+
glBegin(GL_POINTS);
43+
for(unsigned i = 0; i < numpoints; ++i) glVertex2d(pt[i].x, pt[i].y);
44+
glEnd();
45+
// Draw lines
46+
glLineWidth(1.0);
47+
glBegin(GL_LINE_STRIP);
48+
for(unsigned i = 0; i < numpoints; ++i) glVertex2d(pt[i].x, pt[i].y);
49+
glEnd();
50+
glFlush();
51+
}
52+
53+
void handle_mouse(const int btn, const int state, const int x, const int y)
54+
{
55+
if(state != GLUT_DOWN) return;
56+
switch(btn) {
57+
case GLUT_LEFT_BUTTON:
58+
push_point(x, y);
59+
break;
60+
case GLUT_RIGHT_BUTTON:
61+
discard_top();
62+
break;
63+
default: break;
64+
}
65+
glutPostRedisplay();
66+
}
67+
68+
void to_revolution()
69+
{
70+
init_geometry();
71+
shape_t *s = new_shape();
72+
for(unsigned i = 0; i < numpoints; ++i) {
73+
add_vertex(s, new_vertex(pt[i].x, WINDOW_HEIGHT-pt[i].y, 0));
74+
}
75+
new_revolution(s, DIVISION_NUMBER);
76+
flushOBJ(file_out);
77+
finalize_geometry();
78+
}
79+
80+
void handle_keyboard(const unsigned char key, const int x, const int y)
81+
{
82+
#define KEY_ESC 27
83+
#define KEY_ENTER 13
84+
switch (key) {
85+
case KEY_ESC:
86+
case KEY_ENTER:
87+
to_revolution();
88+
exit(EXIT_SUCCESS);
89+
default:
90+
break;
91+
}
92+
}
93+
94+
void reshape(const int w, const int h)
95+
{
96+
glViewport(0, 0, w, h);
97+
glMatrixMode(GL_PROJECTION);
98+
glLoadIdentity();
99+
glOrtho(0.0, (float)w, (float)h, 0.0, -10.0, 10.0);
100+
glMatrixMode(GL_MODELVIEW);
101+
}
102+
103+
int main(int argc, char *argv[])
104+
{
105+
if(argc < 2) {
106+
error_printf("missing file argument\n");
107+
fprintf(stderr, "usage : %s output_filename\n", argv[0]);
108+
exit(EXIT_FAILURE);
109+
}
110+
if((pt = malloc(capacity * sizeof(point_t))) == NULL) {
111+
error_printf("out of memory\n");
112+
exit(EXIT_FAILURE);
113+
}
114+
if((file_out = fopen(argv[1], "w")) == NULL) {
115+
error_printf("failed to open %s for write\n");
116+
FREE(pt);
117+
exit(EXIT_FAILURE);
118+
}
119+
glutInitWindowSize(600, WINDOW_HEIGHT);
120+
glutInitWindowPosition(10, 10);
121+
glutInit(&argc, argv);
122+
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
123+
glutCreateWindow(argv[0]);
124+
glClearColor(1.0, 1.0, 1.0, 0.0);
125+
glColor3f(0.0, 0.0, 0.0);
126+
glPointSize(7.0);
127+
glutDisplayFunc(display);
128+
glutReshapeFunc(reshape);
129+
glutMouseFunc(handle_mouse);
130+
glutKeyboardFunc(handle_keyboard);
131+
glutMainLoop();
132+
return 0;
133+
}
134+

samples/snowman.png

93.4 KB
Loading

samples/sphere.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
#include "geometry.h"
3+
#include "solid.h"
4+
5+
int main()
6+
{
7+
init_geometry();
8+
shape_t *s = new_sphere(50, 30, 30);
9+
flushOBJ(stdout);
10+
free_shape(s);
11+
finalize_geometry();
12+
return 0;
13+
}
14+

samples/taurus.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
#include "geometry.h"
3+
#include "solid.h"
4+
5+
int main()
6+
{
7+
init_geometry();
8+
shape_t *s = new_taurus(30, 15, 30, 30);
9+
flushOBJ(stdout);
10+
free_shape(s);
11+
finalize_geometry();
12+
return 0;
13+
}
14+

samples/wagasa.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#include "geometry.h"
3+
#include "solid.h"
4+
5+
int main()
6+
{
7+
init_geometry();
8+
shape_t *s = new_shape();
9+
add_vertex(s, new_vertex(0, 300, 0));
10+
add_vertex(s, new_vertex(20, 300, 0));
11+
add_vertex(s, new_vertex(20, 290, 0));
12+
add_vertex(s, new_vertex(300, 220, 0));
13+
add_vertex(s, new_vertex(295, 218, 0));
14+
add_vertex(s, new_vertex(60, 260, 0));
15+
add_vertex(s, new_vertex(5, 220, 0));
16+
add_vertex(s, new_vertex(5, 0, 0));
17+
add_vertex(s, new_vertex(0, 0, 0));
18+
shape_t *u = new_revolution(s, 60);
19+
flushOBJ(stdout);
20+
free_shape(s);
21+
free_shape(u);
22+
finalize_geometry();
23+
return 0;
24+
}
25+

0 commit comments

Comments
 (0)