Commit 48878d1c authored by Stephen D's avatar Stephen D
Browse files

Bug fixes, circular galaxies, pngs

parent 74c7f735
galaxysim: main.c qdbmp.c qdbmp.h engine.c engine.h
gcc -o galaxysim main.c qdbmp.c engine.c -lm -fopenmp
gcc -o galaxysim main.c qdbmp.c engine.c -lm -fopenmp -lpng
collide: collide.c
gcc -o collide collide.c
......@@ -20,7 +20,7 @@ void addToTree(struct node_t *tree, RigidMass_t *masses, int n, int rlev)
//printf("n=%d\r\n", n);
if(n == 2)
{
rlev ++; //debuggign only
rlev++;
}
RigidMass_t *masses_topleft;
int n_topleft = 0;
......
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
......@@ -5,9 +6,17 @@
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include "qdbmp.h"
#include <stdint.h>
//#include "qdbmp.h"
#include "engine.h"
typedef struct
{
uint8_t red;
uint8_t green;
uint8_t blue;
} pixel_t;
void reverse(char s[])
{
int i, j;
......@@ -68,8 +77,9 @@ int *starsAtPixel;
void generateStars()
{
int randAddonX = w/2 - spawnSize/2;
int randAddonY = h/2 - spawnSize/2;
int maxRadius = spawnSize / 2;
int randAddonX = w/2;
int randAddonY = h/2;
RigidMass_t new;
new.mass = m;
......@@ -79,27 +89,20 @@ void generateStars()
for(int i = 0; i < nStars; i++)
{
new.x = rand() % spawnSize + randAddonX;
new.y = rand() % spawnSize + randAddonY;
double dx = new.x - w / 2;
double dy = new.y - h / 2;
if(abs(dx) <= abs(dy))
{
new.vx = (double)rand()/(double)(RAND_MAX/maxComponentV);
new.vy = -new.vx * dx / dy;
}
else
{
new.vy = (double)rand()/(double)(RAND_MAX/maxComponentV);
new.vx = -new.vy * dy / dx;
}
if(dx > dy)
{
new.vx *= -1;
new.vy *= -1;
}
//Generate radius between 0 and maxRadius
double radius = (double)rand() / RAND_MAX * maxRadius;
//Generate random x - must be less than r
//Otherwise we end up with sqrt(<0) which makes a big mess
double x = ((double)rand() / RAND_MAX * radius * 2) - radius;
//Use x to make y which satisfies x and r
double yRange = sqrt(radius * radius - x * x);
double y = (double)rand() / RAND_MAX * 2 * yRange - yRange;
new.x = x + randAddonX;
new.y = y + randAddonY;
new.vx = y / maxRadius * maxComponentV; //Scale velocities
new.vy = -x / maxRadius * maxComponentV;
stars[i] = new;
}
......@@ -153,9 +156,10 @@ void hsl_to_rgb(int h, double s, double l, char *rOut, char *gOut, char *bOut)
*bOut = 255 * (b + m);
}
BMP *out;
pixel_t *out;
void addToImage()
{
memset(out, 0, sizeof(pixel_t) * w * h);
memset(starsAtPixel, 0, sizeof(int) * w * h);
#define IMAGE_INC_AMOUNT 50
for(int a = 0; a < nStars; a++)
......@@ -176,13 +180,13 @@ void addToImage()
{
int nStarsAtLoc = starsAtPixel[starX * h + starY] * 10;
//if(nStarsAtLoc > 360) nStarsAtLoc = 360;
char curR = 0, curG = 0, curB = 0;
pixel_t p = {.red = 0, .green = 0, .blue = 0};
if(nStarsAtLoc > 50) nStarsAtLoc = 50;
curR = nStarsAtLoc * 5;
curG = curR;
curB = curR;
p.red = nStarsAtLoc * 5;
p.green = p.red;
p.blue = p.red;
//hsl_to_rgb(nStarsAtLoc, 1, nStarsAtLoc / (double)100, &curR, &curG, &curB);
BMP_SetPixelRGB(out, starX, starY, curR, curG, curB);
out[starX + starY * w] = p;
}
}
}
......@@ -194,13 +198,54 @@ void saveImage(double frame)
strcpy(filename, "img/");
itoa(frame, buffer);
strcat(filename, buffer);
strcat(filename, ".bmp");
BMP_WriteFile(out, filename);
strcat(filename, ".png");
FILE *fp;
fp = fopen(filename, "wb");
if(!fp)
{
printf("ERROR: Could not save image!\r\n");
return;
}
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop info_ptr = png_create_info_struct(png_ptr);
png_set_IHDR (png_ptr,
info_ptr,
w, //image width
h, //height
8, //bit depth
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_byte **row_pointers = png_malloc (png_ptr, h * sizeof (png_byte *));
for(int y = 0; y < h; y++)
{
png_byte *row = png_malloc (png_ptr, sizeof(uint8_t) * w * 3);
row_pointers[y] = row;
for(int x = 0; x < w; x++)
{
pixel_t pixel = out[x + w*y];
*row++ = pixel.red;
*row++ = pixel.green;
*row++ = pixel.blue;
}
}
png_init_io (png_ptr, fp);
png_set_rows (png_ptr, info_ptr, row_pointers);
png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
fclose(fp);
BMP_Free(out);
out = BMP_Create(w, h, 24);
//Free memory
for(int y = 0; y < h; y++)
{
png_free(png_ptr, row_pointers[y]);
}
png_free(png_ptr, row_pointers);
}
void saveState(int it)
......@@ -359,7 +404,9 @@ int main(int argc, char **argv)
printf("%d stars loaded.\r\n", nStars);
printf("Continuing from i=%d\r\n", iteration);
}
out = BMP_Create(w, h, 24);
//malloc some space for our output image
out = malloc(w * h * sizeof(pixel_t));
//Runs until CTRL+C
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment