//! Habilita/desabilita campos da interface
void ProjectionDialog::setEnabledFields(bool mode)
{
  cboBoxProjection->setEnabled(mode);
  cboBoxDatum->setEnabled(mode);
  cboBoxUnits->setEnabled(mode);
  cboBoxZone->setEnabled(mode);
  lineEdtLongOrigem->setEnabled(mode);
  lineEdtLatOrigem->setEnabled(mode);
  lineEdtParaleloPad1->setEnabled(mode);
  lineEdtParaleloPad2->setEnabled(mode);
  lineEdtOffsetX->setEnabled(mode);
  lineEdtOffsetY->setEnabled(mode);
  lineEdtEscala->setEnabled(mode);
  grpBoxHemisferio->setEnabled(mode);
//  cancelBtn->setEnabled(mode);
  if (mode)
  {
    okBtn->setEnabled(false);
    cancelBtn->show();
  }
  else
  {
    okBtn->setEnabled(true);
    cancelBtn->hide();
  }

  if (mode)
    setupProjection(cboBoxProjection->currentIndex());
}
Exemple #2
0
// restore re-activates the display device
//
bool Display::restore() {

    bool rc = false;

    if (d3dd) {
        HRESULT hr;
		hr = d3dd->TestCooperativeLevel();
		if (hr == D3DERR_DEVICENOTRESET)
			// reset the display device
			rc = d3dd->Reset(&d3dpp) == D3D_OK;
		else if (hr == S_OK)
			rc = true;
	}
	if (rc) {
		// reacquire sprite manager references to video memory
		if (scrManager) scrManager->OnResetDevice();
    }

	// complete the restoration
	if (rc) {
        setupProjection();
        setupLighting();
		setupBlending();
	}

    return rc;
}
/** draw
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void ControlScreen::draw() {

	glDisable(GL_LIGHTING);

	setupProjection(m_windowWidth, m_windowHeight);

	/// green color
	glColor3f(0.7f, 1.0f, 0.3f);

	/// line
	glLineWidth(1.0f);

	/// calculate scale amount when viewHeight changes
	int baseHeight = 200;
	float ratio = (float)m_viewHeight / (float)baseHeight;
	
	glTranslatef(10.0, (GLfloat)-m_viewHeight, 0.0);
	glScalef(ratio, ratio, 1.0);

	
	/// draw player name text
	glPushMatrix();
		glTranslatef(0.0, 60.0, 0.0);
		for (char* c = m_playerName; *c != '\0'; c++) {
			glutStrokeCharacter(GLUT_STROKE_ROMAN, *c);
		}
	glPopMatrix();

	/// draw timer text
	if (!m_freezeClock)
		m_currentTime = gameClock.getElapsedTime();
	// conver to string
	std::stringstream ss;
	ss << m_currentTime;

	std::string displayTime1 = ss.str();
	char* displayTime = (char*)displayTime1.c_str();

	glPushMatrix();
		glTranslatef(1000.0, 60.0, 0.0);
		for (char* c = displayTime; *c != '\0'; c++) {
			glutStrokeCharacter(GLUT_STROKE_ROMAN, *c);
		}
	glPopMatrix();

	/// draw victory text
	if (m_displayVictory) {
		if (fmod(g_gameEngine->realtimeClock.getElapsedTime(), 1) < .5) {
			glPushMatrix();
			glTranslatef(1800.0, 60.0, 0.0);
			for (char* c = m_victoryText; *c != '\0'; c++) {
				glutStrokeCharacter(GLUT_STROKE_ROMAN, *c);
			}
			glPopMatrix();
		}
	}

	glEnable(GL_LIGHTING);
}
Exemple #4
0
void Renderer::draw()
{
	//int dw = al_get_display_width(dpy_);
   //int dh = al_get_display_height(dpy_);

	ALLEGRO_TRANSFORM trans;
	al_identity_transform(&trans);

	al_compose_transform(&trans, &camera_transform_);
	al_rotate_transform_3d(&trans, 1.0, 0.0, 0.0, rx_look);

	setupProjection(&trans);
	al_identity_transform(&trans);


	al_use_transform(&trans);

	getWorldPos(camera_pos_);

	al_copy_transform(&cur3d_transform_, al_get_current_transform());

	glEnable(GL_DEPTH_TEST);
	// Accept fragment if it closer to the camera than the former one
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_CULL_FACE);
	glFrontFace(GL_CCW);
	glEnable(GL_ALPHA_TEST);

	if(!setShader(SHADER_DEFAULT))
	{
		NBT_Debug("failed to set default shader");
	}

	glBindVertexArray(vao_);

	resManager_->setAtlasUniforms();

	for(auto &it: chunkData_)
	{
		ChunkData *cd = it.second;

		ALLEGRO_TRANSFORM ctrans;
		al_identity_transform(&ctrans);
		al_translate_transform_3d(&ctrans, cd->x()*15.0, 0.0, cd->z()*15.0);
		al_use_transform(&ctrans);

		cd->draw(&ctrans);
	}

	glBindVertexArray(0);

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	//drawSelection();

	resManager_->unsetAtlasUniforms();
}
Exemple #5
0
// resize resets the aspect ratio and recalculates the projection matrix
//
void Display::resize() {

	if (runInWindow) {
        int width  = context->get(GF_WN_WDTH);
        int height = context->get(GF_WN_HGHT);

		// reset the aspect ratio
		aspect = float(width) / height;
		// reset projection transformation
		setupProjection();
	}
}
Exemple #6
0
// resize resets the aspect ratio and recalculates the projection matrix
//
void Display::resize() {

	if (runInWindow) {
        int width  = context->get(GF_WN_WDTH);
        int height = context->get(GF_WN_HGHT);
        APIText::hasClientArea(width, height);

		// reset the aspect ratio
		aspect = float(width) / height;
        context->set(GF_FR_ASP, aspect);
		// reset projection transformation
		setupProjection();
	}
}
Exemple #7
0
// -----------------------------------------------------------------------------
SceneManager::SceneManager(const glm::ivec2 screenSize,
                           SceneContainer *container)
    : m_world(container->getWorld()), m_camera(container->getCamera()),
      #ifndef WINDOWS
      m_textManager(TextManager(FONT_PATH + FONT_FILE, FONT_HEIGHT, screenSize)),
      #endif
      m_drawer(screenSize),
      m_shadowManager(screenSize),
      m_BACKGROUND_COLOR(container->getBackgroundColor()),
      m_positionMutex(SDL_CreateMutex()) {
  m_lightMask = (2 << (m_world->getLightsNumber() - 1)) - 1;
  setupProjection(screenSize);
  initGPU(container);
  glClearColor(m_BACKGROUND_COLOR.x, m_BACKGROUND_COLOR.y, m_BACKGROUND_COLOR.z,
               m_BACKGROUND_COLOR.w);
  checkOpenGLError("GLInitializer: glClearColor");
}
//! Inicializa valores default da interface
void ProjectionDialog::Init()
{
  bool oldStat = _ignoreChangeEvents;

  QStringList strLst;
  unsigned  i;

  _ignoreChangeEvents = true;

  // Preenche Combo Zonas
  for (i=1; i<=60; i++)
	strLst.push_back("Zone "+QString::number(i));

  cboBoxZone->addItems(strLst);

  // Preenche combo Datum
  std::vector<QString> vetDatum(listDATA_Names, listDATA_Names + sizeof(listDATA_Names)/sizeof(QString));
  _vetDatum = vetDatum;

  strLst.clear();

  for (i=0; i < _vetDatum.size(); i++)
    strLst.push_back(_vetDatum[i]);

  cboBoxDatum->addItems(strLst);

  // Preenche combo Projecoes
  std::vector<pdProjection> vetProj(listProjections, listProjections + sizeof(listProjections)/sizeof(pdProjection));
  _vetProj = vetProj;

  strLst.clear();

  for (i=0; i < _vetProj.size(); i++)
    strLst.push_back(_vetProj[i].name);

  cboBoxProjection->addItems(strLst);

  cboBoxProjection->setCurrentIndex(0);
  setupProjection(cboBoxProjection->currentIndex());

  _ignoreChangeEvents = oldStat;
}
Exemple #9
0
void GLLUTWidget::resizeGL(int width, int height)
{

  //save new viewport dimensions:
  /*if (width < height) {
    vpW=vpH=width;
  } else {
    vpW=vpH=height;
  }*/
  vpW=width;
  vpH=height;
  if (vpW < 1) vpW=1;
  if (vpH < 1) vpH=1;

  //glViewport((width > vpW) ? ((width-vpW) / 2) : 0, (height> vpH) ? ((height-vpH) / 2) : 0, vpW, vpH);
  glViewport(0, 0, vpW, vpH);

  setupProjection();

}
Exemple #10
0
void GLLUTWidget::paintGL()
{
  m.lock();
  if (gl_ready && needs_init && _lut!=0) {
    init();
  }
  m.unlock();

  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);

  setupProjection();

  //setup lens:
  
  //switch to modelview mode
  glMatrixMode( GL_MODELVIEW );
  
  glLoadIdentity();

  glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  //cam.apply();

  if (view_mode==VIEW_SINGLE || view_mode==VIEW_GRID) {
    gluLookAt(0.0,0.0,100.0, 0.0, 0.0, 0.0,0.0,1.0,0.0);
  } else {
    cam.apply();
  }

  if (_lut==0) return;
  //store matrices for reverse mouse-lookup
  for (int i=0;i<(int)slices.size();i++) {
    Slice * s=slices[i];
    if (s->bg_update_pending) {
      if (s->bg->update()==false) printf("draw-slice texture update failed\n");
      s->bg_update_pending=false;
    }
    if (s->selection_update_pending) {
      if (s->selection->update(GL_CLAMP,GL_CLAMP,GL_NEAREST,GL_NEAREST)==false) printf("draw-slice texture update failed\n");
      s->selection_update_pending=false;
    }
    if (s->sampler_update_pending) {
      if (s->sampler->update(GL_CLAMP,GL_CLAMP,GL_LINEAR,GL_NEAREST)==false) printf("draw-slice texture update failed\n");
      s->sampler_update_pending=false;
    }
  }

  
  //let's draw a slice
  //glColor3f(0.0,0.0,0.0);
  if (view_mode==VIEW_SINGLE) {
    glGetIntegerv(GL_VIEWPORT,viewport);
    glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
    glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
    glDrawSlice(slices[state.slice_idx]);
  } else if (view_mode==VIEW_GRID) {
    int cols=(int)(sqrt(slices.size()));
    int rows=slices.size()/cols;
    int c=0;
    int r=0;
    for (int i=0;i<(int)slices.size();i++) {
      glPushMatrix();
      glTranslatef((1.0/(float)cols)*(float)c,(1.0/(float)rows)*(float)r,0.0);
      c++;
      if (c >= cols) { c=0; r++; }

      glScalef((1.0/(float)cols)*0.98,(1.0/(float)rows)*0.98,1.0);
      glTranslatef(0.01,0.01,0.0);
      if (i==state.slice_idx) {
        glGetIntegerv(GL_VIEWPORT,viewport);
        glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
        glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
      }
      glDrawSlice(slices[i]);
      glBindTexture(GL_TEXTURE_2D,0);
      glColor3f(1.0,1.0,1.0);
      if (i==state.slice_idx) {
        glPushMatrix();
        glLineWidth(2.0);
        glBegin(GL_LINES);

        glVertex3f(0.0,0.0,0.01);
        glVertex3f(1.0,0.0,0.01);

        glVertex3f(1.0,0.0,0.01);
        glVertex3f(1.0,1.0,0.01);

        glVertex3f(1.0,1.0,0.01);
        glVertex3f(0.0,1.0,0.01);

        glVertex3f(0.0,1.0,0.01);
        glVertex3f(0.0,0.0,0.01);

        glEnd();
        glPopMatrix();
      }
      glPopMatrix();
    }
  } else if (view_mode==VIEW_CUBE) {

      

      glTranslatef(-0.5,-0.5,-0.5);
      glColor3f(0.7,0.7,0.7);
      glPushMatrix();
        glLineWidth(1.0);
        glBegin(GL_LINES);

        glVertex3f(0.0,0.0,0.0);
        glVertex3f(1.0,0.0,0.0);

        glVertex3f(0.0,1.0,0.0);
        glVertex3f(1.0,1.0,0.0);

        glVertex3f(0.0,0.0,1.0);
        glVertex3f(1.0,0.0,1.0);

        glVertex3f(0.0,1.0,1.0);
        glVertex3f(1.0,1.0,1.0);

        glVertex3f(0.0,0.0,0.0);
        glVertex3f(0.0,1.0,0.0);

        glVertex3f(1.0,0.0,0.0);
        glVertex3f(1.0,1.0,0.0);

        glVertex3f(0.0,0.0,1.0);
        glVertex3f(0.0,1.0,1.0);

        glVertex3f(1.0,0.0,1.0);
        glVertex3f(1.0,1.0,1.0);

        glVertex3f(0.0,0.0,0.0);
        glVertex3f(0.0,0.0,1.0);

        glVertex3f(0.0,1.0,0.0);
        glVertex3f(0.0,1.0,1.0);

        glVertex3f(1.0,0.0,0.0);
        glVertex3f(1.0,0.0,1.0);

        glVertex3f(1.0,1.0,0.0);
        glVertex3f(1.0,1.0,1.0);

        glEnd();
      glPopMatrix();
    for (int i=0;i<(int)slices.size();i++) {
      glPushMatrix();
      glTranslatef(0.0,0.0,(1.0 / (float)slices.size()) * (float)i);
      //glScalef((1.0/(float)cols)*0.98,(1.0/(float)rows)*0.98,1.0);
      //glTranslatef(0.01,0.01,0.0);
      /*if (i==state.slice_idx) {
        glGetIntegerv(GL_VIEWPORT,viewport);
        glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
        glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
      }*/
      glDrawSlice(slices[i]);
      glBindTexture(GL_TEXTURE_2D,0);
      


      glColor3f(1.0,1.0,1.0);
      if (i==state.slice_idx) {
        glPushMatrix();
        glLineWidth(2.0);
        glBegin(GL_LINES);

        glVertex3f(0.0,0.0,0.01);
        glVertex3f(1.0,0.0,0.01);

        glVertex3f(1.0,0.0,0.01);
        glVertex3f(1.0,1.0,0.01);

        glVertex3f(1.0,1.0,0.01);
        glVertex3f(0.0,1.0,0.01);

        glVertex3f(0.0,1.0,0.01);
        glVertex3f(0.0,0.0,0.01);

        glEnd();
        glPopMatrix();
      }
      glPopMatrix();
    }
  }

  c_draw.count();
  //updateVideoStats(stats);
}
Exemple #11
0
int runScene(Drawable & scene){
    
    glfwInit();
    if( !glfwOpenWindow( 500, 500, 0,0,0,0, 16,0, 
			 GLFW_WINDOW ) )
      {
        glfwTerminate();
      }
    glfwSetWindowTitle( "3D" );
   
    glfwEnable( GLFW_STICKY_KEYS );
    glfwEnable( GLFW_MOUSE_CURSOR );
    glfwDisable( GLFW_AUTO_POLL_EVENTS );
  
    setupCallbacks();

    do //Main Loop.
      {
	setupProjection();

	//	stereo_state.Ping();

       	projection_state.Load();


	float zoom=2.0;
	glScalef(zoom*2.0/256.0, zoom*2.0/256.0, zoom*2.0/256.0);
	glDisable (GL_BLEND); 
	glDisable(GL_LIGHTING);
	glClear(GL_COLOR_BUFFER_BIT | 
		GL_DEPTH_BUFFER_BIT | 
		GL_STENCIL_BUFFER_BIT);

	setupModelview();

	/*   Temporary light insertion:
	 */

	navigator.Draw();
	//reference plane
	DrawPlane(V3f(0,0,0), V3f(20,0,0), V3f(0,20,0), 5);

	SetupLighting();
	scene.Draw();
	
	//Clear modified mouse state.
	mouse_state.Moved();
	
       	glfwSwapBuffers();
	if(!stereo_state.enabled){
	  glfwWaitEvents();
	}else{
	  glfwPollEvents();
	  glfwSleep(0.01); //just sleep.
	};

	navigator.idle_move();

      }
    while(!glfwGetKey( GLFW_KEY_ESC ) &&
	  glfwGetWindowParam( GLFW_OPENED ));
 

  return true;
};
Exemple #12
0
void GLFWCALL GLFWWindowRefreshCb(void){
   setupProjection();
};
Exemple #13
0
//{{{GLFW callbacks 
void GLFWCALL GLFWWindowSizeCb(int,int){
  setupProjection();
};
//! Trata alteração da Combo de Projeção. Ao ser alterada, deve atualizar os valores default da interface
void ProjectionDialog::changeComboProjection(int index)
{
	setupProjection(index);
}
Exemple #15
0
// setup creates the display device according to the context
// parameters and associates the device with the application window
// (HWND)hwnd
//
bool Display::setup(void* hwnd) {

    bool rc = false;

	// store address of main application window
    this->hwnd = (HWND)hwnd;

    // set the D3D presentation parameters
    UINT adapter;
	D3DFORMAT d3dFormat;
    ZeroMemory(&d3dpp, sizeof d3dpp);
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferCount = 1;
    d3dpp.EnableAutoDepthStencil = TRUE;
    D3DDISPLAYMODE d3ddm;
    if (!runInWindow) {
		D3DFORMAT Format[] = D3D_DOC_FORMATS;
		d3dFormat = Format[pixel];
        if (FAILED(d3d->EnumAdapterModes(display, d3dFormat, mode, &d3ddm))) {
            error(L"Display::10 Failed to get selected display mode");
            error(L"Display::11 Defaulting to windowed mode");
            runInWindow = true;
        }
        else {
            adapter = display;
            d3dpp.BackBufferWidth  = d3ddm.Width;
            d3dpp.BackBufferHeight = d3ddm.Height;
            d3dpp.BackBufferFormat = d3ddm.Format;
            d3dpp.FullScreen_RefreshRateInHz = d3ddm.RefreshRate;
            width  = d3ddm.Width;
            height = d3ddm.Height;
        }
    }
    if (runInWindow) {
        adapter = D3DADAPTER_DEFAULT;
		d3d->GetAdapterDisplayMode(adapter, &d3ddm);
		d3dFormat = d3ddm.Format;
        d3dpp.Windowed = TRUE;
		d3dpp.BackBufferWidth  = WND_WIDTH;
		d3dpp.BackBufferHeight = WND_HEIGHT;
		d3dpp.BackBufferFormat = d3dFormat;
        width  = WND_WIDTH;
        height = WND_HEIGHT;
    }
	aspect = (float) width / height;
    context->set(GF_FR_ASP, aspect);

    // find the best format for depth buffering and stenciling
    //
    D3DDEVTYPE devtype;
    if (D3D_OK == d3d->CheckDeviceFormat(adapter, D3DDEVTYPE_HAL,
     d3dFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D32)) {
        d3dpp.AutoDepthStencilFormat = D3DFMT_D32; // depth buffer
        devtype = D3DDEVTYPE_HAL;                  // HAL device   
    }
    else if (D3D_OK == d3d->CheckDeviceFormat(adapter, D3DDEVTYPE_HAL, 
     d3dFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16)) {
        d3dpp.AutoDepthStencilFormat = D3DFMT_D16;  // depth buffer
        devtype = D3DDEVTYPE_HAL;                   // HAL Device
    }
    // if the above attempts fail, use the REF (software emulation) device
    // with a 32-bit depth buffer rather than the HAL (hardware accelerated) 
	// device
    else if (D3D_OK == d3d->CheckDeviceFormat(adapter, D3DDEVTYPE_REF,
     d3dFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D32)) {
        d3dpp.AutoDepthStencilFormat = D3DFMT_D32;   // depth buffer
        devtype = D3DDEVTYPE_REF;                    // REF Device
    }
    // if all else fails, use the REF (software emulation) with a 16-bit
    // depth buffer, hoping that it will work. (If it doesn't, we are out
    // of luck anyway.)
    else {
        d3dpp.AutoDepthStencilFormat = D3DFMT_D16;   // depth buffer
        devtype = D3DDEVTYPE_REF;                    // REF Device
    }

    // extract the device capabilities and configure the limits
    D3DCAPS9 caps;
    d3d->GetDeviceCaps(adapter, devtype, &caps);

	// hardware or software vertex processing?
	DWORD behaviorFlags;
	if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0)
	    behaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
	else
	    behaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;

    // retrieve the Interface to the D3D display device
    if (d3dd)
        error(L"Display::11 Pointer to Direct3D interface is not NULL");
    else if (FAILED(d3d->CreateDevice(adapter, devtype, (HWND)hwnd, 
	 behaviorFlags, &d3dpp, &d3dd)))
        error(L"Display::12 Failed to create Direct3D device");
    else {
        // maximum number of lights supported by the display device
		maxLights = caps.MaxActiveLights ? caps.MaxActiveLights : MAX_ACTIVE_LIGHTS;
        // set anisotropic filtering to the maximum available on the device
        if (FAILED(d3dd->SetSamplerState(0, D3DSAMP_MAXANISOTROPY,
         caps.MaxAnisotropy - 1)))
            error(L"Display::17 Failed to set up anisotropic filtering");

		// create a sprite COM object to manage the drawing of the hud texture
		// and the drawing of the text item fonts
		if (!scrManager && FAILED(D3DXCreateSprite(d3dd, &scrManager)))
			error(L"Display::18 Failed to create the sprite manager");

		// setup successful
        APILight::connectsThrough(d3dd);
        d3dd->AddRef();
        APITexture::connectsThrough(d3dd);
        d3dd->AddRef();
        APIGraphic::connectsThrough(d3dd);
        d3dd->AddRef();
        APIText::connectsThrough(d3dd);
        d3dd->AddRef();
        APITexture::isManagedBy(scrManager);
        scrManager->AddRef();
        APIText::isManagedBy(scrManager);
        scrManager->AddRef();
        APITexture::hasClientArea(width, height);
        APIText::hasClientArea(width, height);
        rc = true;
    }

	// complete the setup
	if (rc) {
        setupProjection();
        setupLighting();
		setupBlending();
	}

    return rc;
}
Exemple #16
0
// setup creates the display device according to the context
// parameters and associates the device with the application window
// (HWND)hwnd
//
bool Display::setup(void* hwnd) {

    bool rc = false;

	// store address of main application window
    this->hwnd = (HWND)hwnd;

    // set the D3D presentation parameters
    UINT adapter;
	D3DFORMAT d3dFormat;
    ZeroMemory(&d3dpp, sizeof d3dpp);
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferCount = 1;
    d3dpp.EnableAutoDepthStencil = TRUE;
    D3DDISPLAYMODE d3ddm;
    if (!runInWindow) {
		D3DFORMAT Format[] = D3D_DOC_FORMATS;
		d3dFormat = Format[pixel];
        if (FAILED(d3d->EnumAdapterModes(display, d3dFormat, mode, &d3ddm))) {
            error(L"Display::10 Failed to get selected display mode");
            error(L"Display::11 Defaulting to windowed mode");
            runInWindow = true;
        }
        else {
            adapter = display;
            d3dpp.BackBufferWidth  = d3ddm.Width;
            d3dpp.BackBufferHeight = d3ddm.Height;
            d3dpp.BackBufferFormat = d3ddm.Format;
            d3dpp.FullScreen_RefreshRateInHz = d3ddm.RefreshRate;
            width_  = d3ddm.Width;
            height_ = d3ddm.Height;
        }
    }
    if (runInWindow) {
        adapter = D3DADAPTER_DEFAULT;
		d3d->GetAdapterDisplayMode(adapter, &d3ddm);
		d3dFormat = d3ddm.Format;
        d3dpp.Windowed = TRUE;
		d3dpp.BackBufferWidth  = WND_WIDTH;
		d3dpp.BackBufferHeight = WND_HEIGHT;
		d3dpp.BackBufferFormat = d3dFormat;
        width_  = WND_WIDTH;
        height_ = WND_HEIGHT;
    }
	aspect = (float) width_ / height_;

    // find the best format for depth buffering and stenciling
    //
    D3DDEVTYPE devtype;
    if (D3D_OK == d3d->CheckDeviceFormat(adapter, D3DDEVTYPE_HAL,
     d3dFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D32)) {
        d3dpp.AutoDepthStencilFormat = D3DFMT_D32; // depth buffer
        devtype = D3DDEVTYPE_HAL;                  // HAL device   
    }
    else if (D3D_OK == d3d->CheckDeviceFormat(adapter, D3DDEVTYPE_HAL, 
     d3dFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16)) {
        d3dpp.AutoDepthStencilFormat = D3DFMT_D16;  // depth buffer
        devtype = D3DDEVTYPE_HAL;                   // HAL Device
    }
    // if the above attempts fail, use the REF (software emulation) device
    // with a 32-bit depth buffer rather than the HAL (hardware accelerated) 
	// device
    else if (D3D_OK == d3d->CheckDeviceFormat(adapter, D3DDEVTYPE_REF,
     d3dFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D32)) {
        d3dpp.AutoDepthStencilFormat = D3DFMT_D32;   // depth buffer
        devtype = D3DDEVTYPE_REF;                    // REF Device
    }
    // if all else fails, use the REF (software emulation) with a 16-bit
    // depth buffer, hoping that it will work. (If it doesn't, we are out
    // of luck anyway.)
    else {
        d3dpp.AutoDepthStencilFormat = D3DFMT_D16;   // depth buffer
        devtype = D3DDEVTYPE_REF;                    // REF Device
    }

    // extract the device capabilities and configure the limits
    D3DCAPS9 caps;
    d3d->GetDeviceCaps(adapter, devtype, &caps);
	DWORD behaviorFlags;
	// hardware or software vertex processing?
	if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
	 caps.VertexShaderVersion < D3DVS_VERSION(1,1))
	    behaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
	else
	    behaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
    // retrieve the Interface to the D3D display device
    if (d3dd)
        error(L"Display::11 Pointer to Direct3D interface is not NULL");
    else if (FAILED(d3d->CreateDevice(adapter, devtype, (HWND)hwnd, 
	 behaviorFlags, &d3dpp, &d3dd)))
        error(L"Display::12 Failed to create Direct3D device");
    else {
        // maximum number of lights supported by the display device
		maxLights = caps.MaxActiveLights ? caps.MaxActiveLights : 8;
        // set anisotropic filtering to the maximum available on the device
        if (FAILED(d3dd->SetSamplerState(0, D3DSAMP_MAXANISOTROPY,
         caps.MaxAnisotropy - 1)))
            error(L"Display::13 Failed to set up anisotropic filtering");

		// create a sprite COM object to manage the drawing of the hud texture
		// and the drawing of the text item fonts
		if (!spriteManager_ && FAILED(D3DXCreateSprite(d3dd, &spriteManager_)))
			error(L"Display::14 Failed to create the font manager");

		// setup successful
        rc = true;
    }
	//setup fog
	if (caps.RasterCaps & D3DPRASTERCAPS_FOGRANGE)
	{
		//configure fog distances and thickness
		float fogStart = FOGSTART_DEFAULT;
		float fogEnd = FOGEND_DEFAULT;

		//enable fog
		d3dd->SetRenderState(D3DRS_FOGENABLE,TRUE);
		
		//enable ranged base fog
		d3dd->SetRenderState(D3DRS_RANGEFOGENABLE,TRUE);

		//configure fog color
		d3dd->SetRenderState(D3DRS_FOGCOLOR,FOGCOLOR_DEFAULT);
		
		//set fog to be vertex fog using linear fog formula
		d3dd->SetRenderState(D3DRS_FOGVERTEXMODE,D3DFOG_LINEAR);

		//set fog distances and thickness
		d3dd->SetRenderState(D3DRS_FOGSTART,*(DWORD*)(&fogStart));
		d3dd->SetRenderState(D3DRS_FOGEND,*(DWORD*)(&fogEnd));
	
	}
	// complete the setup
	if (rc) {
        setupProjection();
        setupLighting();
		setupBlending();
	}

    return rc;
}