Mentions légales du service

Skip to content
Snippets Groups Projects
Commit c798d124 authored by LY Mickael's avatar LY Mickael :dog:
Browse files

Dynamic viewer

parent 3b7a30a9
Branches
No related tags found
No related merge requests found
from dynamics.abstract_dynamic_system import *
from dynamics.dummy_dynamic_system import *
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
from abc import ABCMeta, abstractmethod
## Abstract class defining a time-stepper
class AbstractDynamicSystem(metaclass=ABCMeta):
def __init__(self):
## Constructor
# @param self
pass
@abstractmethod
def step(self):
## Step of the dynamic system
# @param self
pass
def __del__(self):
pass
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import numpy as np
from .abstract_dynamic_system import AbstractDynamicSystem
## Dummy dynamic system just to test
class DummyDynamicSystem(AbstractDynamicSystem):
def __init__(self, mesh):
## Constructor
# @param self
# @param mesh Must have methods getPositions & setPositions
# getColours & setColours
super().__init__()
self.mesh = mesh
# Animations parameters
self.it = 60.
self.delta = 1.
self.period = 120.
self.colours = np.copy(self.mesh.getColours())
self.translationVector = np.tile([0.01, 0], self.mesh.nbVertices)
def step(self):
self.mesh.setColours((self.it / self.period) * self.colours)
self.mesh.setPositions(self.mesh.getPositions() \
+ self.delta * self.translationVector)
self.it += self.delta
if (self.it == 0) or (self.it == self.period):
self.delta *= -1.
from graphics.viewer import *
from graphics.abstract_renderable import *
from graphics.mesh2D_renderable import *
from graphics.camera import *
from graphics.shader import *
......@@ -5,7 +5,7 @@ from abc import ABCMeta, abstractmethod
import numpy as np
## Class defining an object to render
## Abstract class defining an object to render
class AbstractRenderable(metaclass=ABCMeta):
def __init__(self):
......@@ -18,6 +18,7 @@ class AbstractRenderable(metaclass=ABCMeta):
# Buffers and data in a dict
self.data = {}
self.buffers = {}
self.locations = {}
@abstractmethod
def draw(self, modelMatrix, viewMatrix, projectionMatrix,
......@@ -26,6 +27,7 @@ class AbstractRenderable(metaclass=ABCMeta):
# @param self
pass
def __del__(self):
## Desctructor
# Release the buffers
......
......@@ -32,18 +32,14 @@ class Mesh2DRenderable(AbstractRenderable):
# Positions
# (doubles needed for the simulations)
# Data storing
positionLocation = 0 # <--
self.locations["positions"] = 0 # <--
self.data["positions"] = np.array(positions, np.float64)
self.buffers["positions"] = GL.glGenBuffers(1)
self.nbVertices = int(positions.size / 2)
# Send data
self.updatePositionsBuffer()
# Drawing instructions
positions = np.array(self.data["positions"], np.float64, copy=False)
positionId = self.buffers["positions"]
GL.glEnableVertexAttribArray(positionLocation)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, positionId)
GL.glBufferData(GL.GL_ARRAY_BUFFER, positions,
GL.GL_STATIC_DRAW)
GL.glVertexAttribPointer(positionLocation, 2,
GL.glVertexAttribPointer(self.locations["positions"], 2,
GL.GL_DOUBLE, False, 0, None)
# Colours
......@@ -54,16 +50,13 @@ class Mesh2DRenderable(AbstractRenderable):
if (colours.size != (3 * self.nbVertices)):
raise Exception("Mesh2DRenderable - wrong buffer size")
# Data
colourLocation = 1 # <--
self.locations["colours"] = 1 # <--
self.data["colours"] = np.array(colours, np.float32)
self.buffers["colours"] = GL.glGenBuffers(1)
# Send data
self.updateColourBuffer()
# Drawing instructions
colours = np.array(self.data["colours"], np.float32, copy=False)
colourId = self.buffers["colours"]
GL.glEnableVertexAttribArray(colourLocation)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, colourId)
GL.glBufferData(GL.GL_ARRAY_BUFFER, colours, GL.GL_STATIC_DRAW)
GL.glVertexAttribPointer(colourLocation, 3,
GL.glVertexAttribPointer(self.locations["colours"], 3,
GL.GL_FLOAT, False, 0, None)
# Indexed drawing or not ?
......@@ -90,6 +83,74 @@ class Mesh2DRenderable(AbstractRenderable):
GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0)
def updatePositionsBuffer(self):
## Update the GPU colour buffer
# @param self
positionLocation = self.locations["positions"]
positions = np.array(self.data["positions"], np.float64, copy=False)
positionId = self.buffers["positions"]
GL.glEnableVertexAttribArray(positionLocation)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, positionId)
GL.glBufferData(GL.GL_ARRAY_BUFFER, positions,
GL.GL_STATIC_DRAW)
def updateColourBuffer(self):
## Update the GPU colour buffer
# @param self
colourLocation = self.locations["colours"]
colours = np.array(self.data["colours"], np.float32, copy=False)
colourId = self.buffers["colours"]
GL.glEnableVertexAttribArray(colourLocation)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, colourId)
GL.glBufferData(GL.GL_ARRAY_BUFFER, colours, GL.GL_STATIC_DRAW)
def getNbVertices(self):
## Getter on the number of vertices
return self.nbVertices
def getPositions(self):
## Getter on the positions
# @param self
# @return The positions
return self.data["positions"]
def setPositions(self, newPos):
## Setter on the positions & update the buffer
# @param self
# @param newPos
self.data["positions"] = newPos
self.updatePositionsBuffer()
def getColours(self):
## Getter on the colours
# @param self
# @return The colours
return self.data["colours"]
def getColors(self):
## Getter on the colours (US)
# @param self
# @return The colours
return self.getColours()
def setColours(self, newCol):
## Setter on the colours & update the buffer
# @param self
# @param newCol
self.data["colours"] = newCol
self.updateColourBuffer()
def setColors(self, newCol):
## Setter on the colours (US) & update the buffer
# @param self
# @param newCol
self.setColours(newCol)
def draw(self, modelMatrix, viewMatrix, projectionMatrix,
shaderProgram, primitive = GL.GL_TRIANGLES):
## Drawing function
......@@ -100,13 +161,14 @@ class Mesh2DRenderable(AbstractRenderable):
# @param shaderProgram
# @param primitive
# Send uniforms
names = ["modelMatrix",
"viewMatrix",
"projectionMatrix"]
locations = {n: GL.glGetUniformLocation(shaderProgram.glId, n)
for n in names}
GL.glUseProgram(shaderProgram.glId)
GL.glUseProgram(shaderProgram.glId)
GL.glUniformMatrix4fv(locations["modelMatrix"], 1, True, modelMatrix)
......
......@@ -4,24 +4,32 @@
import glfw
import numpy as np
from graphics import *
from scenes import *
from viewer import Viewer
if __name__ == '__main__':
# Initialization
glfw.init()
# Separates the main to make sure all objects are deleted
# before glfw.terminate is called
def main():
viewer=Viewer(height=960, width=1280,
bgColor = np.array([0.4, 0.4, 0.4]))
# Loading the scene
#baseTest(viewer)
indexedTest(viewer)
#indexedTest(viewer)
dynamicTest(viewer)
# Main loop
viewer.run()
if __name__ == '__main__':
# Initialization
glfw.init()
main()
# End
glfw.terminate()
......@@ -4,6 +4,7 @@
import numpy as np
from graphics import *
from dynamics import *
......@@ -37,3 +38,25 @@ def indexedTest(viewer):
squareIndexedMesh = Mesh2DRenderable(positions, colours, indices)
viewer.addRenderable(squareIndexedMesh)
def dynamicTest(viewer):
# Indexed square
positions = np.array([0., 0.,
1., 0.,
0., 1.,
1., 1.],
np.float64)
colours = np.array([1., 0., 0.,
0., 0., 1.,
0., 1., 0.,
1., 1., 1.])
indices = np.array([0, 1, 2, # First triangle
1, 2, 3]) # Second triangle
squareIndexedMesh = Mesh2DRenderable(positions, colours, indices)
viewer.addRenderable(squareIndexedMesh)
dyn = DummyDynamicSystem(squareIndexedMesh)
viewer.addDynamicSystem(dyn)
......@@ -6,8 +6,8 @@ import glfw
import numpy as np
from itertools import cycle
from .camera import Camera
from .shader import Shader
from graphics.camera import Camera
from graphics.shader import Shader
## GLFW viewer
class Viewer:
......@@ -54,6 +54,10 @@ class Viewer:
# Renderables
self.renderables = []
# Dynamic systems
self.dynamicOn = True
self.dynamicSystems = []
# Trackball
self.trackball = Camera(self.window)
......@@ -73,6 +77,11 @@ class Viewer:
viewMatrix = self.trackball.viewMatrix()
projectionMatrix = self.trackball.projectionMatrix(windowSize)
# Animate
if self.dynamicOn:
for ds in self.dynamicSystems:
ds.step()
# Draw
for renderable in self.renderables:
renderable.draw(modelMatrix, viewMatrix, projectionMatrix,
......@@ -87,8 +96,14 @@ class Viewer:
## Add new renderables to render
# @param self
# @param renderables
self.renderables.extend(renderables)
self.renderables.extend(renderables)
def addDynamicSystem(self, *ds):
## Add new renderables to render
# @param self
# @param ds
self.dynamicSystems.extend(ds)
def keyCallback(self, win, key, scancode, action, mods):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment