Example #1
0
/* Clip a triangle against the viewport and user clip planes.
 */
static void TAG(clip_draw_triangle)( GLcontext *ctx,
				     TNL_VERTEX *v0,
				     TNL_VERTEX *v1,
				     TNL_VERTEX *v2,
				     GLuint mask )
{
   LOCAL_VARS;
   GET_INTERP_FUNC;
   TNL_VERTEX tmp[MAX_CLIPPED_VERTICES];
   TNL_VERTEX *verts = tmp;
   TNL_VERTEX *(inlist[2][MAX_CLIPPED_VERTICES]);
   TNL_VERTEX **out;
   GLuint in = 0;
   GLuint n = 3;
   GLuint i;

   ASSIGN_3V(inlist, v2, v0, v1 ); /* pv rotated to slot zero */

   POLY_CLIP( CLIP_RIGHT_BIT,  -1,  0,  0, 1 );
   POLY_CLIP( CLIP_LEFT_BIT,    1,  0,  0, 1 );
   POLY_CLIP( CLIP_TOP_BIT,     0, -1,  0, 1 );
   POLY_CLIP( CLIP_BOTTOM_BIT,  0,  1,  0, 1 );
   POLY_CLIP( CLIP_FAR_BIT,     0,  0, -1, 1 );
   POLY_CLIP( CLIP_NEAR_BIT,    0,  0,  1, 1 );

   if ((ctx->_TriangleCaps & DD_FLATSHADE) && v2 != inlist[0]) 
      COPY_PV( ctx, inlist[0], v2 );

   out = inlist[in];
   DRAW_POLYGON( out, n );
}
Example #2
0
static void Display( void )
{
   glClearColor(0.3, 0.3, 0.3, 1);
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
   glPointSize(psz);

   enable( GL_POINT_SMOOTH, pointsmooth );
   enable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB, program_point_size );

   glBegin(prim);


   {
      union vert v[3];

      ASSIGN_3V(v[0].v.color, 0,0,1); 
      ASSIGN_3V(v[0].v.pos,  0.9, -0.9, 0.0);
      ASSIGN_3V(v[1].v.color, 1,0,0); 
      ASSIGN_3V(v[1].v.pos, 0.9, 0.9, 0.0);
      ASSIGN_3V(v[2].v.color, 0,1,0); 
      ASSIGN_3V(v[2].v.pos, -0.9, 0, 0.0);

      subdiv(&v[0], &v[1], &v[2], nr_steps);
   }

   glEnd();


   glFlush();
   if (show_fps) {
      ++frame_cnt;
      glutPostRedisplay();
   }
}
Example #3
0
static void Display( void )
{
   glClearColor(0.3, 0.3, 0.3, 1);
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

   glUseProgram(program);

   glBegin(GL_TRIANGLES);


   {
      union vert v[3];

      ASSIGN_3V(v[0].v.color, 0,0,1);
      ASSIGN_3V(v[0].v.pos,  0.9, -0.9, 0.0);
      ASSIGN_3V(v[1].v.color, 1,0,0);
      ASSIGN_3V(v[1].v.pos, 0.9, 0.9, 0.0);
      ASSIGN_3V(v[2].v.color, 0,1,0);
      ASSIGN_3V(v[2].v.pos, -0.9, 0, 0.0);

      subdiv(&v[0], &v[1], &v[2], nr_steps);
   }

   glEnd();


   glFlush();
}
/**
 * Compute lighting for the raster position.  RGB modes computed.
 * \param ctx the context
 * \param vertex vertex location
 * \param normal normal vector
 * \param Rcolor returned color
 * \param Rspec returned specular color (if separate specular enabled)
 */
