mirror of
https://github.com/zenorogue/hyperrogue.git
synced 2025-07-08 12:22:49 +00:00
more shader cleanup
This commit is contained in:
parent
e2531fb891
commit
1391a7fe4d
@ -2207,6 +2207,8 @@ void sumaura(int v) {
|
|||||||
aurac[AURA][v] = aurac[0][v];
|
aurac[AURA][v] = aurac[0][v];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float coltab[4][4];
|
||||||
|
|
||||||
void drawaura() {
|
void drawaura() {
|
||||||
if(!haveaura()) return;
|
if(!haveaura()) return;
|
||||||
if(stereo::mode) return;
|
if(stereo::mode) return;
|
||||||
@ -2272,9 +2274,7 @@ void drawaura() {
|
|||||||
glhr::set_modelview(glhr::id());
|
glhr::set_modelview(glhr::id());
|
||||||
setcameraangle(true);
|
setcameraangle(true);
|
||||||
|
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
glhr::color_vertices(coltab[0], 4);
|
||||||
float coltab[4][4];
|
|
||||||
glColorPointer(4, GL_FLOAT, 0, coltab);
|
|
||||||
activateGlcoords();
|
activateGlcoords();
|
||||||
|
|
||||||
float cx[AURA+1][11][5];
|
float cx[AURA+1][11][5];
|
||||||
@ -2318,7 +2318,6 @@ void drawaura() {
|
|||||||
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
glDisableClientState(GL_COLOR_ARRAY);
|
|
||||||
setcameraangle(false);
|
setcameraangle(false);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
1
hyper.h
1
hyper.h
@ -2393,7 +2393,6 @@ extern purehookset hooks_fixticks;
|
|||||||
ld realradius();
|
ld realradius();
|
||||||
|
|
||||||
void sdltogl(SDL_Surface *txt, struct glfont_t& f, int ch);
|
void sdltogl(SDL_Surface *txt, struct glfont_t& f, int ch);
|
||||||
void glcolor2(int color);
|
|
||||||
|
|
||||||
void showStartMenu();
|
void showStartMenu();
|
||||||
|
|
||||||
|
2
init.cpp
2
init.cpp
@ -319,7 +319,7 @@ inline Uint8 *SDL_GetKeyState(void *v) { static Uint8 tab[1024]; return tab; }
|
|||||||
} */
|
} */
|
||||||
|
|
||||||
#ifndef CAP_GLEW
|
#ifndef CAP_GLEW
|
||||||
#define CAP_GLEW (CAP_GL && !ISMOBILE && !ISMAC && !ISLINUX)
|
#define CAP_GLEW (CAP_GL && !ISMOBILE && !ISMAC && !ISLINUX && !ISWEB)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if CAP_GL
|
#if CAP_GL
|
||||||
|
@ -336,7 +336,7 @@ void gldraw(int useV, const transmatrix& V, int ps, int pq, int col, int outline
|
|||||||
glColorMask( GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE );
|
glColorMask( GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE );
|
||||||
glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT);
|
glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT);
|
||||||
glStencilFunc( GL_ALWAYS, 0x1, 0x1 );
|
glStencilFunc( GL_ALWAYS, 0x1, 0x1 );
|
||||||
glColor4f(1,1,1,1);
|
glhr::color2(0xFFFFFFFF);
|
||||||
glDrawArrays(tinf ? GL_TRIANGLES : GL_TRIANGLE_FAN, ps, pq);
|
glDrawArrays(tinf ? GL_TRIANGLES : GL_TRIANGLE_FAN, ps, pq);
|
||||||
|
|
||||||
if(flags & POLY_INVERSE) {
|
if(flags & POLY_INVERSE) {
|
||||||
|
2
rug.cpp
2
rug.cpp
@ -1179,7 +1179,7 @@ void drawRugScene() {
|
|||||||
|
|
||||||
glhr::projection_multiply(glhr::ortho(xview, yview, -1000));
|
glhr::projection_multiply(glhr::ortho(xview, yview, -1000));
|
||||||
}
|
}
|
||||||
glColor4f(1.f, 1.f, 1.f, 1.f);
|
glhr::color2(0xFFFFFFFF);
|
||||||
|
|
||||||
glhr::fog_max(
|
glhr::fog_max(
|
||||||
gwhere == gSphere && rug_perspective ? 10 :
|
gwhere == gSphere && rug_perspective ? 10 :
|
||||||
|
129
shaders.cpp
129
shaders.cpp
@ -2,6 +2,8 @@
|
|||||||
// If CAP_SHADER is 0, OpenGL 1.0 is used.
|
// If CAP_SHADER is 0, OpenGL 1.0 is used.
|
||||||
// If CAP_SHADER is 1, GLSL is used.
|
// If CAP_SHADER is 1, GLSL is used.
|
||||||
|
|
||||||
|
#define DEBUG_GL 0
|
||||||
|
|
||||||
// Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details
|
// Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details
|
||||||
|
|
||||||
void glError(const char* GLcall, const char* file, const int line) {
|
void glError(const char* GLcall, const char* file, const int line) {
|
||||||
@ -156,7 +158,9 @@ void init();
|
|||||||
int compileShader(int type, const string& s) {
|
int compileShader(int type, const string& s) {
|
||||||
GLint status;
|
GLint status;
|
||||||
|
|
||||||
// printf("===\ns%s\n===\n", s.c_str());
|
#if DEBUG_GL
|
||||||
|
printf("===\n%s\n===\n", s.c_str());
|
||||||
|
#endif
|
||||||
|
|
||||||
GLint shader = glCreateShader(type);
|
GLint shader = glCreateShader(type);
|
||||||
const char *ss = s.c_str();
|
const char *ss = s.c_str();
|
||||||
@ -186,17 +190,15 @@ int compileShader(int type, const string& s) {
|
|||||||
|
|
||||||
struct GLprogram *current = NULL;
|
struct GLprogram *current = NULL;
|
||||||
|
|
||||||
enum {
|
static const int aPosition = 0;
|
||||||
UNIFORM_MODELVIEWPROJECTION_MATRIX,
|
static const int aColor = 3;
|
||||||
UNIFORM_FOGFACTOR,
|
static const int aTexture = 8;
|
||||||
NUM_UNIFORMS
|
|
||||||
};
|
|
||||||
|
|
||||||
struct GLprogram {
|
struct GLprogram {
|
||||||
GLuint _program;
|
GLuint _program;
|
||||||
GLuint vertShader, fragShader;
|
GLuint vertShader, fragShader;
|
||||||
|
|
||||||
GLint uniforms[NUM_UNIFORMS];
|
GLint uMVP, uFog, uColor, tTexture;
|
||||||
|
|
||||||
GLprogram(string vsh, string fsh) {
|
GLprogram(string vsh, string fsh) {
|
||||||
_program = glCreateProgram();
|
_program = glCreateProgram();
|
||||||
@ -210,6 +212,10 @@ struct GLprogram {
|
|||||||
// Attach fragment shader to program.
|
// Attach fragment shader to program.
|
||||||
glAttachShader(_program, fragShader);
|
glAttachShader(_program, fragShader);
|
||||||
|
|
||||||
|
glBindAttribLocation(_program, aPosition, "aPosition");
|
||||||
|
glBindAttribLocation(_program, aTexture, "aTexture");
|
||||||
|
glBindAttribLocation(_program, aColor, "aColor");
|
||||||
|
|
||||||
GLint status;
|
GLint status;
|
||||||
glLinkProgram(_program);
|
glLinkProgram(_program);
|
||||||
|
|
||||||
@ -231,9 +237,15 @@ struct GLprogram {
|
|||||||
// glBindAttribLocation(_program, GLKVertexAttribPosition, "position"); ??
|
// glBindAttribLocation(_program, GLKVertexAttribPosition, "position"); ??
|
||||||
// glBindAttribLocation(_program, GLKVertexAttribNormal, "normal"); ??
|
// glBindAttribLocation(_program, GLKVertexAttribNormal, "normal"); ??
|
||||||
|
|
||||||
uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX] = glGetUniformLocation(_program, "modelViewProjectionMatrix");
|
uMVP = glGetUniformLocation(_program, "uMVP");
|
||||||
uniforms[UNIFORM_FOGFACTOR] = glGetUniformLocation(_program, "fogfactor");
|
uFog = glGetUniformLocation(_program, "uFog");
|
||||||
// printf("uniforms: %d %d\n", uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], uniforms[UNIFORM_FOGFACTOR]);
|
uColor = glGetUniformLocation(_program, "uColor");
|
||||||
|
tTexture = glGetUniformLocation(_program, "tTexture");
|
||||||
|
|
||||||
|
#if DEBUG_GL
|
||||||
|
printf("uniforms: %d %d %d %d\n", uMVP, uFog, uColor, tTexture);
|
||||||
|
#endif
|
||||||
|
// printf("attributes: %d\n", position_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
~GLprogram() {
|
~GLprogram() {
|
||||||
@ -257,13 +269,17 @@ GLprogram *programs[gmMAX];
|
|||||||
string stringbuilder() { return ""; }
|
string stringbuilder() { return ""; }
|
||||||
|
|
||||||
template<class... T> string stringbuilder(bool i, const string& s, T... t) {
|
template<class... T> string stringbuilder(bool i, const string& s, T... t) {
|
||||||
if(i) return s + stringbuilder(t...);
|
if(i) return s +
|
||||||
|
#if DEBUG_GL
|
||||||
|
"\n" +
|
||||||
|
#endif
|
||||||
|
stringbuilder(t...);
|
||||||
else return stringbuilder(t...);
|
else return stringbuilder(t...);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_modelview(const glmatrix& modelview) {
|
void set_modelview(const glmatrix& modelview) {
|
||||||
glmatrix mvp = modelview * projection;
|
glmatrix mvp = modelview * projection;
|
||||||
glUniformMatrix4fv(current->uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, mvp.as_array());
|
glUniformMatrix4fv(current->uMVP, 1, 0, mvp.as_array());
|
||||||
// glmatrix nm = modelview;
|
// glmatrix nm = modelview;
|
||||||
// glUniformMatrix3fv(current->uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, nm[0]);
|
// glUniformMatrix3fv(current->uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, nm[0]);
|
||||||
}
|
}
|
||||||
@ -274,25 +290,40 @@ GLfloat *currentvertices;
|
|||||||
|
|
||||||
void vertices(GLfloat *f, int qty) {
|
void vertices(GLfloat *f, int qty) {
|
||||||
currentvertices = f;
|
currentvertices = f;
|
||||||
|
#if CAP_SHADER
|
||||||
|
glVertexAttribPointer(aPosition, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), f);
|
||||||
|
#else
|
||||||
glVertexPointer(3, GL_FLOAT, 0, f);
|
glVertexPointer(3, GL_FLOAT, 0, f);
|
||||||
|
#endif
|
||||||
|
// #endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void texture_vertices(GLfloat *f, int qty, int stride = 2) {
|
void texture_vertices(GLfloat *f, int qty, int stride = 2) {
|
||||||
|
#if CAP_SHADER
|
||||||
|
glVertexAttribPointer(aTexture, stride, GL_FLOAT, GL_FALSE, stride * sizeof(GLfloat), f);
|
||||||
|
#else
|
||||||
glTexCoordPointer(stride, GL_FLOAT, 0, f);
|
glTexCoordPointer(stride, GL_FLOAT, 0, f);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void color_vertices(GLfloat *f, int qty) {
|
void color_vertices(GLfloat *f, int qty) {
|
||||||
|
#if CAP_SHADER
|
||||||
|
glVertexAttribPointer(aColor, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), f);
|
||||||
|
#else
|
||||||
glColorPointer(4, GL_FLOAT, 0, f);
|
glColorPointer(4, GL_FLOAT, 0, f);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void color2(int color) {
|
void color2(int color, ld part = 1) {
|
||||||
unsigned char *c = (unsigned char*) (&color);
|
unsigned char *c = (unsigned char*) (&color);
|
||||||
glColor4f(c[3] / 255.0, c[2] / 255.0, c[1]/255.0, c[0] / 255.0);
|
GLfloat cols[4];
|
||||||
}
|
for(int i=0; i<4; i++) cols[i] = c[3-i] / 255.0 * part;
|
||||||
|
#if CAP_SHADER
|
||||||
void color2(int color, ld part) {
|
// glUniform4fv(current->uFog, 4, cols);
|
||||||
unsigned char *c = (unsigned char*) (&color);
|
glUniform4f(current->uColor, cols[0], cols[1], cols[2], cols[3]);
|
||||||
glColor4f(c[3] / 255.0 * part, c[2] / 255.0 * part, c[1]/255.0 * part, c[0] / 255.0);
|
#else
|
||||||
|
glColor4f(cols[0], cols[1], cols[2], cols[3]);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void colorClear(int color) {
|
void colorClear(int color) {
|
||||||
@ -312,16 +343,34 @@ void switch_mode(eMode m) {
|
|||||||
flagtype oldflags = flags[mode] &~ flags[m];
|
flagtype oldflags = flags[mode] &~ flags[m];
|
||||||
if(newflags & GF_TEXTURE) {
|
if(newflags & GF_TEXTURE) {
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
#if CAP_SHADER
|
||||||
|
glEnableVertexAttribArray(aTexture);
|
||||||
|
#else
|
||||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if(oldflags & GF_TEXTURE) {
|
if(oldflags & GF_TEXTURE) {
|
||||||
glDisable(GL_TEXTURE_2D);
|
glDisable(GL_TEXTURE_2D);
|
||||||
|
#if CAP_SHADER
|
||||||
|
glDisableVertexAttribArray(aTexture);
|
||||||
|
#else
|
||||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if(newflags & GF_VARCOLOR)
|
if(newflags & GF_VARCOLOR) {
|
||||||
|
#if CAP_SHADER
|
||||||
|
glEnableVertexAttribArray(aColor);
|
||||||
|
#else
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
if(oldflags & GF_VARCOLOR)
|
#endif
|
||||||
|
}
|
||||||
|
if(oldflags & GF_VARCOLOR) {
|
||||||
|
#if CAP_SHADER
|
||||||
|
glDisableVertexAttribArray(aColor);
|
||||||
|
#else
|
||||||
glDisableClientState(GL_COLOR_ARRAY);
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
if(newflags & GF_LIGHTFOG) {
|
if(newflags & GF_LIGHTFOG) {
|
||||||
#if !CAP_SHADER
|
#if !CAP_SHADER
|
||||||
GLfloat light_ambient[] = { 3.5, 3.5, 3.5, 1.0 };
|
GLfloat light_ambient[] = { 3.5, 3.5, 3.5, 1.0 };
|
||||||
@ -354,15 +403,13 @@ void switch_mode(eMode m) {
|
|||||||
|
|
||||||
void fog_max(ld fogmax) {
|
void fog_max(ld fogmax) {
|
||||||
#if CAP_SHADER
|
#if CAP_SHADER
|
||||||
glUniform1f(current->uniforms[UNIFORM_FOGFACTOR], 1 / fogmax);
|
glUniform1f(current->uFog, 1 / fogmax);
|
||||||
#else
|
#else
|
||||||
glFogf(GL_FOG_END, fogmax);
|
glFogf(GL_FOG_END, fogmax);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
|
||||||
|
|
||||||
#if CAP_GLEW
|
#if CAP_GLEW
|
||||||
if(!glew) {
|
if(!glew) {
|
||||||
glew = true;
|
glew = true;
|
||||||
@ -383,38 +430,42 @@ void init() {
|
|||||||
flagtype f = flags[i];
|
flagtype f = flags[i];
|
||||||
|
|
||||||
bool texture = f & GF_TEXTURE;
|
bool texture = f & GF_TEXTURE;
|
||||||
bool lightfog = f & GF_LIGHTFOG;
|
bool lfog = f & GF_LIGHTFOG;
|
||||||
|
bool varcol = f & GF_VARCOLOR;
|
||||||
|
|
||||||
programs[i] = new GLprogram(stringbuilder(
|
programs[i] = new GLprogram(stringbuilder(
|
||||||
// "attribute vec4 position;"
|
1, "attribute vec4 aPosition;",
|
||||||
|
texture, "attribute vec2 aTexture;",
|
||||||
|
varcol, "attribute vec4 aColor;",
|
||||||
// "attribute vec3 normal;"
|
// "attribute vec3 normal;"
|
||||||
|
|
||||||
1, "varying vec4 vColor;",
|
1, "varying vec4 vColor;",
|
||||||
texture, "varying vec2 vTexCoord;",
|
texture, "varying vec2 vTexCoord;",
|
||||||
|
|
||||||
1, "uniform mat4 modelViewProjectionMatrix;",
|
1, "uniform mat4 uMVP;",
|
||||||
1, "uniform float fogfactor;",
|
1, "uniform float uFog;",
|
||||||
|
!varcol, "uniform vec4 uColor;",
|
||||||
|
|
||||||
1, "void main() {",
|
1, "void main() {",
|
||||||
texture, "vTexCoord = gl_MultiTexCoord0.xy;",
|
texture, "vTexCoord = aTexture;",
|
||||||
lightfog, "vColor = gl_Color * clamp(1.0 + gl_Vertex.z * fogfactor, 0.0, 1.0);",
|
varcol, "vColor = aColor;",
|
||||||
!lightfog, "vColor = gl_Color;",
|
!varcol, "vColor = uColor;",
|
||||||
1, "gl_Position = modelViewProjectionMatrix * gl_Vertex;",
|
lfog, "vColor = vColor * clamp(1.0 + aPosition.z * uFog, 0.0, 1.0);",
|
||||||
|
1, "gl_Position = uMVP * aPosition;",
|
||||||
1, "}"),
|
1, "}"),
|
||||||
|
|
||||||
stringbuilder(
|
stringbuilder(
|
||||||
|
|
||||||
1, "uniform sampler2D myTexture;",
|
1, "uniform sampler2D tTexture;",
|
||||||
1, "varying vec4 vColor;",
|
1, "varying vec4 vColor;",
|
||||||
texture, "varying vec2 vTexCoord;",
|
texture, "varying vec2 vTexCoord;",
|
||||||
1, "void main() {",
|
1, "void main() {",
|
||||||
texture, "gl_FragColor = vColor * texture2D(myTexture, vTexCoord);",
|
texture, "gl_FragColor = vColor * texture2D(tTexture, vTexCoord);",
|
||||||
!texture, "gl_FragColor = vColor;",
|
!texture, "gl_FragColor = vColor;",
|
||||||
1, "}"
|
1, "}"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
|
||||||
switch_mode(gmColored);
|
switch_mode(gmColored);
|
||||||
programs[gmColored]->enable();
|
programs[gmColored]->enable();
|
||||||
#endif
|
#endif
|
||||||
@ -422,7 +473,13 @@ void init() {
|
|||||||
#if !CAP_SHADER
|
#if !CAP_SHADER
|
||||||
switch_mode(gmColored);
|
switch_mode(gmColored);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if CAP_SHADER
|
||||||
|
glEnableVertexAttribArray(aPosition);
|
||||||
|
#else
|
||||||
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
#endif
|
||||||
|
// #endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user