Forum

fragment shader

Discuss programming topics for the various GPL'd game engine sources.

Moderator: InsideQC Admins

fragment shader

Postby revelator » Sun Feb 07, 2010 11:00 am

im having a bit of a weird problem with these.

added to realm and working but if i face down this happens ?

Image

looks like this when i face a bit higher

Image

the code modified from mh's

Code: Select all
void R_DrawWaterSurfaces (void)
{
   gltexture_t *tex;
   msurface_t   *surf;
   glpoly_t   *p;
   int         i;
   float      sadd, tadd;
   float      *v;

   // drawing parameters
   vector_t   warp1scale;
   vector_t   warp2scale;

   float      wateralpha;
   float      warp2sign;

   GLenum      WARP1_TEXENV;
   GLenum      WARP2_TEXENV;

   int         Warp1Texture;
   int         Warp2Texture;

   if (!waterthisframe) return;

   // back to the world matrix
   glLoadMatrixf (cl_entities[0].mvievmatrix);

   // enable blending
   glEnable (GL_BLEND);
   glDepthMask (GL_FALSE);

   glActiveTexture (GL_TEXTURE1);
   glEnable (GL_TEXTURE_2D);

   // enable vertex pointer - index 0 is the regular verts
   glEnableClientState (GL_VERTEX_ARRAY);
   glVertexPointer (3, GL_FLOAT, sizeof (waterverts_t), &waterarray[0]);

   // enable texcoord pointer for textures - index 3 is the st coords for warp 1
   glClientActiveTexture (GL_TEXTURE0);
   glEnableClientState (GL_TEXTURE_COORD_ARRAY);
   glTexCoordPointer (2, GL_FLOAT, sizeof (waterverts_t), &waterarray[3]);

   // enable texcoord pointer for textures - index 5 is the st coords for warp 2
   glClientActiveTexture (GL_TEXTURE1);
   glEnableClientState (GL_TEXTURE_COORD_ARRAY);
   glTexCoordPointer (2, GL_FLOAT, sizeof (waterverts_t), &waterarray[5]);

   // go through the texture list
   for (tex = TextureList; tex; tex = tex->TextureList)
   {
      // nothing chained
      if (!(surf = tex->texturechain)) continue;

      // it ain't liquid
      if (!(surf->flags & SURF_DRAWTURB)) continue;

      // setup rendering parameters
      // these are actually absolutely excellent
      if (surf->flags & SURF_DRAWLAVA)
      {
         glDisable (GL_BLEND);
         glDepthMask (GL_TRUE);

         // because this layer isn't visible, give it a massive size to make rendering it less expensive
         // (actually this doesn't matter cos the texture matrix is ignored here...)
         warp1scale.vec.x = 1.0f / 1024.0f;
         warp1scale.vec.y = 1.0f / 1024.0f;
         warp1scale.vec.z = 1.0f;

         wateralpha = 1.0f;

         warp2scale.vec.x = 1.0f / 128.0f;
         warp2scale.vec.y = 1.0f / 128.0f;
         warp2scale.vec.z = 1.0f;

         warp2sign = 1;

         // special - instruct the renderer to ignore fancy stuff here
         WARP1_TEXENV = GL_ZERO;

         // make the 2nd warp a replace texenv also to avoid dual layer madness with lava
         WARP2_TEXENV = GL_REPLACE;

         // we could use any old crap for warp1 here..
         Warp1Texture = surf->texinfo->texture->gl_texture->gltexnum;
         Warp2Texture = surf->texinfo->texture->gl_texture->gltexnum;
      }
      else if (surf->flags & SURF_DRAWTELE)
      {
         glDisable (GL_BLEND);
         glDepthMask (GL_TRUE);

         warp1scale.vec.x = 1.0f / 64.0f;
         warp1scale.vec.y = 1.0f / 64.0f;
         warp1scale.vec.z = 1.0f;

         wateralpha = 1.0f;

         warp2scale.vec.x = 1.0f / 24.0f;
         warp2scale.vec.y = 1.0f / 24.0f;
         warp2scale.vec.z = 1.0f;

         warp2sign = 1;

         WARP1_TEXENV = GL_REPLACE;
         WARP2_TEXENV = GL_ADD;

         Warp1Texture = surf->texinfo->texture->gl_texture->gltexnum;
         Warp2Texture = surf->texinfo->texture->gl_texture->gltexnum;
      }
      else if (surf->flags & SURF_DRAWSLIME)
      {
         glEnable (GL_BLEND);
         glDepthMask (GL_FALSE);

         warp1scale.vec.x = 1.0f / 128.0f;
         warp1scale.vec.y = 1.0f / 128.0f;
         warp1scale.vec.z = 1.0f;

         wateralpha = 0.45f;

         warp2scale.vec.x = 1.0f / 256.0f;
         warp2scale.vec.y = 1.0f / 256.0f;
         warp2scale.vec.z = 1.0f;

         warp2sign = -1;

         WARP1_TEXENV = GL_MODULATE;
         WARP2_TEXENV = GL_ADD;

         Warp1Texture = surf->texinfo->texture->gl_texture->gltexnum;
         Warp2Texture = surf->texinfo->texture->gl_texture->gltexnum;
      }
      else
      {
         glEnable (GL_BLEND);
         glDepthMask (GL_FALSE);

         warp1scale.vec.x = 1.0f / 128.0f;
         warp1scale.vec.y = 1.0f / 128.0f;
         warp1scale.vec.z = 1.0f;

         wateralpha = 0.4f;

         warp2scale.vec.x = 1.0f / 256.0f;
         warp2scale.vec.y = 1.0f / 256.0f;
         warp2scale.vec.z = 1.0f;

         warp2sign = 1;

         WARP1_TEXENV = GL_MODULATE;
         WARP2_TEXENV = GL_ADD;

         Warp1Texture = surf->texinfo->texture->gl_texture->gltexnum;
         Warp2Texture = surf->texinfo->texture->gl_texture->gltexnum;
      }

      // added arb shader for non nvidia cards
      if (GL_ExtensionBits & HAS_ARBFRAGMENTSHADERS)
      {
         // tmu 1 active here
         glBindTexture (GL_TEXTURE_2D, r_dst_texture.gltexnum);
      }

      // apply parameters to tmu 0
      glActiveTexture (GL_TEXTURE0);

      // only do this if we're doing dual layer, otherwise it's cheaper to do a very basic render than it
      // would be to change TMU states
      if (WARP1_TEXENV != GL_ZERO)
      {
         glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, WARP1_TEXENV);
         glMatrixMode (GL_TEXTURE);
         glLoadIdentity ();
         glScalef (warp1scale.vecs[0], warp1scale.vecs[1], warp1scale.vecs[2]);
         glMatrixMode (GL_MODELVIEW);
      }
      glBindTexture (GL_TEXTURE_2D, Warp1Texture);

      // added arb shader for non nvidia cards
      if (GL_ExtensionBits & HAS_ARBFRAGMENTSHADERS)
      {
         glEnable (GL_FRAGMENT_PROGRAM_ARB);
         glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fragment_programs[F_PROG_WARP]);      
         glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 0.7, 0.7, 0.7, 1);
      }

      // added arb shader for non nvidia cards
      if (GL_ExtensionBits & HAS_ARBFRAGMENTSHADERS)
      {
         // tmu 0 active here
         glBindTexture (GL_TEXTURE_2D, r_dst_texture.gltexnum);
      }

      // apply parameters to tmu 1
      glActiveTexture (GL_TEXTURE1);

      glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, WARP2_TEXENV);
      glMatrixMode (GL_TEXTURE);
      glLoadIdentity ();
      glScalef (warp2scale.vecs[0], warp2scale.vecs[1], warp2scale.vecs[2]);
      glMatrixMode (GL_MODELVIEW);

      glBindTexture (GL_TEXTURE_2D, Warp2Texture);

      // added arb shader for non nvidia cards
      if (GL_ExtensionBits & HAS_ARBFRAGMENTSHADERS)
      {
         glEnable (GL_FRAGMENT_PROGRAM_ARB);
         glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fragment_programs[F_PROG_WARP]);      
         glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 0.7, 0.7, 0.7, 1);
      }

      // set the colour
      glColor4f (1, 1, 1, wateralpha);

      // go through the surfs in the chain
      for (/**/; surf; surf = surf->texturechain)
      {
         // water surfs have a chained list of polys rather than just 1
         for (p = surf->polys; p; p = p->next)
         {
            // need to go through each vert in the list to set the warped texcoords
            for (i = 0; i < p->numverts; i++)
            {
               // get a pointer to the verts to keep the code more readable
               v = p->water[i].v;

               // calculate the amount to add for the warp
               sadd = STSinTable[(int) ((v[surf->warpindex[1]] * 0.025 + realtime) * STMult) & STBitMask];
               tadd = STSinTable[(int) ((v[surf->warpindex[0]] * 0.025 + realtime) * STMult) & STBitMask];

               // set the warp verts
               // fix water warping - derive the texcoords from the verts!!!  mad, but it works!!!
               // warpindex is precalculated in gl_model.c - use the x2 STSinTable for 8 unit warps
               p->water[i].warp1[0] = v[surf->warpindex[0]] + sadd;
               p->water[i].warp1[1] = v[surf->warpindex[1]] + tadd;

               // warp 2
               p->water[i].warp2[0] = (v[surf->warpindex[0]] * warp2sign) + sadd;
               p->water[i].warp2[1] = (v[surf->warpindex[1]] * warp2sign) + tadd;
            }

            // draw the vertex array
            glDrawArrays (GL_TRIANGLE_FAN, p->waterindex, p->numverts);
         }
      }
   }

   // restore state on tmu 0
   glActiveTexture (GL_TEXTURE1);
   glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   glMatrixMode (GL_TEXTURE);
   glLoadIdentity ();
   glMatrixMode (GL_MODELVIEW);
   glDisable (GL_TEXTURE_2D);

   // restore state on tmu 1
   glActiveTexture (GL_TEXTURE0);
   glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   glMatrixMode (GL_TEXTURE);
   glLoadIdentity ();
   glMatrixMode (GL_MODELVIEW);

   // kill vertex arrays
   glClientActiveTexture (GL_TEXTURE1);
   glDisableClientState (GL_TEXTURE_COORD_ARRAY);

   if (GL_ExtensionBits & HAS_ARBFRAGMENTSHADERS)
   {
      glDisable (GL_FRAGMENT_PROGRAM_ARB);
   }
   glClientActiveTexture (GL_TEXTURE0);
   glDisableClientState (GL_TEXTURE_COORD_ARRAY);

   if (GL_ExtensionBits & HAS_ARBFRAGMENTSHADERS)
   {
      glDisable (GL_FRAGMENT_PROGRAM_ARB);
   }
   glColor4f (1, 1, 1, 1);

   // draw fog if we're underwater
   if (!telethisframe)
   {
      float fogdist;
      float fogalpha;

      // if teleports or lava are drawn last they'll leave blending disabled coming in here, so
      // make a potentially redundant state change.  it's only once per frame, so live with it.
      glEnable (GL_BLEND);
      glDepthMask (GL_FALSE);

      glEnableClientState (GL_COLOR_ARRAY);
      glColorPointer (4, GL_FLOAT, sizeof (waterverts_t), &waterarray[7]);

      // turn off texturing and ensure we have smooth shading
      glDisable (GL_TEXTURE_2D);
      glShadeModel (GL_SMOOTH);

      // default blendfunc for this
      glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

      // go through the texture list
      for (tex = TextureList; tex; tex = tex->TextureList)
      {
         // nothing chained
         if (!(surf = tex->texturechain)) continue;

         // we are not below water
         if (!(surf->flags & SURF_UNDERWATER)) continue;

         // it ain't liquid
         if (!(surf->flags & SURF_DRAWTURB)) continue;

         // don't put fog on teleports
         if (surf->flags & SURF_DRAWTELE) continue;

         // go through the surfs in the chain
         for (/**/; surf; surf = surf->texturechain)
         {
            // water surfs have a chained list of polys rather than just 1
            for (p = surf->polys; p; p = p->next)
            {
               // need to go through each vert in the list to set the colours
               for (i = 0; i < p->numverts; i++)
               {
                  // get a pointer to the verts to keep the code more readable
                  v = p->water[i].v;

                  p->water[i].rgba[0] = surf->texinfo->texture->gl_texture->rgba[0];
                  p->water[i].rgba[1] = surf->texinfo->texture->gl_texture->rgba[1];
                  p->water[i].rgba[2] = surf->texinfo->texture->gl_texture->rgba[2];

                  if (surf->flags & SURF_DRAWLAVA)
                  {
                     // constant fog
                     fogalpha = FOG_ALPHA_LAVA_CONST;
                  }
                  else if (surf->flags & SURF_DRAWSLIME)
                  {
                     // constant fog
                     fogalpha = FOG_ALPHA_SLIME_CONST;
                  }
                  else
                  {
                     // distance fog
                     // use the transformed verts to get the dist
                     fogdist = (v[0] - r_origin[0]) * vpn[0] + (v[1] - r_origin[1]) * vpn[1] + (v[2] - r_origin[2]) * vpn[2];

                     if (fogdist < 0)
                     {
                        fogdist = 0;
                     }
                     else if (fogdist > FOG_RANGE)
                     {
                        fogdist = FOG_RANGE;
                     }
                     fogalpha = 0.3f + (fogdist / FOG_RANGE * FOG_ALPHA_MAX) / 2;

                     // boost alpha a little cos the translucent water can make it hard to see
                     fogalpha *= 1.25f;
                  }
                  p->water[i].rgba[3] = fogalpha;
               }

               // draw the vertex array
               glDrawArrays (GL_TRIANGLE_FAN, p->waterindex, p->numverts);
            }
         }
      }

      // bring texturing back
      glEnable (GL_TEXTURE_2D);

      // paranoia!!!
      glColor4f (1, 1, 1, 1);

      // now bring vertex arrays back to the way they were.
      glDisableClientState (GL_COLOR_ARRAY);
   }
   glDisableClientState (GL_VERTEX_ARRAY);

   // potentially redundant if teleports or lava were drawn last and we're not underwater.
   // it's only once per frame so live with it.
   glDisable (GL_BLEND);
   glDepthMask (GL_TRUE);
}
User avatar
revelator
 
Posts: 2567
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Postby ceriux » Sun Feb 07, 2010 3:24 pm

that looks like the half-life chrome effect that they use on mdls , looks bad ass though!
User avatar
ceriux
 
Posts: 2223
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Postby revelator » Sun Feb 07, 2010 5:05 pm

hehe aye i just hope i can get it fixed ;)

problem as far as i can understand is the dual layer code (cheap and dirty shader like effect) leftover from the original code since it uses both tmu's i kinda hit a brick wall erf :?

dunno if at all possible without making a gdamn mess :lol:
User avatar
revelator
 
Posts: 2567
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Postby ceriux » Sun Feb 07, 2010 6:22 pm

if you get it working , you should write a tutorial on what you did =)
User avatar
ceriux
 
Posts: 2223
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Postby revelator » Mon Feb 08, 2010 1:05 am

in standard quake its pretty straight forward :)

uses much the same way as qmb orignally did it with the exception
that qmb's shaders only worked on nvidia cards.

the back bone is this function

Code: Select all
/*
===============
R_InitDSTTex

Create the texture which warps texture shaders
===============
*/
void R_InitDSTTex (void)
{
#define DST_SIZE 16
   signed char data[DST_SIZE][DST_SIZE][2];
   int         x, y;

   srand(GetTickCount());

   for (x = 0; x < DST_SIZE; x++)
   {
      for (y = 0; y < DST_SIZE; y++)
      {
         data[x][y][0] = rand() % 255;
         data[x][y][1] = rand() % 255;
         data[x][y][2] = rand() % 48;
         data[x][y][3] = rand() % 48;
      }
   }

   // set up a gltexture_t and a texture object for it
   memset (&r_dst_texture, 0, sizeof (gltexture_t));
   GL_MakeTextureObjects (1, (GLuint *)&r_dst_texture.gltexnum);

   glBindTexture (GL_TEXTURE_2D, r_dst_texture.gltexnum);
   glTexImage2D (GL_TEXTURE_2D, 0, 4, DST_SIZE, DST_SIZE, 0, GL_RGBA, GL_BYTE, data);

   glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
#undef DST_SIZE
}


