Commit 3813ed6e authored by minus's avatar minus
Browse files

added particles

parent 097519f8
......@@ -9,6 +9,7 @@ import math
#import glu
import strutils
import times
import random
import shader
......@@ -67,6 +68,7 @@ var
uniforms_model = shader_program.glGetUniformLocation("model")
uniforms_view = shader_program.glGetUniformLocation("view")
uniforms_proj = shader_program.glGetUniformLocation("proj")
uniforms_alpha = shader_program.glGetUniformLocation("alpha")
var view = lookAt(
vec3[GLfloat](0.0, 0.0, 2.0), # pos
......@@ -83,6 +85,8 @@ proc reshape(newWidth: cint, newHeight: cint) =
var proj = perspective[GLfloat](radians 45.0, newWidth / newHeight, 0.01, 100.0)
uniforms_proj.glUniformMatrix4fv(GLsizei(1), false, proj.caddr)
### QUAD
var quad_vao: GLuint
proc init_quad =
glGenVertexArrays(1, addr quad_vao)
......@@ -118,15 +122,59 @@ proc init_quad =
glBindVertexArray(0)
init_quad()
proc render(t: float) =
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) # Clear color and depth buffers
proc render_quad(t: float) =
quad_vao.glBindVertexArray()
var model = mat4x4[GLfloat](1.0)
model = model.rotate(vec3[GLfloat](0.0, 1.0, 0.0), radians(t * 36))
uniforms_model.glUniformMatrix4fv(GLsizei(1), false, model.caddr)
GL_TRIANGLE_STRIP.glDrawElements(GLsizei(4), GL_UNSIGNED_INT, cast[pointer](0))
### PARTICLES
type
Particle = object
position: Vec2[float]
velocity: Vec2[float]
remaining_life: float
proc `+=`[T](a: var Vec2[T], b: Vec2[T]) =
a.x = a.x + b.x
a.y = a.y + b.y
var particles: array[50, Particle]
var process_particles_t_prev = 0.0
proc process_particles(t: float) =
var dt = t - process_particles_t_prev
process_particles_t_prev = t
for p in particles.mitems:
if p.remaining_life <= 0:
p.remaining_life = random(2.0)
p.position = vec2(0.0, 0.0)
p.velocity = vec2(random(1.0)-0.5, random(1.0)-0.5)
p.velocity *= 0.97
p.velocity.y = p.velocity.y - 0.02
p.position += p.velocity
p.remaining_life -= dt
proc render_particles(t: float) =
process_particles(t)
quad_vao.glBindVertexArray()
for p in particles:
var model = mat4x4[GLfloat](1.0)
model = model.scale(vec3[GLfloat](0.1))
model = model.translate(vec3[GLfloat](p.position.x, p.position.y, 0.0))
#uniforms_alpha.glUniform1f(p.velocity.length)
uniforms_model.glUniformMatrix4fv(GLsizei(1), false, model.caddr)
GL_TRIANGLE_STRIP.glDrawElements(GLsizei(4), GL_UNSIGNED_INT, cast[pointer](0))
proc render(t: float) =
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) # Clear color and depth buffers
#render_quad(t)
render_particles(t)
window.glSwapWindow() # Swap the front and back frame buffers (double buffering)
......
#version 150
in vec3 color;
in vec4 color;
in vec3 pos;
out vec4 out_color;
void main()
{
out_color = vec4(color, 1.0);
//out_color.rgb = vec3(pos.z/8 + 0.5);
out_color = color;
}
......@@ -3,16 +3,17 @@
in vec3 in_position;
in vec3 in_color;
in vec3 in_normal;
out vec3 color;
out vec4 color;
out vec3 pos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;
uniform float alpha;
void main()
{
color = in_color;
color = vec4(in_color, alpha);
pos = in_position;
gl_Position = proj * view * model * vec4(in_position, 1.0);
}
......@@ -11,3 +11,4 @@ requires "nim >= 0.14.0"
requires "sdl2 >= 1.1.0"
requires "opengl >= 1.0.1"
requires "glm >= 0.1.1"
requires "random >= 0.5.3"
Supports Markdown
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