Trying To Make Alienrain In Python Using The Opengl Function Glmapbufferrange
Solution 1:
First of all, the 2nd parameter of glBufferData
is the buffer data in bytes.
Since the glsl data structure is
structdroplet_t
{float x_offset;
float y_offset;
float orientation;
float unused;
};
layout (std140) uniform droplets
{
droplet_t droplet[256];
};
the buffer size is 4*4*256, because the size of a float
is 4, t he structure has 4 elements of type flaot
and the array has 256 elements
glBufferData(GL_UNIFORM_BUFFER, 256*4*4, None, GL_DYNAMIC_DRAW)
The instruction
droplet = glMapBufferRange(GL_UNIFORM_BUFFER, 0, 256*4*4, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT)
returns a pointer to a allocated memory region with the proper size. You've to fill this memory with data.
The easiest solution is to use the (in python built-in) library ctypes
, which has the handy function .from_address()
:
This method returns a ctypes type instance using the memory specified by address.
so the instruction
float_array = ((ctypes.c_float * 4) * 256).from_address(droplet)
"wraps" a 2 dimensional array to the memory addressed by droplet
, with 256 elements and each element has 4 elements of type float
.
The values can be set to the array, by a simple assignment statement. e.g.:
import random
import math
import ctypes
glBindBufferBase(GL_UNIFORM_BUFFER, 0, rain_buffer);
droplet = glMapBufferRange(GL_UNIFORM_BUFFER, 0, 256*4*4, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT)
float_array = ((ctypes.c_float * 4) * 256).from_address(droplet)
for i inrange(0, 256):
float_array[i][0] = random.random() * 2 -1
float_array[i][1] = random.random() * 2 -1
float_array[i][2] = random.random() * math.pi * 2
float_array[i][3] = 0.0
Post a Comment for "Trying To Make Alienrain In Python Using The Opengl Function Glmapbufferrange"