racing_game/clouds.cpp

130 lines
2.9 KiB
C++
Raw Permalink Normal View History

2019-05-25 14:24:49 +10:00
#include <GL/glut.h>
#include <math.h>
#include <iostream>
#include "PerlinNoise.hpp"
#include "textures.h"
2019-05-25 14:42:53 +10:00
#include "libs/mainloop-api/mainloop.h"
2019-05-25 14:24:49 +10:00
#include "random.h"
#include "math.h"
int cloud_tex;
char* cloud_data;
int cloud_dimentions[2] = {100, 100};
double cloud_fade_distance = 25;
double cloud_frequency = 32;
double cloud_time = 0;
double frequency = 10;
const siv::PerlinNoise perlin;
void clouds_init()
{
// Setup the cloud data
cloud_data = new char[cloud_dimentions[0]*cloud_dimentions[1]*4];
// Fill the clouds with data
for(int x=0;x<cloud_dimentions[0];x++) {
for(int y=0;y<cloud_dimentions[1];y++)
{
// Get the id
int id = (x*cloud_dimentions[1]+y)*4;
// Set some noise
cloud_data[id+0] = 255;
cloud_data[id+1] = 255;
cloud_data[id+2] = 255;
}
}
}
void clouds_move(int *args)
{
// Make this happen again
mainloopRegAction(clouds_move, 10, 0);
// Add to the time
cloud_time += 1e-3;
}
void clouds_render()
{
// Fill the clouds with data
for(int x=0;x<cloud_dimentions[0];x++) {
for(int y=0;y<cloud_dimentions[1];y++)
{
// Get the id
int id = (x*cloud_dimentions[1]+y)*4;
// Get the distance between pixels
double distance = sqrt(
squarei(x - cloud_dimentions[0]/2) +
squarei(y - cloud_dimentions[1]/2)
);
// Is the distance less than 50
if(distance < cloud_dimentions[0]/2.0)
{
// Make some noise
double n = perlin.octaveNoise0_1(
x/cloud_frequency,
y/cloud_frequency,
cloud_time, 10);
char noise;
if(n < 0) noise = 0;
else if(n > 1) noise = 255;
else noise = n*255;
// Is the distance greater than 10
if(distance > cloud_dimentions[0]/2.0-cloud_fade_distance)
{
// Set distance opacity
cloud_data[id+3] = 255-
(distance-(cloud_dimentions[0]/2.0-cloud_fade_distance))/cloud_fade_distance*255;
}
else
{
// Set normal opacity
cloud_data[id+3] = 255;
}
// Set some noise
cloud_data[id+0] = noise;
cloud_data[id+1] = noise;
cloud_data[id+2] = noise;
}
else
{
// Set the opacity to zero
cloud_data[id+3] = 0;
}
}
}
// Load the clouds texture as linear
cloud_tex = loadPixels(cloud_data, cloud_dimentions[0], cloud_dimentions[1], 'L', false);
// Start rendering the clouds
glBegin(GL_QUADS);
// Draw the clouds above the player
glTexCoord2i(0,0); glVertex3d(-10, 1,-10);
glTexCoord2i(1,0); glVertex3d( 10, 1,-10);
glTexCoord2i(1,1); glVertex3d( 10, 1, 10);
glTexCoord2i(0,1); glVertex3d(-10, 1, 10);
// Stop rendering the clouds
glEnd();
// Unbind the texture
glBindTexture(GL_TEXTURE_2D, cloud_tex);
// Clear the depth
glClear(GL_DEPTH_BUFFER_BIT);
}