Skip to content

Commit f202678

Browse files
author
RealBadAngel
committedApr 21, 2014
Textureable sun and moon.
1 parent da3c5c7 commit f202678

File tree

2 files changed

+192
-104
lines changed

2 files changed

+192
-104
lines changed
 

‎src/sky.cpp

+187-103
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id, LocalPlay
5151
m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
5252
//m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
5353

54+
m_sun_texture = mgr->getVideoDriver()->getTexture(
55+
getTexturePath("sun.png").c_str());
56+
m_moon_texture = mgr->getVideoDriver()->getTexture(
57+
getTexturePath("moon.png").c_str());
58+
m_sun_tonemap = mgr->getVideoDriver()->getTexture(
59+
getTexturePath("sun_tonemap.png").c_str());
60+
m_moon_tonemap = mgr->getVideoDriver()->getTexture(
61+
getTexturePath("moon_tonemap.png").c_str());
62+
63+
if (m_sun_texture){
64+
m_materials[3] = mat;
65+
m_materials[3].setTexture(0, m_sun_texture);
66+
m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
67+
if (m_sun_tonemap)
68+
m_materials[3].Lighting = true;
69+
}
70+
if (m_moon_texture){
71+
m_materials[4] = mat;
72+
m_materials[4].setTexture(0, m_moon_texture);
73+
m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
74+
if (m_moon_tonemap)
75+
m_materials[4].Lighting = true;
76+
}
77+
5478
for(u32 i=0; i<SKY_STAR_COUNT; i++){
5579
m_stars[i] = v3f(
5680
myrand_range(-10000,10000),
@@ -135,6 +159,24 @@ void Sky::render()
135159
video::SColor mooncolor = mooncolor_f.toSColor();
136160
video::SColor mooncolor2 = mooncolor2_f.toSColor();
137161

162+
// Calculate offset normalized to the X dimension of a 512x1 px tonemap
163+
float offset=(1.0-fabs(sin((m_time_of_day - 0.5)*irr::core::PI)))*511;
164+
165+
if (m_sun_tonemap){
166+
u8 * texels = (u8 *)m_sun_tonemap->lock();
167+
video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
168+
video::SColor texel_color (255,texel->getRed(),texel->getGreen(), texel->getBlue());
169+
m_sun_tonemap->unlock();
170+
m_materials[3].EmissiveColor = texel_color;
171+
}
172+
if (m_moon_tonemap){
173+
u8 * texels = (u8 *)m_moon_tonemap->lock();
174+
video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
175+
video::SColor texel_color (255,texel->getRed(),texel->getGreen(), texel->getBlue());
176+
m_moon_tonemap->unlock();
177+
m_materials[4].EmissiveColor = texel_color;
178+
}
179+
138180
const f32 t = 1.0f;
139181
const f32 o = 0.0f;
140182
static const u16 indices[4] = {0,1,2,3};
@@ -219,119 +261,161 @@ void Sky::render()
219261
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
220262
}
221263

222-
driver->setMaterial(m_materials[1]);
223-
224264
// Draw sun
225-
if(wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85)
226-
{
227-
float d = sunsize * 1.7;
228-
video::SColor c = suncolor;
229-
c.setAlpha(0.05*255);
230-
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
231-
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
232-
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
233-
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
234-
for(u32 i=0; i<4; i++){
235-
// Switch from -Z (south) to +X (east)
236-
vertices[i].Pos.rotateXZBy(90);
237-
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
238-
}
239-
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
240-
241-
d = sunsize * 1.2;
242-
c = suncolor;
243-
c.setAlpha(0.15*255);
244-
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
245-
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
246-
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
247-
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
248-
for(u32 i=0; i<4; i++){
249-
// Switch from -Z (south) to +X (east)
250-
vertices[i].Pos.rotateXZBy(90);
251-
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
252-
}
253-
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
254-
255-
d = sunsize;
256-
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, suncolor, t, t);
257-
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, suncolor, o, t);
258-
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, suncolor, o, o);
259-
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, suncolor, t, o);
260-
for(u32 i=0; i<4; i++){
261-
// Switch from -Z (south) to +X (east)
262-
vertices[i].Pos.rotateXZBy(90);
263-
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
264-
}
265-
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
266-
267-
d = sunsize * 0.7;
268-
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, suncolor2, t, t);
269-
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, suncolor2, o, t);
270-
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, suncolor2, o, o);
271-
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, suncolor2, t, o);
272-
for(u32 i=0; i<4; i++){
273-
// Switch from -Z (south) to +X (east)
274-
vertices[i].Pos.rotateXZBy(90);
275-
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
265+
if(wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85){
266+
if (!m_sun_texture){
267+
driver->setMaterial(m_materials[1]);
268+
float d = sunsize * 1.7;
269+
video::SColor c = suncolor;
270+
c.setAlpha(0.05*255);
271+
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
272+
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
273+
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
274+
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
275+
for(u32 i=0; i<4; i++){
276+
// Switch from -Z (south) to +X (east)
277+
vertices[i].Pos.rotateXZBy(90);
278+
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
279+
}
280+
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
281+
282+
d = sunsize * 1.2;
283+
c = suncolor;
284+
c.setAlpha(0.15*255);
285+
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
286+
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
287+
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
288+
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
289+
for(u32 i=0; i<4; i++){
290+
// Switch from -Z (south) to +X (east)
291+
vertices[i].Pos.rotateXZBy(90);
292+
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
293+
}
294+
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
295+
296+
d = sunsize;
297+
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, suncolor, t, t);
298+
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, suncolor, o, t);
299+
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, suncolor, o, o);
300+
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, suncolor, t, o);
301+
for(u32 i=0; i<4; i++){
302+
// Switch from -Z (south) to +X (east)
303+
vertices[i].Pos.rotateXZBy(90);
304+
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
305+
}
306+
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
307+
308+
d = sunsize * 0.7;
309+
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, suncolor2, t, t);
310+
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, suncolor2, o, t);
311+
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, suncolor2, o, o);
312+
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, suncolor2, t, o);
313+
for(u32 i=0; i<4; i++){
314+
// Switch from -Z (south) to +X (east)
315+
vertices[i].Pos.rotateXZBy(90);
316+
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
317+
}
318+
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
319+
} else {
320+
driver->setMaterial(m_materials[3]);
321+
float d = sunsize * 1.7;
322+
video::SColor c;
323+
if (m_sun_tonemap)
324+
c = video::SColor (0,0,0,0);
325+
else
326+
c = video::SColor (255,255,255,255);
327+
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
328+
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
329+
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
330+
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
331+
for(u32 i=0; i<4; i++){
332+
// Switch from -Z (south) to +X (east)
333+
vertices[i].Pos.rotateXZBy(90);
334+
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
335+
}
336+
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
276337
}
277-
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
278338
}
339+
279340
// Draw moon
280341
if(wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7)
281342
{
282-
float d = moonsize * 1.9;
283-
video::SColor c = mooncolor;
284-
c.setAlpha(0.05*255);
285-
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
286-
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
287-
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
288-
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
289-
for(u32 i=0; i<4; i++){
290-
// Switch from -Z (south) to -X (west)
291-
vertices[i].Pos.rotateXZBy(-90);
292-
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
293-
}
294-
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
295-
296-
d = moonsize * 1.3;
297-
c = mooncolor;
298-
c.setAlpha(0.15*255);
299-
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
300-
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
301-
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
302-
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
303-
for(u32 i=0; i<4; i++){
304-
// Switch from -Z (south) to -X (west)
305-
vertices[i].Pos.rotateXZBy(-90);
306-
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
307-
}
308-
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
309-
310-
d = moonsize;
311-
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, mooncolor, t, t);
312-
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, mooncolor, o, t);
313-
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, mooncolor, o, o);
314-
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, mooncolor, t, o);
315-
for(u32 i=0; i<4; i++){
316-
// Switch from -Z (south) to -X (west)
317-
vertices[i].Pos.rotateXZBy(-90);
318-
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
319-
}
320-
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
343+
if (!m_moon_texture){
344+
driver->setMaterial(m_materials[1]);
345+
float d = moonsize * 1.9;
346+
video::SColor c = mooncolor;
347+
c.setAlpha(0.05*255);
348+
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
349+
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
350+
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
351+
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
352+
for(u32 i=0; i<4; i++){
353+
// Switch from -Z (south) to -X (west)
354+
vertices[i].Pos.rotateXZBy(-90);
355+
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
356+
}
357+
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
321358

322-
float d2 = moonsize * 0.6;
323-
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, mooncolor2, t, t);
324-
vertices[1] = video::S3DVertex( d2,-d,-1, 0,0,1, mooncolor2, o, t);
325-
vertices[2] = video::S3DVertex( d2, d2,-1, 0,0,1, mooncolor2, o, o);
326-
vertices[3] = video::S3DVertex(-d, d2,-1, 0,0,1, mooncolor2, t, o);
327-
for(u32 i=0; i<4; i++){
328-
// Switch from -Z (south) to -X (west)
329-
vertices[i].Pos.rotateXZBy(-90);
330-
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
359+
d = moonsize * 1.3;
360+
c = mooncolor;
361+
c.setAlpha(0.15*255);
362+
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
363+
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
364+
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
365+
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
366+
for(u32 i=0; i<4; i++){
367+
// Switch from -Z (south) to -X (west)
368+
vertices[i].Pos.rotateXZBy(-90);
369+
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
370+
}
371+
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
372+
373+
d = moonsize;
374+
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, mooncolor, t, t);
375+
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, mooncolor, o, t);
376+
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, mooncolor, o, o);
377+
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, mooncolor, t, o);
378+
for(u32 i=0; i<4; i++){
379+
// Switch from -Z (south) to -X (west)
380+
vertices[i].Pos.rotateXZBy(-90);
381+
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
382+
}
383+
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
384+
385+
float d2 = moonsize * 0.6;
386+
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, mooncolor2, t, t);
387+
vertices[1] = video::S3DVertex( d2,-d,-1, 0,0,1, mooncolor2, o, t);
388+
vertices[2] = video::S3DVertex( d2, d2,-1, 0,0,1, mooncolor2, o, o);
389+
vertices[3] = video::S3DVertex(-d, d2,-1, 0,0,1, mooncolor2, t, o);
390+
for(u32 i=0; i<4; i++){
391+
// Switch from -Z (south) to -X (west)
392+
vertices[i].Pos.rotateXZBy(-90);
393+
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
394+
}
395+
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
396+
} else {
397+
driver->setMaterial(m_materials[4]);
398+
float d = moonsize * 1.9;
399+
video::SColor c;
400+
if (m_moon_tonemap)
401+
c = video::SColor (0,0,0,0);
402+
else
403+
c = video::SColor (255,255,255,255);
404+
vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
405+
vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
406+
vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
407+
vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
408+
for(u32 i=0; i<4; i++){
409+
// Switch from -Z (south) to -X (west)
410+
vertices[i].Pos.rotateXZBy(-90);
411+
vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
412+
}
413+
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
331414
}
332-
driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
333415
}
416+
334417
// Stars
418+
driver->setMaterial(m_materials[1]);
335419
do{
336420
float starbrightness = MYMAX(0, MYMIN(1,
337421
(0.285 - fabs(wicked_time_of_day < 0.5 ?

‎src/sky.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424
#ifndef SKY_HEADER
2525
#define SKY_HEADER
2626

27-
#define SKY_MATERIAL_COUNT 3
27+
#define SKY_MATERIAL_COUNT 5
2828
#define SKY_STAR_COUNT 200
2929

3030
// Skybox, rendered with zbuffer turned off, before all other nodes.
@@ -127,6 +127,10 @@ class Sky : public scene::ISceneNode
127127
v3f m_stars[SKY_STAR_COUNT];
128128
video::S3DVertex m_star_vertices[SKY_STAR_COUNT*4];
129129
LocalPlayer* m_player;
130+
video::ITexture* m_sun_texture;
131+
video::ITexture* m_moon_texture;
132+
video::ITexture* m_sun_tonemap;
133+
video::ITexture* m_moon_tonemap;
130134
};
131135

132136
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.