void Batch3DGeometry::init(Camera3D* cam) { set_camera(cam); create_vertex_array(); m_mesh_manager = MeshManager::get_instance(); indices.resize(0); input_instances = nullptr; }
void Batch3DGeometry::begin() { m_render_batches.clear(); free(input_instances); m_models.clear(); shaders.clear(); create_vertex_array(); }
std::shared_ptr<VertexArray> RenderContext::create_vertex_array(const std::shared_ptr<geo::Mesh>& pMesh) { auto va = create_vertex_array(); va->bind(); //read all attributes and create vertex buffers auto attribs = pMesh->vertex_attrib_array(); for (u32 i = 0; i < attribs.size(); ++i) { i32 n_comp; ComponentDatatype datatype; switch (attribs[i]->type()) { case VertexAttribType::FLOAT32: n_comp = 1; datatype = ComponentDatatype::FLOAT32; break; case VertexAttribType::FLOAT32_2: n_comp = 2; datatype = ComponentDatatype::FLOAT32; break; case VertexAttribType::FLOAT32_3: n_comp = 3; datatype = ComponentDatatype::FLOAT32; break; case VertexAttribType::FLOAT32_4: n_comp = 4; datatype = ComponentDatatype::FLOAT32; break; default: R_LOG_ERR("Unknown attribute type."); } u32 buffersize = attribs[i]->size_in_bytes(); auto vbo = Device::graphics()->create_vertex_buffer(BufferHint::STATIC, buffersize); vbo->copy_raw_from_sys_mem(attribs[i]->raw_data_ptr(), 0, buffersize); auto& pAttrib = va->create_attrib(i, attribs[i]->name(), vbo, datatype, n_comp); } auto indexsize = pMesh->indices<u32>()->size_in_bytes(); auto ibo = Device::graphics()->create_index_buffer(BufferHint::STATIC, indexsize); ibo->copy_from_sys_mem(pMesh->indices<u32>()->data()); va->set_index_buffer(ibo); return va; }
NE_API drawing_shape_id create_quad_array() { shapes.push_back(gl_drawing_shape()); drawing_shape_id shape_id = (drawing_shape_id) shapes.size() - 1; gl_drawing_shape* quad = &shapes[shape_id]; gl_vertex_array* vertex_array = &quad->vertex_array; create_vertex_array(vertex_array); create_gl_buffer(&vertex_array->index_buffer); create_gl_buffer(&vertex_array->vertex_buffer); bind_vertex_array(vertex_array); set_vertex_attribute(vertex_attribute::position, 4, sizeof(vertex), nullptr); set_vertex_attribute(vertex_attribute::color, 4, sizeof(vertex), (void*) (4 * sizeof(float))); set_vertex_attribute(vertex_attribute::tangent, 3, sizeof(vertex), (void*) (8 * sizeof(float))); set_vertex_attribute(vertex_attribute::normal, 3, sizeof(vertex), (void*) (11 * sizeof(float))); set_vertex_attribute(vertex_attribute::tex_coords, 2, sizeof(vertex), (void*) (14 * sizeof(float))); return (drawing_shape_id) (shapes.size() - 1); }
NE_API drawing_shape_id create_quad() { shapes.push_back(gl_drawing_shape()); drawing_shape_id shape_id = (drawing_shape_id) shapes.size() - 1; gl_drawing_shape* quad = &shapes[shape_id]; gl_vertex_array* vertex_array = &quad->vertex_array; create_vertex_array(vertex_array); quad->vertices.insert(quad->vertices.begin(), 4, vertex()); quad->indices = { 0, 1, 2, 3, 2, 0 }; quad->vertices[0].position = { 0.0f, 0.0f, 0.0f, 1.0f }; quad->vertices[1].position = { 1.0f, 0.0f, 0.0f, 1.0f }; quad->vertices[2].position = { 1.0f, 1.0f, 0.0f, 1.0f }; quad->vertices[3].position = { 0.0f, 1.0f, 0.0f, 1.0f }; quad->vertices[0].color = { 1.0f, 1.0f, 1.0f, 1.0f }; quad->vertices[1].color = { 1.0f, 1.0f, 1.0f, 1.0f }; quad->vertices[2].color = { 1.0f, 1.0f, 1.0f, 1.0f }; quad->vertices[3].color = { 1.0f, 1.0f, 1.0f, 1.0f }; vertex_array->index_count = 6; create_gl_buffer(&vertex_array->index_buffer); set_gl_buffer_memory(&vertex_array->index_buffer, (uint8*) &quad->indices[0], 6 * sizeof(uint32)); upload_gl_buffer_data(&vertex_array->index_buffer); create_gl_buffer(&vertex_array->vertex_buffer); set_gl_buffer_memory(&vertex_array->vertex_buffer, (uint8*) &quad->vertices[0], 4 * sizeof(vertex)); set_quad_tex_coords(shape_id, 0.0f, 0.0f, 1.0f, 1.0f); bind_vertex_array(vertex_array); set_vertex_attribute(vertex_attribute::position, 4, sizeof(vertex), nullptr); set_vertex_attribute(vertex_attribute::color, 4, sizeof(vertex), (void*) (4 * sizeof(float))); set_vertex_attribute(vertex_attribute::tangent, 3, sizeof(vertex), (void*) (8 * sizeof(float))); set_vertex_attribute(vertex_attribute::normal, 3, sizeof(vertex), (void*) (11 * sizeof(float))); set_vertex_attribute(vertex_attribute::tex_coords, 2, sizeof(vertex), (void*) (14 * sizeof(float))); return (drawing_shape_id) (shapes.size() - 1); }
int main() { srand(time(0)); for (int i = 0; i < 3; i++) gColor[i] = random_float(); signal(SIGINT, signal_handler); SDL_Init(SDL_INIT_VIDEO); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); /* Create our window centered at 512x512 resolution */ gWindow = SDL_CreateWindow("Hello Triangle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); SDL_GL_CreateContext(gWindow); SDL_GL_SetSwapInterval(1); /* create a black box context */ if (!bbgl_init(&bbgl)) { fprintf(stderr, "[bbgl] (client) failed to connect to black box\n"); return -1; } glViewport(0, 0, 800, 600); glClearColor(0.0f, 0.0f, 1.0f, 1.0f); //const char *vendor = (const char *)glGetString(GL_VENDOR); //const char *renderer = (const char *)glGetString(GL_RENDERER); //const char *version = (const char *)glGetString(GL_VERSION); //const char *shading = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION); //printf("vendor: %s\n", vendor); //printf("renderer: %s\n", renderer); //printf("version: %s\n", version); //printf("shading: %s\n", shading); create_vertex_array(); create_vertex_buffer(); compile_shaders(); int frames = 0; Uint32 start = SDL_GetTicks(); int dirs[3] = { +1, +1, +1 }; while (gRunning) { ++frames; Uint32 elapsed = SDL_GetTicks() - start; if (elapsed) { double seconds = elapsed / 1000.0; double fps = frames / seconds; printf("\r%g FPS\n", fps); } SDL_Event e; while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: gRunning = 0; break; } } render(); for (int i = 0; i < 3; i++) { int value = ((int)(gColor[i] * 255.0f)+dirs[i]); if (value > 255 || value <= 0) dirs[i] = -dirs[i]; gColor[i] = value/255.0f; } } bbgl_destroy(&bbgl); return 0; }