Forum

Can anyone see what's wrong with this?

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

Moderator: InsideQC Admins

Can anyone see what's wrong with this?

Postby hogsy » Wed Dec 07, 2011 5:10 pm

So basically I want to assign a texture to each particle but for some reason in-game instead of the texture I want being assigned just a random texture that's already in memory is being assigned instead... I have no idea what's going on here so if someone can help it'll be of huge help. Ignore the fact that it's messy, I haven't gotten round to cleaning it up yet and I've been busy throwing bits around seeing if I can fix any of it. Even just assigning an already loaded texture won't work (notexture for example).

Code: Select all
#include "quakedef.h"

int      ramp2[8] = {0x6f, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x68, 0x66};
int      ramp3[8] = {0x6d, 0x6b, 6, 5, 4, 3};

particle_t   *active_particles, *free_particles, *particles;

vec3_t   r_pright, r_pup, r_ppn;

int   r_numparticles, texturenum = 0;

float texturescalefactor = 7.0; //johnfitz -- compensate for apparent size of different particle textures

struct   gltexture_s   *parttex[32];

void R_SetParticleTexture(char *texture)
{
   byte   *tex;
   char   name[32];
   int      i,w=64,h=64;

   sprintf(name,"textures/particles/%s",texture);

   Con_Printf("Texture: %s\n",name);   //temp

   // Check for any matching textures...
   for(i=0;i<sizeof(parttex);i++)
      if(parttex[i])
         if(!Q_strcmp(name,parttex[i]->name))
         {
            texturenum = i;
            return;
         }

   // No matching texture...
   for(i=0;i<sizeof(parttex);i++)
      if(!parttex[i])
      {
         tex = Image_LoadImage(name,&w,&h);
         if(tex)
         {
            parttex[i] = TexMgr_LoadImage(NULL,name,w,h,SRC_RGBA,tex,name,0,TEXPREF_ALPHA);
            texturenum = i;
            return;
         }
         else
         {
            parttex[i] = notexture;
            Con_Warning("Failed to load %s\n",name);
            return;
         }
      }
}

void R_InitParticles (void)
{
   int i = COM_CheckParm ("-particles");

   if (i)
   {
      r_numparticles = (int)(Q_atoi(com_argv[i+1]));
      if (r_numparticles < MIN_PARTICLES)
         r_numparticles = MIN_PARTICLES;
   }
   else
      r_numparticles = MAX_PARTICLES;

   particles = (particle_t *)Hunk_AllocName (r_numparticles * sizeof(particle_t), "particles");
}

#define NUMVERTEXNORMALS   162
extern   float   r_avertexnormals[NUMVERTEXNORMALS][3];
vec3_t   avelocities[NUMVERTEXNORMALS];
float   beamlength = 16;
vec3_t   avelocity = {23, 7, 3};
float   partstep = 0.01;
float   timescale = 0.01;

void R_EntityParticles (entity_t *ent)
{
   int         count = 50;
   int         i;
   particle_t   *p;
   float      angle;
   float      sr, sp, sy, cr, cp, cy;
   vec3_t      forward;
   float      dist = 64;

   if (!avelocities[0][0])
      for (i=0 ; i<NUMVERTEXNORMALS*3 ; i++)
         avelocities[0][i] = (rand()&255) * 0.01;

   for (i=0 ; i<NUMVERTEXNORMALS ; i++)
   {
      angle = cl.time * avelocities[i][0];
      sy = sin(angle);
      cy = cos(angle);
      angle = cl.time * avelocities[i][1];
      sp = sin(angle);
      cp = cos(angle);
      angle = cl.time * avelocities[i][2];
      sr = sin(angle);
      cr = cos(angle);

      forward[0] = cp*cy;
      forward[1] = cp*sy;
      forward[2] = -sp;

      if (!free_particles)
         return;
      p = free_particles;
      free_particles = p->next;
      p->next = active_particles;
      active_particles = p;

      p->die = cl.time + 0.01;
      p->r = p->g = p->b = 255;
      p->type = pt_explode;
      p->org[0] = ent->origin[0] + r_avertexnormals[i][0]*dist + forward[0]*beamlength;
      p->org[1] = ent->origin[1] + r_avertexnormals[i][1]*dist + forward[1]*beamlength;
      p->org[2] = ent->origin[2] + r_avertexnormals[i][2]*dist + forward[2]*beamlength;
   }
}

