How to use glClearBufferData to initialize a buffer - opengl

GLuint buffer;
glCreateBuffers(1, &buffer);
glNamedBufferStorage(buffer, 1024, NULL, GL_MAP_WRITE_BIT | GL_MAP_READ_BIT | GL_DYNAMIC_STORAGE_BIT);
unsigned int x = 0x01010101;
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glClearBufferData(GL_ARRAY_BUFFER, GL_R32UI, GL_RED, GL_UNSIGNED_INT, &x);
int* p = (int*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
for (int i = 0; i < 4; ++i)
cout << hex << p[i] << " ";
glUnmapBuffer(GL_ARRAY_BUFFER);
However, the output is '1010102' rather than '1010101'.

glClearBufferData(GL_ARRAY_BUFFER, GL_R32UI, GL_RED, GL_UNSIGNED_INT, &x);
The proper format parameter should be GL_RED_INTEGER, not GL_RED.

Related

glBufferSubData calling multiple times overrides the first data

I am using glBufferSubData to change positions of vertices.
Everything works fine, when I call the code below once.
If I run this whole code two times to add two pointclouds the vertex positions is updated only for the last geometry.
Please look at only following methods of the class, because the rest of the code is just a constructor to initialize the clouds:
void opengl_init()
void opengl_draw()
I think something is wrong with winding to the vao buffer.
Question:
How can I correctly update vertex positions glBufferSubData so that both pointclouds would move?
class pointcloud {
public:
int id = 0;
std::vector<vertex> vertices;
std::vector<unsigned int> indices;//pairs
unsigned int vao, vbo, ibo;
//individual polylines
pointcloud(const std::vector<float>& coord, const std::vector<float>& colors)//, std::vector<Texture> textures
{
vertices.reserve(coord.size());
indices.reserve((coord.size() - 1) * 2);
for (int i = 0; i < coord.size(); i += 3) {
vertex vertex;
vertex.position = glm::vec3(coord[i + 0], coord[i + 1], coord[i + 2]);
vertex.color = glm::vec3(colors[i + 0], colors[i + 1], colors[i + 2]);
vertices.emplace_back(vertex);
}
for (int i = 0; i < (coord.size() / 3) - 1; i++) {
indices.emplace_back(i + 0);
indices.emplace_back(i + 1);
}
// now that we have all the required data, set the vertex buffers and its attribute pointers.
//setup_polyline();
}
//merged polylines
pointcloud(const std::vector<std::vector<float>>& coord, const std::vector<std::vector<float>>& colors)//, std::vector<Texture> textures
{
//reserve memory
int v_count = 0;
int i_count = 0;
for (int i = 0; i < coord.size(); i++) {
v_count += coord[i].size();
i_count += coord[i].size() - 1;
}
vertices.reserve(v_count);
indices.reserve(i_count);
//fill vertics and indices lists
for (int i = 0; i < coord.size(); i++) {
for (int j = 0; j < coord[i].size(); j += 3) {
vertex vertex;
vertex.position = glm::vec3(coord[i][j + 0], coord[i][j + 1], coord[i][j + 2]);
vertex.color = glm::vec3(colors[i][j + 0], colors[i][j + 1], colors[i][j + 2]);
vertices.emplace_back(vertex);
}
}
v_count = 0;
for (int i = 0; i < coord.size(); i++) {
for (int j = 0; j < (coord[i].size() / 3) - 1; j++) {
//std::cout << v_count + j + 0 << " " << v_count + j + 1 << std::endl;
indices.emplace_back(v_count + j + 0);
indices.emplace_back(v_count + j + 1);
}
v_count += (coord[i].size() / 3);
}
//std::cout << vertices.size() << std::endl;
// now that we have all the required data, set the vertex buffers and its attribute pointers.
//setup_polyline();
}
//// initializes all the buffer objects/arrays
void opengl_init(bool draw_dynamic = true, int _id = 0)
{
id = _id + 1;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// init vertex-array and vertex-array-buffer
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//vertex array
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//bind vertex-array-buffer to the vertex-array
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//declare array with data or empty array depending how data will be displayed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
auto type = !draw_dynamic ? GL_STATIC_DRAW : GL_STREAM_DRAW;
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertex), &vertices[0], type); // target | size | data (poinnting to first element e.g. glm::value_ptr(vertices[0])) | usage
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// set attributes that corresponds to layout id in the vertex shader
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// vertex Positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)0);
// vertex normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)offsetof(vertex, color));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//bind buffers vao | vbo | ibo
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//glBindVertexArray(0);
}
// render the mesh
void opengl_draw(opengl_shaders::shader& shader, bool draw_dynamic = true)
{
//update
//https://learnopengl.com/Advanced-OpenGL/Advanced-Data
if (draw_dynamic) {
for (auto& v : vertices)
v.position.y += 0.001;
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(vertex), &vertices[0]);
}
//draw
glBindVertexArray(vao);
glDrawArrays(GL_POINTS, 0, vertices.size());
//glBindVertexArray(0);
}
void opengl_clear(opengl_shaders::shader& shader) {
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
shader.delete_shader();
}
};
glBufferSubData updates a subset of the data store of a buffer object that is currently bound to the specified target. You must bind the buffer before you can modify its data:
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(vertex), &vertices[0]);
The GL_ARRAY_BUFFER binding is a global state. This binding is maintained until it is changed. As for your code it works for 1 pointcloud, but it doesn't work if you have more than 1 pointcloud and vertex buffer.

