1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2025-10-10 20:27:39 +00:00

uniformizing vertex attribute arrays

This commit is contained in:
Zeno Rogue
2018-02-09 01:46:14 +01:00
parent 64adb6f0ea
commit 120797d7ce
7 changed files with 135 additions and 118 deletions

View File

@@ -501,12 +501,9 @@ bool gl_print(int x, int y, int shift, int size, const char *s, int color, int a
glfont_t& f(*glfont[gsiz]); glfont_t& f(*glfont[gsiz]);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glhr::be_textured(); glhr::be_textured();
glcolor2((color << 8) | 0xFF); glhr::color2((color << 8) | 0xFF);
int tsize = 0; int tsize = 0;
@@ -537,13 +534,11 @@ bool gl_print(int x, int y, int shift, int size, const char *s, int color, int a
tver[1] = tver[10] = -hi; tver[1] = tver[10] = -hi;
tver[6] = tver[9] = wi; tver[6] = tver[9] = wi;
tver[12+4] = tver[12+7] = fy; tver[12+3] = tver[12+5] = fy;
tver[12+6] = tver[12+9] = fx; tver[12+4] = tver[12+6] = fx;
activateVertexArray(tver, 8); glhr::vertices(tver, 4);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glhr::texture_vertices(tver+12, 4);
glTexCoordPointer(3, GL_FLOAT, 0, &tver[12]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
} }
if(stereo::active() && shift) stereo::set_mask(0); if(stereo::active() && shift) stereo::set_mask(0);
@@ -553,8 +548,6 @@ bool gl_print(int x, int y, int shift, int size, const char *s, int color, int a
x += wi; x += wi;
} }
glDisable(GL_TEXTURE_2D);
return clicked; return clicked;
} }
#endif #endif
@@ -900,7 +893,7 @@ void drawCircle(int x, int y, int size, int color) {
glhr::be_nontextured(); glhr::be_nontextured();
glhr::set_modelview(glhr::id()); glhr::set_modelview(glhr::id());
qglcoords = 0; qglcoords = 0;
glcolor2(color); glhr::color2(color);
x -= vid.xcenter; y -= vid.ycenter; x -= vid.xcenter; y -= vid.ycenter;
int pts = size * 4; int pts = size * 4;
if(pts > 1500) pts = 1500; if(pts > 1500) pts = 1500;

View File

@@ -2268,7 +2268,7 @@ void drawaura() {
#endif #endif
#if CAP_GL #if CAP_GL
glhr::be_nontextured(); glhr::switch_mode(glhr::gmVarColored);
glhr::set_modelview(glhr::id()); glhr::set_modelview(glhr::id());
setcameraangle(true); setcameraangle(true);

View File

@@ -2389,7 +2389,6 @@ extern purehookset hooks_drawmap;
extern hookset<bool(eLand&)> *hooks_music; extern hookset<bool(eLand&)> *hooks_music;
extern hookset<bool()> *hooks_prestats; extern hookset<bool()> *hooks_prestats;
extern purehookset hooks_fixticks; extern purehookset hooks_fixticks;
void activateVertexArray(GLfloat *f, int qty);
ld realradius(); ld realradius();

View File

@@ -111,10 +111,6 @@ vector<polytodraw*> ptds2;
GLfloat glcoords[POLYMAX][3]; GLfloat glcoords[POLYMAX][3];
int qglcoords; int qglcoords;
#if CAP_GL
GLfloat *currentvertices;
#endif
GLfloat *ourshape = NULL; GLfloat *ourshape = NULL;
void initPolyForGL() { void initPolyForGL() {
@@ -132,7 +128,7 @@ void initPolyForGL() {
} }
#if CAP_GL #if CAP_GL
currentvertices = NULL; glhr::currentvertices = NULL;
#endif #endif
} }
#endif #endif
@@ -141,25 +137,16 @@ void initPolyForGL() {
GLuint shapebuffer; GLuint shapebuffer;
extern void glcolor(int color);
void activateVertexArray(GLfloat *f, int qty) {
currentvertices = f;
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, f);
}
extern GLfloat *ourshape; extern GLfloat *ourshape;
void activateShapes() { void activateShapes() {
if(currentvertices != ourshape) { if(glhr::currentvertices != ourshape) {
activateVertexArray(ourshape, qhpc); glhr::vertices(ourshape, qhpc);
} }
} }
void activateGlcoords() { void activateGlcoords() {
activateVertexArray(glcoords[0], qglcoords); glhr::vertices(glcoords[0], qglcoords);
} }
#endif #endif
@@ -283,11 +270,6 @@ void drawTexturedTriangle(SDL_Surface *s, int *px, int *py, GLfloat *tv, int col
#endif #endif
#if CAP_GL #if CAP_GL
void glcolor2(int color) {
unsigned char *c = (unsigned char*) (&color);
glColor4f(c[3] / 255.0, c[2] / 255.0, c[1]/255.0, c[0] / 255.0);
}
void glapplymatrix(const transmatrix& V) { void glapplymatrix(const transmatrix& V) {
GLfloat mat[16]; GLfloat mat[16];
int id = 0; int id = 0;
@@ -322,11 +304,9 @@ int tinfshift;
void gldraw(int useV, const transmatrix& V, int ps, int pq, int col, int outline, int flags, textureinfo *tinf) { void gldraw(int useV, const transmatrix& V, int ps, int pq, int col, int outline, int flags, textureinfo *tinf) {
if(tinf) { if(tinf) {
glEnable(GL_TEXTURE_2D);
glhr::be_textured(); glhr::be_textured();
glBindTexture(GL_TEXTURE_2D, tinf->texture_id); glBindTexture(GL_TEXTURE_2D, tinf->texture_id);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glhr::texture_vertices(&tinf->tvertices[tinfshift], 0, 3);
glTexCoordPointer(3, GL_FLOAT, 0, &tinf->tvertices[tinfshift]);
} }
else glhr::be_nontextured(); else glhr::be_nontextured();
@@ -352,7 +332,7 @@ void gldraw(int useV, const transmatrix& V, int ps, int pq, int col, int outline
if(flags & POLY_INVERSE) { if(flags & POLY_INVERSE) {
stereo::set_mask(ed); stereo::set_mask(ed);
glcolor2(col); glhr::color2(col);
glStencilOp( GL_ZERO, GL_ZERO, GL_ZERO); glStencilOp( GL_ZERO, GL_ZERO, GL_ZERO);
glStencilFunc( GL_NOTEQUAL, 1, 1); glStencilFunc( GL_NOTEQUAL, 1, 1);
GLfloat xx = vid.xres; GLfloat xx = vid.xres;
@@ -361,16 +341,16 @@ void gldraw(int useV, const transmatrix& V, int ps, int pq, int col, int outline
-xx, -yy, stereo::scrdist, +xx, -yy, stereo::scrdist, -xx, -yy, stereo::scrdist, +xx, -yy, stereo::scrdist,
+xx, +yy, stereo::scrdist, -xx, +yy, stereo::scrdist +xx, +yy, stereo::scrdist, -xx, +yy, stereo::scrdist
}; };
GLfloat *cur = currentvertices; GLfloat *cur = glhr::currentvertices;
activateVertexArray(scr, 4); glhr::vertices(scr, 4);
glhr::set_modelview(glhr::id()); glhr::set_modelview(glhr::id());
glDrawArrays(tinf ? GL_TRIANGLES : GL_TRIANGLE_FAN, 0, 4); glDrawArrays(tinf ? GL_TRIANGLES : GL_TRIANGLE_FAN, 0, 4);
activateVertexArray(cur, 0); glhr::vertices(cur, 0);
draw = false; goto again; draw = false; goto again;
} }
else { else {
stereo::set_mask(ed); stereo::set_mask(ed);
glcolor2(col); glhr::color2(col);
glStencilOp( GL_ZERO, GL_ZERO, GL_ZERO); glStencilOp( GL_ZERO, GL_ZERO, GL_ZERO);
glStencilFunc( GL_EQUAL, 1, 1); glStencilFunc( GL_EQUAL, 1, 1);
glDrawArrays(tinf ? GL_TRIANGLES : GL_TRIANGLE_FAN, ps, pq); glDrawArrays(tinf ? GL_TRIANGLES : GL_TRIANGLE_FAN, ps, pq);
@@ -380,17 +360,12 @@ void gldraw(int useV, const transmatrix& V, int ps, int pq, int col, int outline
} }
if(outline) { if(outline) {
glcolor2(outline); glhr::color2(outline);
glDrawArrays(GL_LINE_STRIP, ps, pq); glDrawArrays(GL_LINE_STRIP, ps, pq);
} }
} }
if(stereo::active()) stereo::set_projection(0), stereo::set_viewport(0), stereo::set_mask(0); if(stereo::active()) stereo::set_projection(0), stereo::set_viewport(0), stereo::set_mask(0);
if(tinf) {
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
}
} }
#endif #endif
@@ -531,8 +506,8 @@ void drawpolyline(polytodraw& p) {
#if CAP_GL #if CAP_GL
if(vid.usingGL && pmodel == mdDisk && !spherespecial) { if(vid.usingGL && pmodel == mdDisk && !spherespecial) {
const int pq = pp.cnt; const int pq = pp.cnt;
if(currentvertices != pp.tab) if(glhr::currentvertices != pp.tab)
activateVertexArray(pp.tab, pq); glhr::vertices(pp.tab, pq);
const int ps=0; const int ps=0;
glLineWidth(linewidthat(tC0(pp.V), pp.minwidth)); glLineWidth(linewidthat(tC0(pp.V), pp.minwidth));
gldraw(1, pp.V, ps, pq, p.col, pp.outline, 0, pp.tinf); gldraw(1, pp.V, ps, pq, p.col, pp.outline, 0, pp.tinf);

54
rug.cpp
View File

@@ -1108,36 +1108,13 @@ void prepareTexture() {
double xview, yview; double xview, yview;
void glcolorClear(int color) {
unsigned char *c = (unsigned char*) (&color);
glClearColor(c[3] / 255.0, c[2] / 255.0, c[1]/255.0, c[0] / 255.0);
}
void drawRugScene() { void drawRugScene() {
GLfloat light_ambient[] = { 3.5, 3.5, 3.5, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
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);
#else
glLightModelx(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
#endif
GLERR("lighting");
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glbuf->use_as_texture(); glbuf->use_as_texture();
if(backcolor == 0) if(backcolor == 0)
glClearColor(0.05,0.05,0.05,1); glClearColor(0.05,0.05,0.05,1);
else else
glcolorClear(backcolor << 8 | 0xFF); glhr::colorClear(backcolor << 8 | 0xFF);
#ifdef GLES_ONLY #ifdef GLES_ONLY
glClearDepthf(1.0f); glClearDepthf(1.0f);
#else #else
@@ -1146,8 +1123,7 @@ void drawRugScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_BLEND); glDisable(GL_BLEND);
glEnable(GL_TEXTURE_2D); glhr::switch_mode(glhr::gmVarColored);
glhr::be_textured();
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
@@ -1210,38 +1186,26 @@ void drawRugScene() {
} }
glColor4f(1.f, 1.f, 1.f, 1.f); glColor4f(1.f, 1.f, 1.f, 1.f);
if(rug_perspective && gwhere >= gSphere) { fog_max(
glEnable(GL_FOG); gwhere == gSphere ? 10 :
#ifndef GLES_ONLY gwhere == gElliptic ? 4 :
glFogi(GL_FOG_MODE, GL_LINEAR); 100
#else );
glFogx(GL_FOG_MODE, GL_LINEAR);
#endif
glFogf(GL_FOG_START, 0);
glFogf(GL_FOG_END, gwhere == gSphere ? 10 : 4);
}
glhr::set_modelview(glhr::id()); glhr::set_modelview(glhr::id());
for(int t=0; t<size(triangles); t++) for(int t=0; t<size(triangles); t++)
drawTriangle(triangles[t]); drawTriangle(triangles[t]);
glEnableClientState(GL_VERTEX_ARRAY); glhr::vertices(&vertex_array[0], 0);
glVertexPointer(3, GL_FLOAT, 0, &vertex_array[0]); glhr::texture_vertices(&tvertex_array[0], 0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, &tvertex_array[0]);
glDrawArrays(GL_TRIANGLES, 0, size(vertex_array)/3); glDrawArrays(GL_TRIANGLES, 0, size(vertex_array)/3);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
stereo::set_mask(0); stereo::set_mask(0);
} }
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glDisable(GL_FOG);
stereo::set_mask(0), stereo::set_viewport(0); stereo::set_mask(0), stereo::set_viewport(0);
stereo::set_projection(0); stereo::set_projection(0);

View File

@@ -9,7 +9,17 @@
namespace glhr { namespace glhr {
enum mode { gmColored, gmTextured, gmMAX}; enum eMode { gmColored, gmTextured, gmVarColored, gmLightFog, gmMAX};
static const flagtype GF_TEXTURE = 1;
static const flagtype GF_VARCOLOR = 2;
static const flagtype GF_LIGHTFOG = 4;
flagtype flags[gmMAX] = { 0, GF_TEXTURE, GF_VARCOLOR, GF_TEXTURE | GF_LIGHTFOG };
eMode mode;
void switch_mode(eMode m);
struct glmatrix { struct glmatrix {
GLfloat a[4][4]; GLfloat a[4][4];
@@ -84,9 +94,9 @@ void set_modelview(const glmatrix& m) {
glLoadMatrixf(m.as_array()); glLoadMatrixf(m.as_array());
} }
void be_nontextured() { } void init() {
void be_textured() { } glEnableClientState(GL_VERTEX_ARRAY);
void init() { } }
#endif #endif
@@ -210,7 +220,7 @@ struct GLprogram {
}; };
GLprogram *textured, *nontextured; GLprogram *programs[gmMAX];
void init() { void init() {
projection = id(); projection = id();
@@ -219,7 +229,7 @@ void init() {
auto texture_only = [=] (string s) -> string { if(i) return s; else return ""; }; auto texture_only = [=] (string s) -> string { if(i) return s; else return ""; };
auto not_texture_only = [=] (string s) -> string { if(!i) return s; else return ""; }; auto not_texture_only = [=] (string s) -> string { if(!i) return s; else return ""; };
(i==1?textured:nontextured) = new GLprogram( programs[i] = new GLprogram(
// "attribute vec4 position;" // "attribute vec4 position;"
// "attribute vec3 normal;" // "attribute vec3 normal;"
@@ -253,7 +263,12 @@ void init() {
); );
} }
nontextured->enable(); programs[2] = programs[0];
programs[3] = programs[1];
glEnableClientState(GL_VERTEX_ARRAY);
switch_mode(gmColored);
programs[gmColored]->enable();
} }
void set_modelview(const glmatrix& modelview) { void set_modelview(const glmatrix& modelview) {
@@ -263,8 +278,83 @@ void set_modelview(const glmatrix& modelview) {
// glUniformMatrix3fv(current->uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, nm[0]); // glUniformMatrix3fv(current->uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, nm[0]);
} }
void be_nontextured() { nontextured->enable(); }
void be_textured() { textured->enable(); }
#endif #endif
GLfloat *currentvertices;
void vertices(GLfloat *f, int qty) {
currentvertices = f;
glVertexPointer(3, GL_FLOAT, 0, f);
}
void texture_vertices(GLfloat *f, int qty, int stride = 2) {
glTexCoordPointer(stride, GL_FLOAT, 0, f);
}
void color_vertices(GLfloat *f, int qty) {
glColorPointer(4, GL_FLOAT, 0, f);
}
void color2(int color) {
unsigned char *c = (unsigned char*) (&color);
glColor4f(c[3] / 255.0, c[2] / 255.0, c[1]/255.0, c[0] / 255.0);
}
void colorClear(int color) {
unsigned char *c = (unsigned char*) (&color);
glClearColor(c[3] / 255.0, c[2] / 255.0, c[1]/255.0, c[0] / 255.0);
}
void be_nontextured() { switch_mode(gmColored); }
void be_textured() { switch_mode(gmTextured); }
void switch_mode(eMode m) {
if(m == mode) return;
#if CAP_SHADER
programs[m]->enable();
#endif
flagtype newflags = flags[m] &~ flags[mode];
flagtype oldflags = flags[mode] &~ flags[m];
if(newflags & GF_TEXTURE) {
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
if(oldflags & GF_TEXTURE) {
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
if(newflags & GF_VARCOLOR)
glEnableClientState(GL_COLOR_ARRAY);
if(oldflags & GF_VARCOLOR)
glDisableClientState(GL_COLOR_ARRAY);
if(newflags & GF_LIGHTFOG) {
GLfloat light_ambient[] = { 3.5, 3.5, 3.5, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
GLERR("lighting");
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogf(GL_FOG_START, 0);
}
if(oldflags & GF_LIGHTFOG) {
glDisable(GL_FOG);
glDisable(GL_LIGHTING);
}
mode = m;
}
void fog_max(ld fogmax) {
glFogf(GL_FOG_END, fogmax);
}
} }

View File

@@ -465,9 +465,8 @@ void saveFullTexture() {
bool newmove = false; bool newmove = false;
void drawRawTexture() { void drawRawTexture() {
glEnable(GL_TEXTURE_2D);
glhr::be_textured(); glhr::be_textured();
glcolor2(0xFFFFFF20); glhr::color2(0xFFFFFF20);
glhr::set_modelview(glhr::translate(0, 0, stereo::scrdist)); glhr::set_modelview(glhr::translate(0, 0, stereo::scrdist));
glBindTexture(GL_TEXTURE_2D, textureid); glBindTexture(GL_TEXTURE_2D, textureid);
vector<GLfloat> tver, sver; vector<GLfloat> tver, sver;
@@ -485,12 +484,9 @@ void drawRawTexture() {
sver.push_back(y * vid.scrsize); sver.push_back(y * vid.scrsize);
sver.push_back(0); sver.push_back(0);
} }
activateVertexArray(&sver[0], 4); glhr::vertices(&sver[0], 4);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glhr::texture_vertices(&tver[0], 4, 3);
glTexCoordPointer(3, GL_FLOAT, 0, &tver[0]);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
} }
struct magicmapper_point { struct magicmapper_point {