📄 s_triangle.c
字号:
/*
* Mesa 3-D graphics library
* Version: 6.3
*
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* When the device driver doesn't implement triangle rasterization it
* can hook in _swrast_Triangle, which eventually calls one of these
* functions to draw triangles.
*/
#include "glheader.h"
#include "context.h"
#include "colormac.h"
#include "imports.h"
#include "macros.h"
#include "texformat.h"
#include "s_aatriangle.h"
#include "s_context.h"
#include "s_depth.h"
#include "s_feedback.h"
#include "s_span.h"
#include "s_triangle.h"
/*
* Just used for feedback mode.
*/
GLboolean
_swrast_culltriangle( GLcontext *ctx,
const SWvertex *v0,
const SWvertex *v1,
const SWvertex *v2 )
{
GLfloat ex = v1->win[0] - v0->win[0];
GLfloat ey = v1->win[1] - v0->win[1];
GLfloat fx = v2->win[0] - v0->win[0];
GLfloat fy = v2->win[1] - v0->win[1];
GLfloat c = ex*fy-ey*fx;
if (c * SWRAST_CONTEXT(ctx)->_BackfaceSign > 0)
return 0;
return 1;
}
/*
* Render a flat-shaded color index triangle.
*/
#define NAME flat_ci_triangle
#define INTERP_Z 1
#define INTERP_FOG 1
#define SETUP_CODE \
span.interpMask |= SPAN_INDEX; \
span.index = FloatToFixed(v2->index);\
span.indexStep = 0;
#define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
#include "s_tritemp.h"
/*
* Render a smooth-shaded color index triangle.
*/
#define NAME smooth_ci_triangle
#define INTERP_Z 1
#define INTERP_FOG 1
#define INTERP_INDEX 1
#define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
#include "s_tritemp.h"
/*
* Render a flat-shaded RGBA triangle.
*/
#define NAME flat_rgba_triangle
#define INTERP_Z 1
#define INTERP_FOG 1
#define SETUP_CODE \
ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
span.interpMask |= SPAN_RGBA; \
span.red = ChanToFixed(v2->color[0]); \
span.green = ChanToFixed(v2->color[1]); \
span.blue = ChanToFixed(v2->color[2]); \
span.alpha = ChanToFixed(v2->color[3]); \
span.redStep = 0; \
span.greenStep = 0; \
span.blueStep = 0; \
span.alphaStep = 0;
#define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
#include "s_tritemp.h"
/*
* Render a smooth-shaded RGBA triangle.
*/
#define NAME smooth_rgba_triangle
#define INTERP_Z 1
#define INTERP_FOG 1
#define INTERP_RGB 1
#define INTERP_ALPHA 1
#define SETUP_CODE \
{ \
/* texturing must be off */ \
ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
}
#define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
#include "s_tritemp.h"
/*
* Render an RGB, GL_DECAL, textured triangle.
* Interpolate S,T only w/out mipmapping or perspective correction.
*
* No fog.
*/
#define NAME simple_textured_triangle
#define INTERP_INT_TEX 1
#define S_SCALE twidth
#define T_SCALE theight
#define SETUP_CODE \
struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];\
struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
const GLint b = obj->BaseLevel; \
const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
const GLint smask = obj->Image[0][b]->Width - 1; \
const GLint tmask = obj->Image[0][b]->Height - 1; \
if (!texture) { \
/* this shouldn't happen */ \
return; \
}
#define RENDER_SPAN( span ) \
GLuint i; \
span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
span.intTex[1] -= FIXED_HALF; \
for (i = 0; i < span.end; i++) { \
GLint s = FixedToInt(span.intTex[0]) & smask; \
GLint t = FixedToInt(span.intTex[1]) & tmask; \
GLint pos = (t << twidth_log2) + s; \
pos = pos + pos + pos; /* multiply by 3 */ \
span.array->rgb[i][RCOMP] = texture[pos]; \
span.array->rgb[i][GCOMP] = texture[pos+1]; \
span.array->rgb[i][BCOMP] = texture[pos+2]; \
span.intTex[0] += span.intTexStep[0]; \
span.intTex[1] += span.intTexStep[1]; \
} \
rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, span.array->rgb, NULL);
#include "s_tritemp.h"
/*
* Render an RGB, GL_DECAL, textured triangle.
* Interpolate S,T, GL_LESS depth test, w/out mipmapping or
* perspective correction.
* Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
*
* No fog.
*/
#define NAME simple_z_textured_triangle
#define INTERP_Z 1
#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
#define INTERP_INT_TEX 1
#define S_SCALE twidth
#define T_SCALE theight
#define SETUP_CODE \
struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];\
struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
const GLint b = obj->BaseLevel; \
const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
const GLint smask = obj->Image[0][b]->Width - 1; \
const GLint tmask = obj->Image[0][b]->Height - 1; \
if (!texture) { \
/* this shouldn't happen */ \
return; \
}
#define RENDER_SPAN( span ) \
GLuint i; \
span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
span.intTex[1] -= FIXED_HALF; \
for (i = 0; i < span.end; i++) { \
const GLdepth z = FixedToDepth(span.z); \
if (z < zRow[i]) { \
GLint s = FixedToInt(span.intTex[0]) & smask; \
GLint t = FixedToInt(span.intTex[1]) & tmask; \
GLint pos = (t << twidth_log2) + s; \
pos = pos + pos + pos; /* multiply by 3 */ \
span.array->rgb[i][RCOMP] = texture[pos]; \
span.array->rgb[i][GCOMP] = texture[pos+1]; \
span.array->rgb[i][BCOMP] = texture[pos+2]; \
zRow[i] = z; \
span.array->mask[i] = 1; \
} \
else { \
span.array->mask[i] = 0; \
} \
span.intTex[0] += span.intTexStep[0]; \
span.intTex[1] += span.intTexStep[1]; \
span.z += span.zStep; \
} \
rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, \
span.array->rgb, span.array->mask);
#include "s_tritemp.h"
#if CHAN_TYPE != GL_FLOAT
struct affine_info
{
GLenum filter;
GLenum format;
GLenum envmode;
GLint smask, tmask;
GLint twidth_log2;
const GLchan *texture;
GLfixed er, eg, eb, ea;
GLint tbytesline, tsize;
};
static INLINE GLint
ilerp(GLint t, GLint a, GLint b)
{
return a + ((t * (b - a)) >> FIXED_SHIFT);
}
static INLINE GLint
ilerp_2d(GLint ia, GLint ib, GLint v00, GLint v10, GLint v01, GLint v11)
{
const GLint temp0 = ilerp(ia, v00, v10);
const GLint temp1 = ilerp(ia, v01, v11);
return ilerp(ib, temp0, temp1);
}
/* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
* textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
* texture env modes.
*/
static INLINE void
affine_span(GLcontext *ctx, struct sw_span *span,
struct affine_info *info)
{
GLchan sample[4]; /* the filtered texture sample */
/* Instead of defining a function for each mode, a test is done
* between the outer and inner loops. This is to reduce code size
* and complexity. Observe that an optimizing compiler kills
* unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
*/
#define NEAREST_RGB \
sample[RCOMP] = tex00[RCOMP]; \
sample[GCOMP] = tex00[GCOMP]; \
sample[BCOMP] = tex00[BCOMP]; \
sample[ACOMP] = CHAN_MAX
#define LINEAR_RGB \
sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
sample[ACOMP] = CHAN_MAX;
#define NEAREST_RGBA COPY_CHAN4(sample, tex00)
#define LINEAR_RGBA \
sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
sample[ACOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3])
#define MODULATE \
dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
#define DECAL \
dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
>> (FIXED_SHIFT + 8); \
dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
>> (FIXED_SHIFT + 8); \
dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
>> (FIXED_SHIFT + 8); \
dest[ACOMP] = FixedToInt(span->alpha)
#define BLEND \
dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
+ (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
+ (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
+ (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
#define REPLACE COPY_CHAN4(dest, sample)
#define ADD \
{ \
GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
}
/* shortcuts */
#define NEAREST_RGB_REPLACE \
NEAREST_RGB; \
dest[0] = sample[0]; \
dest[1] = sample[1]; \
dest[2] = sample[2]; \
dest[3] = FixedToInt(span->alpha);
#define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
#define SPAN_NEAREST(DO_TEX, COMPS) \
for (i = 0; i < span->end; i++) { \
/* Isn't it necessary to use FixedFloor below?? */ \
GLint s = FixedToInt(span->intTex[0]) & info->smask; \
GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
GLint pos = (t << info->twidth_log2) + s; \
const GLchan *tex00 = info->texture + COMPS * pos; \
DO_TEX; \
span->red += span->redStep; \
span->green += span->greenStep; \
span->blue += span->blueStep; \
span->alpha += span->alphaStep; \
span->intTex[0] += span->intTexStep[0]; \
span->intTex[1] += span->intTexStep[1]; \
dest += 4; \
}
#define SPAN_LINEAR(DO_TEX, COMPS) \
for (i = 0; i < span->end; i++) { \
/* Isn't it necessary to use FixedFloor below?? */ \
const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
const GLint pos = (t << info->twidth_log2) + s; \
const GLchan *tex00 = info->texture + COMPS * pos; \
const GLchan *tex10 = tex00 + info->tbytesline; \
const GLchan *tex01 = tex00 + COMPS; \
const GLchan *tex11 = tex10 + COMPS; \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -