-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcube.c
More file actions
227 lines (182 loc) · 7.62 KB
/
cube.c
File metadata and controls
227 lines (182 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* Copyright (c) 2009 Robert Kooima */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a */
/* copy of this software and associated documentation files (the "Software"), */
/* to deal in the Software without restriction, including without limitation */
/* the rights to use, copy, modify, merge, publish, distribute, sublicense, */
/* and/or sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in */
/* all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL */
/* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER */
/* DEALINGS IN THE SOFTWARE. */
#include <stdlib.h>
#include <assert.h>
#include <GL/glew.h>
#include "image.h"
#include "cube.h"
/*----------------------------------------------------------------------------*/
struct cube
{
GLuint tex[6];
GLuint vbo[1];
GLuint ebo[1];
};
/*----------------------------------------------------------------------------*/
/* The following data give the vertices of the standard unit cube. These */
/* may be stored in a vertex array or buffer object and used directly with */
/* a call to glDrawArrays(GL_QUADS, 0, 24). The vertex ordering and texture */
/* coordinates provided give face orientations in line with the definition */
/* of GL_TEXTURE_CUBE_MAP. */
struct vert
{
GLfloat v[3];
GLfloat n[3];
GLfloat t[2];
};
static const struct vert verts[24] = {
/* +X */
{{ 1.f, 1.f, 1.f }, { 1.f, 0.f, 0.f }, { 0.f, 1.f }},
{{ 1.f, -1.f, 1.f }, { 1.f, 0.f, 0.f }, { 0.f, 0.f }},
{{ 1.f, -1.f, -1.f }, { 1.f, 0.f, 0.f }, { 1.f, 0.f }},
{{ 1.f, 1.f, -1.f }, { 1.f, 0.f, 0.f }, { 1.f, 1.f }},
/* -X */
{{ -1.f, 1.f, -1.f }, { -1.f, 0.f, 0.f }, { 0.f, 1.f }},
{{ -1.f, -1.f, -1.f }, { -1.f, 0.f, 0.f }, { 0.f, 0.f }},
{{ -1.f, -1.f, 1.f }, { -1.f, 0.f, 0.f }, { 1.f, 0.f }},
{{ -1.f, 1.f, 1.f }, { -1.f, 0.f, 0.f }, { 1.f, 1.f }},
/* +Y */
{{ -1.f, 1.f, -1.f }, { 0.f, 1.f, 0.f }, { 0.f, 0.f }},
{{ -1.f, 1.f, 1.f }, { 0.f, 1.f, 0.f }, { 0.f, 1.f }},
{{ 1.f, 1.f, 1.f }, { 0.f, 1.f, 0.f }, { 1.f, 1.f }},
{{ 1.f, 1.f, -1.f }, { 0.f, 1.f, 0.f }, { 1.f, 0.f }},
/* -Y */
{{ -1.f, -1.f, 1.f }, { 0.f, -1.f, 0.f }, { 0.f, 0.f }},
{{ -1.f, -1.f, -1.f }, { 0.f, -1.f, 0.f }, { 0.f, 1.f }},
{{ 1.f, -1.f, -1.f }, { 0.f, -1.f, 0.f }, { 1.f, 1.f }},
{{ 1.f, -1.f, 1.f }, { 0.f, -1.f, 0.f }, { 1.f, 0.f }},
/* +Z */
{{ -1.f, 1.f, 1.f }, { 0.f, 0.f, 1.f }, { 0.f, 0.f }},
{{ -1.f, -1.f, 1.f }, { 0.f, 0.f, 1.f }, { 0.f, 1.f }},
{{ 1.f, -1.f, 1.f }, { 0.f, 0.f, 1.f }, { 1.f, 1.f }},
{{ 1.f, 1.f, 1.f }, { 0.f, 0.f, 1.f }, { 1.f, 0.f }},
/* -Z */
{{ 1.f, 1.f, -1.f }, { 0.f, 0.f, -1.f }, { 0.f, 0.f }},
{{ 1.f, -1.f, -1.f }, { 0.f, 0.f, -1.f }, { 0.f, 1.f }},
{{ -1.f, -1.f, -1.f }, { 0.f, 0.f, -1.f }, { 1.f, 1.f }},
{{ -1.f, 1.f, -1.f }, { 0.f, 0.f, -1.f }, { 1.f, 0.f }},
};
static const GLushort elems[36] = {
0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
8, 9, 10, 8, 10, 11,
12, 13, 14, 12, 14, 15,
16, 17, 18, 16, 18, 19,
20, 21, 22, 20, 22, 23
};
static void init_vbo(struct cube *C)
{
glBindBuffer(GL_ARRAY_BUFFER, C->vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, C->ebo[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elems), elems, GL_STATIC_DRAW);
}
/*----------------------------------------------------------------------------*/
static const char *names[6] = {
"cubepx.png",
"cubenx.png",
"cubepy.png",
"cubeny.png",
"cubepz.png",
"cubenz.png",
};
/* Load all texture images and initialize all OpenGL exture objects. */
static void init_tex(struct cube *C)
{
void *p;
int w;
int h;
int c;
int b;
int i;
for (i = 0; i < 6; ++i)
if ((p = image_read(names[i], &w, &h, &c, &b)))
{
int f = image_internal_form(c, b);
int e = image_external_form(c);
int t = image_external_type(b);
glBindTexture(GL_TEXTURE_2D, C->tex[i]);
glTexImage2D (GL_TEXTURE_2D, 0, f, w, h, 0, e, t, p);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
}
/*----------------------------------------------------------------------------*/
/* Allocate and initialize a new cube object. There must be a current OpenGL */
/* context at the time. */
cube *cube_create(void)
{
cube *C;
if ((C = (cube *) malloc(sizeof (cube))))
{
glGenBuffers (1, C->vbo);
glGenBuffers (1, C->ebo);
glGenTextures(6, C->tex);
init_vbo(C);
init_tex(C);
}
return C;
}
/* Release the given cube structure and all resources held by it. */
void cube_delete(cube *C)
{
assert(C);
glDeleteTextures(6, C->tex);
glDeleteBuffers (1, C->ebo);
glDeleteBuffers (1, C->vbo);
free(C);
}
/*----------------------------------------------------------------------------*/
/* Render the given cube structure. */
void cube_render(cube *C)
{
const size_t sz = sizeof (GLfloat);
assert(C);
/* Enable the necessary array pointers. */
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
{
/* Bind the array pointers to the array buffer object. */
glBindBuffer(GL_ARRAY_BUFFER, C->vbo[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, C->ebo[0]);
{
size_t i;
glVertexPointer( 3, GL_FLOAT, sz * 8, (GLvoid *) ( 0));
glNormalPointer( GL_FLOAT, sz * 8, (GLvoid *) (sz * 3));
glTexCoordPointer(2, GL_FLOAT, sz * 8, (GLvoid *) (sz * 6));
/* Draw six quads. */
for (i = 0; i < 6; ++i)
{
glBindTexture(GL_TEXTURE_2D, C->tex[i]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT,
(const GLvoid *) (i * 12));
}
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);
}
/*----------------------------------------------------------------------------*/