Jump to content
  • Advertisement
Sign in to follow this  
Wolvorine

OpenGL Slow speed when camera nears to the model

This topic is 4426 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have create a simple program that contains a box with texture, when i move far away from the box then everything works smooth but as i come near to the model box , my movement became stucky, i think fps gets too much low at that time... why is this happening ? is there anyway to make it fine? below is my source

// Gl_Engine.cpp: implementation of the Gl_Engine class.
//
//////////////////////////////////////////////////////////////////////

#include "Gl_Engine.h"
#include <stdio.h>
#include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Gl_Engine::Gl_Engine()
{
	m_hRC=NULL;
	m_RotDeg =m_RotDeg2 = 10;
}

Gl_Engine::~Gl_Engine()
{

}

int Gl_Engine::InitGL(GLvoid)
{

						// Enables Smooth Shading

	

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);					// Black Background

	glClearDepth(1.0f);							// Depth Buffer Setup

	glEnable(GL_DEPTH_TEST);						// Enables Depth Testing
	
	glDepthFunc(GL_LESS);							// The Type Of Depth Test To Do

	glShadeModel(GL_SMOOTH);

	glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
	

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);			// Really Nice Perspective Calculations


	StrArray tmpvar;
	tmpvar.push_back("C:\\Kasamba clinets\\OpenGlScene\\Textures\\NeHe.bmp");
	LoadTexture(tmpvar);
	//AddLight();

	return TRUE;								// Initialization Went OK

}


int Gl_Engine::DrawGLScene(GLvoid)								// Here's Where We Do All The Drawing
{

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear The Screen And The Depth Buffer
	
//	glDisable(GL_LIGHTING);
	
	glLoadIdentity();							// Reset The Current Modelview Matrix


	RotateWorldObjects();
	SetWorldObjectDirection();

	// Reset The Current Modelview Matrix
	Build3dHouse();
	
	//Sleep(30);
return TRUE;								// Everything Went OK 

}


int Gl_Engine::CreateGlContext(HDC &r_hdc)
{

	if (!(m_hRC=wglCreateContext(r_hdc)))					// Are We Able To Get A Rendering Context?
	{
		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;							// Return FALSE
	}

	if(!wglMakeCurrent(r_hdc,m_hRC))						// Try To Activate The Rendering Context
	{
		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;							// Return FALSE
	}

return TRUE;
}

void Gl_Engine::CloseGl_Engine()
{

	if (!wglMakeCurrent(NULL,NULL))					// Are We Able To Release The DC And RC Contexts?
	{

		MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);

	}


	if (!wglDeleteContext(m_hRC))					// Are We Able To Delete The RC?
	{
		MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
	}



	this->m_hRC=NULL;

}


void Gl_Engine::ReSizeGLScene(GLsizei width, GLsizei height)				// Resize And Initialize The GL Window
{
	if (height==0)								// Prevent A Divide By Zero By
	{
		height=1;							// Making Height Equal One
	}

	glViewport(0, 0, width, height);					// Reset The Current Viewport
	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();							// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);						// Select The Modelview Matrix
	glLoadIdentity();							// Reset The Modelview Matrix
}


int Gl_Engine::LoadTexture(StrArray strFileNames)
{

	int iSize = strFileNames.size();

	if(iSize<1)
	{
		return 0;
	}


	memset(TextureImage,0,sizeof(void *)*10); //

	for(int Loop=0; Loop<iSize; Loop++)
	{

		if(LoadBMPS((char*)strFileNames[Loop].c_str(),TextureImage[0])==FALSE)
		{

			string strTmp = "Problem in loading file from ";
			strTmp+=strFileNames[Loop].c_str();
			MessageBox(NULL,strTmp.c_str(),"Error",MB_OK);
		}

		
		glGenTextures(iSize, &m_Texture[Loop]); // Create The Texture

		// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, m_Texture[Loop]);
		
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering

		
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[Loop]->sizeX, TextureImage[Loop]->sizeY, 0, GL_RGB,
		GL_UNSIGNED_BYTE, TextureImage[Loop]->data);
	
		

		/*
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[Loop]->sizeX, TextureImage[Loop]->sizeY, GL_RGB,
							GL_UNSIGNED_BYTE, TextureImage[Loop]->data); 
*/


	}
    


	for(int Loop2=0; Loop2<iSize; Loop2++)
	{
		if (TextureImage[Loop2]) // If Texture Exists
		{
			if (TextureImage[Loop2]->data) // If Texture Image Exists
			{
				free(TextureImage[Loop2]->data); // Free The Texture Image Memory
			}
			
			free(TextureImage[Loop2]); // Free The Image Structure
		}

	}


