Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
4TT1L4 committed Nov 9, 2019
1 parent d6d7165 commit ec34dc6
Show file tree
Hide file tree
Showing 16 changed files with 935 additions and 0 deletions.
Binary file added doc/GPGPU-GameOfLife-VisualStudioSolution.zip
Binary file not shown.
Binary file added doc/GameOfLife-Version1.0.zip
Binary file not shown.
34 changes: 34 additions & 0 deletions src/PutLifeShader.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#version 130

uniform sampler2D inputMap;
uniform ivec2 Coord;
uniform int button;

out vec4 outColor;

in vec2 fTexCoord;

void main(void){
ivec2 pos = ivec2(gl_FragCoord[0],gl_FragCoord[1]);
vec4 data = texelFetch(inputMap, pos, 0);
ivec2 MCoord = ivec2(Coord.x, Coord.y);

if(pos == MCoord && button == 0)
{
data[0] = 1;
data[1] = 1;
data[2] = 1;
data[3] = 1;
}
if(pos == MCoord && button == 2)
{
data[0] = 0;
data[1] = 0;
data[2] = 0;
data[3] = 0;
}
else

outColor = data;
}

83 changes: 83 additions & 0 deletions src/framebuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "framebuffer.hpp"

#include <string>
#include <iostream>

Framebuffer::Framebuffer(GLuint width, GLuint height, GLuint planes){
this->width = width;
this->height = height;
this->planes = planes;

buffers = new GLenum[planes];

glGenFramebuffers(1, &handle);
colorBuffer = new GLuint[planes];
glGenTextures(planes, colorBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, handle);

for(unsigned int i = 0; i < planes; ++i){
glBindTexture(GL_TEXTURE_2D, colorBuffer[i]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, colorBuffer[i], 0);
buffers[i] = GL_COLOR_ATTACHMENT0 + i;
if(glGetError() != GL_NO_ERROR){
std::cout << "Framebuffer: Error creating color attachment" << std::endl;
}
}

glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
if(glGetError() != GL_NO_ERROR){
std::cout << "Framebuffer: Error creating depth attachment" << std::endl;
}

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE){
std::cout << "Framebuffer: Incomplete framebuffer (";
switch(status){
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
std::cout << "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT";
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
std::cout << "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";
break;
case GL_FRAMEBUFFER_UNSUPPORTED:
std::cout << "GL_FRAMEBUFFER_UNSUPPORTED";
break;
}
std::cout << ")" << std::endl;
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

Framebuffer::~Framebuffer(){
glDeleteFramebuffers(1, &handle);
glDeleteRenderbuffers(1, &depthBuffer);
}

void Framebuffer::setRenderTarget(){
glBindFramebuffer(GL_FRAMEBUFFER, handle);
glDrawBuffers(planes, buffers);
glViewport(0,0, width, height);
}

void Framebuffer::disableRenderTarget(){
GLenum tmpBuff[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, tmpBuff);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void Framebuffer::clear(){
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
setRenderTarget();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
disableRenderTarget();
}
40 changes: 40 additions & 0 deletions src/framebuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef _FRAMEBUFFER_
#define _FRAMEBUFFER_

#include <GL/glew.h>

class Framebuffer{
private:
GLuint handle;
GLuint* colorBuffer;
GLuint depthBuffer;

GLuint width, height;
GLuint planes;

GLenum* buffers;

public:
Framebuffer(GLuint width, GLuint height, GLuint planes);
~Framebuffer();

void setRenderTarget();
void disableRenderTarget();

GLuint getColorBuffer(unsigned int plane){
return colorBuffer[plane];
}

void clear();

int getWidth(){
return width;
}

int getHeight(){
return height;
}
};

#endif

19 changes: 19 additions & 0 deletions src/init.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 130


uniform int type;

in vec2 fTexCoord;

out vec4 outColor;

void main(void){
if(type == 0)
{
outColor = vec4((tan(fTexCoord.x*25) * sin(fTexCoord.y*3) + sin(fTexCoord.y*12)),0, 0 ,1 );
}
else
{
outColor = vec4(0,0, 0 ,1 );
}
}
72 changes: 72 additions & 0 deletions src/life.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#version 130

uniform sampler2D inputMap;

out vec4 outColor;

in vec2 fTexCoord;

void main(void){
// Is current cell alive?
vec4 data = texelFetch(inputMap, ivec2(gl_FragCoord), 0);
float IsAlive = data[0];

// Count Neighbours

float NumberOfNeighbours = round(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2(-1, 0), 0).x) +
round(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2( 1, 0), 0).x) +
round(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2( 0, -1), 0).x) +
round(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2( 0, 1), 0).x) +
round(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2(-1, 1), 0).x) +
round(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2( 1, 1), 0).x) +
round(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2( 1, -1), 0).x) +
round(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2(-1, -1), 0).x);

/*
float NumberOfNeighbours = floor(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2(-1, 0), 0).x) +
floor(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2( 1, 0), 0).x) +
floor(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2( 0, -1), 0).x) +
floor(texelFetch(inputMap, ivec2(gl_FragCoord) + ivec2( 0, 1), 0).x);
*/
if(IsAlive > 0.1)
{
//Any live cell with fewer than two live neighbours dies, as if caused by under-population.
if(NumberOfNeighbours<1.5)
{
data[0] = 0;
data[1] = 0.7;
data[2] = 0.2;
}
else
//Any live cell with two or three live neighbours lives on to the next generation.
if(NumberOfNeighbours>1.9 && NumberOfNeighbours<3.1)
{
data[0] = 1;
data[1] = 0.3;
data[2] = 0.3;
}
else
//Any live cell with more than three live neighbours dies, as if by overcrowding.
if(NumberOfNeighbours>3.1)
{
data[0] = 0;
data[1] = 0;
data[2] = 1;
}
}
else
{
//Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
if(NumberOfNeighbours<3.1 && NumberOfNeighbours > 2.9)
{
data[0] = 1;
data[1] = 1;
data[2] = 1;
}
else
{
data = data * 0.95;
}
}
outColor = vec4(data[0], data[1], data[2], data[3]);
}
Loading

0 comments on commit ec34dc6

Please sign in to comment.