From 95cad2c3d1d49e3e603a00d2893de36baba538a0 Mon Sep 17 00:00:00 2001 From: Lee Salzman Date: Mon, 27 Oct 2008 07:41:46 +0000 Subject: [PATCH] removed libGLU dependency git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@3280 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- source/src/Makefile | 2 +- source/src/md2.h | 4 +- source/src/md3.h | 4 +- source/src/protos.h | 2 + source/src/rendergl.cpp | 12 +++- source/src/scale.h | 131 ++++++++++++++++++++++++++++++++++++++ source/src/shadow.cpp | 2 +- source/src/texture.cpp | 135 ++++++++++++++++++++++++++++++++++------ source/src/vertmodel.h | 2 +- source/vcpp/cube.vcproj | 10 +-- 10 files changed, 273 insertions(+), 31 deletions(-) create mode 100644 source/src/scale.h diff --git a/source/src/Makefile b/source/src/Makefile index 115956c5..a5bf6db2 100644 --- a/source/src/Makefile +++ b/source/src/Makefile @@ -3,7 +3,7 @@ INCLUDES=-I/usr/X11R6/include -I../enet/include -I../src `sdl-config --cflags` CXXFLAGS=$(CXXOPTFLAGS) $(INCLUDES) -fsigned-char -Wall -Wno-deprecated -rdynamic PLATFORM_PREFIX=native -CLIENT_LIBS=-L/usr/X11R6/lib -L../enet -lenet `sdl-config --libs` -lSDL_image -lz -lGL -lGLU -lopenal -lvorbisfile +CLIENT_LIBS=-L/usr/X11R6/lib -L../enet -lenet `sdl-config --libs` -lSDL_image -lz -lGL -lopenal -lvorbisfile CLIENT_OBJS= \ client.o \ clientgame.o \ diff --git a/source/src/md2.h b/source/src/md2.h index e9b4c914..6eb2f28d 100644 --- a/source/src/md2.h +++ b/source/src/md2.h @@ -171,7 +171,9 @@ struct md2 : vertmodel void begingenshadow() { - glRotatef(180, 0, 0, 1); + matrixpos = 0; + matrixstack[0].identity(); + matrixstack[0].rotate_around_z(180*RAD); } }; diff --git a/source/src/md3.h b/source/src/md3.h index 18c5b73b..52a8489b 100644 --- a/source/src/md3.h +++ b/source/src/md3.h @@ -147,7 +147,9 @@ struct md3 : vertmodel void begingenshadow() { - glRotatef(180, 0, 0, 1); + matrixpos = 0; + matrixstack[0].identity(); + matrixstack[0].rotate_around_z(180*RAD); } }; diff --git a/source/src/protos.h b/source/src/protos.h index f619e2af..7bf19446 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -223,6 +223,7 @@ extern void blendbox(int x1, int y1, int x2, int y2, bool border, int tex = -1, extern void quad(GLuint tex, float x, float y, float s, float tx, float ty, float tsx, float tsy = 0); extern void quad(GLuint tex, vec &c1, vec &c2, float tx, float ty, float tsx, float tsy); extern void circle(GLuint tex, float x, float y, float r, float tx, float ty, float tr, int subdiv = 32); +extern void setperspective(float fovy, float aspect, float nearplane, float farplane); extern void sethudgunperspective(bool on); extern void gl_drawframe(int w, int h, float changelod, float curfps); extern void clearminimap(); @@ -245,6 +246,7 @@ struct Texture }; extern Texture *notexture, *noworldtexture; +extern void scaletexture(uchar *src, uint sw, uint sh, uint bpp, uchar *dst, uint dw, uint dh); extern void createtexture(int tnum, int w, int h, void *pixels, int clamp, bool mipmap, GLenum format); extern Texture *textureload(const char *name, int clamp = 0); extern Texture *lookuptexture(int tex, Texture *failtex = notexture); diff --git a/source/src/rendergl.cpp b/source/src/rendergl.cpp index cdeefb35..c00ec9c0 100644 --- a/source/src/rendergl.cpp +++ b/source/src/rendergl.cpp @@ -782,6 +782,12 @@ int xtraverts; VARP(hudgun, 0, 1, 1); +void setperspective(float fovy, float aspect, float nearplane, float farplane) +{ + GLdouble ydist = nearplane * tan(fovy/2*RAD), xdist = ydist * aspect; + glFrustum(-xdist, xdist, -ydist, ydist, nearplane, farplane); +} + void sethudgunperspective(bool on) { glMatrixMode(GL_PROJECTION); @@ -789,9 +795,9 @@ void sethudgunperspective(bool on) if(on) { glScalef(1, 1, 0.5f); // fix hudugns colliding with map geometry - gluPerspective(75.0f, aspect, 0.3f, farplane); // y fov fixed at 75° + setperspective(75.0f, aspect, 0.3f, farplane); // y fov fixed at 75° } - else gluPerspective(fovy, aspect, 0.15f, farplane); + else setperspective(fovy, aspect, 0.15f, farplane); glMatrixMode(GL_MODELVIEW); } @@ -890,7 +896,7 @@ void gl_drawframe(int w, int h, float changelod, float curfps) } farplane = fog*5/2; - gluPerspective(fovy, aspect, 0.15f, farplane); + setperspective(fovy, aspect, 0.15f, farplane); glMatrixMode(GL_MODELVIEW); transplayer(); diff --git a/source/src/scale.h b/source/src/scale.h new file mode 100644 index 00000000..6563cd15 --- /dev/null +++ b/source/src/scale.h @@ -0,0 +1,131 @@ +static void FUNCNAME(halvetexture)(uchar *src, uint sw, uint sh, uchar *dst) +{ + uint stride = sw*BPP; + for(uchar *yend = &src[sh*stride]; src < yend;) + { + for(uchar *xend = &src[stride]; src < xend; src += 2*BPP, dst += BPP) + { + #define OP(c, n) dst[n] = (uint(src[n]) + uint(src[n+BPP]) + uint(src[stride+n]) + uint(src[stride+n+BPP]))>>2 + PIXELOP + #undef OP + } + src += stride; + } +} + +static void FUNCNAME(shifttexture)(uchar *src, uint sw, uint sh, uchar *dst, uint dw, uint dh) +{ + uint stride = sw*BPP, wfrac = sw/dw, hfrac = sh/dh, wshift = 0, hshift = 0; + while(dw<>tshift + PIXELOP + #undef OP + } + src += (hfrac-1)*stride; + } +} + +static void FUNCNAME(scaletexture)(uchar *src, uint sw, uint sh, uchar *dst, uint dw, uint dh) +{ + uint stride = sw*BPP, wfrac = (sw<<12)/dw, hfrac = (sh<<12)/dh; + for(uint dy = 0, y = 0, yi = 0; dy < dh; dy++) + { + uint y2 = y + hfrac, yi2 = y2>>12, + h = y2 - y, ih = yi2 - yi, + ylow, yhigh; + if(yi < yi2) { ylow = 0x1000U - (y&0xFFFU); yhigh = y2&0xFFFU; } + else { ylow = y2 - y; yhigh = 0; } + + for(uint dx = 0, x = 0, xi = 0; dx < dw; dx++) + { + uint x2 = x + wfrac, xi2 = x2>>12, + w = x2 - x, iw = xi2 - xi, iy = 0, + xlow, xhigh; + if(xi < xi2) { xlow = 0x1000U - (x&0xFFFU); xhigh = x2&0xFFFU; } + else { xlow = x2 - x; xhigh = 0; } + + #define OP(c, n) c##t = 0 + DEFPIXEL + #undef OP + for(uchar *ycur = &src[xi*BPP], *xend = &ycur[max(iw, 1U)*BPP], *yend = &ycur[stride*(max(ih, 1U) + (yhigh ? 1 : 0))]; + ycur < yend; + ycur += stride, xend += stride, iy++) + { + #define OP(c, n) c = (ycur[n]*xlow)>>12 + DEFPIXEL + #undef OP + for(uchar *xcur = &ycur[BPP]; xcur < xend; xcur += BPP) + { + #define OP(c, n) c += xcur[n] + PIXELOP + #undef OP + } + if(xhigh) + { + #define OP(c, n) c += (xend[n]*xhigh)>>12 + PIXELOP + #undef OP + } + #define OP(c, n) c = (c<<12)/w + PIXELOP + #undef OP + if(!iy) + { + #define OP(c, n) c##t += (c*ylow)>>12 + PIXELOP + #undef OP + } + else if(iy==ih) + { + #define OP(c, n) c##t += (c*yhigh)>>12 + PIXELOP + #undef OP + } + else + { + #define OP(c, n) c##t += c + PIXELOP + #undef OP + } + } + + #define OP(c, n) dst[n] = ((c##t)<<12)/h + PIXELOP + #undef OP + + dst += BPP; + x = x2; + xi = xi2; + } + + src += ih*stride; + y = y2; + yi = yi2; + } +} + +#undef FUNCNAME +#undef DEFPIXEL +#undef PIXELOP +#undef BPP + diff --git a/source/src/shadow.cpp b/source/src/shadow.cpp index 6d4e3206..eb22dba4 100644 --- a/source/src/shadow.cpp +++ b/source/src/shadow.cpp @@ -437,7 +437,7 @@ void drawstencilshadows() glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(fovy, aspect, 0.15f, farplane); + setperspective(fovy, aspect, 0.15f, farplane); glMatrixMode(GL_MODELVIEW); glPopMatrix(); diff --git a/source/src/texture.cpp b/source/src/texture.cpp index 8d64e572..913b9257 100644 --- a/source/src/texture.cpp +++ b/source/src/texture.cpp @@ -3,6 +3,64 @@ #include "pch.h" #include "cube.h" +#define FUNCNAME(name) name##1 +#define DEFPIXEL uint OP(r, 0); +#define PIXELOP OP(r, 0); +#define BPP 1 +#include "scale.h" + +#define FUNCNAME(name) name##2 +#define DEFPIXEL uint OP(r, 0), OP(g, 1); +#define PIXELOP OP(r, 0); OP(g, 1); +#define BPP 2 +#include "scale.h" + +#define FUNCNAME(name) name##3 +#define DEFPIXEL uint OP(r, 0), OP(g, 1), OP(b, 2); +#define PIXELOP OP(r, 0); OP(g, 1); OP(b, 2); +#define BPP 3 +#include "scale.h" + +#define FUNCNAME(name) name##4 +#define DEFPIXEL uint OP(r, 0), OP(g, 1), OP(b, 2), OP(a, 3); +#define PIXELOP OP(r, 0); OP(g, 1); OP(b, 2); OP(a, 3); +#define BPP 4 +#include "scale.h" + +void scaletexture(uchar *src, uint sw, uint sh, uint bpp, uchar *dst, uint dw, uint dh) +{ + if(sw == dw*2 && sh == dh*2) + { + switch(bpp) + { + case 1: return halvetexture1(src, sw, sh, dst); + case 2: return halvetexture2(src, sw, sh, dst); + case 3: return halvetexture3(src, sw, sh, dst); + case 4: return halvetexture4(src, sw, sh, dst); + } + } + else if(sw < dw || sh < dh || sw&(sw-1) || sh&(sh-1)) + { + switch(bpp) + { + case 1: return scaletexture1(src, sw, sh, dst, dw, dh); + case 2: return scaletexture2(src, sw, sh, dst, dw, dh); + case 3: return scaletexture3(src, sw, sh, dst, dw, dh); + case 4: return scaletexture4(src, sw, sh, dst, dw, dh); + } + } + else + { + switch(bpp) + { + case 1: return shifttexture1(src, sw, sh, dst, dw, dh); + case 2: return shifttexture2(src, sw, sh, dst, dw, dh); + case 3: return shifttexture3(src, sw, sh, dst, dw, dh); + case 4: return shifttexture4(src, sw, sh, dst, dw, dh); + } + } +} + Texture *notexture = NULL, *noworldtexture = NULL; hashtable textures; @@ -12,6 +70,63 @@ VARFP(maxtexsize, 0, 0, 1<<12, initwarning("texture quality", INIT_LOAD)); VARFP(trilinear, 0, 1, 1, initwarning("texture filtering", INIT_LOAD)); VARFP(bilinear, 0, 1, 1, initwarning("texture filtering", INIT_LOAD)); +int formatsize(GLenum format) +{ + switch(format) + { + case GL_LUMINANCE: + case GL_ALPHA: return 1; + case GL_LUMINANCE_ALPHA: return 2; + case GL_RGB: return 3; + case GL_RGBA: return 4; + default: return 4; + } +} + +void resizetexture(int w, int h, bool mipmap, GLenum target, int &tw, int &th) +{ + int hwlimit = hwtexsize, + sizelimit = mipmap && maxtexsize ? min(maxtexsize, hwlimit) : hwlimit; + w = min(w, sizelimit); + h = min(h, sizelimit); + if(mipmap || w&(w-1) || h&(h-1)) + { + tw = th = 1; + while(tw < w) tw *= 2; + while(th < h) th *= 2; + if(w < tw - tw/2) tw /= 2; + if(h < th - th/2) th /= 2; + } + else + { + tw = w; + th = h; + } +} + +void uploadtexture(GLenum target, GLenum internal, int tw, int th, GLenum format, GLenum type, void *pixels, int pw, int ph, bool mipmap) +{ + int bpp = formatsize(format); + uchar *buf = NULL; + if(pw!=tw || ph!=th) + { + buf = new uchar[tw*th*bpp]; + scaletexture((uchar *)pixels, pw, ph, bpp, buf, tw, th); + } + for(int level = 0;; level++) + { + uchar *src = buf ? buf : (uchar *)pixels; + glTexImage2D(target, level, internal, tw, th, 0, format, type, src); + if(!mipmap || max(tw, th) <= 1) break; + int srcw = tw, srch = th; + if(tw > 1) tw /= 2; + if(th > 1) th /= 2; + if(!buf) buf = new uchar[tw*th*bpp]; + scaletexture(src, srcw, srch, bpp, buf, tw, th); + } + if(buf) delete[] buf; +} + void createtexture(int tnum, int w, int h, void *pixels, int clamp, bool mipmap, GLenum format) { glBindTexture(GL_TEXTURE_2D, tnum); @@ -27,24 +142,8 @@ void createtexture(int tnum, int w, int h, void *pixels, int clamp, bool mipmap, (bilinear ? GL_LINEAR : GL_NEAREST)); int tw = w, th = h; - if(pixels) - { - int sizelimit = maxtexsize ? min(maxtexsize, hwtexsize) : hwtexsize; - while(tw>sizelimit || th>sizelimit) { tw /= 2; th /= 2; } - if(tw!=w || th!=h) - { - if(gluScaleImage(format, w, h, GL_UNSIGNED_BYTE, pixels, tw, th, GL_UNSIGNED_BYTE, pixels)) - { - tw = w; - th = h; - } - } - } - if(mipmap) - { - if(gluBuild2DMipmaps(GL_TEXTURE_2D, format, tw, th, format, GL_UNSIGNED_BYTE, pixels)) fatal("could not build mipmaps"); - } - else glTexImage2D(GL_TEXTURE_2D, 0, format, tw, th, 0, format, GL_UNSIGNED_BYTE, pixels); + if(pixels) resizetexture(w, h, mipmap, GL_TEXTURE_2D, tw, th); + uploadtexture(GL_TEXTURE_2D, format, tw, th, format, GL_UNSIGNED_BYTE, pixels, w, h, mipmap); } GLenum texformat(int bpp) diff --git a/source/src/vertmodel.h b/source/src/vertmodel.h index bc187201..16ed994d 100644 --- a/source/src/vertmodel.h +++ b/source/src/vertmodel.h @@ -1006,7 +1006,7 @@ struct vertmodel : model SDL_FreeSurface(img); #endif if(aasize > 1<