-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskybox.c
executable file
·169 lines (142 loc) · 4.91 KB
/
skybox.c
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
/*
* Hostile Takeover:
* CS248 Final Project, Fall 2004
*
* Written by Jonathan Reeves
* jrreeves@stanford.edu
*
* A simple 3D video game demonstrating the use of OpenGL for graphics
* rendering with a number of advanced techniques and optimizations to make
* it interesting. See the README file which came with this package for more
* details. It also includes a list of sources which were consulted while
* building this software package.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "texture.h"
#include "skybox.h"
skybox_t *SkyboxLoad(float width, float length, float height,
float centerX, float centerZ){
float x, y, z;
texture_t tempTex;
skybox_t *box;
x = centerX - width/2;
y = -height/2;
z = centerZ - length/2;
/* wont do much without this... */
box = (skybox_t *)malloc(sizeof(skybox_t));
if(box == NULL){
fprintf(stderr, "error getting skybox memory\n");
return NULL;
}
/* load all textures */
box->texID[SKY_BACK] = TextureLoadSky(&tempTex, "textures/skyback.ppm");
box->texID[SKY_FRONT] = TextureLoadSky(&tempTex, "textures/skyfront.ppm");
box->texID[SKY_BOTTOM] = TextureLoadSky(&tempTex, "textures/skybottom.ppm");
box->texID[SKY_TOP] = TextureLoadSky(&tempTex, "textures/skytop.ppm");
box->texID[SKY_LEFT] = TextureLoadSky(&tempTex, "textures/skyleft.ppm");
box->texID[SKY_RIGHT] = TextureLoadSky(&tempTex, "textures/skyright.ppm");
/* set the back face */
VectorSet(box->back, x+width, y, z);
VectorSet(box->back + 1, x+width, y+height, z);
VectorSet(box->back + 2, x, y+height, z);
VectorSet(box->back + 3, x, y, z);
/* set the front face */
VectorSet(box->front, x, y, z + length);
VectorSet(box->front + 1, x, y + height, z + length);
VectorSet(box->front + 2, x + width, y+height, z + length);
VectorSet(box->front + 3, x + width, y, z + length);
/* etc... */
VectorSet(box->bottom, x, y, z);
VectorSet(box->bottom + 1, x, y, z + length);
VectorSet(box->bottom + 2, x + width, y, z + length);
VectorSet(box->bottom + 3, x + width, y, z);
VectorSet(box->top, x + width, y + height, z);
VectorSet(box->top + 1, x + width, y + height, z + length);
VectorSet(box->top + 2, x, y + height, z + length);
VectorSet(box->top + 3, x, y + height, z);
VectorSet(box->left, x + width, y, z);
VectorSet(box->left + 1, x + width, y, z + length);
VectorSet(box->left + 2, x + width, y + height, z + length);
VectorSet(box->left + 3, x + width, y + height, z);
VectorSet(box->right, x, y + height, z);
VectorSet(box->right + 1, x, y + height, z + length);
VectorSet(box->right + 2, x, y, z + length);
VectorSet(box->right + 3, x, y, z);
return box;
}
void SkyboxDraw(skybox_t *box){
glBindTexture(GL_TEXTURE_2D, box->texID[SKY_BACK]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0);
glVertex3fv((float *)(box->back));
glTexCoord2f(0.0, 0.0);
glVertex3fv((float *)(box->back + 1));
glTexCoord2f(1.0, 0.0);
glVertex3fv((float *)(box->back + 2));
glTexCoord2f(1.0, 1.0);
glVertex3fv((float *)(box->back + 3));
glEnd();
glBindTexture(GL_TEXTURE_2D, box->texID[SKY_FRONT]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0);
glVertex3fv((float *)(box->front));
glTexCoord2f(0.0, 0.0);
glVertex3fv((float *)(box->front + 1));
glTexCoord2f(1.0, 0.0);
glVertex3fv((float *)(box->front + 2));
glTexCoord2f(1.0, 1.0);
glVertex3fv((float *)(box->front + 3));
glEnd();
glBindTexture(GL_TEXTURE_2D, box->texID[SKY_BOTTOM]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0);
glVertex3fv((float *)(box->bottom));
glTexCoord2f(0.0, 0.0);
glVertex3fv((float *)(box->bottom + 1));
glTexCoord2f(1.0, 0.0);
glVertex3fv((float *)(box->bottom + 2));
glTexCoord2f(1.0, 1.0);
glVertex3fv((float *)(box->bottom + 3));
glEnd();
glBindTexture(GL_TEXTURE_2D, box->texID[SKY_TOP]);
glBegin(GL_QUADS);
glTexCoord2f(1.0, 0.0);
glVertex3fv((float *)(box->top));
glTexCoord2f(1.0, 1.0);
glVertex3fv((float *)(box->top + 1));
glTexCoord2f(0.0, 1.0);
glVertex3fv((float *)(box->top + 2));
glTexCoord2f(0.0, 0.0);
glVertex3fv((float *)(box->top + 3));
glEnd();
glBindTexture(GL_TEXTURE_2D, box->texID[SKY_LEFT]);
glBegin(GL_QUADS);
glTexCoord2f(1.0, 1.0);
glVertex3fv((float *)(box->left));
glTexCoord2f(0.0, 1.0);
glVertex3fv((float *)(box->left + 1));
glTexCoord2f(0.0, 0.0);
glVertex3fv((float *)(box->left + 2));
glTexCoord2f(1.0, 0.0);
glVertex3fv((float *)(box->left + 3));
glEnd();
glBindTexture(GL_TEXTURE_2D, box->texID[SKY_RIGHT]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv((float *)(box->right));
glTexCoord2f(1.0, 0.0);
glVertex3fv((float *)(box->right + 1));
glTexCoord2f(1.0, 1.0);
glVertex3fv((float *)(box->right + 2));
glTexCoord2f(0.0, 1.0);
glVertex3fv((float *)(box->right + 3));
glEnd();
return;
}
void SkyboxDestroy(skybox_t *skybox){
free(skybox);
}