Fixed a build problem.
[gljewel.git] / pyramid.c
blob29b53f5fb2b59e9da2eea55479f3bb0ea2d9c21a
1 /**
2 * @file pyramid.c
3 * @brief The pyramid module.
5 * Copyright 2001, 2008 David Ashley <dashxdr@gmail.com>
6 * Copyright 2008 Stephen M. Webb <stephen.webb@bregmasoft.ca>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of Version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "pyramid.h"
23 #if defined(_WIN32) && !defined(__CYGWIN__)
24 # define WIN32_LEAN_AND_MEAN 1
25 # include <windows.h>
26 #endif
28 #include <GL/gl.h>
29 #include "vbo.h"
31 static const int row_width = (3*2);
32 static const int bottom_vertex_count = 4;
33 static const int side_vertex_count = 6;
34 #define stride (row_width * sizeof(GLfloat))
36 static GLuint vbo;
39 static void create(float size)
41 GLfloat pyramid[] = {
42 /* bottom */
43 0.0f, -1.0f, 0.0f, size, -size, size,
44 0.0f, -1.0f, 0.0f, size, -size, -size,
45 0.0f, -1.0f, 0.0f, -size, -size, -size,
46 0.0f, -1.0f, 0.0f, -size, -size, size,
47 /* sides */
48 0.0f, 1.0f, 0.0f, 0.0f, size, 0.0f,
49 1.0f, -1.0f, 1.0f, size, -size, size,
50 1.0f, -1.0f, -1.0f, size, -size, -size,
51 -1.0f, -1.0f, -1.0f, -size, -size, -size,
52 -1.0f, -1.0f, 1.0f, -size, -size, size,
53 1.0f, -1.0f, 1.0f, size, -size, size
55 glGenBuffers(1, &vbo);
56 glBindBuffer(GL_ARRAY_BUFFER, vbo);
57 glBufferData(GL_ARRAY_BUFFER, sizeof(pyramid), pyramid, GL_STATIC_DRAW);
58 glBindBuffer(GL_ARRAY_BUFFER, 0);
62 static void draw(void)
64 GLfloat *pyramid_normals = 0;
65 GLfloat *pyramid_verteces = pyramid_normals + 3;
66 glEnable(GL_NORMALIZE);
67 glEnableClientState(GL_VERTEX_ARRAY);
68 glEnableClientState(GL_NORMAL_ARRAY);
69 glBindBuffer(GL_ARRAY_BUFFER, vbo);
70 glNormalPointer(GL_FLOAT, stride, pyramid_normals);
71 glVertexPointer(3, GL_FLOAT, stride, pyramid_verteces);
72 glDrawArrays(GL_TRIANGLE_FAN, 0, bottom_vertex_count);
73 glDrawArrays(GL_TRIANGLE_FAN, bottom_vertex_count, side_vertex_count);
74 glBindBuffer(GL_ARRAY_BUFFER, 0);
75 glDisableClientState(GL_NORMAL_ARRAY);
76 glDisableClientState(GL_VERTEX_ARRAY);
77 glDisable(GL_NORMALIZE);
81 static char* name(void)
83 return "pyramid";
87 void create_pyramid(shape* shape)
89 shape->create = create;
90 shape->draw = draw;
91 shape->name = name;
95 void makepyramid(float size)
97 GLfloat pyramid[] = {
98 /* bottom */
99 0.0f, -1.0f, 0.0f, size, -size, size,
100 0.0f, -1.0f, 0.0f, size, -size, -size,
101 0.0f, -1.0f, 0.0f, -size, -size, -size,
102 0.0f, -1.0f, 0.0f, -size, -size, size,
103 /* sides */
104 0.0f, 1.0f, 0.0f, 0.0f, size, 0.0f,
105 1.0f, -1.0f, 1.0f, size, -size, size,
106 1.0f, -1.0f, -1.0f, size, -size, -size,
107 -1.0f, -1.0f, -1.0f, -size, -size, -size,
108 -1.0f, -1.0f, 1.0f, -size, -size, size,
109 1.0f, -1.0f, 1.0f, size, -size, size
112 const GLfloat *pyramid_normals = pyramid;
113 const GLfloat *pyramid_verteces = pyramid_normals + 3;
115 glEnable(GL_NORMALIZE);
116 glEnableClientState(GL_VERTEX_ARRAY);
117 glEnableClientState(GL_NORMAL_ARRAY);
119 glNormalPointer(GL_FLOAT, stride, pyramid_normals);
120 glVertexPointer(3, GL_FLOAT, stride, pyramid_verteces);
121 glDrawArrays(GL_TRIANGLE_FAN, 0, bottom_vertex_count);
122 glDrawArrays(GL_TRIANGLE_FAN, bottom_vertex_count, side_vertex_count);
123 glDisableClientState(GL_NORMAL_ARRAY);
124 glDisableClientState(GL_VERTEX_ARRAY);