return 1;
}

BOOL Gl_Engine::LoadBMPS(char *szFileName, AUX_RGBImageRec *&r_RGBrec)
{

	FILE *pfile=NULL;

	if(!szFileName)
	{
		return FALSE;
	}

	pfile= fopen(szFileName,"r");

	if(!pfile)
	{
		return FALSE;
	}
	else
	{
		 fclose(pfile); //it was just used to check it if file exist or not
		 AUX_RGBImageRec *Tmp_RGBrec= auxDIBImageLoad(szFileName);
		 r_RGBrec= Tmp_RGBrec;
	}

	return TRUE;
}

int Gl_Engine::AddLight()
{

	GLfloat fLightAmb[]={0.5f,0.5f,0.5f,1.0f};
	GLfloat fLightDiffuse[]={1.0f,1.0f,1.0f,1.0f};
	GLfloat fLightPosition[]={0.0f,0.0f,2.0f,0.0f};

	glLightfv(GL_LIGHT1,GL_AMBIENT,fLightAmb);
	glLightfv(GL_LIGHT1,GL_DIFFUSE,fLightDiffuse);
	glLightfv(GL_LIGHT1,GL_POSITION,fLightPosition);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT1);
	return 1;
}


void Gl_Engine::CameraMoveLeft()
{
	m_Camera.fYRot += 1.5f;
}

void Gl_Engine::CameraMoveRight()
{
	m_Camera.fYRot -= 1.5f;
}

void Gl_Engine::CameraMoveForward()
{
	float fRad = PIOVER;
	m_Camera.fX -= (float)sin(m_Camera.fYRot * fRad) * 1.05f;
	m_Camera.fZ -= (float)cos(m_Camera.fYRot * fRad) * 1.05f;
}

void Gl_Engine::CameraMoveBackward()
{
	float fRad = PIOVER;
	m_Camera.fX += (float)sin(m_Camera.fYRot * fRad) * 1.05f;
	m_Camera.fZ += (float)cos(m_Camera.fYRot * fRad) * 1.05f;
}

void Gl_Engine::RotateWorldObjects()
{
	GLfloat sceneroty = 360.0f - m_Camera.fYRot;
	glRotatef(sceneroty,0,1.0f,0);
}

void Gl_Engine::SetWorldObjectDirection()
{
	GLfloat worldx,worldy,worldz;
	worldx= -m_Camera.fX;
	worldz = -m_Camera.fZ;
	worldy = -0.25f;

	glTranslatef(worldx,worldy,worldz);
}

void Gl_Engine::Build3dHouse()
{

	//it is necessary to off the debth buffer for blending effects
	 glColor4f(1.0f,1.0f,1.0f,0.5f);     //Blend the object
	 glTranslatef(0.5f,0.0f,0.0f); // Move Right 3 Units
	//glRotatef(m_RotDeg2--,0.0f,1.0f,0.0f);
	
	glBindTexture(GL_TEXTURE_2D, m_Texture[0]); // Select Our Texture

	glBegin(GL_QUADS); // Draw A Quad
	//Front.
	glNormal3f(0.0f,0.0f,1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
	// Back Face
	glNormal3f(0.0f,0.0f,-1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture andQuad
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
	// Top Face
	glNormal3f(0.0f,1.0f,0.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
	// Bottom Face
	glNormal3f(0.0f,1.0f,0.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
	// Right face
	glNormal3f(1.0f,0.0f,0.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
	// Left Face
	glNormal3f(-1.0f,0.0f,0.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
	glEnd();
//	glDisable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )

}



Share this post


Link to post
Share on other sites
Advertisement
sounds like your app is fillrate limited. try looking up the latest drivers for your video card (they may add extra hardware acceleration support). other than that, build mipmaps (you have it commented out in your code).

a few other things to help if you have to live in software mode: disable blending, enable back face culling and use a display list to draw your house.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!