OpenGL glBufferSubData Offset issue

I have a problem copying/upload all the data into the buffer using glBufferSubData(). I want to copy chunk by chunk to the buffer. So I have used this approach. I am seeing a blank screen when I try to render. Please find my code below. do you see any problem in calculating the buffer offset? Or is this not the way to copy data into the buffer?
Below is the data structure
struct DisplayIndexID {
int idx;
DrawStateT drawState;
//Every display Index ID has its own draw models.
std::vector<std::unique_ptr<vertexModel>> readytoDrawModels;
};
void initVboData(std::vector<DisplayIndexID> & v)
{
glBindBuffer(GL_ARRAY_BUFFER, geomVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D) * 4096, NULL, GL_DYNAMIC_DRAW);
std::vector<QVector3D> vecToDraw;
GLintptr offset = 0;
for (int i = 0; i < v.size(); i++)
for (auto& vModel : v[i].readytoDrawModels)
{
if (vModel) {
vecToDraw = vModel->getVertices();
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(QVector3D) * vecToDraw.size(), &vecToDraw[0]);
offset += sizeof(QVector3D) * vecToDraw.size(); // is this offset calculation fine?
}
}
}
//Below is my draw function
void drawDisplayLists(std::vector<DisplayIndexID> & v)
{
initVboData(v);
for (int i = 0; i < v.size(); i++)
{
//Make context current
makeCurrent();
bool isTextureUsed = false;
//Apply Projection Matrix.
GLint mvp_mat = 0;
GLint mvp_matText = 0;
///***********PRINT AREA***********************/
for (auto& vModel : v[i].readytoDrawModels)
{
// Code related to Shaders
......
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(QVector3D), nullptr); how to get the offset and add offset here.
switch (vModel->getDrawMode())
{
case 0: //GL_POINTS
glDrawArrays(GL_POINTS, 0, vModel->getVertices().size());
break;
case 1: //GL_LINES
glDrawArrays(GL_LINES, 0, vModel->getVertices().size());
break;
case 2: //GL_LINE_LOOP
glDrawArrays(GL_LINE_LOOP, 0, vModel->getVertices().size());
break;
}
}
}
}
Considering that you have persistent VBO initialized like that:
GLintptr offset = 0;
for (int i = 0; i < v.size(); i++) {
for (auto& vModel : v[i].readytoDrawModels) {
if (vModel) {
vecToDraw = vModel->getVertices();
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(QVector3D) * vecToDraw.size(), &vecToDraw[0]);
offset += sizeof(QVector3D) * vecToDraw.size();
}
}
}
then you should replicate the same offsets within drawing loop.
This can be done either by passing offset in bytes to glVertexAttribPointer() computed exactly in the same way as during VBO initialization:
GLintptr offset = 0;
for (int i = 0; i < v.size(); i++) {
for (auto& vModel : v[i].readytoDrawModels) {
if (vModel) {
vecToDraw = vModel->getVertices();
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(QVector3D), offset);
glDrawArrays(GL_LINES, 0, vecToDraw.size());
offset += sizeof(QVector3D) * vecToDraw.size();
}
}
}
or by passing offset in number of vertices to glDrawArrays(), in case if all models have the same vertex definition as in your code snippet (in vec3 Position):
GLintptr aFirstIndex = 0;
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(QVector3D), nullptr);
for (int i = 0; i < v.size(); i++) {
for (auto& vModel : v[i].readytoDrawModels) {
if (vModel) {
vecToDraw = vModel->getVertices();
glDrawArrays(GL_LINES, aFirstIndex, vecToDraw.size());
aFirstIndex += vecToDraw.size();
}
}
}
Make sure that allocated VBO has a proper size and 4096 is replaced by a real size:
glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D) * 4096, NULL, GL_DYNAMIC_DRAW);
By the way, grouping models by GL_POINTS/GL_LINES array type and same presentation attributes (color/material/etc.) would allow rendering each group with a single glDrawArrays() call, instead of numerous glDrawArrays() for every model, which would be more optimal from performance point of view.

3d point cloud of an object using Kinect

I've got a point cloud of an object using kinect and I want to create a 3d representation of the point cloud and save the image. I'm not sure how I can approach this task. I want it to be able to detect an object and derive the point cloud of the object. For example for 3d scanning purposes, it would be able to 3d scan the object from all directions and then get a point cloud from it.
main.cpp
#include "main.h"
#include "glut.h"
#include <cmath>
#include <cstdio>
#include <Windows.h>
#include <Ole2.h>
#include <NuiApi.h>
#include <NuiImageCamera.h>
#include <NuiSensor.h>
// OpenGL Variables
long depthToRgbMap[width*height*2];
// We'll be using buffer objects to store the kinect point cloud
GLuint vboId;
GLuint cboId;
// Kinect variables
HANDLE depthStream;
HANDLE rgbStream;
INuiSensor* sensor;
bool initKinect() {
// Get a working kinect sensor
int numSensors;
if (NuiGetSensorCount(&numSensors) < 0 || numSensors < 1) return false;
if (NuiCreateSensorByIndex(0, &sensor) < 0) return false;
// Initialize sensor
sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR);
sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, // Depth camera or rgb camera?
NUI_IMAGE_RESOLUTION_640x480, // Image resolution
0, // Image stream flags, e.g. near mode
2, // Number of frames to buffer
NULL, // Event handle
&depthStream);
sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, // Depth camera or rgb camera?
NUI_IMAGE_RESOLUTION_640x480, // Image resolution
0, // Image stream flags, e.g. near mode
2, // Number of frames to buffer
NULL, // Event handle
&rgbStream);
return sensor;
}
void getDepthData(GLubyte* dest) {
float* fdest = (float*) dest;
long* depth2rgb = (long*) depthToRgbMap;
NUI_IMAGE_FRAME imageFrame;
NUI_LOCKED_RECT LockedRect;
if (sensor->NuiImageStreamGetNextFrame(depthStream, 0, &imageFrame) < 0) return;
INuiFrameTexture* texture = imageFrame.pFrameTexture;
texture->LockRect(0, &LockedRect, NULL, 0);
if (LockedRect.Pitch != 0) {
const USHORT* curr = (const USHORT*) LockedRect.pBits;
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
// Get depth of pixel in millimeters
USHORT depth = NuiDepthPixelToDepth(*curr++);
// Store coordinates of the point corresponding to this pixel
Vector4 pos = NuiTransformDepthImageToSkeleton(i, j, depth<<3, NUI_IMAGE_RESOLUTION_640x480);
*fdest++ = pos.x/pos.w;
*fdest++ = pos.y/pos.w;
*fdest++ = pos.z/pos.w;
// Store the index into the color array corresponding to this pixel
NuiImageGetColorPixelCoordinatesFromDepthPixelAtResolution(
NUI_IMAGE_RESOLUTION_640x480, NUI_IMAGE_RESOLUTION_640x480, NULL,
i, j, depth<<3, depth2rgb, depth2rgb+1);
depth2rgb += 2;
}
}
}
texture->UnlockRect(0);
sensor->NuiImageStreamReleaseFrame(depthStream, &imageFrame);
}
void getRgbData(GLubyte* dest) {
float* fdest = (float*) dest;
long* depth2rgb = (long*) depthToRgbMap;
NUI_IMAGE_FRAME imageFrame;
NUI_LOCKED_RECT LockedRect;
if (sensor->NuiImageStreamGetNextFrame(rgbStream, 0, &imageFrame) < 0) return;
INuiFrameTexture* texture = imageFrame.pFrameTexture;
texture->LockRect(0, &LockedRect, NULL, 0);
if (LockedRect.Pitch != 0) {
const BYTE* start = (const BYTE*) LockedRect.pBits;
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
// Determine rgb color for each depth pixel
long x = *depth2rgb++;
long y = *depth2rgb++;
// If out of bounds, then don't color it at all
if (x < 0 || y < 0 || x > width || y > height) {
for (int n = 0; n < 3; ++n) *(fdest++) = 0.0f;
}
else {
const BYTE* curr = start + (x + width*y)*4;
for (int n = 0; n < 3; ++n) *(fdest++) = curr[2-n]/255.0f;
}
}
}
}
texture->UnlockRect(0);
sensor->NuiImageStreamReleaseFrame(rgbStream, &imageFrame);
}
void getKinectData() {
GLubyte* ptr;
glBindBuffer(GL_ARRAY_BUFFER, vboId);
ptr = (GLubyte*) glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (ptr) {
getDepthData(ptr);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, cboId);
ptr = (GLubyte*) glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (ptr) {
getRgbData(ptr);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
}
void rotateCamera() {
static double angle = 0.;
static double radius = 3.;
double x = radius*sin(angle);
double z = radius*(1-cos(angle)) - radius/2;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x,0,z,0,0,radius/2,0,1,0);
angle += 0.05;
}
void drawKinectData() {
getKinectData();
rotateCamera();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, cboId);
glColorPointer(3, GL_FLOAT, 0, NULL);
glPointSize(1.f);
glDrawArrays(GL_POINTS, 0, width*height);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
int main(int argc, char* argv[]) {
if (!init(argc, argv)) return 1;
if (!initKinect()) return 1;
// OpenGL setup
glClearColor(0,0,0,0);
glClearDepth(1.0f);
// Set up array buffers
const int dataSize = width*height * 3 * 4;
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, dataSize, 0, GL_DYNAMIC_DRAW);
glGenBuffers(1, &cboId);
glBindBuffer(GL_ARRAY_BUFFER, cboId);
glBufferData(GL_ARRAY_BUFFER, dataSize, 0, GL_DYNAMIC_DRAW);
// Camera setup
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width /(GLdouble) height, 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,0,0,0,1,0,1,0);
// Main loop
execute();
return 0;
}

OpenGL- glDrawElements only draws first element

Hi everyone i'm making a little 3d engine for my games and i created a class for vertex arrays and VBO. It was working until i tried to add indexing to the vertex array. Now it only draws the first element indexed!
Here is the function build, that generates the VAO and the VBOs.
void build() {
vboID = new GLuint[vboS];
if (vertexIndexing) {
for (int n = 0, m = 0, i = 0; n < vertexes.size(); m++) {
if (i + m >= vertexes.size()) {
break;
}
indices.push_back((GLushort)(i+m));
if (m >= s) {
m = 0;
n++;
i+=s/2;
std::cout << "\n";
}
std::cout << i + m << " , ";
}
std::cout << "\n";
}
shader->use();
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
glGenBuffers(vboS, vboID);
if (vertexIndexing) {
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLushort), &indices[0], GL_STATIC_DRAW);
}
int n = 0;
if (options.vertex) {
shader->addAttribute("vertex");
glBindBuffer(GL_ARRAY_BUFFER, vboID[n]);
glBufferData(GL_ARRAY_BUFFER, pos.size() * sizeof(GLfloat), &pos[0], GL_STATIC_DRAW);
glVertexAttribPointer(shader->attribute("vertex"), 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(shader->attribute("vertex"));
n++;
}
if (options.color) {
shader->addAttribute("color");
glBindBuffer(GL_ARRAY_BUFFER, vboID[n]);
glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(GLfloat), &colors[0], GL_STATIC_DRAW);
glVertexAttribPointer(shader->attribute("color"), 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(shader->attribute("color"));
n++;
}
if (options.normal) {
shader->addAttribute("normal");
glBindBuffer(GL_ARRAY_BUFFER, vboID[n]);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(GLfloat), &normals[0], GL_STATIC_DRAW);
glVertexAttribPointer(shader->attribute("normal"), 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(shader->attribute("normal"));
n++;
}
if (options.texcoord) {
shader->addAttribute("texcoord");
glBindBuffer(GL_ARRAY_BUFFER, vboID[n]);
glBufferData(GL_ARRAY_BUFFER, texcoords.size() * sizeof(GLfloat), &texcoords[0], GL_STATIC_DRAW);
glVertexAttribPointer(shader->attribute("texcoord"), 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(shader->attribute("texcoord"));
n++;
}
glBindVertexArray(0);
shader->disable();
}
Here is the function draw, that draws the VAO.
void draw() {
glBindVertexArray(vaoID);
if (vertexIndexing) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
shader->use();
glDrawElements(mode, indices.size(), GL_UNSIGNED_SHORT, 0);
shader->disable();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
else {
shader->use();
glDrawArrays(mode, 0, vertexes.size());
shader->disable();
}
glBindVertexArray(0);
}
Note: I removed glEnableClientState(GL_VERTEX_ARRAY) because it isnt doing anything.
Ok, I fixed it. It wasn't openGL's problem.
It was with the indices generation.
NEW INDICES GENERATION:
for (int n = 0, m = 0, i = 0; n < vertexes.size(); m++) {
if (i + m >= vertexes.size()) {
break;
}
if (m >= s) {
m = 0;
n++;
i+=s/2;
}
indices.push_back((GLushort)(i + m));
}

OpenGL Compute Shader SSBO

I want a compute shader that writes 1's in the output buffer. I compile the shader and attach it to the program without problem, then I call glDispatchCompute() function and I wait until compute shader ends. But when I see the array, there are only 0's.
Can anyone tell me where is the mistake?
This is my compute shader code:
#version 430 core
layout (local_size_x = 2) in;
layout(std430, binding=0) writeonly buffer Pos{
float Position[];
};
void main(){
Position[gl_GlobalInvocationID.x] = 1.0f;
}
And this is some of my main:
GLuint program_compute = 0, SSBO = 0;
//...(Create, compile and link the program with the shader)...
vector<GLfloat> initPos;
int num_numeros = 12;
for (int i = 0; i < num_numeros; i++){
initPos.push_back(0.0f);
}
glUseProgram(program_compute);
glGenBuffers(1, &SSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, SSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, num_numeros * sizeof(GLfloat), &initPos, GL_DYNAMIC_DRAW);
glDispatchCompute(num_numeros/2, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
for (int i = 0; i < num_numeros; i++){
cout << "p" << i << ": " << initPos[i] << endl;
}
cout << endl;
EDIT: Finally it works. Thank you BDL.
As BDL has said, I've forgotten read the buffer back from GPU memory. Now it works. This is the new code:
GLuint program_compute = 0, SSBO = 0;
// ...The same code as above
glDispatchCompute(num_numeros/2, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);
GLfloat *ptr;
ptr = (GLfloat *) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY);
initPos.clear();
for (int i = 0; i < num_numeros; i++){
initPos.push_back(ptr[i]);
}
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
for (int i = 0; i < num_numeros; i++){
cout << "p" << i << ": " << initPos[i] << endl;
}
cout << endl;
EDIT: Thank you Andon M. Coleman.
I read from a write only buffer. Here is the line fixed:
ptr = (GLfloat *) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
glBufferData copies the content of initPos into the SSBO. The shader then operates on the buffer, not on the cpu memory array. Unless you read the buffer back from GPU to CPU memory somewhere, initPos will never change.