void Chunk::render(char material, int *rendered) /*const*/ { glPushMatrix(); glTranslated((cx-m_World.m_Player.m_Position.cx)*CHUNK_SIZE_XZ, 0, (cz-m_World.m_Player.m_Position.cz)*CHUNK_SIZE_XZ); GLenum mode = GL_EXECUTE; int list_to_render = 0; if (material == MAT_WATER) { list_to_render = 1; } if (m_NeedToRender[list_to_render] == RENDER_NEED) { mode = GL_COMPILE; } else if (m_NeedToRender[list_to_render] == RENDER_MAYBE) { int prob = 1000/(*rendered*5 + 1); int r = rand()%1000; if (r <= prob /*|| 1*/) mode = GL_COMPILE; } // todo: GL_RENDER dont compile chunk Chunk *chunk = m_World.m_Player.m_pChunk; if (chunk) { if (chunk->cx >= cx - 1 && chunk->cz >= cz - 1 && chunk->cx <= cx + 1 && chunk->cz <= cz + 1) { m_NeedToRender[list_to_render] = RENDER_NEED; mode = GL_RENDER; } } if (mode == GL_COMPILE || mode == GL_COMPILE_AND_EXECUTE || mode == GL_RENDER) { if (mode != GL_RENDER) { glNewList(m_RenderList + list_to_render, mode); } //1-sided tiles if (material == MAT_WATER) { glTranslated(0.0, -0.95/8, 0.0); glDisable(GL_CULL_FACE); } else { glEnable(GL_CULL_FACE); } glBegin(GL_QUADS); std::list<Block*> *p_tiles; BlockCoord x, y, z; BlockInWorld temp, this_pos = BlockInWorld(cx, cz); for (int side = 0; side < 6; side++) { if (material == MAT_WATER) { p_tiles = &m_pDisplayedWaterTiles[side]; } else { p_tiles = &m_pDisplayedTiles[side]; } auto it = p_tiles->begin(); while (it != p_tiles->end()) { if (getBlockPositionByPointer(*it, &x, &y, &z)) { Light::blockLight(m_World, *this, side, x, y, z); temp = this_pos + BlockInWorld(x, y, z); drawTile(temp, *it, side); } ++it; } } glEnd(); if (material == MAT_WATER) { glTranslated(0.0, 0.95/8, 0.0); } if (m_NeedToRender[list_to_render] == RENDER_MAYBE) { (*rendered) ++; } if (mode != GL_RENDER) { glEndList(); m_NeedToRender[list_to_render] = RENDER_NO_NEED; } } if (mode != GL_COMPILE_AND_EXECUTE && mode != GL_RENDER) { glCallList(m_RenderList + list_to_render); } glPopMatrix(); }
void Character::Control(GLdouble FrameInterval) { GLdouble step = WALK_SPEED; if(bSpecial[GLUT_KEY_SHIFT_L]) step *= SPRINT_KOEF; if(bKeyboard['W']) { dVelocityX -= FrameInterval*AIR_ACCEL*step*sin(TORAD(dSpinY)); dVelocityZ -= FrameInterval*AIR_ACCEL*step*cos(TORAD(dSpinY)); } if(bKeyboard['S']) { dVelocityX += FrameInterval*AIR_ACCEL*step*sin(TORAD(dSpinY)); dVelocityZ += FrameInterval*AIR_ACCEL*step*cos(TORAD(dSpinY)); } if(bKeyboard['D']) { dVelocityX += FrameInterval*AIR_ACCEL*step*cos(TORAD(dSpinY)); dVelocityZ -= FrameInterval*AIR_ACCEL*step*sin(TORAD(dSpinY)); } if(bKeyboard['A']) { dVelocityX -= FrameInterval*AIR_ACCEL*step*cos(TORAD(dSpinY)); dVelocityZ += FrameInterval*AIR_ACCEL*step*sin(TORAD(dSpinY)); } if(bKeyboard['R']) { dVelocityY += FrameInterval*AIR_ACCEL*step; } if(bKeyboard['F']) { dVelocityY -= FrameInterval*AIR_ACCEL*step; } GLdouble ko = dVelocityX*dVelocityX + dVelocityZ*dVelocityZ; if(ko > WALK_SPEED*WALK_SPEED*SPRINT_KOEF*SPRINT_KOEF) { ko = pow(ko, 0.5); dVelocityX = dVelocityX*WALK_SPEED*SPRINT_KOEF/ko; dVelocityZ = dVelocityZ*WALK_SPEED*SPRINT_KOEF/ko; } if(bKeyboard[VK_SPACE]) { } if(bKeyboard['X']) { dVelocityX = 0; dVelocityY = 0; dVelocityZ = 0; } GLdouble yerr, xerr, zerr; GetPlane(&xerr, &yerr, &zerr); PosInWorld pos; if(zerr < xerr && zerr < yerr) { if((position.bz < centerPos.bz && position.cz == centerPos.cz) || position.cz < centerPos.cz) { pos = PosInWorld(0, 0, 0.5); } else { pos = PosInWorld(0, 0, -0.5); } } else if(xerr < zerr && xerr < yerr) { if((position.bx < centerPos.bx && position.cx == centerPos.cx) || position.cx < centerPos.cx) { pos = PosInWorld(0.5, 0, 0); } else { pos = PosInWorld(-0.5, 0, 0); } } else if(yerr < xerr && yerr < zerr) { if(position.by < centerPos.by) { pos = PosInWorld(0, 0.5, 0); } else { pos = PosInWorld(0, -0.5, 0); } } aimedBlock = BlockInWorld(centerPos + pos); freeBlock = BlockInWorld(centerPos + pos.inv()); int num = 100; int sq = 100; int sqb2 = sq/2; if(bKeyboard['1']) { int i = 0; while(i < num) { if(wWorld.AddBlock(BlockInWorld(rand()%sq-sqb2, abs(rand()%sq-sqb2), rand()%sq-sqb2), rand()%14+1, true)) i++; } } if(bKeyboard['2']) { int i = 0; while(i < num) { if(wWorld.RemoveBlock(BlockInWorld(rand()%sq-sqb2, rand()%sq-sqb2, rand()%sq-sqb2), true)) i++; } } if(bKeyboard['3']) { for(BlockCoord i = -sqb2; i <= sqb2; i++) { for(BlockCoord j = -sqb2; j <= sqb2; j++) { for(BlockCoord k = -sqb2; k <= sqb2; k++) { wWorld.RemoveBlock(BlockInWorld(i, j, k), true); } } } } if(bKeyboard['4']) { wWorld.LoadChunk(0, 0); bKeyboard['4'] = false; } if(bKeyboard['5']) { wWorld.UnLoadChunk(0, 0); bKeyboard['5'] = false; } if(bKeyboard['6']) { for(int i = 0; i < 8; i++) for(int j = 0; j < 8; j++) wWorld.LoadChunk(i, j); bKeyboard['6'] = false; } if(bKeyboard['7']) { for(int i = 0; i < 8; i++) for(int j = 0; j < 8; j++) wWorld.UnLoadChunk(i, j); bKeyboard['7'] = false; } if(bKeyboard['0']) { static Param par = {0, 1, &wWorld}; _beginthread(LoadNGenerate, 0, &par); WaitForSingleObject(wWorld.parget2, INFINITE); ResetEvent(wWorld.parget2); par.z++; bKeyboard['0'] = false; } if(bKeyboard['C']) { Chunk *chunk; int index; wWorld.FindBlock(aimedBlock, &chunk, &index); if ((chunk)&&(chunk->bBlocks[index].cMaterial == MAT_DIRT)) { chunk->bBlocks[index].bVisible ^= (1 << SNOWCOVERED); } } if(bKeyboard['V']) { Chunk *chunk; int index; wWorld.FindBlock(aimedBlock, &chunk, &index); if ((chunk)&&(chunk->bBlocks[index].cMaterial == MAT_DIRT)) { chunk->bBlocks[index].bVisible ^= (1 << GRASSCOVERED); } } if(bKeyboard['E']) { wWorld.RemoveBlock(aimedBlock, true); } if(bKeyboard['Q']) { wWorld.AddBlock(freeBlock, MAT_PUMPKIN_SHINE, true); } if(bKeyboard['O']) { wWorld.SaveChunks(); } position = position + PosInWorld(FrameInterval*dVelocityX, FrameInterval*dVelocityY, FrameInterval*dVelocityZ); /*{ signed short xx, yy, zz; GLdouble wx = gfPosX + gfVelX; GLdouble wy = gfPosY - PLAYER_HEIGHT + 0.1; GLdouble wz = gfPosZ; xx = floor(wx/TILE_SIZE + 0.5); zz = floor(wz/TILE_SIZE + 0.5); yy = floor(wy/TILE_SIZE); if((FindTile(xx, yy, zz) == NULL)&&(FindTile(xx, yy + 1, zz) == NULL)) gfPosX += g_FrameInterval*gfVelX; else gfVelX = 0; } { signed short xx, yy, zz; GLdouble wx = gfPosX; GLdouble wy = gfPosY - PLAYER_HEIGHT + 0.1; GLdouble wz = gfPosZ + gfVelZ; xx = floor(wx/TILE_SIZE + 0.5); zz = floor(wz/TILE_SIZE + 0.5); yy = floor(wy/TILE_SIZE); if((FindTile(xx, yy, zz) == NULL)&&(FindTile(xx, yy + 1, zz) == NULL)) gfPosZ += g_FrameInterval*gfVelZ; else gfVelZ = 0; } */ /*if(falling) { gfPosY -= g_FrameInterval*gfVelY; if(gfVelY < MAX_DOWNSTEP) gfVelY += g_FrameInterval*STEP_DOWNSTEP; }*/ /* { signed short xx, yy, zz; GLdouble wx = gfPosX; GLdouble wy = gfPosY - PLAYER_HEIGHT; GLdouble wz = gfPosZ; xx = floor(wx/TILE_SIZE + 0.5); zz = floor(wz/TILE_SIZE + 0.5); yy = floor(wy/TILE_SIZE); if(FindTile(xx, yy, zz) == NULL) falling = true; else { gfVelY = 0; if(falling) { falling = false; gfPosY = (yy + 1)*TILE_SIZE + PLAYER_HEIGHT - 0.001; } } } if(!falling) { gfVelX = 0; gfVelZ = 0; }*/ //falling = 1; // if(dPositionX >= LOCATION_SIZE_XZ*TILE_SIZE) { dPositionX -= LOCATION_SIZE_XZ*TILE_SIZE; lnwPositionX++;} // if(dPositionX < 0) { dPositionX += LOCATION_SIZE_XZ*TILE_SIZE; lnwPositionX--;} // if(dPositionZ >= LOCATION_SIZE_XZ*TILE_SIZE) { dPositionZ -= LOCATION_SIZE_XZ*TILE_SIZE; lnwPositionZ++;} // if(dPositionZ < 0) { dPositionZ += LOCATION_SIZE_XZ*TILE_SIZE; lnwPositionZ--;} }