static int _evas_gl_common_shader_program_binary_save(Evas_GL_Program *p, const char *pname, Eet_File *ef) { void* data = NULL; GLenum format; int length = 0, size = 0; if (!glsym_glGetProgramBinary) return 0; glGetProgramiv(p->prog, GL_PROGRAM_BINARY_LENGTH, &length); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); if (length <= 0) return 0; data = malloc(length); if (!data) return 0; glsym_glGetProgramBinary(p->prog, length, &size, &format, data); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); if (length != size) { free(data); return 0; } if (eet_write(ef, pname, data, length, 1) < 0) { if (data) free(data); return 0; } if (data) free(data); return 1; }
void resize(int w, int h) { glViewport(0, 0, w, h); //projection = glm::perspective(45.f, w / (float)h, 0.1f, 1000.f); projection = glm::perspective((90.f * 3.14159f) / 180.f, w / (float)h, 0.1f, 1000.f); view = set_camera_pos_and_dir(srp::Vec3f(256, 256, 256), (srp::Vec3f(1, 1, 1)).NormalizeSelf()); GLERR(); }
bglContext* eglOpen(Config *cfg){ /* EGL Setup */ static EGLint s_pbufferAttribs[] = { EGL_WIDTH, 0, EGL_HEIGHT, 0, EGL_NONE }; bglContext* context = malloc(sizeof(bglContext)); if ( context ) { static EGLint s_configAttribs[] = { EGL_RED_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_BLUE_SIZE, 5, EGL_ALPHA_SIZE, 0, EGL_DEPTH_SIZE, 16, EGL_STENCIL_SIZE, 0, //EGL_SAMPLE_BUFFERS, 1, //EGL_SAMPLES, 4, EGL_NONE }; static const EGLint ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLint numConfigs; EGLint majorVersion; EGLint minorVersion; /*s_configAttribs[1] = cfg->rBits; s_configAttribs[3] = cfg->gBits; s_configAttribs[5] = cfg->bBits; s_configAttribs[7] = cfg->aBits; s_configAttribs[9] = cfg->depthBits; s_configAttribs[11] = cfg->stencilBits;*/ s_pbufferAttribs[1]= cfg->winW; s_pbufferAttribs[3]= cfg->winH; context->display = eglGetDisplay(EGL_DEFAULT_DISPLAY); EGLERR(); eglBindAPI(EGL_OPENGL_ES_API); eglInitialize(context->display, &majorVersion, &minorVersion); EGLERR(); eglGetConfigs(context->display, NULL, 0, &numConfigs); EGLERR(); eglChooseConfig(context->display, s_configAttribs, &context->config, 1, &numConfigs); EGLERR(); context->context = eglCreateContext(context->display, context->config, NULL, ContextAttribs); EGLERR(); context->surface = eglCreatePbufferSurface(context->display, context->config, s_pbufferAttribs); EGLERR(); eglMakeCurrent(context->display, context->surface, context->surface, context->context); EGLERR(); GLERR(); } return context; }
static int _evas_gl_common_shader_program_source_init(Evas_GL_Program *p, Evas_GL_Program_Source *vert, Evas_GL_Program_Source *frag, const char *name) { GLint ok; p->vert = glCreateShader(GL_VERTEX_SHADER); p->frag = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(p->vert, 1, (const char **)&(vert->src), NULL); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); glCompileShader(p->vert); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); ok = 0; glGetShaderiv(p->vert, GL_COMPILE_STATUS, &ok); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); if (!ok) { gl_compile_link_error(p->vert, "compile vertex shader"); ERR("Abort compile of shader vert (%s): %s", name, vert->src); return 0; } glShaderSource(p->frag, 1, (const char **)&(frag->src), NULL); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); glCompileShader(p->frag); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); ok = 0; glGetShaderiv(p->frag, GL_COMPILE_STATUS, &ok); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); if (!ok) { gl_compile_link_error(p->frag, "compile fragment shader"); ERR("Abort compile of shader frag (%s): %s", name, frag->src); return 0; } p->prog = glCreateProgram(); #ifdef GL_GLES #else if ((glsym_glGetProgramBinary) && (glsym_glProgramParameteri)) glsym_glProgramParameteri(p->prog, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); #endif glAttachShader(p->prog, p->vert); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); glAttachShader(p->prog, p->frag); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); glBindAttribLocation(p->prog, SHAD_VERTEX, "vertex"); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); glBindAttribLocation(p->prog, SHAD_COLOR, "color"); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); glBindAttribLocation(p->prog, SHAD_TEXUV, "tex_coord"); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); glBindAttribLocation(p->prog, SHAD_TEXUV2, "tex_coord2"); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); glBindAttribLocation(p->prog, SHAD_TEXUV3, "tex_coord3"); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); glBindAttribLocation(p->prog, SHAD_TEXM, "tex_coordm"); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); glLinkProgram(p->prog); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); ok = 0; glGetProgramiv(p->prog, GL_LINK_STATUS, &ok); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); if (!ok) { gl_compile_link_error(p->prog, "link fragment and vertex shaders"); ERR("Abort compile of shader frag (%s): %s", name, frag->src); ERR("Abort compile of shader vert (%s): %s", name, vert->src); return 0; } return 1; }
static int _evas_gl_common_shader_program_binary_init(Evas_GL_Program *p, const char *pname, Eet_File *ef) { int res = 0, num = 0, length = 0; int *formats = NULL; void *data = NULL; GLint ok = 0; if (!ef) return res; data = eet_read(ef, pname, &length); if ((!data) || (length <= 0)) goto finish; glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &num); if (num <= 0) goto finish; formats = calloc(num, sizeof(int)); if (!formats) goto finish; glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, formats); if (!formats[0]) goto finish; p->prog = glCreateProgram(); #if 1 // TODO: invalid rendering error occurs when attempting to use a // glProgramBinary. in order to render correctly we should create a dummy // vertex shader. p->vert = glCreateShader(GL_VERTEX_SHADER); glAttachShader(p->prog, p->vert); p->frag = glCreateShader(GL_FRAGMENT_SHADER); glAttachShader(p->prog, p->frag); #endif glsym_glProgramBinary(p->prog, formats[0], data, length); glBindAttribLocation(p->prog, SHAD_VERTEX, "vertex"); glBindAttribLocation(p->prog, SHAD_COLOR, "color"); glBindAttribLocation(p->prog, SHAD_TEXUV, "tex_coord"); glBindAttribLocation(p->prog, SHAD_TEXUV2, "tex_coord2"); glBindAttribLocation(p->prog, SHAD_TEXUV3, "tex_coord3"); glBindAttribLocation(p->prog, SHAD_TEXM, "tex_coordm"); glGetProgramiv(p->prog, GL_LINK_STATUS, &ok); GLERR(__FUNCTION__, __FILE__, __LINE__, ""); if (!ok) { gl_compile_link_error(p->prog, "load a program object"); ERR("Abort load of program (%s)", pname); goto finish; } res = 1; finish: if (formats) free(formats); if (data) free(data); if ((!res) && (p->prog)) { glDeleteProgram(p->prog); p->prog = 0; } return res; }
void display_func(void) { char dispbuf[DISPLAY_BUF_SIZE]; frame += 1; panel_z = 0.5 * ( sin(frame / float(100)) + 1) * root->GetDepth(); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); GLERR(); glm::mat4 real_view; real_view = glm::rotate(view, frame * 3.1415926f / 180.f, glm::vec3(0, 1, 0)); real_view = glm::translate(real_view, glm::vec3(-(root->GetWidth() / 2.f), 0, -(root->GetDepth() / 2.f))); basic->Bind(); GLERR(); basic->Upload("projection_matrix", projection); basic->Upload("view_matrix", real_view); GLERR(); state.SetCurrentShader(basic); // AXIS RENDER axis->Render(state); // DATA RENDER render_time->Enter(); rendered_data->Render(state); render_time->Leave(); // PANEL RENDER textured->Bind(); state.SetCurrentShader(textured); textured->Upload("projection_matrix", projection); glm::mat4 translated = glm::translate(real_view, glm::vec3(0, 0, panel_z)); textured->Upload("view_matrix", translated); GLERR(); set_texture_data(*root, panel_z); face->Render(state); render_time->Finalize(); snprintf(dispbuf, DISPLAY_BUF_SIZE, "Average: %8.4fns|Standard Deviation: %8.4fns", render_time->GetAverage(), render_time->GetStandardDeviation()); srp::ogl::ui::TextDrawBegin(state); srp::ogl::ui::TextDrawColor(0, 1, 0); srp::ogl::ui::TextDrawString(5, 5, dispbuf); snprintf(dispbuf, DISPLAY_BUF_SIZE, "%ldns", render_time->GetMin()); srp::ogl::ui::TextDrawString(render_time_graph->GetX() + render_time_graph->GetWidth(), render_time_graph->GetY(), dispbuf); snprintf(dispbuf, DISPLAY_BUF_SIZE, "%ldns", render_time->GetMax()); srp::ogl::ui::TextDrawString(render_time_graph->GetX() + render_time_graph->GetWidth(), render_time_graph->GetY() + render_time_graph->GetSampleHeight(render_time->GetMax()), dispbuf); { snprintf(dispbuf, DISPLAY_BUF_SIZE, "%8.4fns", render_time->GetAverage()); srp::ogl::ui::TextDrawColor(1, 0, 0); srp::ogl::ui::TextDrawString(render_time_graph->GetX() + render_time_graph->GetWidth(), render_time_graph->GetY() + render_time_graph->GetSampleHeight(render_time->GetAverage()), dispbuf); } srp::ogl::ui::TextDrawEnd(state); render_time_graph->Render(state); // buffer swap window->SwapBuffers(); GLERR(); }
void gl_init() { glClearColor(0.1f, 0.1f, 0.4f, 0.0f); glEnable(GL_DEPTH_TEST); GLERR(); // glEnable(GL_CULL_FACE); basic = srp::ogl::CreateShader("base.vert", "base.frag"); textured = srp::ogl::CreateShader("texture.vert", "texture.frag"); { axis = new srp::ogl::VertexBuffer(GL_LINES, GL_STATIC_DRAW); srp::ogl::Vertex origin; origin.x = origin.y = origin.z = 0; origin.r = origin.g = origin.b = 1; origin.nx = origin.ny = 0; origin.nz = 1; srp::ogl::Vertex plus_x; plus_x.x = root->GetWidth(); plus_x.y = plus_x.z = 0; plus_x.r = plus_x.g = plus_x.b = 1; plus_x.ny = plus_x.nz = 0; plus_x.nx = 1; srp::ogl::Vertex plus_y; plus_y.y = root->GetHeight(); plus_y.x = plus_y.z = 0; plus_y.r = plus_y.g = plus_y.b = 1; plus_x.ny = plus_y.nz = 0; plus_y.ny = 1; srp::ogl::Vertex plus_z; plus_z.z = root->GetDepth(); plus_z.x = plus_z.y = 0; plus_z.r = plus_z.g = plus_z.b = 1; plus_z.ny = plus_z.nx = 0; plus_z.nz = 1; srp::ogl::Vertex center; center.x = root->GetWidth() / 2.f; center.z = root->GetDepth() / 2.f; center.y = 0; center.r = center.g = 1; center.b = 0; axis->AddVertex(origin); axis->AddVertex(plus_x); axis->AddVertex(origin); axis->AddVertex(plus_y); axis->AddVertex(origin); axis->AddVertex(plus_z); axis->AddVertex(center); center.y = root->GetHeight(); axis->AddVertex(center); axis->Sync(); } { face = new srp::ogl::TexturedVertexBuffer(GL_TRIANGLES, GL_STATIC_DRAW); srp::ogl::QueueTexturedRectangle(*face, glm::vec3(0, 0, 0), glm::vec3(root->GetWidth(), root->GetHeight(), 0), glm::vec2(0, 0), glm::vec2(1, 1)); face->Sync(); } set_texture_data(*dstore, panel_z); GLERR(); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); srp::ogl::ui::TextInit(*window); srp::ogl::ui::UIInit(*window); }