?? hair.cpp
字號(hào):
#include "hair.h"
#include <stdio.h>
Hair::Hair()
{
pDecal = NULL;
pixelx = 0;
pixely = 0;
xangle = 0.0;
yangle = 0.0;
xpos = 0.0;
ypos = 0.0;
fWhiteDif[0] = 1.0;
fWhiteDif[1] = 1.0;
fWhiteDif[2] = 1.0;
fWhiteDif[3] = 1.0;
fBlackDif[0] = 0.0;
fBlackDif[1] = 0.0;
fBlackDif[2] = 0.0;
fBlackDif[3] = 1.0;
}
Hair::~Hair()
{
}
void Hair::CreateDisplayList(GLuint *id)
{
if ( id && ( ! (*id) ) )
{
GLfloat fPyrVerts[5][4] =
{
{ 0.0, 0.0, 0.0 },
{ -HAIR_PYRAMID_WIDTH, -HAIR_PYRAMID_WIDTH, HAIR_PYRAMID_HEIGHT },
{ -HAIR_PYRAMID_WIDTH, HAIR_PYRAMID_WIDTH, HAIR_PYRAMID_HEIGHT },
{ HAIR_PYRAMID_WIDTH, HAIR_PYRAMID_WIDTH, HAIR_PYRAMID_HEIGHT },
{ HAIR_PYRAMID_WIDTH, -HAIR_PYRAMID_WIDTH, HAIR_PYRAMID_HEIGHT },
};
// Only create the display list once
GLfloat fPyramidAmb[] = {0.0, 0.0, 0.0, 1.0};
GLfloat fPyramidSpec[] = {1.0, 1.0, 1.0, 1.0};
*id = glGenLists(1);
glNewList(*id, GL_COMPILE);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, fWhiteDif);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, fPyramidAmb);
// glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, fPyramidSpec);
// Create a simple inverted pyramid
glBegin(GL_TRIANGLES);
// Sides
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, fWhiteDif);
glVertex3fv(fPyrVerts[0]);
glVertex3fv(fPyrVerts[1]);
glVertex3fv(fPyrVerts[2]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, fBlackDif);
glVertex3fv(fPyrVerts[0]);
glVertex3fv(fPyrVerts[2]);
glVertex3fv(fPyrVerts[3]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, fWhiteDif);
glVertex3fv(fPyrVerts[0]);
glVertex3fv(fPyrVerts[3]);
glVertex3fv(fPyrVerts[4]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, fBlackDif);
glVertex3fv(fPyrVerts[0]);
glVertex3fv(fPyrVerts[4]);
glVertex3fv(fPyrVerts[1]);
// Cap
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, fBlackDif);
glVertex3fv(fPyrVerts[1]);
glVertex3fv(fPyrVerts[2]);
glVertex3fv(fPyrVerts[3]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, fWhiteDif);
glVertex3fv(fPyrVerts[1]);
glVertex3fv(fPyrVerts[3]);
glVertex3fv(fPyrVerts[4]);
glEnd();
glEndList();
}
// Set the internal id for this hair instance
DisplayID = *id;
}
void Hair::Draw()
{
glPushMatrix();
glTranslatef( xpos, ypos, 0.0 );
glRotatef( xangle, 1.0, 0.0, 0.0 );
glRotatef( yangle, 0.0, 1.0, 0.0 );
glCallList(DisplayID);
glPopMatrix();
}
float Hair::PixelToPhysicalPos(GLuint pixel, float fPhysicalSize, GLuint nPixelSize)
{
return ( (float) pixel + 0.5f ) * fPhysicalSize / ( (float) nPixelSize );
}
void Hair::SetPixelPos(GLuint x, GLuint y)
{
pixelx = x;
pixely = y;
if (pDecal)
{
xpos = PixelToPhysicalPos(x, pDecal->fPhysicalTextureWidth, pDecal->nPixelTextureWidth);
ypos = PixelToPhysicalPos(y, pDecal->fPhysicalTextureHeight, pDecal->nPixelTextureHeight);
SetAnglesWithPixelLoc(x, y);
/*
float angle = 0.0;
angle = GetAngleFromBytes(0, 0);
angle = GetAngleFromBytes(0, 255);
angle = GetAngleFromBytes(64, 0);
angle = GetAngleFromBytes(128, 0);
angle = GetAngleFromBytes(128, 128);
angle = GetAngleFromBytes(150, 0);
angle = GetAngleFromBytes(255, 0);
angle = GetAngleFromBytes(255, 255);
*/
}
}
void Hair::SetTextureInfo(Decal *pInput)
{
pDecal = pInput;
}
void Hair::SetAnglesWithPixelLoc(GLuint x, GLuint y)
{
GLubyte r, g, b, a;
if (pDecal)
pDecal->GetPixel(x, y, r, g, b, a);
SetAnglesWithPixelColor(r, g, b, a);
}
void Hair::SetAnglesWithPixelColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a)
{
xangle = GetAngleFromBytes(r, g);
yangle = GetAngleFromBytes(b, a);
}
float Hair::GetAngleFromBytes(GLubyte high, GLubyte low)
{
float angle = 0.0;
if ( ( high != 128 ) || low )
{
angle = (float) high - 128.0f;
// Only bother computing the extra precision if the low byte is more than 0
if (low)
angle += (float) low / 255.0f;
angle *= 90.0;
if ( (high >= 0) && ( high <= 127 ) )
{
// left/down
angle /= 128.0f;
}
else if ( (high >= 129) && ( high <= 255 ) )
{
// right/up
angle /= 127.0f;
}
}
// printf("high = %3d, low = %3d, angle = %10.5f\n", high, low, angle);
return angle;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -