NE_API void load_texture_asset(texture_asset* texture) { texture->resource_handle = create_texture(); if (texture->pixels) { load_texture_from_pixels(texture, texture->pixels, texture->size.x, texture->size.y); } else { load_texture_from_file(texture); } }
int main(int arg_count, const char** arguments) { // const std::vector<const std::string* const> auto return_arguments = handle_command_line_arguments(arg_count, arguments); // Set up and tear down automatically done. glfw_helper glfw(GL_FALSE); GLFWwindow* window = glfw.create_window(WIDTH, HEIGHT, "OpenGl Example"); glfw.set_window_context(window); std::thread* server_thread = new std::thread([] () { ev9::server<false>* server = new ev9::server<false>([] (std::vector<char>& input, std::vector<char>& output) { std::lock_guard<std::mutex> lock(g_lock); g_ring_buffer.push(new std::string(input.begin(), input.end())); }); server->start(); }); add_call_backs(glfw, window); set_up_glew(WIDTH, HEIGHT); //generate texture GLuint texture = load_texture_from_file((*return_arguments)[0]->c_str()); //TODO - find a better way to reference these files paths. ev10::eIIe::shader shader((*return_arguments)[1]->c_str(), (*return_arguments)[2]->c_str()); GLuint shader_program = shader.get_program(); GLuint VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindVertexArray(VAO); // 2. Copy our vertices array in a vertex buffer for OpenGL to use glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // 3. Copy our index array in a element buffer for OpenGL to use glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vert_indices), vert_indices, GL_STATIC_DRAW); // 3. Then set the vertex attributes pointers glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat))); glEnableVertexAttribArray(2); // 4. Unbind VAO (NOT the EBO) glBindVertexArray(0); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); // Background color glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); shader.use_program(); std::string* direction = nullptr; { std::lock_guard<std::mutex> lock(g_lock); if (!g_ring_buffer.empty()) { direction = g_ring_buffer.pop(); } } if (direction) { if ((*direction)[0] == '-') { g_keys[253] = true; } else { g_keys[252] = true; } } float delta = 0.0; if (direction) { std::stringstream parse_string(*direction); parse_string >> delta; std::cout << "delta: " << std::to_string(delta) << "\r" << std::flush; } // Up if (g_keys[GLFW_KEY_UP]) { g_location[1] -= DELTA; glm::mat4 transform; transform = glm::translate(transform, glm::vec3(g_location[0], g_location[1], 0.0f)); GLint transform_location = glGetUniformLocation(shader.get_program(), "transform"); glUniformMatrix4fv(transform_location, 1, GL_FALSE, glm::value_ptr(transform)); g_keys[GLFW_KEY_UP] = false; } // Down if (g_keys[GLFW_KEY_DOWN]) { g_location[1] -= DELTA; glm::mat4 transform; transform = glm::translate(transform, glm::vec3(g_location[0], g_location[1], 0.0f)); GLint transform_location = glGetUniformLocation(shader.get_program(), "transform"); glUniformMatrix4fv(transform_location, 1, GL_FALSE, glm::value_ptr(transform)); g_keys[GLFW_KEY_DOWN] = false; } // Right if (g_keys[252]) { g_location[0] += delta; glm::mat4 transform; transform = glm::translate(transform, glm::vec3(g_location[0], g_location[1], 0.0f)); GLint transform_location = glGetUniformLocation(shader.get_program(), "transform"); glUniformMatrix4fv(transform_location, 1, GL_FALSE, glm::value_ptr(transform)); g_keys[252] = false; } // Left if (g_keys[253]) { g_location[0] += delta; glm::mat4 transform; transform = glm::translate(transform, glm::vec3(g_location[0], g_location[1], 0.0f)); GLint transform_location = glGetUniformLocation(shader.get_program(), "transform"); glUniformMatrix4fv(transform_location, 1, GL_FALSE, glm::value_ptr(transform)); g_keys[253] = false; } else { glm::mat4 transform; transform = glm::translate(transform, glm::vec3(g_location[0], g_location[1], 0.0f)); GLint transform_location = glGetUniformLocation(shader.get_program(), "transform"); glUniformMatrix4fv(transform_location, 1, GL_FALSE, glm::value_ptr(transform)); } //check image reset flag (triggered by pressing 'r'?) if (image_should_reset) { g_location[0] = 0.0; glm::mat4 transform; transform = glm::translate(transform, glm::vec3(g_location[0], g_location[1], 0.0f)); GLint transform_location = glGetUniformLocation(shader.get_program(), "transform"); glUniformMatrix4fv(transform_location, 1, GL_FALSE, glm::value_ptr(transform)); image_should_reset = false; } glBindTexture(GL_TEXTURE_2D, texture); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glBindVertexArray(0); glfwSwapBuffers(window); } // Properly de-allocate all resources once they've outlived their purpose glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &EBO); return 0; }
i4_bool r1_opengl_texture_manager_class::immediate_mip_load(r1_mip_load_info * load_info) { if (no_of_textures_loaded > 0) { if (load_info->dest_mip->last_frame_used != -1) { load_info->error = R1_MIP_LOAD_BUSY; return i4_F; } } r1_miplevel_t * mip = load_info->dest_mip; used_node * new_used = make_new_used_node(load_info); if (!new_used) { return i4_F; } new_used->async_fp=0; new_used->mip=0; new_used->data = (w8 *) new w8[(mip->width*mip->height*2) ]; //(w8 *)i4_malloc(mip->width * mip->height * 2,"failure calling i4_malloc"); if (load_info->src_file) { //for opengl, we currently use only this case, since we support only 16 bit color depth //This is for loading data from the tex_cache only, where the data format //is always known. //Unfortunatelly, the above statement is just plain wrong... This //code is also used to load up the lowest miplevel (distant terrain) //texture. Therefore, we convert here if (load_info->flags & R1_MIPFLAGS_SRC32) { w32 co=0,r=0,g=0,b=0; w16 * tex=(w16 *)(new_used->data); const i4_pal * p=i4_pal_man.default_no_alpha_32(); for (int i=0; i<mip->width*mip->height; i++) { //The order here is a bit strange (argb backwards). //I don't know why it should be like this (testing shows it's right on my system, //but it might as well be hardware dependent) b=load_info->src_file->read_8(); g=load_info->src_file->read_8(); r=load_info->src_file->read_8(); load_info->src_file->read_8(); co=p->convert(r<<16|g<<8|b,®ular_format); tex[i]=(w16)co; } } else { load_info->src_file->read(new_used->data,mip->width * mip->height * 2); } } else { load_texture_from_file(load_info->texture_name, mip->entry->id,new_used->data, mip->width, mip->height, mip->width*2, 2, mip->entry->is_transparent(), mip->entry->is_alphatexture()); } mip->vram_handle = new_used; if (mip->last_frame_used != -1) { mip->last_frame_used = frame_count; } new_used->mip = mip; new_used->async_fp = 0; glGenTextures(1,&new_used->gltexname); glBindTexture(GL_TEXTURE_2D, new_used->gltexname); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); teximage2d(new_used); //i4_free(new_used->data); delete [] new_used->data; new_used->data = 0; //bytes_loaded += mip->width * mip->height * 2; //no_of_textures_loaded++; return i4_T; }