void R_ClearParticles (void)
{
   int      i;

   free_particles = &particles[0];
   active_particles = NULL;

   for (i=0 ;i<r_numparticles ; i++)
      particles[i].next = &particles[i+1];
   particles[r_numparticles-1].next = NULL;
}

void R_ReadPointFile_f (void)
{
   FILE   *f;
   vec3_t   org;
   int      r;
   int      c;
   particle_t   *p;
   char   name[MAX_OSPATH];

   sprintf (name,"maps/%s.pts", sv.name);

   COM_FOpenFile (name, &f);
   if (!f)
   {
      Con_Printf ("couldn't open %s\n", name);
      return;
   }

   Con_Printf ("Reading %s...\n", name);
   c = 0;
   for ( ;; )
   {
      r = fscanf (f,"%f %f %f\n", &org[0], &org[1], &org[2]);
      if (r != 3)
         break;
      c++;

      if (!free_particles)
      {
         Con_Printf ("Not enough free particles\n");
         break;
      }
      p = free_particles;
      free_particles = p->next;
      p->next = active_particles;
      active_particles = p;

      p->die = 99999;
      p->r = p->g = p->b = 255;   //temp
      p->type = pt_static;
      VectorCopy (vec3_origin, p->vel);
      VectorCopy (org, p->org);
   }

   fclose (f);
   Con_Printf ("%i points read\n", c);
}

void R_ParseParticleEffect (void)
{
   vec3_t   org, dir;
   int      i, count, msgcount;
   int      r,g,b;
   char   *texture;

   for (i=0 ; i<3 ; i++)
      org[i] = MSG_ReadCoord ();
   for (i=0 ; i<3 ; i++)
      dir[i] = MSG_ReadChar () * (1.0/16);

   texture = MSG_ReadString();

   msgcount = MSG_ReadByte();

   if (msgcount == 255)
      count = 1024;
   else
      count = msgcount;

   R_RunParticleEffect(org, dir, 7, count, texture);
}

void R_ParticleExplosion (vec3_t org)
{
   int         i, j;
   particle_t   *p;

   for (i=0 ; i<1024 ; i++)
   {
      if (!free_particles)
         return;
      p = free_particles;
      free_particles = p->next;
      p->next = active_particles;
      active_particles = p;

      p->die = cl.time + 5;
      p->r = 255;
      p->g = 204;
      p->b = 51;
      
      p->ramp = rand()&3;
      if (i & 1)
      {
         p->type = pt_explode;
         for (j=0 ; j<3 ; j++)
         {
            p->org[j] = org[j] + ((rand()%32)-16);
            p->vel[j] = (rand()%512)-256;
         }
      }
      else
      {
         p->type = pt_explode2;
         for (j=0 ; j<3 ; j++)
         {
            p->org[j] = org[j] + ((rand()%32)-16);
            p->vel[j] = (rand()%512)-256;
         }
      }
   }
}

void R_ParticleExplosion2 (vec3_t org, int colorStart, int colorLength)
{
   int         i, j;
   particle_t   *p;
   int         colorMod = 0;

   for (i=0; i<512; i++)
   {
      if (!free_particles)
         return;
      p = free_particles;
      free_particles = p->next;
      p->next = active_particles;
      active_particles = p;

      p->die = cl.time + 0.3;
      p->r = p->g = p->b = colorStart + (colorMod % colorLength);

      colorMod++;

      p->type = pt_blob;
      for (j=0 ; j<3 ; j++)
      {
         p->org[j] = org[j] + ((rand()%32)-16);
         p->vel[j] = (rand()%512)-256;
      }
   }
}