call it from R_Init

GL_MakeTextureObjects is basically the same as glGenTextures

and heres the shader portion

Code: Select all
/*
=============================================================================

PROGRAM MANAGEMENT

=============================================================================
*/

//
// local[0] - distortion strength (comes from cvar, 1.0 is default)
//

static char fragment_program_heathazemask[] = 
"!!ARBfp1.0\n"
"OPTION ARB_precision_hint_fastest;\n"

"TEMP R0, R1, R2;\n"

"TEX R0, fragment.texcoord[0], texture[0], 2D;\n"

// fetch the water texture
"ADD R1, R0, fragment.texcoord[2];\n"

//"TEX R1, R1, texture[2], 2D;\n" // bump 3th tex
"TEX R1, fragment.texcoord[2], texture[2], 2D;\n" // dont bump 3th tex

// scale offset by the distortion factor
"MUL R0, R0, fragment.texcoord[3];\n"   //error was MUL

// apply the alpha mask
"TEX R2, fragment.texcoord[2], texture[3], 2D;\n"
"MUL R0, R0, R2;\n"

// calculate the screen texcoord in the 0.0 to 1.0 range
"MUL R2, fragment.position, program.local[0];\n"

// offset by the scaled ds/dt and clamp it to 0.0 - 1.0
"ADD_SAT R2, R0, R2;\n"

