-
Notifications
You must be signed in to change notification settings - Fork 2
/
skybox.c
161 lines (130 loc) · 4.74 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
/*
* skybox.c
*
* code to load, create display list for and display skybox
*
* some of this code was used form other sources where noted
*/
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// current ship heading in cubeview.c
extern float shiphdg;
// this function taken from http://www.nullterminator.net/gltexture.html
// shouldn't this be mmap() for speed?
// load a 2048x2048 RGB .RAW file as a texture
GLuint LoadTextureRAW( const char * filename, int wrap )
{
GLuint texture;
int width, height;
char * data;
FILE * file;
// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
// allocate buffer
width = 2048;
height = 2048;
data = malloc( width * height * 3 );
// read texture data
fread( data, width * height * 3, 1, file );
fclose( file );
// allocate a texture name
glGenTextures( 1, &texture );
// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );
// select modulate to mix texture with color for shading
//glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
GL_RGB, GL_UNSIGNED_BYTE, data );
// free buffer
free( data );
return texture;
}
// render skybox around camera
void rendersky(GLuint sky){
glPushMatrix();
glRotatef(shiphdg, 0.0, 1.0, 0.0);
glCallList(sky);
glPopMatrix();
}
// most of this is taken from
// http://sidvind.com/wiki?title=Skybox_tutorial&oldid=2196
// build a skybox from the six textures loaded
GLuint buildrendersky(GLuint *skybox){
unsigned int sky = glGenLists(1);
glNewList(sky,GL_COMPILE);
assert(NULL != skybox);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Enable/Disable features
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);
//glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
// Just in case we set all vertices to white.
glColor4f(1,1,1,1);
// Render the front quad
glBindTexture(GL_TEXTURE_2D, skybox[0]);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3d( 300.5f, -300.5f, -300.5f );
glTexCoord2f(1, 1); glVertex3d( -300.5f, -300.5f, -300.5f );
glTexCoord2f(1, 0); glVertex3d( -300.5f, 300.5f, -300.5f );
glTexCoord2f(0, 0); glVertex3d( 300.5f, 300.5f, -300.5f );
glEnd();
// Render the left quad
glBindTexture(GL_TEXTURE_2D, skybox[1]);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3d( 300.5f, -300.5f, 300.5f );
glTexCoord2f(1, 1); glVertex3d( 300.5f, -300.5f, -300.5f );
glTexCoord2f(1, 0); glVertex3d( 300.5f, 300.5f, -300.5f );
glTexCoord2f(0, 0); glVertex3d( 300.5f, 300.5f, 300.5f );
glEnd();
// Render the back quad
glBindTexture(GL_TEXTURE_2D, skybox[2]);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3d( -300.5f, -300.5f, 300.5f );
glTexCoord2f(1, 1); glVertex3d( 300.5f, -300.5f, 300.5f );
glTexCoord2f(1, 0); glVertex3d( 300.5f, 300.5f, 300.5f );
glTexCoord2f(0, 0); glVertex3d( -300.5f, 300.5f, 300.5f );
glEnd();
// Render the right quad
glBindTexture(GL_TEXTURE_2D, skybox[3]);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3d( -300.5f, -300.5f, -300.5f );
glTexCoord2f(1, 1); glVertex3d( -300.5f, -300.5f, 300.5f );
glTexCoord2f(1, 0); glVertex3d( -300.5f, 300.5f, 300.5f );
glTexCoord2f(0, 0); glVertex3d( -300.5f, 300.5f, -300.5f );
glEnd();
// Render the top quad
glBindTexture(GL_TEXTURE_2D, skybox[4]);
glBegin(GL_QUADS);
glTexCoord2f(1, 1); glVertex3d( -300.5f, 300.5f, -300.5f );
glTexCoord2f(1, 0); glVertex3d( -300.5f, 300.5f, 300.5f );
glTexCoord2f(0, 0); glVertex3d( 300.5f, 300.5f, 300.5f );
glTexCoord2f(0, 1); glVertex3d( 300.5f, 300.5f, -300.5f );
glEnd();
// Render the bottom quad
glBindTexture(GL_TEXTURE_2D, skybox[5]);
glBegin(GL_QUADS);
glTexCoord2f(1, 0); glVertex3d( -300.5f, -300.5f, -300.5f );
glTexCoord2f(1, 1); glVertex3d( -300.5f, -300.5f, 300.5f );
glTexCoord2f(0, 1); glVertex3d( 300.5f, -300.5f, 300.5f );
glTexCoord2f(0, 0); glVertex3d( 300.5f, -300.5f, -300.5f );
glEnd();
// Restore enable bits and matrix
glPopAttrib();
glEndList();
return sky;
}