I’ve got 2 image2D texture buffers, one containing vertex position data, and the other containing vertex colour data. I’m trying to copy this data into a vertex buffer directly on the GPU. Is this the right way to do it?
I have confirmed the textures contain the correct data using a preexisting raytracing shader, but my aim is to use this texture data to create a mesh without having to pull the texture data back to the CPU.
I create my vao/vbo/ebo like so:
const size_t num_vertices = _map_terrain_texture_shape.x * _map_terrain_texture_shape.y;
const size_t total_vertex_position_bytes = num_vertices * sizeof(glm::vec4);
const size_t total_vertex_colour_bytes = num_vertices * sizeof(glm::vec4);
const size_t total_vertex_bytes = total_vertex_position_bytes + total_vertex_colour_bytes;
std::vector<uint32_t> indices = _make_indices(_map_terrain_texture_shape);
const size_t total_index_bites = indices.size() * sizeof(uint32_t);
glGenVertexArrays(1, &_vao);
glGenBuffers(1, &_vbo);
glGenBuffers(1, &_ebo);
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, total_vertex_bytes, nullptr, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, total_index_bites, indices.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(VERTEX_POSITION_ATTRIB_INDEX);
glEnableVertexAttribArray(VERTEX_COLOUR_ATTRIB_INDEX);
// vertex draw positions
glVertexAttribPointer(VERTEX_POSITION_ATTRIB_INDEX, glm::vec4::length(), GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (void*)0);
// vertex colours
glVertexAttribPointer(VERTEX_COLOUR_ATTRIB_INDEX, glm::vec4::length(), GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (void*)total_vertex_position_bytes);
glDisableVertexAttribArray(VERTEX_POSITION_ATTRIB_INDEX);
glDisableVertexAttribArray(VERTEX_COLOUR_ATTRIB_INDEX);
glBindVertexArray(0);
And then I copy the data from the pre-existing textures into the vertex array like so:
const size_t num_vertices = _map_terrain_texture_shape.x * _map_terrain_texture_shape.y;
const size_t total_vertex_position_bytes = num_vertices * sizeof(glm::vec4);
const size_t total_vertex_colour_bytes = num_vertices * sizeof(glm::vec4);
const auto position_texture_id = _main_state.terrain_generator->map_terrain_vertex_position_texture->id;
const auto colour_texture_id = _main_state.terrain_generator->map_terrain_vertex_colour_texture->id;
glBindBuffer(GL_COPY_WRITE_BUFFER, _vbo);
glBindBuffer(GL_COPY_READ_BUFFER, position_texture_id);
glCopyBufferSubData(position_texture_id, _vbo,
0, 0,
total_vertex_position_bytes);
glBindBuffer(GL_COPY_READ_BUFFER, colour_texture_id);
glCopyBufferSubData(colour_texture_id, _vbo,
0, total_vertex_position_bytes,
total_vertex_colour_bytes);
glBindBuffer(GL_COPY_READ_BUFFER, 0);
glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
Currently I’m getting a black screen when I run my rasterizing shader instead of my raytrace shader. I have checked and double checked how the rasterizing renderer is setup, and I cannot find a fault there so far, so I’m wondering if my mistake is in how I’m copying this data (as I’ve never tried to do this before)