// scale by the screen non-power-of-two-adjust
"MUL R2, R2, program.local[1];\n"

// fetch the screen texture
"TEX R2, R2, texture[1], 2D;\n"

"MUL R1, R1, fragment.color;\n"
//"ADD result.color, R2, R1;\n"
"ADD R2, R2, R1;\n"
"MUL result.color, R2, program.local[3];\n"

"END\n";

// KRIGSSVIN: modified for visual equality with 3 pass version
static char fragment_program_warp[] =
"!!ARBfp1.0\n"
//"OPTION ARB_precision_hint_fastest;\n"
"OPTION ARB_precision_hint_nicest;\n"

"PARAM rgbscale = program.local[0];\n"
"TEMP offset, coord, dist, col;\n"

"TEX offset, fragment.texcoord[0], texture[0], 2D;\n"
"MUL offset, offset, 0.5;\n"

// fetch the water texture
"ADD coord.x, fragment.texcoord[1].x, offset.z;\n"
"ADD coord.y, fragment.texcoord[1].y, offset.w;\n"
"TEX dist, coord, texture[1], 2D;\n"
"MUL col, dist, fragment.color;\n"
"MUL result.color, col, rgbscale;\n"

"END\n";


static char fragment_program_water_distort[] =
"!!ARBfp1.0\n"