void R_BlobExplosion (vec3_t org)
{
   int         i, j;
   particle_t   *p;

   for (i=0 ; i<1024 ; i++)
   {
      if (!free_particles)
         return;
      p = free_particles;
      free_particles = p->next;
      p->next = active_particles;
      active_particles = p;

      p->die = cl.time + 1 + (rand()&8)*0.05;

      if (i & 1)
      {
         p->type = pt_blob;
         p->r = p->g = p->b = 66 + rand()%6;

         for (j=0 ; j<3 ; j++)
         {
            p->org[j] = org[j] + ((rand()%32)-16);
            p->vel[j] = (rand()%512)-256;
         }
      }
      else
      {
         p->type = pt_blob2;
         p->r = p->g = p->b = 150 + rand() %6;

         for (j=0 ; j<3 ; j++)
         {
            p->org[j] = org[j] + ((rand()%32)-16);
            p->vel[j] = (rand()%512)-256;
         }
      }
   }
}

void R_RunParticleEffect(vec3_t org, vec3_t dir, float scale, int count, char *texture)
{
   int         i, j;
   particle_t   *p;

   for (i=0 ; i<count ; i++)
   {
      if (!free_particles)
         return;
      p = free_particles;
      free_particles = p->next;
      p->next = active_particles;
      active_particles = p;

      p->texture = texture;

      if (count == 1024)
      {
         // rocket explosion
         p->die = cl.time + 5;
         p->ramp = rand()&3;
         p->scale = scale;
         if (i & 1)
         {
            p->type = pt_explode;
            for (j=0 ; j<3 ; j++)
            {
               p->org[j] = org[j] + ((rand()%32)-16);
               p->vel[j] = (rand()%512)-256;
            }
         }
         else
         {
            p->type = pt_explode2;
            for (j=0 ; j<3 ; j++)
            {
               p->org[j] = org[j] + ((rand()%32)-16);
               p->vel[j] = (rand()%512)-256;
            }
         }
      }
      else
      {
         p->die = cl.time + 0.1*(rand()%5);
         p->type = pt_slowgrav;
         p->scale = scale;
         for (j=0 ; j<3 ; j++)
         {
            p->org[j] = org[j] + ((rand()&15)-8);
            p->vel[j] = dir[j]*15;
         }
      }
   }
}

void R_LavaSplash (vec3_t org)
{
   int         i, j, k;
   particle_t   *p;
   float      vel;
   vec3_t      dir;

   for (i=-16 ; i<16 ; i++)
      for (j=-16 ; j<16 ; j++)
         for (k=0 ; k<1 ; k++)
         {
            if (!free_particles)
               return;
            p = free_particles;
            free_particles = p->next;
            p->next = active_particles;
            active_particles = p;

            p->die = cl.time + 2 + (rand()&31) * 0.02;
            p->r = p->g = p->b = 255;
            p->type = pt_slowgrav;

            dir[0] = j*8 + (rand()&7);
            dir[1] = i*8 + (rand()&7);
            dir[2] = 256;

            p->org[0] = org[0] + dir[0];
            p->org[1] = org[1] + dir[1];
            p->org[2] = org[2] + (rand()&63);

            VectorNormalize (dir);
            vel = 50 + (rand()&63);
            VectorScale (dir, vel, p->vel);
         }
}

void R_TeleportSplash (vec3_t org)
{
   int         i, j, k;
   particle_t   *p;
   float      vel;
   vec3_t      dir;

   for (i=-16 ; i<16 ; i+=4)
      for (j=-16 ; j<16 ; j+=4)
         for (k=-24 ; k<32 ; k+=4)
         {
            if (!free_particles)
               return;
            p = free_particles;
            free_particles = p->next;
            p->next = active_particles;
            active_particles = p;

            p->die = cl.time + 0.2 + (rand()&7) * 0.02;
            p->r = p->g = p->b = 7 + (rand()&7);
            p->type = pt_slowgrav;

            dir[0] = j*8;
            dir[1] = i*8;
            dir[2] = k*8;

            p->org[0] = org[0] + i + (rand()&3);
            p->org[1] = org[1] + j + (rand()&3);
            p->org[2] = org[2] + k + (rand()&3);

            VectorNormalize (dir);
            vel = 50 + (rand()&63);
            VectorScale (dir, vel, p->vel);
         }
}