static void
shade_rastpos(struct gl_context *ctx,
              const GLfloat vertex[4],
              const GLfloat normal[3],
              GLfloat Rcolor[4],
              GLfloat Rspec[4])
{
   /*const*/ GLfloat (*base)[3] = ctx->Light._BaseColor;
   GLbitfield mask;
   GLfloat diffuseColor[4], specularColor[4];  /* for RGB mode only */

   COPY_3V(diffuseColor, base[0]);
   diffuseColor[3] = CLAMP(
      ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3], 0.0F, 1.0F );
   ASSIGN_4V(specularColor, 0.0, 0.0, 0.0, 1.0);

   mask = ctx->Light._EnabledLights;
   while (mask) {
      const int i = u_bit_scan(&mask);
      struct gl_light *light = &ctx->Light.Light[i];
      GLfloat attenuation = 1.0;
      GLfloat VP[3]; /* vector from vertex to light pos */
      GLfloat n_dot_VP;
      GLfloat diffuseContrib[3], specularContrib[3];

      if (!(light->_Flags & LIGHT_POSITIONAL)) {
         /* light at infinity */
	 COPY_3V(VP, light->_VP_inf_norm);
	 attenuation = light->_VP_inf_spot_attenuation;
      }
      else {
         /* local/positional light */
	 GLfloat d;

         /* VP = vector from vertex pos to light[i].pos */
	 SUB_3V(VP, light->_Position, vertex);
         /* d = length(VP) */
	 d = (GLfloat) LEN_3FV( VP );
	 if (d > 1.0e-6F) {
            /* normalize VP */
	    GLfloat invd = 1.0F / d;
	    SELF_SCALE_SCALAR_3V(VP, invd);
	 }

         /* atti */
	 attenuation = 1.0F / (light->ConstantAttenuation + d *
			       (light->LinearAttenuation + d *
				light->QuadraticAttenuation));

	 if (light->_Flags & LIGHT_SPOT) {
	    GLfloat PV_dot_dir = - DOT3(VP, light->_NormSpotDirection);

	    if (PV_dot_dir<light->_CosCutoff) {
	       continue;
	    }
	    else {
               GLfloat spot = powf(PV_dot_dir, light->SpotExponent);
	       attenuation *= spot;
	    }
	 }
      }

      if (attenuation < 1e-3F)
	 continue;

      n_dot_VP = DOT3( normal, VP );

      if (n_dot_VP < 0.0F) {
	 ACC_SCALE_SCALAR_3V(diffuseColor, attenuation, light->_MatAmbient[0]);
	 continue;
      }

      /* Ambient + diffuse */
      COPY_3V(diffuseContrib, light->_MatAmbient[0]);
      ACC_SCALE_SCALAR_3V(diffuseContrib, n_dot_VP, light->_MatDiffuse[0]);

      /* Specular */
      {
         const GLfloat *h;
         GLfloat n_dot_h;

         ASSIGN_3V(specularContrib, 0.0, 0.0, 0.0);

	 if (ctx->Light.Model.LocalViewer) {
	    GLfloat v[3];
	    COPY_3V(v, vertex);
	    NORMALIZE_3FV(v);
	    SUB_3V(VP, VP, v);
            NORMALIZE_3FV(VP);
	    h = VP;
	 }
	 else if (light->_Flags & LIGHT_POSITIONAL) {
	    ACC_3V(VP, ctx->_EyeZDir);
            NORMALIZE_3FV(VP);
	    h = VP;
	 }
         else {
	    h = light->_h_inf_norm;
	 }

	 n_dot_h = DOT3(normal, h);

	 if (n_dot_h > 0.0F) {
	    GLfloat shine;
	    GLfloat spec_coef;

	    shine = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
	    spec_coef = powf(n_dot_h, shine);

	    if (spec_coef > 1.0e-10F) {
               if (ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR) {
                  ACC_SCALE_SCALAR_3V( specularContrib, spec_coef,
                                       light->_MatSpecular[0]);
               }
               else {
                  ACC_SCALE_SCALAR_3V( diffuseContrib, spec_coef,
                                       light->_MatSpecular[0]);
               }
	    }
	 }
      }

      ACC_SCALE_SCALAR_3V( diffuseColor, attenuation, diffuseContrib );
      ACC_SCALE_SCALAR_3V( specularColor, attenuation, specularContrib );
   }

   Rcolor[0] = CLAMP(diffuseColor[0], 0.0F, 1.0F);
   Rcolor[1] = CLAMP(diffuseColor[1], 0.0F, 1.0F);
   Rcolor[2] = CLAMP(diffuseColor[2], 0.0F, 1.0F);
   Rcolor[3] = CLAMP(diffuseColor[3], 0.0F, 1.0F);
   Rspec[0] = CLAMP(specularColor[0], 0.0F, 1.0F);
   Rspec[1] = CLAMP(specularColor[1], 0.0F, 1.0F);
   Rspec[2] = CLAMP(specularColor[2], 0.0F, 1.0F);
   Rspec[3] = CLAMP(specularColor[3], 0.0F, 1.0F);
}
Example #5
0
static int test_norm_function( normal_func func, int mtype, long *cycles )
{
   GLvector4f source[1], dest[1], dest2[1], ref[1], ref2[1];
   GLmatrix mat[1];
   GLfloat s[TEST_COUNT][5], d[TEST_COUNT][4], r[TEST_COUNT][4];
   GLfloat d2[TEST_COUNT][4], r2[TEST_COUNT][4], length[TEST_COUNT];
   GLfloat scale;
   GLfloat *m;
   int i, j;
#ifdef  RUN_DEBUG_BENCHMARK
   int cycle_i;		/* the counter for the benchmarks we run */
#endif

   (void) cycles;

   mat->m = (GLfloat *) _mesa_align_malloc( 16 * sizeof(GLfloat), 16 );
   mat->inv = m = mat->m;

   init_matrix( m );

   scale = 1.0F + rnd () * norm_scale_types[mtype];

   for ( i = 0 ; i < 4 ; i++ ) {
      for ( j = 0 ; j < 4 ; j++ ) {
         switch ( norm_templates[mtype][i * 4 + j] ) {
         case NIL:
            m[j * 4 + i] = 0.0;
            break;
         case ONE:
            m[j * 4 + i] = 1.0;
            break;
         case NEG:
            m[j * 4 + i] = -1.0;
            break;
         case VAR:
            break;
         default:
            exit(1);
         }
      }
   }

   for ( i = 0 ; i < TEST_COUNT ; i++ ) {
      ASSIGN_3V( d[i],  0.0, 0.0, 0.0 );
      ASSIGN_3V( s[i],  0.0, 0.0, 0.0 );
      ASSIGN_3V( d2[i], 0.0, 0.0, 0.0 );
      for ( j = 0 ; j < 3 ; j++ )
         s[i][j] = rnd();
      length[i] = 1 / SQRTF( LEN_SQUARED_3FV( s[i] ) );
   }

   source->data = (GLfloat(*)[4]) s;
   source->start = (GLfloat *) s;
   source->count = TEST_COUNT;
   source->stride = sizeof(s[0]);
   source->flags = 0;

   dest->data = d;
   dest->start = (GLfloat *) d;
   dest->count = TEST_COUNT;
   dest->stride = sizeof(float[4]);
   dest->flags = 0;

   dest2->data = d2;
   dest2->start = (GLfloat *) d2;
   dest2->count = TEST_COUNT;
   dest2->stride = sizeof(float[4]);
   dest2->flags = 0;

   ref->data = r;
   ref->start = (GLfloat *) r;
   ref->count = TEST_COUNT;
   ref->stride = sizeof(float[4]);
   ref->flags = 0;

   ref2->data = r2;
   ref2->start = (GLfloat *) r2;
   ref2->count = TEST_COUNT;
   ref2->stride = sizeof(float[4]);
   ref2->flags = 0;

   if ( norm_normalize_types[mtype] == 0 ) {
      ref_norm_transform_rescale( mat, scale, source, NULL, ref );
   } else {
      ref_norm_transform_normalize( mat, scale, source, NULL, ref );
      ref_norm_transform_normalize( mat, scale, source, length, ref2 );
   }

   if ( mesa_profile ) {
      BEGIN_RACE( *cycles );
      func( mat, scale, source, NULL, dest );
      END_RACE( *cycles );
      func( mat, scale, source, length, dest2 );
   } else {
      func( mat, scale, source, NULL, dest );
      func( mat, scale, source, length, dest2 );
   }

   for ( i = 0 ; i < TEST_COUNT ; i++ ) {
      for ( j = 0 ; j < 3 ; j++ ) {
         if ( significand_match( d[i][j], r[i][j] ) < REQUIRED_PRECISION ) {
            printf( "-----------------------------\n" );
            printf( "(i = %i, j = %i)\n", i, j );
            printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
		    d[i][0], r[i][0], r[i][0]/d[i][0],
		    MAX_PRECISION - significand_match( d[i][0], r[i][0] ) );
            printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
		    d[i][1], r[i][1], r[i][1]/d[i][1],
		    MAX_PRECISION - significand_match( d[i][1], r[i][1] ) );
            printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
		    d[i][2], r[i][2], r[i][2]/d[i][2],
		    MAX_PRECISION - significand_match( d[i][2], r[i][2] ) );
            return 0;
         }

         if ( norm_normalize_types[mtype] != 0 ) {
            if ( significand_match( d2[i][j], r2[i][j] ) < REQUIRED_PRECISION ) {
               printf( "------------------- precalculated length case ------\n" );
               printf( "(i = %i, j = %i)\n", i, j );
               printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
		       d2[i][0], r2[i][0], r2[i][0]/d2[i][0],
		       MAX_PRECISION - significand_match( d2[i][0], r2[i][0] ) );
               printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
		       d2[i][1], r2[i][1], r2[i][1]/d2[i][1],
		       MAX_PRECISION - significand_match( d2[i][1], r2[i][1] ) );
               printf( "%f \t %f \t [ratio = %e - %i bit missed]\n",
		       d2[i][2], r2[i][2], r2[i][2]/d2[i][2],
		       MAX_PRECISION - significand_match( d2[i][2], r2[i][2] ) );
               return 0;
            }
         }
      }
   }

   _mesa_align_free( mat->m );
   return 1;
}