// Scroll and scale the distortion texture coordinates.
// Scroll coordinates are specified externally.
"PARAM scroll1 = program.local[0];\n"
"PARAM scroll2 = program.local[1];\n"
"PARAM texScale1 = { 0.008, 0.008, 1.0, 1.0 };\n"
"PARAM texScale2 = { 0.007, 0.007, 1.0, 1.0 };\n"
"TEMP texCoord1;\n"
"TEMP texCoord2;\n"
"MUL texCoord1, fragment.texcoord[1], texScale1;\n"
"MUL texCoord2, fragment.texcoord[1], texScale2;\n"
"ADD texCoord1, texCoord1, scroll1;\n"
"ADD texCoord2, texCoord2, scroll2;\n"

// Load the distortion textures and add them together.
"TEMP distortColor;\n"
"TEMP distortColor2;\n"
"TXP distortColor, texCoord1, texture[1], 2D;\n"
"TXP distortColor2, texCoord2, texture[1], 2D;\n"
"ADD distortColor, distortColor, distortColor2;\n"

// Subtract 1.0 and scale by 2.0.
// Textures will be distorted from -2.0 to 2.0 texels.
"PARAM scaleFactor = { 2.0, 2.0, 2.0, 2.0 };\n"
"PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n"
"SUB distortColor, distortColor, one;\n"
"MUL distortColor, distortColor, scaleFactor;\n"

// Apply distortion to reflection texture coordinates.
"TEMP distortCoord;\n"
"TEMP endColor;\n"
"ADD distortCoord, distortColor, fragment.texcoord[0];\n"
"TXP endColor, distortCoord, texture, 2D;\n"

// Get a vector from the surface to the view origin
"PARAM vieworg = program.local[2];\n"
"TEMP eyeVec;\n"
"TEMP trans;\n"
"SUB eyeVec, vieworg, fragment.texcoord[1];\n"

// Normalize the vector to the eye position
"TEMP temp;\n"
"TEMP invLen;\n"
"DP3 temp, eyeVec, eyeVec;\n"
"RSQ invLen, temp.x;\n"
"MUL eyeVec, eyeVec, invLen;\n"
"ABS eyeVec.z, eyeVec.z;\n" // so it works underwater, too