/*
FIXME -- rename function and use #defined types instead of numbers
*/
void R_RocketTrail (vec3_t start, vec3_t end, int type)
{
   vec3_t      vec;
   float      len;
   int         j;
   particle_t   *p;
   int         dec;
   static int   tracercount;

   VectorSubtract (end, start, vec);
   len = VectorNormalize (vec);
   if (type < 128)
      dec = 3;
   else
   {
      dec = 1;
      type -= 128;
   }

   while (len > 0)
   {
      len -= dec;

      if (!free_particles)
         return;
      p = free_particles;
      free_particles = p->next;
      p->next = active_particles;
      active_particles = p;

      VectorCopy (vec3_origin, p->vel);
      p->die = cl.time + 2;

      switch (type)
      {
         case 0:   // rocket trail
            p->ramp = (rand()&3);
//            p->color = ramp3[(int)p->ramp];
            p->type = pt_fire;
            for (j=0 ; j<3 ; j++)
               p->org[j] = start[j] + ((rand()%6)-3);
            break;

         case 1:   // smoke smoke
            p->ramp = (rand()&3) + 2;
//            p->color = ramp3[(int)p->ramp];
            p->type = pt_fire;
            for (j=0 ; j<3 ; j++)
               p->org[j] = start[j] + ((rand()%6)-3);
            break;

         case 2:   // blood
            p->type = pt_grav;
            p->r = 200 + (rand()&3);
            p->g = p->b = 0;

            for (j=0 ; j<3 ; j++)
               p->org[j] = start[j] + ((rand()%6)-3);
            break;
         case 3:
         case 5:   // tracer
            p->die = cl.time + 0.5;
            p->type = pt_static;

            if (type == 3)
               p->r = p->g = p->b = 52 + ((tracercount&4)<<1);
            else
               p->r = p->g = p->b = 230 + ((tracercount&4)<<1);

            tracercount++;

            VectorCopy (start, p->org);
            if (tracercount & 1)
            {
               p->vel[0] = 30*vec[1];
               p->vel[1] = 30*-vec[0];
            }
            else
            {
               p->vel[0] = 30*-vec[1];
               p->vel[1] = 30*vec[0];
            }
            break;

         case 4:   // slight blood
            p->type = pt_grav;
            p->r = 67 + (rand()&3);
            p->g = p->b = 0;
            for (j=0 ; j<3 ; j++)
               p->org[j] = start[j] + ((rand()%6)-3);
            len -= 3;
            break;

         case 6:   // voor trail
            p->r = p->g = p->b = 9*16 + 8 + (rand()&3);
            p->type = pt_static;
            p->die = cl.time + 0.3;
            for (j=0 ; j<3 ; j++)
               p->org[j] = start[j] + ((rand()&15)-8);
            break;
      }

      VectorAdd (start, vec, start);
   }
}

