From 0a7c07fa7d1557893ca8810a27b6b8d10c0af35a Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Sat, 3 Feb 2018 19:19:27 +0100 Subject: [PATCH] more changes to make Rug work on Android --- basegraph.cpp | 16 ++++----- init.cpp | 3 ++ renderbuffer.cpp | 13 ++++--- rug.cpp | 90 ++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 95 insertions(+), 27 deletions(-) diff --git a/basegraph.cpp b/basegraph.cpp index ce14be29..a2d63502 100644 --- a/basegraph.cpp +++ b/basegraph.cpp @@ -332,6 +332,14 @@ void setGLProjection(int col) { stereo::set_projection(0); } +inline int next_p2 (int a ) +{ + int rval=1; + // rval<<=1 Is A Prettier Way Of Writing rval*=2; + while(rval #include #include + #include + #define GLES_ONLY #else #include #include diff --git a/renderbuffer.cpp b/renderbuffer.cpp index bb9a02d6..4b840424 100644 --- a/renderbuffer.cpp +++ b/renderbuffer.cpp @@ -40,7 +40,6 @@ renderbuffer::renderbuffer(int x, int y, bool gl) : x(x), y(y) { if(gl) { tx = next_p2(x); ty = next_p2(y); - s = NULL; #if CAP_GLEW if(!glew) { glew = true; @@ -53,7 +52,7 @@ renderbuffer::renderbuffer(int x, int y, bool gl) : x(x), y(y) { #endif FramebufferName = renderedTexture = depth_stencil_rb = 0; - glGenFramebuffers(1, &FramebufferName); + glGenFramebuffers(1, &FramebufferName); // glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName); glGenTextures(1, &renderedTexture); @@ -67,8 +66,8 @@ renderbuffer::renderbuffer(int x, int y, bool gl) : x(x), y(y) { #else glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderedTexture, 0); #endif - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; - glDrawBuffers(1, DrawBuffers); + // GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; + // glDrawBuffers(1, DrawBuffers); glGenRenderbuffers(1, &depth_stencil_rb); glBindRenderbuffer(GL_RENDERBUFFER, depth_stencil_rb); @@ -96,7 +95,7 @@ void renderbuffer::make_surface() { } SDL_Surface *renderbuffer::render() { - make_surface(); + make_surface() ; if(FramebufferName) { glReadPixels(0, 0, vid.xres, vid.yres, GL_BGRA, GL_UNSIGNED_BYTE, srf->pixels); for(int y=0; yw,s_screen->h); + #endif return; } #endif @@ -147,6 +148,7 @@ void renderbuffer::use_as_texture() { if(FramebufferName) { glBindTexture( GL_TEXTURE_2D, renderedTexture); } +#if CAP_SDL else { if(!expanded_data) expanded_data = new Uint32[tx * ty]; @@ -155,6 +157,7 @@ void renderbuffer::use_as_texture() { glBindTexture( GL_TEXTURE_2D, renderedTexture); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tx, ty, 0, GL_BGRA, GL_UNSIGNED_BYTE, expanded_data ); } +#endif } #endif diff --git a/rug.cpp b/rug.cpp index 3bcf41b5..f37fde1d 100644 --- a/rug.cpp +++ b/rug.cpp @@ -871,6 +871,20 @@ void addNewPoints() { if(qvalid != oqvalid) { printf("adding new points %4d %4d %4d %.9lf %9d %9d\n", oqvalid, qvalid, size(points), dist, dt, queueiter); } } +#if !CAP_SDL +#include +#include +long long getVa() { + struct timeval tval; + gettimeofday(&tval, NULL); + return tval.tv_sec * 1000000 + tval.tv_usec; + } + +int SDL_GetTicks() { + return getVa() / 1000; + } +#endif + void physics() { if(good_shape) return; @@ -981,6 +995,8 @@ bool project_ods(hyperpoint azeq, hyperpoint& h1, hyperpoint& h2, bool eye) { } #endif +vector vertex_array, tvertex_array; + void drawTriangle(triangle& t) { using namespace hyperpoint_vec; for(int i: {0,1,2}) { @@ -1054,8 +1070,10 @@ void drawTriangle(triangle& t) { glNormal3f(hc[0]/hch,hc[1]/hch,hc[2]/hch); for(int i: {0,1,2}) { - glTexCoord2f(t.m[i]->x1, t.m[i]->y1); - glVertex3f(h[i][0], h[i][1], h[i][2]); + tvertex_array.push_back(t.m[i]->x1); + tvertex_array.push_back(t.m[i]->y1); + for(int j: {0,1,2}) + vertex_array.push_back(h[i][j]); } } @@ -1104,8 +1122,12 @@ void drawRugScene() { glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_POSITION, light_position); +#ifndef GLES_ONLY glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); GLERR("lighting"); +#else + glLightModelx(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); +#endif glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); @@ -1116,7 +1138,7 @@ void drawRugScene() { glClearColor(0.05,0.05,0.05,1); else glcolorClear(backcolor << 8 | 0xFF); - glClearDepth(1.0f); + glClearDepthf(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_BLEND); @@ -1126,13 +1148,18 @@ void drawRugScene() { for(int ed=stereo::active() && stereo::mode != stereo::sODS ? -1 : 0; ed < 2; ed += 2) { use_precompute = false; + vertex_array.clear(); + tvertex_array.clear(); stereo::set_mask(ed), stereo::set_viewport(ed); if(ed == 1 && stereo::mode == stereo::sAnaglyph) glClear(GL_DEPTH_BUFFER_BIT); start_projection(ed); - if(stereo::mode == stereo::sODS) + if(stereo::mode == stereo::sODS) { +#ifndef GLES_ONLY glOrtho(-M_PI, M_PI, -M_PI, M_PI, 0, -M_PI * 2); +#endif + } else if(rug_perspective || stereo::active()) { ld vnear = .001; @@ -1140,7 +1167,15 @@ void drawRugScene() { ld sca = vnear * stereo::tanfov / vid.xres; xview = stereo::tanfov; yview = stereo::tanfov * vid.yres / vid.xres; - glFrustum(-sca * vid.xres, sca * vid.xres, -sca * vid.yres, sca * vid.yres, vnear, vfar); + + //glFrustum(-sca * vid.xres, sca * vid.xres, -sca * vid.yres, sca * vid.yres, vnear, vfar); + + GLfloat frustum[16] = { + GLfloat(vnear / sca / vid.xres), 0, 0, 0, + 0, GLfloat(vnear / sca / vid.yres), 0, 0, + 0, 0, GLfloat(-(vnear+vfar)/(vfar-vnear)), -1, + 0, 0, GLfloat(-2*vnear*vfar/(vfar-vnear)), 1}; + glMultMatrixf(frustum); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -1160,21 +1195,38 @@ void drawRugScene() { else { xview = stereo::tanfov * model_distance; yview = stereo::tanfov * model_distance * vid.yres / vid.xres; - glOrtho(-xview, xview, yview, -yview, -1000, 1000); + // glOrtho(-xview, xview, yview, -yview, -1000, 1000); + + GLfloat ortho[16] = { + GLfloat(1/xview), 0, 0, 0, + 0, GLfloat(1/yview), 0, 0, + 0, 0, GLfloat(.001), 0, + 0, 0, 0, 1}; + glMultMatrixf(ortho); } glColor4f(1.f, 1.f, 1.f, 1.f); if(rug_perspective && gwhere >= gSphere) { glEnable(GL_FOG); +#ifndef GLES_ONLY glFogi(GL_FOG_MODE, GL_LINEAR); +#else + glFogx(GL_FOG_MODE, GL_LINEAR); +#endif glFogf(GL_FOG_START, 0); glFogf(GL_FOG_END, gwhere == gSphere ? 10 : 4); } - - glBegin(GL_TRIANGLES); + for(int t=0; t