// Load the ripple normal map
"TEMP normalColor;\n"
"TEMP normalColor2;\n"
// Scale texture
"MUL texCoord1, fragment.texcoord[2], texScale2;\n"
"MUL texCoord2, fragment.texcoord[2], texScale1;\n"
// Scroll texture
"ADD texCoord1, texCoord1, scroll1;\n"
"ADD texCoord2, texCoord2, scroll2;\n"
// Get texel color
"TXP normalColor, texCoord1, texture[2], 2D;\n"
"TXP normalColor2, texCoord2, texture[2], 2D;\n"
// Combine normal maps
"ADD normalColor, normalColor, normalColor2;\n"
"SUB normalColor, normalColor, 1.0;\n"

// Normalize normal texture
"DP3 temp, normalColor, normalColor;\n"
"RSQ invLen, temp.x;\n"
"MUL normalColor, invLen, normalColor;\n"

// Fresenel approximation
"DP3 trans.w, normalColor, eyeVec;\n"
"SUB endColor.w, 1.0, trans.w;\n"
"MAX endColor.w, endColor.w, 0.2;\n" // MAX sets the min?  How odd.
"MIN endColor.w, endColor.w, 0.8;\n" // Leave a LITTLE bit of transparency always

// Put the color in the output (TODO: put this in final OP)
"MOV result.color, endColor;\n"

"END\n";


static char vertex_program_distort[] =
"!!ARBvp1.0\n"
"OPTION ARB_position_invariant;\n"

"PARAM vec = { 1.0, 0.0, 0.0, 1.0 };\n"
"PARAM dtc = { 0.0, 0.5, 0.0, 1.0 };\n"

"TEMP R0, R1, R2;\n"

"MOV          R0, vec;\n"
"DP4          R0.z, vertex.position, state.matrix.modelview.row[2];\n"

"DP4          R1, R0, state.matrix.projection.row[0];\n"
"DP4          R2, R0, state.matrix.projection.row[3];\n"

// don't let the recip get near zero for polygons that cross the view plane
"MAX          R2, R2, 1.0;\n"

"RCP          R2, R2.w;\n"
"MUL          R1, R1, R2;\n"

// clamp the distance so the deformations don't get too wacky near the view
"MIN          R1, R1, 0.02;\n"

"MOV          result.texcoord[0], dtc;\n"
"DP4          result.texcoord[0].x, vertex.texcoord[0], state.matrix.texture[0].row[0];\n"
"DP4          result.texcoord[0].y, vertex.texcoord[0], state.matrix.texture[0].row[1];\n"

"MOV          result.texcoord[2], dtc;\n"
"DP4          result.texcoord[2].x, vertex.texcoord[2], state.matrix.texture[2].row[0];\n"
"DP4          result.texcoord[2].y, vertex.texcoord[2], state.matrix.texture[2].row[1];\n"

"MUL          result.texcoord[3], R1, program.local[0];\n"

"MOV          result.color, vertex.color;\n"

"END\n";

static char *fragment_progs[NUM_FRAGMENT_PROGRAM] =
{
   fragment_program_heathazemask,
   fragment_program_warp,
   fragment_program_water_distort
};

static char *vertex_progs[NUM_VERTEX_PROGRAM] =
{
   vertex_program_distort
};

GLuint   fragment_programs[NUM_FRAGMENT_PROGRAM];
GLuint   vertex_programs[NUM_VERTEX_PROGRAM];

/*
=============
GL_InitShaders
=============
*/
void GL_InitShaders (void)
{
   int            i, error_pos;
   const GLubyte   *errors;

   if (GL_ExtensionBits & HAS_ARBFRAGMENTSHADERS)
   {
      // fragment programs
      glGenProgramsARB(NUM_FRAGMENT_PROGRAM, &fragment_programs[0]);

      for (i = 0; i < NUM_FRAGMENT_PROGRAM; i++)
      {
         glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fragment_programs[i]);
         glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei) strlen(fragment_progs[i]), fragment_progs[i]);
         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);

         if (error_pos != -1)
         {
            errors = glGetString(GL_PROGRAM_ERROR_STRING_ARB);   
            Con_Printf ("FragmentProgram error at position %d in shader %d\nARB_ERROR: %s\n", error_pos, fragment_programs[i], errors);
            glDeleteProgramsARB(1, &fragment_programs[i]);
            break;
         }
      }
   }

   // vertex programs
   if (GL_ExtensionBits & HAS_ARBVERTEXPROGRAMS)
   {
      glGenProgramsARB(NUM_VERTEX_PROGRAM, &vertex_programs[0]);

      for (i = 0; i < NUM_VERTEX_PROGRAM; i++)
      {
         glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertex_programs[i]);
         glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei) strlen(vertex_progs[i]), vertex_progs[i]);
         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);

         if (error_pos != -1)
         {
            errors = glGetString(GL_PROGRAM_ERROR_STRING_ARB);   
            Con_Printf ("VertexProgram error at position %d in shader %d\nARB_ERROR: %s\n", error_pos, vertex_programs[i], errors);
            glDeleteProgramsARB(1, &vertex_programs[i]);
            break;
         }
      }
   }
}


call GL_InitShaders first in R_Init so its visible to the entire renderer

most newer cards can do arb shading with a few exceptions so to be safe we need to check if that extension is availiable.

now in EmitWaterpolys we need to be in tmu 0 (problem i have is mine uses tmu1 as the incomming call)

so at the top before anything else we declare this

Code: Select all
   GL_SelectTexture(GL_TEXTURE0_ARB);
   glBindTexture(GL_TEXTURE_2D,fa->texinfo->texture->gl_texturenum);


and after that

Code: Select all
   if (gl_shader) {
      glBindTexture(GL_TEXTURE_2D,dst_texture);

      
      GL_EnableTMU(GL_TEXTURE1_ARB);
      glBindTexture(GL_TEXTURE_2D,fa->texinfo->texture->gl_texturenum);

      GL_Enable (GL_FRAGMENT_PROGRAM_ARB);
      qglBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fragment_programs[F_PROG_WARP]);
      qglProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 0.7, 0.7, 0.7, 1.0);

   }
   else
   {
                                // normal rendering
      GL_EnableTMU(GL_TEXTURE1_ARB);
      glBindTexture(GL_TEXTURE_2D,fa->texinfo->texture->gl_texturenum);

      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   }


and after the for loop and what else we need to take down the fragment shader.

if (gl_shader)
{
glDisable(GL_FRAGMENT_PROGRAM_ARB);
}

and thats pretty much it :)

this is a pretty simple fragment shader

glProgramLocalParameter4fARB can handle pretty much everything
the for loop does also but thats quite a mouthfull to take in as a starter project ;)

glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, texturemovementcode , same, same, 1)

glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, -texturemovementcode , -same, -same, 1)

glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 2, watercolor , same, same, 1)

glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 3, lightmapcoordinates , same, same, 1)

see :)
User avatar
revelator
 
Posts: 2567
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Postby ceriux » Mon Feb 08, 2010 2:07 am

so you got it working correctly?
User avatar
ceriux
 
Posts: 2223
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Postby revelator » Mon Feb 08, 2010 5:34 am

better but nope still bugs when viewing down :S

noticed it only affecting lava surfs the texenv might be fubared at the second pass or i t might be an incompatibility with the lava haze fragment program ?

tbh im itchint to rewrite the entire :( thing
User avatar
revelator
 
Posts: 2567
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Postby Teiman » Mon Feb 08, 2010 2:47 pm

try to disable particle rendering, or bmodels rendering other than the map, or something. maybe you have a conflict.

you have tested this server outside or quake and it works? maybe is rendering what is supposed to.
Teiman
 
Posts: 309
Joined: Sun Jun 03, 2007 9:39 am

Postby r00k » Mon Feb 08, 2010 5:39 pm

not sure if this is related, but gl_subdivide_size have any effect on this? I've only seen the effect of your 1st picture if modifyiny gl_subdivide_size 1024 etc...


warp1scale.vec.x = 1.0f / 1024.0f;
warp1scale.vec.y = 1.0f / 1024.0f;



change that 1024 and see what happens.
r00k
 
Posts: 1110
Joined: Sat Nov 13, 2004 10:39 pm

Postby mh » Mon Feb 08, 2010 6:42 pm

It looks like texture scaling has gone wrong somewhere. To be honest I don't even remember the original code any more, and if it was me I'd be inclined to rewrite.
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
User avatar
mh
 
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Postby revelator » Tue Feb 09, 2010 4:17 am

aye its a weird bugger :/

another thing i noticed is that lava surface will shift direction based on distance (far away looks ok near and the surface changes direction) ? again only lava is affected :lol:

ill try the various things you suggested maybe i get lucky ;)
User avatar
revelator
 
Posts: 2567
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger


Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 1 guest