void CL_RunParticles (void)
{
   particle_t      *p, *kill;
   int            i;
   float         time1, time2, time3, dvel, frametime, scale, grav;
   extern   cvar_t   sv_gravity;

   frametime = cl.time - cl.oldtime;
   time3 = frametime * 15;
   time2 = frametime * 10;
   time1 = frametime * 5;
   grav = frametime * sv_gravity.value * 0.05;
   dvel = 4*frametime;

   for ( ;; )
   {
      kill = active_particles;
      if (kill && kill->die < cl.time)
      {
         active_particles = kill->next;
         kill->next = free_particles;
         free_particles = kill;
         continue;
      }
      break;
   }

   for (p=active_particles ; p ; p=p->next)
   {
      for ( ;; )
      {
         kill = p->next;
         if (kill && kill->die < cl.time)
         {
            p->next = kill->next;
            kill->next = free_particles;
            free_particles = kill;
            continue;
         }
         break;
      }

      p->org[0] += p->vel[0]*frametime;
      p->org[1] += p->vel[1]*frametime;
      p->org[2] += p->vel[2]*frametime;

      switch (p->type)
      {
      case pt_static:
         break;
      case pt_fire:
         p->ramp += time1;
         if (p->ramp >= 6)
            p->die = -1;
         else
            p->r = p->g = p->b = ramp3[(int)p->ramp];
         p->vel[2] += grav;
         break;

      case pt_explode:
         p->ramp += time2;
         if (p->ramp >=8)
            p->die = -1;
         for (i=0 ; i<3 ; i++)
            p->vel[i] += p->vel[i]*dvel;
         p->vel[2] -= grav;
         break;

      case pt_explode2:
         p->ramp += time3;
         if (p->ramp >=8)
            p->die = -1;
         else
            p->r = p->g = p->b = ramp2[(int)p->ramp];
         for (i=0 ; i<3 ; i++)
            p->vel[i] -= p->vel[i]*frametime;
         p->vel[2] -= grav;
         break;

      case pt_blob:
         for (i=0 ; i<3 ; i++)
            p->vel[i] += p->vel[i]*dvel;
         p->vel[2] -= grav;
         break;

      case pt_blob2:
         for (i=0 ; i<2 ; i++)
            p->vel[i] -= p->vel[i]*dvel;
         p->vel[2] -= grav;
         break;

      case pt_grav:
      case pt_slowgrav:
         p->vel[2] -= grav;
         break;
      }
   }
}

void R_DrawParticles(void)
{
   particle_t      *p;
   float         scale;
   vec3_t         up, right, p_up, p_right, p_upright; //johnfitz -- p_ vectors
   gltexture_t      *tex;
   int            i;

   VectorScale (vup, 1.5, up);
   VectorScale (vright, 1.5, right);

   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glEnable (GL_BLEND);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   glDepthMask (GL_FALSE);
   glBegin (GL_QUADS);

   for (p=active_particles ; p ; p=p->next)
   {
      // hack a scale up to keep particles from disapearing
      scale = (p->org[0] - r_origin[0]) * vpn[0] + (p->org[1] - r_origin[1]) * vpn[1] + (p->org[2] - r_origin[2]) * vpn[2];

      if (scale < 20)
         scale = 1 + 0.08; //johnfitz -- added .08 to be consistent
      else
         scale = 1 + scale * 0.004;
      scale /= 2.0; //quad is half the size of triangle
      if(p->scale == 0)
         p->scale = 7.0;
      scale *= p->scale;   //texturescalefactor; //johnfitz -- compensate for apparent size of different particle textures

      // Sometimes p->texture just becomes null... the fuck is going on?
      if(!p->texture)
         GL_Bind(notexture);
      else
      {
         R_SetParticleTexture(p->texture);

         GL_Bind(parttex[texturenum]);
      }

      glColor4f(1,1,1,CLAMP(0, p->die + 0.5 - cl.time, 1));

      glTexCoord2f (0,0);
      glVertex3fv (p->org);

      glTexCoord2f(1,0);
      VectorMA (p->org, scale, up, p_up);
      glVertex3fv(p_up);

      glTexCoord2f(1,1);
      VectorMA (p_up, scale, right, p_upright);
      glVertex3fv(p_upright);

      glTexCoord2f (0,1);
      VectorMA (p->org, scale, right, p_right);
      glVertex3fv (p_right);

      rs_particles++; //johnfitz //FIXME: just use r_numparticles
   }
   glEnd ();

   glDepthMask (GL_TRUE); //johnfitz -- fix for particle z-buffer bug
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glDisable (GL_BLEND);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   glColor3f(1,1,1);
}

