VECTOR4D::operator VECTOR3D() { if(w==0.0f || w==1.0f) return VECTOR3D(x, y, z); else return VECTOR3D(x/w, y/w, z/w); }
void clsObject::Rotate(float y, float z, float x) { MATRIX3D M; world.x = x; world.y = y; world.z = z; Matrix3DRotateAxis(VECTOR3D(.0f, 1.0f, .0f), world.y, M); Matrix3DTransformNormal(M, fWd, fWd); Matrix3DTransformNormal(M, rWd, rWd); Matrix3DTransformNormal(M, uWd, uWd); Matrix3DTransformCoord(M, pos, pos); Matrix3DRotateAxis(VECTOR3D(.0f, .0f, 1.0f), world.z, M); Matrix3DTransformNormal(M, fWd, fWd); Matrix3DTransformNormal(M, rWd, rWd); Matrix3DTransformNormal(M, uWd, uWd); Matrix3DTransformCoord(M, pos, pos); Matrix3DRotateAxis(VECTOR3D(1.0f, .0f, .0f), world.x, M); Matrix3DTransformNormal(M, fWd, fWd); Matrix3DTransformNormal(M, rWd, rWd); Matrix3DTransformNormal(M, uWd, uWd); Matrix3DTransformCoord(M, pos, pos); Vector3DNormalize(fWd, fWd); Vector3DNormalize(rWd, rWd); Vector3DNormalize(uWd, uWd); }
clsObject::clsObject( const VECTOR3D &pt, float p, float y, float r, CLASS_ID clsID) : ClassID(clsID), ID(Counter++), Name(NULL) { Name = new TCHAR[MAX_OBJECT_NAME_LEN]; Name[0] = '\0'; //UCS = NULL; pos = pt; fWd = VECTOR3D(1.0f, .0f, .0f); rWd = VECTOR3D(.0f, 1.0f, .0f); uWd = VECTOR3D(.0f, .0f, 1.0f); world = VECTOR3D(); worldScale = VECTOR3D(1.0f, 1.0f, 1.0f); localScale = worldScale; Pitch(p); Yaw(y); Roll(r); }
Level::Level() { room1_size = 32.0; room2_size = 16.0; room3_size = 64.0; room1_Origin = VECTOR3D(0.0, 0.0, 0.0); room2_Origin = VECTOR3D(-(room1_size/2.0+room2_size/2.0), 0.0, 0.0); room3_Origin = VECTOR3D((room1_size/2.0+room3_size/2.0), 0.0, 0.0); initLevel(); }
//----------------------------------------------------------------------------- // инициализация переменных камеры для игры //----------------------------------------------------------------------------- void InitGameCamera() { GamePoint = VECTOR3D(0.0f, 0.0f, 0.0f); GameCameraLastUpdate = vw_GetTime(1); GameCameraDeviation = 0.0f; GameCameraDeviationTime = 0.0f; GameCameraNeedDeviation = 0.0f; GameCameraDeviationPower = 0.0f; GameCameraNeedStartDeviation = 0.0f; GameCameraDeviationAge = 0.0f; vw_SetCameraDeviation(VECTOR3D(0,0,0)); }
//********************************************************************************** // calculateVisibleNodes // // marks this node and all children as visible or not visible, depending whether // or not they are in the view frustum //********************************************************************************** void Node::calculateVisibleNodes(Frustum view_frustum) { VECTOR3D min = m_bounding_box.min_point; VECTOR3D max = m_bounding_box.max_point; VECTOR3D this_nodes_bounding_box[8]; this_nodes_bounding_box[0] = VECTOR3D(min.x, min.y, min.z); this_nodes_bounding_box[1] = VECTOR3D(max.x, min.y, min.z); this_nodes_bounding_box[2] = VECTOR3D(min.x, max.y, min.z); this_nodes_bounding_box[3] = VECTOR3D(max.x, max.y, min.z); this_nodes_bounding_box[4] = VECTOR3D(min.x, min.y, max.z); this_nodes_bounding_box[5] = VECTOR3D(max.x, min.y, max.z); this_nodes_bounding_box[6] = VECTOR3D(min.x, max.y, max.z); this_nodes_bounding_box[7] = VECTOR3D(max.x, max.y, max.z); if(view_frustum.IsBoundingBoxInside(this_nodes_bounding_box)) //if(view_frustum.IsPointInside(m_bounding_box.min_point) || view_frustum.IsPointInside(m_bounding_box.max_point)) { //mark this node as visible m_visible = true; } else { m_visible = false; } //check to see which children are visible std::vector<Node *>::iterator iter = m_children.begin(); while(iter != m_children.end()) { (*iter)->calculateVisibleNodes(view_frustum); iter++; } }
VECTOR3D VECTOR3D::GetRotatedAxis(double angle, const VECTOR3D & axis) const { if(angle==0.0) return (*this); VECTOR3D u=axis.GetNormalized(); VECTOR3D rotMatrixRow0, rotMatrixRow1, rotMatrixRow2; float sinAngle=(float)sin(M_PI*angle/180); float cosAngle=(float)cos(M_PI*angle/180); float oneMinusCosAngle=1.0f-cosAngle; rotMatrixRow0.x=(u.x)*(u.x) + cosAngle*(1-(u.x)*(u.x)); rotMatrixRow0.y=(u.x)*(u.y)*(oneMinusCosAngle) - sinAngle*u.z; rotMatrixRow0.z=(u.x)*(u.z)*(oneMinusCosAngle) + sinAngle*u.y; rotMatrixRow1.x=(u.x)*(u.y)*(oneMinusCosAngle) + sinAngle*u.z; rotMatrixRow1.y=(u.y)*(u.y) + cosAngle*(1-(u.y)*(u.y)); rotMatrixRow1.z=(u.y)*(u.z)*(oneMinusCosAngle) - sinAngle*u.x; rotMatrixRow2.x=(u.x)*(u.z)*(oneMinusCosAngle) - sinAngle*u.y; rotMatrixRow2.y=(u.y)*(u.z)*(oneMinusCosAngle) + sinAngle*u.x; rotMatrixRow2.z=(u.z)*(u.z) + cosAngle*(1-(u.z)*(u.z)); return VECTOR3D( this->DotProduct(rotMatrixRow0), this->DotProduct(rotMatrixRow1), this->DotProduct(rotMatrixRow2)); }
VECTOR3D& VECTOR3D::operator &= (const VECTOR3D& _fp) { float tx = y * _fp.z - _fp.y * z; float ty = z * _fp.x - _fp.z * x; float tz = x * _fp.y - _fp.x * y; return *this = VECTOR3D(tx, ty, tz); }
VECTOR3D VECTOR3D::operator & (const VECTOR3D& _fp) const { float tx = y * _fp.z - _fp.y * z; float ty = z * _fp.x - _fp.z * x; float tz = x * _fp.y - _fp.x * y; return VECTOR3D(tx, ty, tz); }
//Set up variables bool DemoInit() { if(!window.Init("Metaballs", 512, 512, 32, 24, 8, WINDOWED_SCREEN)) return 0; //quit if not created //set up grid if(!cubeGrid.CreateMemory()) return false; if(!cubeGrid.Init(gridSize)) return false; //set up metaballs for(int i=0; i<numMetaballs; i++) metaballs[i].Init(VECTOR3D(0.0f, 0.0f, 0.0f), 5.0f+float(i)); //Set Up Colors diffuseColors[0].Set(0.345f, 0.843f, 0.902f, 1.0f); diffuseColors[1].Set(0.047f, 0.839f, 0.271f, 1.0f); diffuseColors[2].Set(0.976f, 0.213f, 0.847f, 1.0f); //reset timer for start timer.Reset(); return true; }
void Balloon::initBalloon(float terrainHeight) { position = VECTOR3D(0.0f, terrainHeight+10.0, 0.0f); GLfloat planes[] = {0.0, 0.0, 0.3, 0.0}; GLfloat planet[] = {0.0, 0.3, 0.0, 0.0}; // Setup Texture Mapping balloon_pix[0].readBMPFile("textures/balloon_top.bmp"); balloon_pix[1].readBMPFile("textures/balloon_rope.bmp"); balloon_pix[2].readBMPFile("textures/balloon_basket.bmp"); glGenTextures(3, balloon_tex); // Texture Properties for(int i = 0; i < 3; ++i) { glBindTexture(GL_TEXTURE_2D, balloon_tex[i]); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glTexGenfv(GL_S, GL_OBJECT_PLANE, planes); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glTexGenfv(GL_T, GL_OBJECT_PLANE, planet); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, balloon_pix[i].nCols, balloon_pix[i].nRows, 0, GL_RGB, GL_UNSIGNED_BYTE, balloon_pix[i].pixel); } }
// read from file VECTOR3D clsTranslator::readVector(FILE* file) { float x, y, z; _ftscanf_s(file, _T("%f"), &x); _ftscanf_s(file, _T("%f"), &y); _ftscanf_s(file, _T("%f"), &z); return VECTOR3D(x, y, z); }
//Set up variables bool DemoInit() { if(!window.Init("Project Template", 640, 480, 32, 24, 8, WINDOWED_SCREEN)) return 0; //quit if not created SetUpARB_multitexture(); SetUpEXT_texture3D(); SetUpEXT_texture_edge_clamp(); SetUpNV_register_combiners(); SetUpNV_texture_shader(); SetUpNV_vertex_program(); if( !EXT_texture_edge_clamp_supported || !ARB_multitexture_supported || !NV_vertex_program_supported || !NV_register_combiners_supported) return false; //Check we have at least 3 texture units GLint maxTextureUnitsARB; glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &maxTextureUnitsARB); if(maxTextureUnitsARB<3) { errorLog.OutputError("I require at least 3 texture units"); return false; } //Set light colors lightColors[0].Set(1.0f, 1.0f, 1.0f, 1.0f); lightColors[1].Set((float)47/255, (float)206/255, (float)240/255, 1.0f); lightColors[2].Set((float)254/255, (float)48/255, (float)18/255, 1.0f); lightColors[3].Set((float)83/255, (float)243/255, (float)29/255, 1.0f); //Load textures //Decal image decalImage.Load("decal.tga"); glGenTextures(1, &decalTexture); glBindTexture(GL_TEXTURE_2D, decalTexture); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, decalImage.width, decalImage.height, 0, decalImage.format, GL_UNSIGNED_BYTE, decalImage.data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //Create light textures if(!InitLightTextures( atten1DTexture, atten2DTexture, atten3DTexture, gaussian1DTexture, gaussian2DTexture)) return false; camera.Init(VECTOR3D(0.0f, 0.0f, 3.5f)); //reset timer for start timer.Reset(); return true; }
float clsObject::Distance(const VECTOR3D &vDst) { VECTOR3D v = VECTOR3D( pos.x - vDst.x, pos.y - vDst.y, pos.z - vDst.z ); return Vector3DLength(v); }
void rectangle::init() { //init the vertices; m_vertices.push_back(VECTOR3D(0,0,0)); for (int i=0; i<m_numberPoint; i++) { m_vertices.push_back(VECTOR3D(m_R * cos(i*offsetAngle), m_R * sin(i*offsetAngle), 0)); } //index m_index.push_back(0); for (int i=1; i<m_numberPoint+1; i++) { m_index.push_back(i); } m_index.push_back(1); //init VAO VBO glGenVertexArrays(1,&m_vao); glGenBuffers(1,&m_vbo); glBindVertexArray(m_vao); glBindBuffer(GL_ARRAY_BUFFER, m_vbo); glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * 3 * sizeof(GL_FLOAT),m_vertices.data(), GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); //glDrawArrays(GL_rectangle_FAN,0,8); /*glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(1); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[2]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index), index, GL_STATIC_DRAW);*/ glBindBuffer(GL_ARRAY_BUFFER,0); //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); glBindVertexArray(0); }
VECTOR3D VECTOR3D::GetPackedTo01() const { VECTOR3D temp(*this); temp.Normalize(); temp=temp*0.5f+VECTOR3D(0.5f, 0.5f, 0.5f); return temp; }
void Vector3DMultV( const VECTOR3D &in1, const VECTOR3D &in2, VECTOR3D &out) { out = VECTOR3D( in1.y * in2.z - in1.z * in2.y, in1.z * in2.x - in1.x * in2.z, in1.x * in2.y - in1.y * in2.x ); }
void clsObject::LookAt(const VECTOR3D &lookAt, const LPVECTOR3D upOrient) { VECTOR3D lookDir = lookAt - pos; VECTOR3D vUp = abs(uWd.z) < EPSILON ? uWd : VECTOR3D(0, 0, 1) * uWd.z; if ( Vector3DLength(lookDir) > EPSILON ) { if ( upOrient != NULL && Vector3DLength(*upOrient) > EPSILON ) Vector3DNormalize(*upOrient, vUp); Vector3DNormalize(lookDir, fWd); Vector3DMultV(vUp, fWd, rWd); Vector3DNormalize(rWd, rWd); if ( Vector3DLength(rWd) < EPSILON ) rWd = VECTOR3D(0, 1, 0) * (FLOAT)((uWd.z >= 0) - (uWd.z < 0)); Vector3DMultV(fWd, rWd, uWd); } }
VECTOR3D VECTOR3D::GetRotatedX(double angle) const { if(angle==0.0) return (*this); float sinAngle=(float)sin(M_PI*angle/180); float cosAngle=(float)cos(M_PI*angle/180); return VECTOR3D( x, y*cosAngle - z*sinAngle, y*sinAngle + z*cosAngle); }
VECTOR3D VECTOR3D::GetRotatedZ(double angle) const { if(angle==0.0) return (*this); float sinAngle=(float)sin(M_PI*angle/180); float cosAngle=(float)cos(M_PI*angle/180); return VECTOR3D(x*cosAngle - y*sinAngle, x*sinAngle + y*cosAngle, z); }
VECTOR3D tagPolygon::Normal(const LPVERT_LIST vs, size_t startVert) { VECTOR3D v1, v2, ans; switch (startVert) { case 1: v1 = VECTOR3D(vs->at(second).x - vs->at(first).x, vs->at(second).y - vs->at(first).y, vs->at(second).z - vs->at(first).z); v2 = VECTOR3D(vs->at(third).x - vs->at(first).x, vs->at(third).y - vs->at(first).y, vs->at(third).z - vs->at(first).z); break; case 2: v1 = VECTOR3D(vs->at(first).x - vs->at(second).x, vs->at(first).y - vs->at(second).y, vs->at(first).z - vs->at(second).z); v2 = VECTOR3D(vs->at(third).x - vs->at(second).x, vs->at(third).y - vs->at(second).y, vs->at(third).z - vs->at(second).z); break; case 3: v1 = VECTOR3D(vs->at(first).x - vs->at(third).x, vs->at(first).y - vs->at(third).y, vs->at(first).z - vs->at(third).z); v2 = VECTOR3D(vs->at(second).x - vs->at(third).x, vs->at(second).y - vs->at(third).y, vs->at(second).z - vs->at(third).z); } Vector3DMultV(v1, v2, ans); return ans; }
// Implementation of tagDirectPolygon struct: VECTOR3D tagDirectPolygon::Normal(size_t startVert) { VECTOR3D v1, v2, ans; switch (startVert) { case 1: v1 = VECTOR3D(second.x - first.x, second.y - first.y, second.z - first.z); v2 = VECTOR3D(third.x - first.x, third.y - first.y, third.z - first.z); break; case 2: v1 = VECTOR3D(first.x - second.x, first.y - second.y, first.z - second.z); v2 = VECTOR3D(third.x - second.x, third.y - second.y, third.z - second.z); break; case 3: v1 = VECTOR3D(first.x - third.x, first.y - third.y, first.z - third.z); v2 = VECTOR3D(second.x - third.x, second.y - third.y, second.z - third.z); } Vector3DMultV(v1, v2, ans); return ans; }
VECTOR3D tagPolygon::Normal(const LPVECTOR3D vs, size_t startVert) { VECTOR3D v1, v2, ans; switch (startVert) { case 1: v1 = VECTOR3D(vs[second].x - vs[first].x, vs[second].y - vs[first].y, vs[second].z - vs[first].z); v2 = VECTOR3D(vs[third].x - vs[first].x, vs[third].y - vs[first].y, vs[third].z - vs[first].z); break; case 2: v1 = VECTOR3D(vs[first].x - vs[second].x, vs[first].y - vs[second].y, vs[first].z - vs[second].z); v2 = VECTOR3D(vs[third].x - vs[second].x, vs[third].y - vs[second].y, vs[third].z - vs[second].z); break; case 3: v1 = VECTOR3D(vs[first].x - vs[third].x, vs[first].y - vs[third].y, vs[first].z - vs[third].z); v2 = VECTOR3D(vs[second].x - vs[third].x, vs[second].y - vs[third].y, vs[second].z - vs[third].z); } Vector3DMultV(v1, v2, ans); return ans; }
//----------------------------------------------------------------------------- // Установка углов поворота объекта //----------------------------------------------------------------------------- void CGroundObject::SetRotation(VECTOR3D NewRotation) { // вызываем родительскую функцию ::CObject3D::SetRotation(NewRotation); // оружие VECTOR3D RotationBase = Rotation; VECTOR3D BaseBoundTMP = BaseBound; RotatePoint(&BaseBoundTMP, RotationBase); VECTOR3D RotationMiddle = Rotation; VECTOR3D MiddleBoundTMP = MiddleBound; if (TargetHorizObject != 0) { RotationMiddle = DrawObjectList[TargetHorizObject[0]].Rotation + Rotation; } RotatePoint(&MiddleBoundTMP, RotationMiddle); VECTOR3D RotationWeapon = Rotation; if (TargetVertObject != 0) { RotationWeapon = DrawObjectList[TargetVertObject[0]].Rotation + Rotation; } if (Weapon != 0) for (int i=0; i<WeaponQuantity; i++) if (Weapon[i] != 0) { VECTOR3D WeaponBoundTMP = WeaponBound[i]; RotatePoint(&WeaponBoundTMP, RotationWeapon); WeaponLocation[i] = BaseBoundTMP + MiddleBoundTMP + WeaponBoundTMP; // особый случай, испускаем без вращающихся частей (антиматерия, ион) if (TargetHorizObject == 0 && TargetVertObject == 0) if (!DoNotCalculateRotation) // и если нужно считать... { RotationWeapon = VECTOR3D(TargetVertObjectNeedAngle, TargetHorizObjectNeedAngle, 0.0f)+Rotation; } Weapon[i]->SetRotation(Weapon[i]->Rotation^(-1.0f)); Weapon[i]->SetRotation(RotationWeapon); Weapon[i]->SetLocation(Location + WeaponLocation[i]); } }
VECTOR3D ScreenToWorld(int x, int y){ GLint viewport[4]; GLdouble modelview[16]; GLdouble projection[16]; GLfloat winX, winY, winZ; GLdouble posX, posY, posZ; glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); glGetDoublev( GL_PROJECTION_MATRIX, projection ); glGetIntegerv( GL_VIEWPORT, viewport ); winX = (float)x; winY = (float)viewport[3] - (float)y; // Read all pixels at given screen XY from the Depth Buffer glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ ); gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ); return VECTOR3D((float)posX, (float)posY, (float)posZ); }// ScreenToWorld()
void Matrix3DTransformNormal( const MATRIX3D &T, const VECTOR3D &in, VECTOR3D &out ) { out = VECTOR3D( T._11 * in.x + T._21 * in.y + T._31 * in.z, T._12 * in.x + T._22 * in.y + T._32 * in.z, T._13 * in.x + T._23 * in.y + T._33 * in.z ); }
float lineCrossTriangle(VECTOR3D start, VECTOR3D end, VECTOR3D t1, VECTOR3D t2, VECTOR3D t3) { float s, t, ret; end -= start; t1 -= start; t2 -= start; t3 -= start; start = VECTOR3D(0.0f, 0.0f, 1.0f); t1.Rotate(start, end); t2.Rotate(start, end); t3.Rotate(start, end); ret = t1.z + t2.z + t3.z; t1.z = 0; t2.z = 0; t3.z = 0; s = (t1 & t2).z; t = (t2 & t3).z; if ((s < 0) ^ (t < 0)) return -1.0f; s = (t3 & t1).z; if ((s < 0) ^ (t < 0)) return -1.0f; return ret; }
void Matrix3DTransformCoord( const MATRIX3D &T, const VECTOR3D &in, VECTOR3D &out ) { out = VECTOR3D( T._11 * in.x + T._21 * in.y + T._31 * in.z + T._41, T._12 * in.x + T._22 * in.y + T._32 * in.z + T._42, T._13 * in.x + T._23 * in.y + T._33 * in.z + T._43 ); }
//----------------------------------------------------------------------------- // Конструктор, инициализация всех переменных //----------------------------------------------------------------------------- CSpaceObject::CSpaceObject(void) { ObjectStatus = 1; // чужой Speed = 0.0f; Velocity = RotationSpeed = VECTOR3D(0.0f,0.0f,0.0f); GFXQuantity = 0; GFX = 0; GFXLocation = 0; BossPartCountDown = -1.0f; LastCameraPoint = GamePoint; // подключаем к своему списку Next = Prev = 0; AttachSpaceObject(this); }
clsObject::clsObject(CLASS_ID clsID) : ClassID(clsID), ID(Counter++), Name(NULL) { Name = new TCHAR[MAX_OBJECT_NAME_LEN]; Name[0] = '\0'; //UCS = NULL; pos = VECTOR3D(.0f, .0f, .0f); fWd = VECTOR3D(1.0f, .0f, .0f); rWd = VECTOR3D(.0f, 1.0f, .0f); uWd = VECTOR3D(.0f, .0f, 1.0f); world = VECTOR3D(); worldScale = VECTOR3D(1.0f, 1.0f, 1.0f); localScale = worldScale; }