Generate stars
Description
Texture needs to be sufficiently large to avoid showing corners of box.
Screenshot
Commands
git clone git@github.com:atsheehan/iridium
cd iridium
git checkout 2b487843b740b5da4b44180b837f20447b364620
cargo run --release
Code Changes
Modified src/render.rsGitHub
@@ -166,81 +166,33 @@166166 gl::GenTextures(1, &mut skybox_texture_id);
167167 gl::BindTexture(gl::TEXTURE_CUBE_MAP, skybox_texture_id);
168168
169- let image_data: Vec<u8> = vec![100, 100, 100, 200, 200, 200, 50, 50, 50, 210, 210, 100];
169+ const CUBEMAP_LENGTH: usize = 512;
170+ const NUMBER_OF_STARS: u32 = 200;
170171
171- gl::TexImage2D(
172- gl::TEXTURE_CUBE_MAP_POSITIVE_Z,
173- 0,
174- gl::RGB8 as GLint,
175- 2,
176- 2,
177- 0,
178- gl::RGB,
179- gl::UNSIGNED_BYTE,
180- image_data.as_ptr() as *const c_void,
181- );
182- gl::TexImage2D(
183- gl::TEXTURE_CUBE_MAP_NEGATIVE_Z,
184- 0,
185- gl::RGB8 as GLint,
186- 2,
187- 2,
188- 0,
189- gl::RGB,
190- gl::UNSIGNED_BYTE,
191- image_data.as_ptr() as *const c_void,
192- );
193- gl::TexImage2D(
194- gl::TEXTURE_CUBE_MAP_POSITIVE_Y,
195- 0,
196- gl::RGB8 as GLint,
197- 2,
198- 2,
199- 0,
200- gl::RGB,
201- gl::UNSIGNED_BYTE,
202- image_data.as_ptr() as *const c_void,
203- );
204- gl::TexImage2D(
205- gl::TEXTURE_CUBE_MAP_NEGATIVE_Y,
206- 0,
207- gl::RGB8 as GLint,
208- 2,
209- 2,
210- 0,
211- gl::RGB,
212- gl::UNSIGNED_BYTE,
213- image_data.as_ptr() as *const c_void,
214- );
215- gl::TexImage2D(
216- gl::TEXTURE_CUBE_MAP_POSITIVE_X,
217- 0,
218- gl::RGB8 as GLint,
219- 2,
220- 2,
221- 0,
222- gl::RGB,
223- gl::UNSIGNED_BYTE,
224- image_data.as_ptr() as *const c_void,
225- );
226- gl::TexImage2D(
227- gl::TEXTURE_CUBE_MAP_NEGATIVE_X,
228- 0,
229- gl::RGB8 as GLint,
230- 2,
231- 2,
232- 0,
233- gl::RGB,
234- gl::UNSIGNED_BYTE,
235- image_data.as_ptr() as *const c_void,
236- );
172+ let image_data =
173+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 2);
174+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_POSITIVE_X, CUBEMAP_LENGTH, &image_data);
175+
176+ let image_data =
177+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 3);
178+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_POSITIVE_Y, CUBEMAP_LENGTH, &image_data);
179+
180+ let image_data =
181+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 4);
182+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_POSITIVE_Z, CUBEMAP_LENGTH, &image_data);
183+
184+ let image_data =
185+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 5);
186+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_NEGATIVE_X, CUBEMAP_LENGTH, &image_data);
187+
188+ let image_data =
189+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 6);
190+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_NEGATIVE_Y, CUBEMAP_LENGTH, &image_data);
191+
192+ let image_data =
193+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 7);
194+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_NEGATIVE_Z, CUBEMAP_LENGTH, &image_data);
237195
238- // gl::TexParameteri(gl::TEXTURE_CUBE_MAP, gl::TEXTURE_MAG_FILTER, gl::NEAREST as GLint);
239- // gl::TexParameteri(
240- // gl::TEXTURE_CUBE_MAP,
241- // gl::TEXTURE_MIN_FILTER,
242- // gl::NEAREST_MIPMAP_NEAREST as GLint,
243- // );
244196 gl::TexParameteri(
245197 gl::TEXTURE_CUBE_MAP,
246198 gl::TEXTURE_MIN_FILTER,
@@ -275,6 +227,43 @@275227 }
276228 }
277229
230+ fn upload_texture(target: GLenum, length: usize, image_data: &[u8]) {
231+ unsafe {
232+ gl::TexImage2D(
233+ target,
234+ 0,
235+ gl::RGB8 as GLint,
236+ length as i32,
237+ length as i32,
238+ 0,
239+ gl::RGB,
240+ gl::UNSIGNED_BYTE,
241+ image_data.as_ptr() as *const c_void,
242+ );
243+ }
244+ }
245+
246+ fn generate_image_with_random_stars(
247+ image_length: usize,
248+ number_of_stars: u32,
249+ seed: u32,
250+ ) -> Vec<u8> {
251+ let image_size = image_length * image_length;
252+ let mut grayscale_pixels = vec![0; image_size];
253+ let mut rng = RandomNumberGenerator::with_seed(seed);
254+
255+ for _ in 0..number_of_stars {
256+ let x = rng.gen_range(0, image_length as u32) as usize;
257+ let y = rng.gen_range(0, image_length as u32) as usize;
258+ let intensity = rng.gen_range(10, 256) as u8;
259+
260+ let index = y * image_length + x;
261+ grayscale_pixels[index] = intensity;
262+ }
263+
264+ grayscale_pixels.iter().flat_map(|p| [*p, *p, *p]).collect()
265+ }
266+
278267 pub(crate) fn window_id(&self) -> WindowId {
279268 self.window.id()
280269 }
@@ -166,81 +166,33 @@166 gl::GenTextures(1, &mut skybox_texture_id);
167 gl::BindTexture(gl::TEXTURE_CUBE_MAP, skybox_texture_id);
168
169- let image_data: Vec<u8> = vec![100, 100, 100, 200, 200, 200, 50, 50, 50, 210, 210, 100];
170
171- gl::TexImage2D(
172- gl::TEXTURE_CUBE_MAP_POSITIVE_Z,
173- 0,
174- gl::RGB8 as GLint,
175- 2,
176- 2,
177- 0,
178- gl::RGB,
179- gl::UNSIGNED_BYTE,
180- image_data.as_ptr() as *const c_void,
181- );
182- gl::TexImage2D(
183- gl::TEXTURE_CUBE_MAP_NEGATIVE_Z,
184- 0,
185- gl::RGB8 as GLint,
186- 2,
187- 2,
188- 0,
189- gl::RGB,
190- gl::UNSIGNED_BYTE,
191- image_data.as_ptr() as *const c_void,
192- );
193- gl::TexImage2D(
194- gl::TEXTURE_CUBE_MAP_POSITIVE_Y,
195- 0,
196- gl::RGB8 as GLint,
197- 2,
198- 2,
199- 0,
200- gl::RGB,
201- gl::UNSIGNED_BYTE,
202- image_data.as_ptr() as *const c_void,
203- );
204- gl::TexImage2D(
205- gl::TEXTURE_CUBE_MAP_NEGATIVE_Y,
206- 0,
207- gl::RGB8 as GLint,
208- 2,
209- 2,
210- 0,
211- gl::RGB,
212- gl::UNSIGNED_BYTE,
213- image_data.as_ptr() as *const c_void,
214- );
215- gl::TexImage2D(
216- gl::TEXTURE_CUBE_MAP_POSITIVE_X,
217- 0,
218- gl::RGB8 as GLint,
219- 2,
220- 2,
221- 0,
222- gl::RGB,
223- gl::UNSIGNED_BYTE,
224- image_data.as_ptr() as *const c_void,
225- );
226- gl::TexImage2D(
227- gl::TEXTURE_CUBE_MAP_NEGATIVE_X,
228- 0,
229- gl::RGB8 as GLint,
230- 2,
231- 2,
232- 0,
233- gl::RGB,
234- gl::UNSIGNED_BYTE,
235- image_data.as_ptr() as *const c_void,
236- );
237
238- // gl::TexParameteri(gl::TEXTURE_CUBE_MAP, gl::TEXTURE_MAG_FILTER, gl::NEAREST as GLint);
239- // gl::TexParameteri(
240- // gl::TEXTURE_CUBE_MAP,
241- // gl::TEXTURE_MIN_FILTER,
242- // gl::NEAREST_MIPMAP_NEAREST as GLint,
243- // );
244 gl::TexParameteri(
245 gl::TEXTURE_CUBE_MAP,
246 gl::TEXTURE_MIN_FILTER,
@@ -275,6 +227,43 @@275 }
276 }
277
278 pub(crate) fn window_id(&self) -> WindowId {
279 self.window.id()
280 }
@@ -166,81 +166,33 @@166 gl::GenTextures(1, &mut skybox_texture_id);
167 gl::BindTexture(gl::TEXTURE_CUBE_MAP, skybox_texture_id);
168
169+ const CUBEMAP_LENGTH: usize = 512;
170+ const NUMBER_OF_STARS: u32 = 200;
171
172+ let image_data =
173+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 2);
174+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_POSITIVE_X, CUBEMAP_LENGTH, &image_data);
175+
176+ let image_data =
177+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 3);
178+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_POSITIVE_Y, CUBEMAP_LENGTH, &image_data);
179+
180+ let image_data =
181+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 4);
182+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_POSITIVE_Z, CUBEMAP_LENGTH, &image_data);
183+
184+ let image_data =
185+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 5);
186+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_NEGATIVE_X, CUBEMAP_LENGTH, &image_data);
187+
188+ let image_data =
189+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 6);
190+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_NEGATIVE_Y, CUBEMAP_LENGTH, &image_data);
191+
192+ let image_data =
193+ Self::generate_image_with_random_stars(CUBEMAP_LENGTH, NUMBER_OF_STARS, 7);
194+ Self::upload_texture(gl::TEXTURE_CUBE_MAP_NEGATIVE_Z, CUBEMAP_LENGTH, &image_data);
195
196 gl::TexParameteri(
197 gl::TEXTURE_CUBE_MAP,
198 gl::TEXTURE_MIN_FILTER,
@@ -275,6 +227,43 @@227 }
228 }
229
230+ fn upload_texture(target: GLenum, length: usize, image_data: &[u8]) {
231+ unsafe {
232+ gl::TexImage2D(
233+ target,
234+ 0,
235+ gl::RGB8 as GLint,
236+ length as i32,
237+ length as i32,
238+ 0,
239+ gl::RGB,
240+ gl::UNSIGNED_BYTE,
241+ image_data.as_ptr() as *const c_void,
242+ );
243+ }
244+ }
245+
246+ fn generate_image_with_random_stars(
247+ image_length: usize,
248+ number_of_stars: u32,
249+ seed: u32,
250+ ) -> Vec<u8> {
251+ let image_size = image_length * image_length;
252+ let mut grayscale_pixels = vec![0; image_size];
253+ let mut rng = RandomNumberGenerator::with_seed(seed);
254+
255+ for _ in 0..number_of_stars {
256+ let x = rng.gen_range(0, image_length as u32) as usize;
257+ let y = rng.gen_range(0, image_length as u32) as usize;
258+ let intensity = rng.gen_range(10, 256) as u8;
259+
260+ let index = y * image_length + x;
261+ grayscale_pixels[index] = intensity;
262+ }
263+
264+ grayscale_pixels.iter().flat_map(|p| [*p, *p, *p]).collect()
265+ }
266+
267 pub(crate) fn window_id(&self) -> WindowId {
268 self.window.id()
269 }