void togglePBOs(void) { usePBOs = !usePBOs; if (usePBOs) { glutChangeToMenuEntry(1, "Toggle PBO usage (currently ON)", 1); // first upload client memory to PBOs, then free them for (int i = 0; i < 3; i++) { glBindBuffer(GL_PIXEL_PACK_BUFFER, i+1); glBufferData(GL_PIXEL_PACK_BUFFER, dataHeight * dataPitch, pixels[i], usageHint); assert(pixels[i]); free(pixels[i]); pixels[i] = NULL; } glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); } else { glutChangeToMenuEntry(1, "Toggle PBO usage (currently OFF)", 1); // allocate read buffer, size of window for (int i = 0; i < 3; i++) { assert(!pixels[i]); pixels[i] = (GLubyte*)malloc(dataHeight * dataPitch); assert(pixels[i]); // upload PBO data, then delete glBindBuffer(GL_PIXEL_PACK_BUFFER, i+1); glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, dataHeight * dataPitch, pixels[i]); } GLuint names[3] = {1, 2, 3}; glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); glDeleteBuffers(3, names); } }
PIGLIT_GL_TEST_CONFIG_END void piglit_init(int argc, char **argv) { bool pass = true; GLuint bo; uint8_t in_data[1] = {0xaa}; uint8_t out_data[1] = {0xd0}; void *ptr1, *ptr2; piglit_require_extension("GL_ARB_uniform_buffer_object"); glGenBuffers(1, &bo); glBindBuffer(GL_UNIFORM_BUFFER, bo); pass = pass && piglit_check_gl_error(0); glBufferData(GL_UNIFORM_BUFFER, 1, NULL, GL_STATIC_READ); pass = pass && piglit_check_gl_error(0); glBufferSubData(GL_UNIFORM_BUFFER, 0, 1, in_data); pass = pass && piglit_check_gl_error(0); ptr1 = glMapBuffer(GL_UNIFORM_BUFFER, GL_READ_ONLY); pass = pass && piglit_check_gl_error(0); glGetBufferPointerv(GL_UNIFORM_BUFFER, GL_BUFFER_MAP_POINTER, &ptr2); pass = pass && piglit_check_gl_error(0); assert(ptr1 == ptr2); glUnmapBuffer(GL_UNIFORM_BUFFER); pass = pass && piglit_check_gl_error(0); glGetBufferSubData(GL_UNIFORM_BUFFER, 0, 1, out_data); pass = pass && piglit_check_gl_error(0); assert(memcmp(in_data, out_data, sizeof(in_data)) == 0); piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL); }
void Clouds::particleSorting() { // read depth vbo glBindBuffer(GL_ARRAY_BUFFER, vbo_cloudParticleBuffer_Write[2]); glGetBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * maxNumCloudParticles, particleDepthBuffer.get()); glBindBuffer(GL_ARRAY_BUFFER, 0); // sort by depth std::sort(particleIndexBuffer.get(), particleIndexBuffer.get() + maxNumCloudParticles, [&](size_t i, size_t j) { return particleDepthBuffer[i] > particleDepthBuffer[j]; }); // count num visible (the cloudMove.vert wrote a large depth value for all particles left/right/up/down/behind frustum) unsigned int numParticlesInvalid = 0; while(numParticlesInvalid < maxNumCloudParticles && particleDepthBuffer[particleIndexBuffer[numParticlesInvalid]] > farPlaneDistance) ++numParticlesInvalid; numParticlesRender = maxNumCloudParticles - numParticlesInvalid; // write ibo glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cloudParticleRendering); glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(unsigned short) * numParticlesRender, particleIndexBuffer.get() + numParticlesInvalid); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); std::cout << "Num Particles rendered: " << numParticlesRender << " \r"; }
PIGLIT_GL_TEST_CONFIG_END void piglit_init(int argc, char *argv[]) { bool pass = true; uint32_t dummy_data_1[4], dummy_data_2[4]; uint32_t good_data[4] = {0, 1, 2, 3}; uint32_t result_data[4]; bool subtest_pass; size_t size = sizeof(good_data); GLuint buffer_handles[2]; memset(dummy_data_1, 0xaa, size); memset(dummy_data_2, 0xbb, size); piglit_require_extension("GL_ARB_copy_buffer"); glGenBuffers(2, buffer_handles); glBindBuffer(GL_COPY_READ_BUFFER, buffer_handles[0]); glBindBuffer(GL_COPY_WRITE_BUFFER, buffer_handles[1]); glBufferData(GL_COPY_READ_BUFFER, 4096, NULL, GL_STREAM_COPY); glBufferData(GL_COPY_WRITE_BUFFER, 4096, NULL, GL_STREAM_COPY); glBufferSubData(GL_COPY_READ_BUFFER, 0, size, good_data); glBufferSubData(GL_COPY_WRITE_BUFFER, 0, size, dummy_data_1); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); glBufferSubData(GL_COPY_READ_BUFFER, 0, size, dummy_data_2); memset(result_data, 0xd0, size); glGetBufferSubData(GL_COPY_WRITE_BUFFER, 0, size, result_data); subtest_pass = memcmp(good_data, result_data, size) == 0; if (!subtest_pass) { fprintf(stderr, "found 0x%08x 0x%08x 0x%08x 0x%08x\n", result_data[0], result_data[1], result_data[2], result_data[3]); pass = false; } piglit_report_subtest_result(subtest_pass ? PIGLIT_PASS : PIGLIT_FAIL, "overwrite source data"); glBufferData(GL_COPY_READ_BUFFER, 4096, NULL, GL_STREAM_COPY); glBufferData(GL_COPY_WRITE_BUFFER, 4096, NULL, GL_STREAM_COPY); glBufferSubData(GL_COPY_READ_BUFFER, 0, size, dummy_data_1); glBufferSubData(GL_COPY_WRITE_BUFFER, 0, size, dummy_data_2); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); glBufferSubData(GL_COPY_WRITE_BUFFER, 0, size, good_data); memset(result_data, 0xd0, size); glGetBufferSubData(GL_COPY_WRITE_BUFFER, 0, size, result_data); subtest_pass = memcmp(good_data, result_data, size) == 0; if (!subtest_pass) { fprintf(stderr, "found 0x%08x 0x%08x 0x%08x 0x%08x\n", result_data[0], result_data[1], result_data[2], result_data[3]); pass = false; } piglit_report_subtest_result(subtest_pass ? PIGLIT_PASS : PIGLIT_FAIL, "overwrite destination data"); piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL); }
void vertex_buffer<Scalar, Target>::get_data(Scalar* data) { if (!bound()) { throw std::runtime_error("vertex_buffer::get_data(): Buffer not bound." + SPOT); } glGetBufferSubData(Target, 0, data_size_, reinterpret_cast<GLvoid*>(data)); }
int render_export_obj(char **buffer) { IF_FAILED0(init && buffer); int element_buffer_size = 0, vertex_buffer_size = 0, normal_buffer_size = 0; GLuint vertex_vbo, index_vbo, normal_vbo; GLint last_array_buffer, last_element_array_buffer; glBindVertexArray(0); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer); glGenBuffers(1, &vertex_vbo); glGenBuffers(1, &index_vbo); glGenBuffers(1, &normal_vbo); if(!marching_cubes_create_vbos(volume, volume_size, grid_size, isolevel, vertex_vbo, index_vbo, normal_vbo, volume_func, NULL)) { ERROR_MSG("Marching Cubes: nothing to generate"); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer); return 0; } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_vbo); glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &element_buffer_size); glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo); glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &vertex_buffer_size); glBindBuffer(GL_ARRAY_BUFFER, normal_vbo); glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &normal_buffer_size); if(element_buffer_size <= 0 || vertex_buffer_size <= 0 || normal_buffer_size <= 0) { glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer); return 0; } float *vertex_data = (float*) malloc(vertex_buffer_size); float *normal_data = (float*) malloc(normal_buffer_size); unsigned int *element_data = (unsigned int*) malloc(element_buffer_size); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_vbo); glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, element_buffer_size, element_data); glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo); glGetBufferSubData(GL_ARRAY_BUFFER, 0, vertex_buffer_size, vertex_data); glBindBuffer(GL_ARRAY_BUFFER, normal_vbo); glGetBufferSubData(GL_ARRAY_BUFFER, 0, normal_buffer_size, normal_data); // выделяем как можно больше памяти, чтобы вместились все данные *buffer = (char*) malloc(sizeof(char) * (element_buffer_size + vertex_buffer_size + normal_buffer_size)*8); *buffer[0] = '\0'; strcat(*buffer, "# Generated via VRender\n"); char *temp = (char*) malloc(sizeof(char) * 64); sprintf(temp, "# isolevel: %.3f\n", isolevel); strcat(*buffer, temp); sprintf(temp, "# volume size: x %i y %i z %i\n", volume_size.x, volume_size.y, volume_size.z); strcat(*buffer, temp); sprintf(temp, "# grid size: x %i y %i z %i\n", grid_size.x, grid_size.y, grid_size.z); strcat(*buffer, temp); unsigned buffer_begin = strlen(*buffer); unsigned num_chars = 0; char *ptr = *buffer; ptr += buffer_begin; num_chars = sprintf(temp, "\n# Vertices\n"); strcat(ptr, temp); ptr += num_chars; for(unsigned i = 0; i < (vertex_buffer_size / sizeof(float)); i += 3) { num_chars = sprintf(temp, "v %f %f %f\n", vertex_data[i], vertex_data[i+1], vertex_data[i+2]); strcat(ptr, temp); ptr += num_chars; } num_chars = sprintf(temp, "\n# Normals\n"); strcat(ptr, temp); ptr += num_chars; for(unsigned i = 0; i < (normal_buffer_size / sizeof(float)); i += 3) { num_chars = sprintf(temp, "vn %f %f %f\n", normal_data[i], normal_data[i+1], normal_data[i+2]); strcat(ptr, temp); ptr += num_chars; } num_chars = sprintf(temp, "\n# Faces\n"); strcat(ptr, temp); ptr += num_chars; for(unsigned i = 0; i < (element_buffer_size / sizeof(unsigned int)) - 3; i += 3) { num_chars = sprintf(temp, "f %i//%i %i//%i %i//%i\n", element_data[i]+1, element_data[i]+1, element_data[i+1]+1, element_data[i+1]+1, element_data[i+2]+1, element_data[i+2]+1); strcat(ptr, temp); ptr += num_chars; } num_chars = sprintf(temp, "\n# End\n"); strcat(ptr, temp); ptr += num_chars; free(vertex_data); free(element_data); free(normal_data); free(temp); glDeleteBuffers(1, &vertex_vbo); glDeleteBuffers(1, &index_vbo); glDeleteBuffers(1, &normal_vbo); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer); return 1; }
void BufferObj::getBufferSubData( GLintptr offset, GLsizeiptr size, GLvoid *data ) { ScopedBuffer bufferBind( mTarget, mId ); glGetBufferSubData( mTarget, offset, size, data ); }
int main() { srand(time(0)); s_settings settings; settings.mode = MODE_AUTO; settings.w = 960; settings.h = 480; settings.tiled_view = true; settings.paused = false; if(glfwInit() == GL_FALSE) { std::cerr << "Failed to init GLFW" << std::endl; return 1; } glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); GLFWwindow *window; if((window = glfwCreateWindow(settings.w, settings.h, "Painting Evolution", 0, 0)) == 0) { std::cerr << "Failed to open window" << std::endl; glfwTerminate(); return 1; } glfwSetWindowUserPointer(window, &settings); glfwMakeContextCurrent(window); glfwSetWindowSizeCallback(window, glfw_window_size_callback); glfwSetCursorPosCallback(window, glfw_cursor_position_callback); glfwSetScrollCallback(window, glfw_mouse_scroll_callback); glfwSetKeyCallback(window, glfw_keyboard_callback); glfwSetMouseButtonCallback(window, glfw_mouse_button_callback); // start GLEW extension handler glewExperimental = GL_TRUE; GLenum err = glewInit(); while((err = glGetError()) != GL_NO_ERROR) { std::cout << "glewInit error: " << err << std::endl; std::cout << std::endl; } #ifndef NDEBUG const GLubyte* renderer = glGetString(GL_RENDERER); const GLubyte* version = glGetString(GL_VERSION); std::cout << "Debug info:" << std::endl; std::cout << " Date: " << __DATE__ << std::endl; std::cout << " Time: " << __TIME__ << std::endl; std::cout << " Renderer: " << renderer << std::endl; std::cout << " OpenGL version supported: " << version << std::endl; std::cout << " Max textures: " << GL_MAX_TEXTURE_UNITS << std::endl; std::cout << std::endl; #endif glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); bool r; GLuint vertex_shader, fragment_shader, compute_shader; r = vertex_shader_load(&vertex_shader, "shaders\\vertex_shader.glsl"); if(r == false) { glfwDestroyWindow(window); glfwTerminate(); } r = fragment_shader_load(&fragment_shader, "shaders\\fragment_shader.glsl"); if(r == false) { glfwDestroyWindow(window); glfwTerminate(); } r = compute_shader_load(&compute_shader, "shaders\\compute_shader.glsl"); if(r == false) { glfwDestroyWindow(window); glfwTerminate(); } GLuint shader_program, similarity_program; // create program shader_program = glCreateProgram(); glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); glLinkProgram(shader_program); check_program_link_status(shader_program); // create program similarity_program = glCreateProgram(); glAttachShader(similarity_program, compute_shader); glLinkProgram(similarity_program); check_program_link_status(similarity_program); /**** side by side view ****/ GLuint vao, vbo, cbo, uvbo; glGenVertexArrays(1, &vao); glBindVertexArray(vao); // Vertices glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (char*)0 + 0*sizeof(GLfloat)); // Colours glGenBuffers(1, &cbo); glBindBuffer(GL_ARRAY_BUFFER, cbo); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (char*)0 + 0*sizeof(GLfloat)); // uvs glGenBuffers(1, &uvbo); glBindBuffer(GL_ARRAY_BUFFER, uvbo); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (char*)0 + 0*sizeof(GLfloat)); /**** side by side view ****/ s_sim sim; bmp_load(&sim.target, "input.bmp"); sim.grid_pos = 0; sim.grid_w = 8; sim.grid_h = 8; sim.tile_w = sim.target.w/sim.grid_w; sim.tile_h = sim.target.h/sim.grid_h; settings.sim = ∼ assert(sim.target.w%16 == 0); assert(sim.target.h%16 == 0); assert(sim.tile_w%16 == 0); assert(sim.tile_h%16 == 0); s_painting temp; for(int p = 0; p < 36; ++p) { sim.paintings.push_back(temp); painting_init(&sim.paintings[p], sim.tile_w, sim.tile_h); } for(int p = 0; p < sim.grid_w*sim.grid_h; ++p) { sim.grid_paintings.push_back(temp); painting_init(&sim.grid_paintings[p], sim.tile_w, sim.tile_h); } int num_workgroups = sim.tile_w*sim.tile_h/16/16; std::vector<GLfloat> scores(num_workgroups); // generate vao_compute and scores_bo GLuint vao_compute, scores_bo; glGenVertexArrays(1, &vao_compute); glBindVertexArray(vao_compute); glGenBuffers(1, &scores_bo); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, scores_bo); glBufferData(GL_SHADER_STORAGE_BUFFER, num_workgroups*sizeof(scores[0]), &scores[0], GL_DYNAMIC_DRAW); // GL_STATIC_DRAW glGenTextures(1, &sim.target_id); glBindTexture(GL_TEXTURE_2D, sim.target_id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, sim.target.w, sim.target.h, 0, GL_RGB, GL_UNSIGNED_BYTE, sim.target.data); glGenTextures(1, &sim.result_id); glBindTexture(GL_TEXTURE_2D, sim.result_id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, sim.target.w, sim.target.h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); std::vector<GLubyte> emptyData(3 * sim.target.w * sim.target.h, 0); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sim.target.w, sim.target.h, GL_RGB, GL_UNSIGNED_BYTE, &emptyData[0]); GLuint highres_texture_id = 0; glGenTextures(1, &highres_texture_id); glBindTexture(GL_TEXTURE_2D, highres_texture_id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, sim.tile_w, sim.tile_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); // create a framebuffer object GLuint highres_fbo = 0; glGenFramebuffers(1, &highres_fbo); glBindFramebuffer(GL_FRAMEBUFFER, highres_fbo); // attach the texture to FBO color attachment point glFramebufferTexture2D(GL_FRAMEBUFFER, // 1. fbo target: GL_FRAMEBUFFER GL_COLOR_ATTACHMENT0, // 2. attachment point GL_TEXTURE_2D, // 3. tex target: GL_TEXTURE_2D highres_texture_id, // 4. tex ID 0); // 5. mipmap level: 0(base) glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, 0); // check FBO status GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if(status != GL_FRAMEBUFFER_COMPLETE) { std::cout << "Framebuffer error:" << status<< std::endl; return -1; } GLuint texture_slice = 0; glGenTextures(1, &texture_slice); glActiveTexture(GL_TEXTURE0 + texture_slice); glBindTexture(GL_TEXTURE_2D, texture_slice); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, sim.tile_w, sim.tile_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glBindTexture(GL_TEXTURE_2D, 0); glPointSize(2.0); GLuint query; glGenQueries(1, &query); GLubyte *data = new GLubyte[3*sim.tile_w*sim.tile_h]; unsigned int parent1_id = 0; unsigned int parent2_id = 0; float parent1_score = FLT_MIN; float parent2_score = FLT_MIN; int x_offset = 0; int y_offset = 0; int tile_x = 0; int tile_y = 0; int last_grid_pos = -1; int auto_generation = 0; float start_score = 0.0; while(!glfwWindowShouldClose(window)) { glBeginQuery(GL_TIME_ELAPSED, query); if(settings.paused == false) { // Auto mode will spend a set number of generations per grid position and then move to the new lowest scoring if(settings.mode == MODE_AUTO && auto_generation >= AUTO_GENERATIONS) { // Set score rate sim.grid_paintings[sim.grid_pos].score_rate = (sim.grid_paintings[sim.grid_pos].score - start_score)/AUTO_GENERATIONS; assert(sim.grid_paintings[sim.grid_pos].score_rate >= 0.0); assert(sim.grid_paintings[sim.grid_pos].score_rate <= 1.0/AUTO_GENERATIONS); float fastest = FLT_MIN; for(unsigned int p = 0; p < sim.grid_paintings.size(); ++p) { if(sim.grid_paintings[p].score_rate > fastest) { sim.grid_pos = p; fastest = sim.grid_paintings[p].score_rate; } } start_score = sim.grid_paintings[sim.grid_pos].score; auto_generation = 0; } // Next tile if(last_grid_pos != sim.grid_pos) { if(last_grid_pos != -1) { // Update results texture - do all grid paintings because new ones might've been loaded in for(unsigned int p = 0; p < sim.grid_paintings.size(); ++p) { if(sim.grid_paintings[p].generation == 0) {continue;} tile_x = p%sim.grid_w; tile_y = p/sim.grid_w; x_offset = tile_x * sim.tile_w; y_offset = tile_y * sim.tile_h; // Save current best paiting to the results glViewport(0, 0, sim.tile_w, sim.tile_h); // Redraw glBindFramebuffer(GL_FRAMEBUFFER, highres_fbo); glClearColor(sim.grid_paintings[p].r, sim.grid_paintings[p].g, sim.grid_paintings[p].b, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Bind the current vao glBindVertexArray(vao); // Vertices glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, 2 * 3 * sim.grid_paintings[p].num_triangles * sizeof(GLfloat), &sim.grid_paintings[p].positions[0], GL_STATIC_DRAW); // Colours glBindBuffer(GL_ARRAY_BUFFER, cbo); glBufferData(GL_ARRAY_BUFFER, 3 * 3 * sim.grid_paintings[p].num_triangles * sizeof(GLfloat), &sim.grid_paintings[p].colours[0], GL_STATIC_DRAW); // Texture coords glBindBuffer(GL_ARRAY_BUFFER, uvbo); glBufferData(GL_ARRAY_BUFFER, 2 * 3 * sim.grid_paintings[p].num_triangles * sizeof(GLfloat), &sim.grid_paintings[p].uvs[0], GL_STATIC_DRAW); glUseProgram(shader_program); // Set the uniforms glm::mat4 view = glm::ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glUniformMatrix4fv(0, 1, GL_FALSE, glm::value_ptr(view)); // Draw glDrawArrays(GL_TRIANGLES, 0, 3*sim.grid_paintings[p].num_triangles); // Get tile data glBindTexture(GL_TEXTURE_2D, highres_texture_id); glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, data); // Write tile to result texture glBindTexture(GL_TEXTURE_2D, sim.result_id); glTexSubImage2D(GL_TEXTURE_2D, 0, x_offset, y_offset, sim.tile_w, sim.tile_h, GL_RGB, GL_UNSIGNED_BYTE, data); } glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, settings.w, settings.h); } // If we've never been at this grid position before, we should start with some random paintings // If we have been here before, just copy the painting in storage if(sim.grid_paintings[sim.grid_pos].generation == 0) { for(unsigned int p = 0; p < sim.paintings.size(); ++p) { painting_randomise(&sim.paintings[p]); } } else { for(unsigned int p = 0; p < sim.paintings.size(); ++p) { painting_copy(&sim.paintings[p], &sim.grid_paintings[sim.grid_pos]); } } tile_x = sim.grid_pos%sim.grid_w; tile_y = sim.grid_pos/sim.grid_w; x_offset = tile_x * sim.tile_w; y_offset = tile_y * sim.tile_h; for(int y = 0; y < sim.tile_h; ++y) { for(int x = 0; x < sim.tile_w; ++x) { data[3*y*sim.tile_w + 3*x + 0] = sim.target.data[3*(y + y_offset) *sim.target.w + 3*(x + x_offset) + 0]; data[3*y*sim.tile_w + 3*x + 1] = sim.target.data[3*(y + y_offset) *sim.target.w + 3*(x + x_offset) + 1]; data[3*y*sim.tile_w + 3*x + 2] = sim.target.data[3*(y + y_offset) *sim.target.w + 3*(x + x_offset) + 2]; } } glBindTexture(GL_TEXTURE_2D, texture_slice); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sim.tile_w, sim.tile_h, GL_RGB, GL_UNSIGNED_BYTE, data); std::cout << std::endl; std::cout << "Pos: " << tile_x << " " << tile_y << std::endl; std::cout << "Score: " << sim.grid_paintings[sim.grid_pos].score << std::endl; std::cout << "Rate: " << sim.grid_paintings[sim.grid_pos].score_rate << std::endl; std::cout << "Gen: " << sim.grid_paintings[sim.grid_pos].generation << std::endl; std::cout << std::endl; last_grid_pos = sim.grid_pos; } // Draw paintings glViewport(0, 0, sim.tile_w, sim.tile_h); for(unsigned int p = 0; p < sim.paintings.size(); ++p) { glBindFramebuffer(GL_FRAMEBUFFER, sim.paintings[p].fbo); glClearColor(sim.paintings[p].r, sim.paintings[p].g, sim.paintings[p].b, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // bind the current vao glBindVertexArray(vao); // Vertices glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, 2 * 3 * sim.paintings[p].num_triangles * sizeof(GLfloat), &sim.paintings[p].positions[0], GL_STATIC_DRAW); // Colours glBindBuffer(GL_ARRAY_BUFFER, cbo); glBufferData(GL_ARRAY_BUFFER, 3 * 3 * sim.paintings[p].num_triangles * sizeof(GLfloat), &sim.paintings[p].colours[0], GL_STATIC_DRAW); // Texture coords glBindBuffer(GL_ARRAY_BUFFER, uvbo); glBufferData(GL_ARRAY_BUFFER, 2 * 3 * sim.paintings[p].num_triangles * sizeof(GLfloat), &sim.paintings[p].uvs[0], GL_STATIC_DRAW); glUseProgram(shader_program); // Set the uniforms glm::mat4 view = glm::ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glUniformMatrix4fv(0, 1, GL_FALSE, glm::value_ptr(view)); // Draw glDrawArrays(GL_TRIANGLES, 0, 3*sim.paintings[p].num_triangles); } glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, settings.w, settings.h); // Comparisons for(unsigned int p = 0; p < sim.paintings.size(); ++p) { glUseProgram(similarity_program); glActiveTexture(GL_TEXTURE0 + texture_slice); glBindTexture(GL_TEXTURE_2D, texture_slice); glActiveTexture(GL_TEXTURE0 + sim.paintings[p].texture_id); glBindTexture(GL_TEXTURE_2D, sim.paintings[p].texture_id); // Setup uniforms glUniform1i(0, texture_slice); glUniform1i(1, sim.paintings[p].texture_id); glUniform1i(2, p); // Compute int groups_x = sim.tile_w/16; int groups_y = sim.tile_h/16; glDispatchCompute(groups_x, groups_y, 1); // Get scores glBindBuffer(GL_SHADER_STORAGE_BUFFER, scores_bo); glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, num_workgroups*sizeof(scores[0]), &scores[0]); // Calculate similarity percentage sim.paintings[p].score = 0.0; for(int n = 0; n < num_workgroups; ++n) { sim.paintings[p].score += scores[n]; } sim.paintings[p].score = 1.0 - sim.paintings[p].score/(3*sim.tile_w*sim.tile_h); } glActiveTexture(GL_TEXTURE0); // Find best painting parent1_id = sim.paintings.size(); parent1_score = FLT_MIN; parent2_id = sim.paintings.size(); parent2_score = FLT_MIN; for(unsigned int p = 0; p < sim.paintings.size(); ++p) { if(sim.paintings[p].score >= parent1_score) { parent2_id = parent1_id; parent2_score = parent1_score; parent1_id = p; parent1_score = sim.paintings[p].score; continue; } if(sim.paintings[p].score >= parent2_score) { parent2_id = p; parent2_score = sim.paintings[p].score; } } assert(parent1_id != sim.paintings.size()); assert(parent2_id != sim.paintings.size()); assert(parent1_id != parent2_id); assert(parent1_score >= 0.0); assert(parent2_score >= 0.0); // To be used when a screenshot is taken settings.best_painting = sim.result_id; // Create new sim.paintings from best for(unsigned int p = 0; p < sim.paintings.size(); ++p) { if(p == parent1_id || p == parent2_id) {continue;} paintings_breed(&sim.paintings[p], &sim.paintings[parent1_id], &sim.paintings[parent2_id]); } // Mutate sim.paintings for(unsigned int p = 0; p < sim.paintings.size(); ++p) { if(p == parent1_id || p == parent2_id) {continue;} painting_jiggle(&sim.paintings[p]); } // Print scores occasionally if(sim.grid_paintings[sim.grid_pos].generation%250 == 0) { //std::cout << "Gen " << sim.grid_paintings[sim.grid_pos].generation << ": " << parent1_id << " - " << sim.grid_paintings[sim.grid_pos].score*100.0 << "% " << std::endl; //std::cout << "Gen " << sim.grid_paintings[sim.grid_pos].generation << ": " << parent1_id << " - " << sim.grid_paintings[sim.grid_pos].score*100.0 << "% " << sim.grid_paintings[sim.grid_pos].score_rate << std::endl; } // Save best painting if it's an improvement if(sim.paintings[parent1_id].score > sim.grid_paintings[sim.grid_pos].score) { int temp = sim.grid_paintings[sim.grid_pos].generation; painting_copy(&sim.grid_paintings[sim.grid_pos], &sim.paintings[parent1_id]); sim.grid_paintings[sim.grid_pos].generation = temp; } // Count generations sim.grid_paintings[sim.grid_pos].generation++; if(settings.mode == MODE_AUTO) { auto_generation++; } } // Render target and best sim.paintings glBindFramebuffer(GL_FRAMEBUFFER, 0); glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindVertexArray(vao); glUseProgram(shader_program); glm::mat4 view = glm::ortho(-1.0, 1.0, 0.0, 1.0, -1.0, 1.0); glUniformMatrix4fv(0, 1, GL_FALSE, glm::value_ptr(view)); // Render target to the screen glBindTexture(GL_TEXTURE_2D, sim.target_id); GLfloat positions[8] = {-1.0, 0.0, -1.0, 1.0, 0.0, 1.0, 0.0, 0.0}; GLfloat colours[12] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; GLfloat uvs[8] = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0}; glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), &positions, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, cbo); glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), &colours, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, uvbo); glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), &uvs, GL_STATIC_DRAW); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); if(settings.tiled_view == true) { unsigned int x_num = ceil(sqrt(sim.paintings.size())); unsigned int y_num = ceil(sqrt(sim.paintings.size())); if(x_num*(y_num-1) >= sim.paintings.size()) { y_num -= 1; } float width = 1.0/x_num; float height = 1.0/y_num; for(unsigned int y = 0; y < y_num; ++y) { for(unsigned int x = 0; x < x_num; ++x) { if(y*x_num + x >= sim.paintings.size()) {break;} glBindTexture(GL_TEXTURE_2D, sim.paintings[y*y_num + x].texture_id); positions[0] = x*width; positions[1] = y*height; positions[2] = x*width; positions[3] = (y+1)*height; positions[4] = (x+1)*width; positions[5] = (y+1)*height; positions[6] = (x+1)*width; positions[7] = y*height; glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), &positions, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, cbo); glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), &colours, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, uvbo); glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), &uvs, GL_STATIC_DRAW); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } } } else { // Draw the results texture glBindTexture(GL_TEXTURE_2D, sim.result_id); positions[0] = 0.0; positions[1] = 0.0; positions[2] = 0.0; positions[3] = 1.0; positions[4] = 1.0; positions[5] = 1.0; positions[6] = 1.0; positions[7] = 0.0; glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), &positions, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, cbo); glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), &colours, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, uvbo); glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), &uvs, GL_STATIC_DRAW); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); // Draw current best over the top of the results texture glBindTexture(GL_TEXTURE_2D, sim.paintings[parent1_id].texture_id); float x_gap = 1.0/sim.grid_w; float y_gap = 1.0/sim.grid_h; positions[0] = x_gap*tile_x; positions[1] = y_gap*tile_y; positions[2] = x_gap*tile_x; positions[3] = y_gap*(tile_y+1); positions[4] = x_gap*(tile_x+1); positions[5] = y_gap*(tile_y+1); positions[6] = x_gap*(tile_x+1); positions[7] = y_gap*tile_y; glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), &positions, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, cbo); glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), &colours, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, uvbo); glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), &uvs, GL_STATIC_DRAW); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } glfwSwapBuffers(window); // check for errors GLenum error = glGetError(); if(error != GL_NO_ERROR) { std::cout << "Error: " << error << std::endl; } // FPS glEndQuery(GL_TIME_ELAPSED); GLuint64 result; glGetQueryObjectui64v(query, GL_QUERY_RESULT, &result); std::stringstream tmp; tmp << "Painting Evolution: " << int(1e9/result) << " FPS"; glfwSetWindowTitle(window, tmp.str().c_str()); glfwPollEvents(); } delete[] data; // delete the created objects glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &scores_bo); glDetachShader(shader_program, vertex_shader); glDetachShader(shader_program, fragment_shader); glDeleteShader(vertex_shader); glDeleteShader(fragment_shader); glDeleteProgram(shader_program); glDetachShader(similarity_program, compute_shader); glDeleteShader(compute_shader); glDeleteProgram(similarity_program); glfwDestroyWindow(window); glfwTerminate(); return 0; }
void VBO::copyData(void *ptr) const { glBindBuffer(GL_ARRAY_BUFFER, *m_id); glGetBufferSubData(GL_ARRAY_BUFFER, 0, m_nbElts * m_data_size * sizeof(float), ptr); }
enum piglit_result piglit_display(void) { float green[4] = {0.0, 1.0, 0.0, 1.0}; float red[4] = {1.0, 0.0, 0.0, 1.0}; bool pass = true; int i, j; glEnable(GL_DEPTH_TEST); glViewport(0, 0, piglit_width, piglit_height); sample_mask = (GLint*) malloc (sizeof(GLint) * (piglit_width * piglit_height)); for (i = 0; i < piglit_width * piglit_height; i++) { sample_mask[i] = 0; } glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLint) * (piglit_width * piglit_height), &sample_mask[0], GL_DYNAMIC_COPY); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbo); glClearColor(0.0, 0.0, 0.0, 1.0); glEnable(GL_DEPTH_TEST); glEnable(GL_STENCIL_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glUseProgram(prog1); glStencilFunc(GL_ALWAYS, 1, 0xFF); glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); glDrawArrays(GL_TRIANGLES, 0, 6); glUseProgram(prog2); glStencilFunc(GL_NOTEQUAL, 1, 0xFF); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); glUniform1i(2, piglit_width); glDrawArrays(GL_TRIANGLES, 6, 6); glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(GLint) * piglit_width * piglit_height, &sample_mask[0]); for (i = 0; i < piglit_width; i++) { for (j = 0; j < piglit_height; j++) { if (i >= piglit_width / 2) { if (sample_mask[piglit_width * j + i] != 1) { pass = false; break; } } else { if (sample_mask[piglit_width * j + i] != 0) { pass = false; break; } } } } pass = piglit_probe_rect_rgba(0, 0, piglit_width / 2, piglit_height, green) && pass; pass = piglit_probe_rect_rgba(piglit_width / 2, 0, piglit_width / 2, piglit_height, red) && pass; piglit_present_results(); pass = piglit_check_gl_error(GL_NO_ERROR) && pass; free(sample_mask); return pass ? PIGLIT_PASS : PIGLIT_FAIL; }
//void GetBufferSubData(enum target, intptr offset, sizeiptr size, void *data); void VertexBufferObject::GetSubData(VertexBufferObject::Target target, GLintptr offset, GLsizeiptr size, void* data) { glGetBufferSubData(target, offset, size, data); }
void OsdPtexMeshData::updateGeometry(const MHWRender::MVertexBuffer *points, const MHWRender::MVertexBuffer *normals) { // Update coarse vertex int nCoarsePoints = _pointArray.length(); GLuint mayaPositionVBO = *static_cast<GLuint*>(points->resourceHandle()); GLuint mayaNormalVBO = normals ? *static_cast<GLuint*>(normals->resourceHandle()) : NULL; int size = nCoarsePoints * 3 * sizeof(float); OpenSubdiv::FarKernelBatchVector const &batches = _farmesh->GetKernelBatches(); if (_kernel == kCPU || _kernel == kOPENMP) { float *d_pos = _cpuPositionBuffer->BindCpuBuffer(); glBindBuffer(GL_ARRAY_BUFFER, mayaPositionVBO); glGetBufferSubData(GL_ARRAY_BUFFER, 0, size, d_pos); g_cpuComputeController->Refine(_cpuComputeContext, batches, _cpuPositionBuffer); if (not _adaptive) { d_pos = _cpuNormalBuffer->BindCpuBuffer(); glBindBuffer(GL_ARRAY_BUFFER, mayaNormalVBO); glGetBufferSubData(GL_ARRAY_BUFFER, 0, size, d_pos); g_cpuComputeController->Refine(_cpuComputeContext, batches, _cpuNormalBuffer); } glBindBuffer(GL_ARRAY_BUFFER, 0); #ifdef OPENSUBDIV_HAS_CUDA } else if (_kernel == kCUDA) { glBindBuffer(GL_COPY_READ_BUFFER, mayaPositionVBO); glBindBuffer(GL_COPY_WRITE_BUFFER, _cudaPositionBuffer->BindVBO()); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); g_cudaComputeController->Refine(_cudaComputeContext, batches, _cudaPositionBuffer); if (not _adaptive) { glBindBuffer(GL_COPY_READ_BUFFER, mayaNormalVBO); glBindBuffer(GL_COPY_WRITE_BUFFER, _cudaNormalBuffer->BindVBO()); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); g_cudaComputeController->Refine(_cudaComputeContext, batches, _cudaNormalBuffer); } glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); #endif #ifdef OPENSUBDIV_HAS_OPENCL } else if (_kernel == kCL) { glBindBuffer(GL_COPY_READ_BUFFER, mayaPositionVBO); glBindBuffer(GL_COPY_WRITE_BUFFER, _clPositionBuffer->BindVBO()); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); g_clComputeController->Refine(_clComputeContext, batches, _clPositionBuffer); if (not _adaptive) { glBindBuffer(GL_COPY_READ_BUFFER, mayaNormalVBO); glBindBuffer(GL_COPY_WRITE_BUFFER, _clNormalBuffer->BindVBO()); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); g_clComputeController->Refine(_clComputeContext, batches, _clNormalBuffer); } glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); #endif } _needsUpdate = false; }
fm::Result Buffer::cpyData(void *ptr,fm::Size beg,fm::Size len) { bind(); return glCheck(glGetBufferSubData(m_type,beg,len,ptr)); }
void Buffer::readGL(void* data)throw(BufferException) { glGetBufferSubData(mGlBufferTargetEnum,0,mBufferInfo->bufferSizeInByte,data); }
void VBO::GetBufferSubData(GLintptr offset, GLsizeiptr size, GLvoid* data) { GL_C(glGetBufferSubData(m_target, offset, size, data)); }
//called every frame this functions draw void Draw(void) { int now = glutGet(GLUT_ELAPSED_TIME); // Rotation float rotation = now*0.001; ////////////////////////////////////////////////////////////////////////// gloost::Matrix cameraTransform; cameraTransform.setIdentity(); //cameraTransform.setRotateX(-0.38); cameraTransform.setTranslate(0.0,0.0,10.0); cameraTransform.invert(); //reset the modelmatrix ModelViewMatrixStack.clear(); ModelViewMatrixStack.loadMatrix(cameraTransform); gloost::Matrix normalMatrix; //Schleife für 8 Objekte for(int i=0; i<11; ++i) { //save the current transformation onto the MatrixStack ModelViewMatrixStack.push(); switch (i) { case(0): //Sonne - no transformation //ModelViewMatrixStack.rotate(rotation,0.0,1.0,0.0); //ModelViewMatrixStack.scale(8); break; case(1): //Merkur ModelViewMatrixStack.rotate(rotation*2,0.0,1.0,0.0); ModelViewMatrixStack.translate(1.0,0.0,0.0); ModelViewMatrixStack.scale(0.45); break; case(2): //Venus ModelViewMatrixStack.rotate(rotation*3,0.0,1.0,0.0); ModelViewMatrixStack.translate(2.15,0.0,0.0); ModelViewMatrixStack.scale(0.4); break; case(3): //Erde ModelViewMatrixStack.rotate(-rotation*2.3,0.0,1.0,0.0); ModelViewMatrixStack.translate(-3.1,0.0,0.0); ModelViewMatrixStack.scale(0.4); //um bumpmapping zu prüfen, nur diese transformationen für die erde zulassen: /*ModelViewMatrixStack.translate(0.0,0.0,8.5); ModelViewMatrixStack.rotate(-rotation*0.1,0.0,1.0,0.0); ModelViewMatrixStack.scale(1);*/ break; case(4): //Erde-mond ModelViewMatrixStack.rotate(-rotation*2.3,0.0,1.0,0.0); ModelViewMatrixStack.translate(-3.1,0.0,0.0); ModelViewMatrixStack.rotate(-rotation*7,0.0,1.0,0.0); ModelViewMatrixStack.translate(0.45,0.0,0.0); ModelViewMatrixStack.scale(0.2); break; case(5): //Mars ModelViewMatrixStack.rotate(rotation*1.5,0.0,1.0,0.0); ModelViewMatrixStack.translate(4.3,0.0,0.0); ModelViewMatrixStack.scale(0.6); break; case(6): //Jupiter ModelViewMatrixStack.rotate(-rotation*0.5,0.0,1.0,0.0); ModelViewMatrixStack.translate(5.8,0.0,0.0); ModelViewMatrixStack.scale(0.8); break; case(7): //Saturn ModelViewMatrixStack.rotate(-rotation*0.2,0.0,1.0,0.0); ModelViewMatrixStack.translate(7.3,0.0,0.0); ModelViewMatrixStack.scale(0.5); break; case(8): //Jupiter-Titan ModelViewMatrixStack.rotate(-rotation*0.5,0.0,1.0,0.0); ModelViewMatrixStack.translate(5.8,0.0,0.0); ModelViewMatrixStack.rotate(rotation*2.33,0.0,1.0,0.0); ModelViewMatrixStack.translate(0.6,0.0,0.0); ModelViewMatrixStack.scale(0.2); break; case(9): //Saturn-ring ModelViewMatrixStack.rotate(-rotation*0.2,0.0,1.0,0.0); ModelViewMatrixStack.translate(7.3,0.0,0.0); ModelViewMatrixStack.rotate(0.4,1.0,0.0,0.0); ModelViewMatrixStack.scale(1.5,0.01,1.5); break; case(10): //Komet ModelViewMatrixStack.rotate(rotation*0.2*cometSpeedMul,0.0,1.0,0.0); /*ModelViewMatrixStack.translate(8.0,0.0,0.0); ModelViewMatrixStack.scale(0.2);*/ ModelViewMatrixStack.translate(0.0,0.0,8.5); //ModelViewMatrixStack.rotate(-rotation*0.1,0.0,1.0,0.0); ModelViewMatrixStack.scale(0.2); break; default: break; } { if(shadingModel == 0) { glUseProgram(ShaderIds[0]); } else { glUseProgram(ShaderIds[3]); } // transfer ModelViewMatrix for Geometry 1 to Shaders glUniformMatrix4fv(ModelViewMatrixUniformLocation[shadingModel], 1, GL_FALSE, ModelViewMatrixStack.top().data()); //set the NormalMatrix for Geometry 1 normalMatrix = ModelViewMatrixStack.top(); normalMatrix.invert(); normalMatrix.transpose(); // transfer NormalMatrix for Geometry 1 to Shaders glUniformMatrix4fv(NormalMatrixUniformLocation[shadingModel], 1, GL_FALSE, normalMatrix.data()); glUniformMatrix4fv(ProjectionMatrixUniformLocation[shadingModel], 1, GL_FALSE, ProjectionMatrix.data()); //bind the Geometry glBindVertexArray(BufferIds[0]); //texuren aktivieren glEnable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE0); //location bekannt machen glUniform1i(textureUniformLocation[shadingModel],0); //textur binden glBindTexture(GL_TEXTURE_2D,textures[i]); //Planet3 (erde) hat normalmap if(i == 3) { glActiveTexture(GL_TEXTURE1); glUniform1i(bumpmapUniformLocation,1); glBindTexture(GL_TEXTURE_2D,textures[12]); } else { glActiveTexture(GL_TEXTURE1); glUniform1i(bumpmapUniformLocation,1); glBindTexture(GL_TEXTURE_2D,0); } // draw Geometry 1 //if(i != 10)//nur um partikel im kometen zu sehen glDrawElements(GL_TRIANGLES, mesh->getTriangles().size()*3, GL_UNSIGNED_INT, 0); } //##################particles##################### if(i == 10) { //nur beim Kometen zeichnen glUseProgram(ShaderIds[6]); //activate the particle shader Programm //deklarate the uniform data glUniformMatrix4fv(ModelViewMatrixUniformLocation[2],1,GL_FALSE,ModelViewMatrixStack.top().data()); glUniformMatrix4fv(ProjectionMatrixUniformLocation[2],1,GL_FALSE,ProjectionMatrix.data()); glUniform4f(particleColUniformLocation,particleCol.r,particleCol.g,particleCol.b,particleCol.a); glUniform1i(particleLifetimeUniformLocation,particleLifetime); glBindVertexArray(VertexArrayIds[0]); //bind the VAO //****FeedbackBuffer befüllen start**** glEnable(GL_RASTERIZER_DISCARD); glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, VertexArrayIds[2]); //Feedbackbuffer ist VertexArrayIds[2] glBeginTransformFeedback(GL_POINTS); glDrawArrays(GL_POINTS, 0, particleCount); glEndTransformFeedback(); glFlush(); glDisable(GL_RASTERIZER_DISCARD); ///****FeedbackBuffer befüllen ende**** Particle shaderFeedback[maxParticleCount]; //Feedback Array anlegen glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(shaderFeedback), shaderFeedback); //feedback data in das array legen //std::cout<<shaderFeedback[1].x<<" "<<shaderFeedback[1].y<<" "<<shaderFeedback[1].z<<" "<<shaderFeedback[1].w<<" "<<shaderFeedback[1].age<<std::endl; glBindVertexArray(VertexArrayIds[1]); //DataBuffer binden glBufferData(GL_ARRAY_BUFFER, sizeof(shaderFeedback), shaderFeedback, GL_STATIC_DRAW); //DataBuffer mit Feedback Daten befüllen glEnable(GL_BLEND); //Blending aktivieren glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Blending Funktion setzen glDrawArrays(GL_POINTS, 0, particleCount); //Particles Zeichnen glDisable(GL_BLEND); //Blending deaktivieren } //########################################################### //load last transformation from stack ModelViewMatrixStack.pop(); } glDisable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); glUseProgram(ShaderIds[3]); glBindVertexArray(BufferIds[0]); //skybox ModelViewMatrixStack.push(); ModelViewMatrixStack.scale(100); glUniformMatrix4fv(ModelViewMatrixUniformLocation[shadingModel], 1, GL_FALSE, ModelViewMatrixStack.top().data()); //set the NormalMatrix for Geometry 1 normalMatrix = ModelViewMatrixStack.top(); normalMatrix.invert(); normalMatrix.transpose(); // transfer NormalMatrix for Geometry 1 to Shaders glUniformMatrix4fv(NormalMatrixUniformLocation[shadingModel], 1, GL_FALSE, normalMatrix.data()); glUniformMatrix4fv(ProjectionMatrixUniformLocation[shadingModel], 1, GL_FALSE, ProjectionMatrix.data()); //bind the Geometry glBindVertexArray(BufferIds[1]); glEnable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE0); glUniform1i(textureUniformLocation[shadingModel],0); glBindTexture(GL_TEXTURE_2D,textures[11]); // draw skybox glDrawElements(GL_TRIANGLES,mesh2->getTriangles().size()*3, GL_UNSIGNED_INT, 0); ModelViewMatrixStack.pop(); //the next transformation will be relative to bunny 1 glBindVertexArray(0); glUseProgram(0); }
void Buffer::get(size_t offset, size_t size, void* data) const { BufferBindguard guard(GL_COPY_READ_BUFFER, *this); glGetBufferSubData(GL_COPY_READ_BUFFER, offset, size, data); }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL15C_nglGetBufferSubData__IJJJ(JNIEnv *__env, jclass clazz, jint target, jlong offset, jlong size, jlong dataAddress) { glGetBufferSubDataPROC glGetBufferSubData = (glGetBufferSubDataPROC)tlsGetFunction(439); intptr_t data = (intptr_t)dataAddress; UNUSED_PARAM(clazz) glGetBufferSubData(target, (intptr_t)offset, (intptr_t)size, data); }
ross_internal void render(Input input, Input last_input, GLuint shader_program, ProgramBuffers program_buffers) { ross_persist u64 vertex_count = 0; glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shader_program); if(input.mouse.left) { GLfloat vertex[2] = { 0.0f, 0.0f }; GLdouble mouse_obj_x; GLdouble mouse_obj_y; GLdouble mouse_obj_z; GLdouble model[16]; GLdouble proj[16]; GLint view[4]; glGetDoublev(GL_MODELVIEW_MATRIX, model); glGetDoublev(GL_PROJECTION_MATRIX, proj); glGetIntegerv(GL_VIEWPORT, view); GLdouble win_x, win_y, win_z; win_x = (r64)input.mouse.x; win_y = (r64)view[3] - (r64)input.mouse.y; glReadPixels(win_x, win_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &win_z); gluUnProject(win_x, win_y, win_z, model, proj, view, &mouse_obj_x, &mouse_obj_y, &mouse_obj_z); #if 0 GLint buffer_size; glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &buffer_size); GLfloat *buffer = (GLfloat *)ross_alloc(buffer_size * sizeof(GLfloat)); glGetBufferSubData(GL_ARRAY_BUFFER, 0, buffer_size, buffer); b8 repeated_data = 0; for(u64 i = 0; i < buffer_size; ++i) { if(buffer[i] == mouse_obj_x && buffer[i+1] == mouse_obj_y) { printf("%lu\n", i); repeated_data = 1; break; } } ross_free(buffer); if(!repeated_data) { vertex[0] = mouse_obj_x; vertex[1] = mouse_obj_y; vertex_count++; glBindBuffer(GL_ARRAY_BUFFER, program_buffers.object_buffer); glBufferSubData(GL_ARRAY_BUFFER, vertex_count * sizeof(vertex), sizeof(vertex), vertex); //printf("mousex: %.02f, mousey: %.02f \n", vertices[0], vertices[1]); } #endif vertex[0] = mouse_obj_x; vertex[1] = mouse_obj_y; glBindBuffer(GL_ARRAY_BUFFER, program_buffers.object_buffer); glBufferSubData(GL_ARRAY_BUFFER, vertex_count * sizeof(vertex), sizeof(vertex), vertex); vertex_count++; } //printf("%lu\n", vertex_count); glPointSize(10); GLint position_loc = glGetAttribLocation(shader_program, "vs_position"); glEnableVertexAttribArray(position_loc); glVertexAttribPointer(position_loc, 2, GL_FLOAT, GL_FALSE, 0, 0); check_gl_errors(); glDrawArrays(GL_POINTS, 0, vertex_count); glDisableVertexAttribArray(position_loc); }
void operator() (T data[]) { glBindBuffer (GL_ARRAY_BUFFER, buffer_id); glGetBufferSubData (GL_ARRAY_BUFFER, 0, buffer_size, data); glBindBuffer (GL_ARRAY_BUFFER, 0); }
static void fill_buffer_with_table(lua_State* L, bufferobject* b, int idx, int offset) { int count = lua_objlen(L, idx); void* data = NULL; #define _FILL(type) \ b->element_size = sizeof(type); \ data = malloc(count * sizeof(type)); \ if (NULL == data) \ luaL_error(L, "Out of memory"); \ for (int i = 1; i <= count; ++i) { \ lua_rawgeti(L, idx, i); \ lua_Number v = lua_tonumber(L, -1); \ ((type*)data)[i-1] = (type)v; \ } \ lua_pop(L, count) switch (b->element_type) { case GL_BYTE: _FILL(GLbyte); break; case GL_UNSIGNED_BYTE: _FILL(GLubyte); break; case GL_SHORT: _FILL(GLshort); break; case GL_UNSIGNED_SHORT: _FILL(GLushort); break; case GL_INT: _FILL(GLint); break; case GL_UNSIGNED_INT: _FILL(GLuint); break; case GL_FLOAT: _FILL(GLfloat); break; case GL_DOUBLE: _FILL(GLdouble); break; default: luaL_error(L, "Invalid data type"); }; #undef _FILL int size_old = b->count * b->element_size; int size_new = count * b->element_size; offset = b->element_size; glBindBuffer(b->target, b->id); if (size_old >= size_new + offset) { // reuse old buffer glBufferSubData(b->target, offset, size_new, data); } else if (b->count == 0) { // this is a new buffer glBufferData(b->target, size_new, data, b->usage); b->count = count; } else { // new data extends buffer. bollux. void* new_data = malloc(size_new + offset); if (NULL == new_data) { free(data); luaL_error(L, "Out of memory"); } // merge unchanged with new part void* old_data = NULL; glGetBufferSubData(b->target, 0, offset, old_data); memcpy(new_data, old_data, offset); memcpy((void*)((char*)new_data + offset), data, size_new); glBufferData(b->target, size_new + offset, new_data, b->usage); free(new_data); b->count = count; } free(data); }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL15_nglGetBufferSubData(JNIEnv *env, jclass clazz, jint target, jlong offset, jlong size, jobject data, jint data_position, jlong function_pointer) { GLvoid *data_address = ((GLvoid *)(((char *)(*env)->GetDirectBufferAddress(env, data)) + data_position)); glGetBufferSubDataPROC glGetBufferSubData = (glGetBufferSubDataPROC)((intptr_t)function_pointer); glGetBufferSubData(target, offset, size, data_address); }