void R_DrawParticles_ShowTris (void)
{
   particle_t      *p;
   float         scale;
   vec3_t         up, right, p_up, p_right, p_upright;

   VectorScale (vup, 1.5, up);
   VectorScale (vright, 1.5, right);

   for (p=active_particles ; p ; p=p->next)
   {
      glBegin (GL_TRIANGLE_FAN);

      // hack a scale up to keep particles from disapearing
      scale = (p->org[0] - r_origin[0]) * vpn[0] + (p->org[1] - r_origin[1]) * vpn[1] + (p->org[2] - r_origin[2]) * vpn[2];
      if (scale < 20)
         scale = 1 + 0.08; //johnfitz -- added .08 to be consistent
      else
         scale = 1 + scale * 0.004;

      scale /= 2.0; //quad is half the size of triangle

      scale *= texturescalefactor; //compensate for apparent size of different particle textures

      glVertex3fv (p->org);

      VectorMA (p->org, scale, up, p_up);
      glVertex3fv (p_up);

      VectorMA (p_up, scale, right, p_upright);
      glVertex3fv (p_upright);

      VectorMA (p->org, scale, right, p_right);
      glVertex3fv (p_right);

      glEnd ();
   }
}
User avatar
hogsy
 
Posts: 198
Joined: Wed Aug 03, 2011 3:44 pm
Location: UK

Re: Can anyone see what's wrong with this?

Postby szo » Wed Dec 07, 2011 7:08 pm

Your use of sizeof(parttex) is wrong: you need something like sizeof(parttex)/sizeof(void*) to match your 32.
szo
 
Posts: 132
Joined: Mon Dec 06, 2010 4:42 pm

Re: Can anyone see what's wrong with this?

Postby hogsy » Wed Dec 07, 2011 8:18 pm

That might be so but it's not the cause of the problem.
User avatar
hogsy
 
Posts: 198
Joined: Wed Aug 03, 2011 3:44 pm
Location: UK

Re: Can anyone see what's wrong with this?

Postby Baker » Thu Dec 08, 2011 12:19 am

This is probably a 5 second problem in the debugger (set a breakpoint or browser the variables).

I am guessing that you are using a FitzQuake code base and Windows.

1) What FitzQuake did you use as a basis for the project? Was it the official FitzQuake 0.85?
2) What version of Visual Studio are you using (if that is what you are using)?
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Can anyone see what's wrong with this?

Postby hogsy » Thu Dec 08, 2011 12:53 am

1 - Yes.
2 - Studio 2010.
User avatar
hogsy
 
Posts: 198
Joined: Wed Aug 03, 2011 3:44 pm
Location: UK

Re: Can anyone see what's wrong with this?

Postby Baker » Thu Dec 08, 2011 1:13 am

Anyway ....

Suggestions:

NO: void R_SetParticleTexture(char *texture)
YES: int R_SetParticleTexture (const char *texture)

^^
1. The const char just means that the function will not change the contents of where *texture is pointing
2. We also want to return a value for the function, hence "int"

NO: int r_numparticles, texturenum = 0;
YES: int r_numparticles;

^^
3. You have a global that is only used in R_SetParticleTexture, kill that global.
4. By the way, the "= 0 " is not necessary as any variable declared outside a function in C is automatically 0 or NULL in initialization

Code: Select all
         if(tex)
         {
            parttex[i] = TexMgr_LoadImage(NULL,name,w,h,SRC_RGBA,tex,name,0,TEXPREF_ALPHA);
            texturenum = i;
            return;
         }
         else
         {
            parttex[i] = notexture;
            Con_Warning("Failed to load %s\n",name);
            return;
         }


In the above, what are we setting the texturenum to if "Failed to load". Who knows what texturenum's value is. This is why you should have the function return int and not void.

Anyways ...

Code: Select all
const int maxnumpartex = sizeof(partex)/sizeof(partex[0])

#define StringMatch(s1, s2)   !strcmp(s1, s2)

int R_SetParticleTexture (const char *inTextureName)
{
    char    currentTextureFullpath[32];
    int     existingTextureNum;
    int     newTextureSlot;
    int     w = 64, h = 64;
    byte    *texdata;

    sprintf (currentTextureFullpath, "textures/particles/%s", inTextureName);

    Con_Printf ("Texture: %s\n", currentTextureFullpath);   //temp

    // Check for any matching textures...
    for (existingTextureNum = 0; existingTextureNum < maxnumpartex; existingTextureNum++)
        if (parttex[existingTextureNum] && StringMatch (inTextureName, parttex[existingTextureNum]->name)
            return existingTextureNum;

    // No matching texture... Baker: I guess we are loading it
    for (newTextureSlot = 0; newTextureSlot < maxnumpartex; newTextureSlot++)   // Locate first empty slot
       if (parttex[newTextureSlot] == NULL) break;
           
    if (newTextureSlot  == maxnumpartex) Sys_Error ("No available particle texture slots");
   
    // If we got here, we have a texture slot, load it ...
    texdata = Image_LoadImage (currentTextureFullpath, &w, &h);     
   
    // Warn if we don't have texture data from loading image
    if (!texdata) Con_Warning("Failed to load %s\n", currentTextureFullpath);

    parttex[newTextureSlot] = !texdata ? notexture : TexMgr_LoadImage (NULL, name, w, h, SRC_RGBA, texdata, currentTextureFullpath, 0, TEXPREF_ALPHA); // Upload image
    return newTextureSlot;
}



This probably works as is .... ^^^^ Might solve your problem, might not. I think it very well may.

Also change this
...

Code: Select all
         R_SetParticleTexture(p->texture);

         GL_Bind(parttex[texturenum]);


to

Code: Select all
GL_Bind (partex[R_SetParticleTexture(p->texture)]); // A little messy of a line for my tastes, but that's life sometimes



// Haven't used MSVS 2010 but if you set the configuration to Debug and run it ... well you should be able to use breakpoints and the watch window etc. That is not working for you?
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Can anyone see what's wrong with this?

Postby mh » Thu Dec 08, 2011 1:19 am

Creating and uploading a new texture is illegal between a glBegin/glEnd pair, as is changing the current texture.

http://www.opengl.org/sdk/docs/man/xhtm ... mage2D.xml
"GL_INVALID_OPERATION is generated if glTexImage2D is executed between the execution of glBegin and the corresponding execution of glEnd."

http://www.opengl.org/sdk/docs/man/xhtm ... exture.xml
"GL_INVALID_OPERATION is generated if glBindTexture is executed between the execution of glBegin and the corresponding execution of glEnd."

The right solution is not to put your glBegin/glEnd inside the loop; that will cause your performance to plummet like a stone. What you want instead is to check if the texture has changed from the last one used, if so you call glEnd, then switch texture, then call glBegin again. (you can optimize out the case where no particles have yet been drawn later on).

It's also not a good idea to load textures at runtime like this, you'll want to load them upfront during initialization, otherwise it will also hurt performance.
Last edited by mh on Thu Dec 08, 2011 1:27 am, edited 1 time in total.
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

Re: Can anyone see what's wrong with this?

Postby hogsy » Thu Dec 08, 2011 1:27 am

@Baker
None of that would fix it. I've already stated I'm aware it's messy :roll:

@mh
I'll sort that out and yes I'm aware it's a bad idea but we're doing it anyway :lol:

Edit:
Yup that fixed it, thanks mh!
User avatar
hogsy
 
Posts: 198
Joined: Wed Aug 03, 2011 3:44 pm
Location: UK

Re: Can anyone see what's wrong with this?

Postby Baker » Thu Dec 08, 2011 2:06 pm

hogsy wrote:@Baker
None of that would fix it. I've already stated I'm aware it's messy :roll:

My game plan from the start was to try to draw MH's attention to your problem. :D Mission accomplished!

I shall now have a well deserved beer for solving your woes. No need for thanks, this tasty beer is reward enough. :D
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Can anyone see what's wrong with this?

Postby hogsy » Thu Dec 08, 2011 2:59 pm

"Catch me later and I'll buy you a beer" :lol:

Thanks for your suggestions anyway :wink:
User avatar
hogsy
 
Posts: 198
Joined: Wed Aug 03, 2011 3:44 pm
Location: UK


Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 1 guest