   Help with speed of my terrian
  Any suggestion of how to improve the speed of my program would be appreciated Its a renders a 256x256 terrain. It uses cull face and stepSize for the terrain. Any how, the rendering speed is about few seconds. Here is the code : Also it would be nice if you can comment on the code--on how to improve it, since I know its not very good. My AllFiles.h :#ifndef _ALL_FILES_H_#define _ALL_FILES_H_#include<GL/glut.h>#include<cmath>#include<windows.h>#include<fstream>#include<vector>#include<iostream>using std::cout;using std::cin;using std::endl;using std::vector;using std::ifstream;/******Variable and constants declarations*******/#define PI  3.14159265#define DEGTORAD 0.0174532925 // Same as PI/180typedef unsigned int uInt;typedef unsigned char uChar;class HeightMap{private :vector<vector<vector<float> > > vecMap; //Holds our Terrian Datavector<uChar> vecHeight; //Records the height//Holds our texture Coordinatesfloat texI;float texJ;float texIplus1;float texJplus1;//The sqrt of the size of our imageint sqrtImgSize;//Determines LOD of our terrianuInt StepSize;public:HeightMap();bool LoadHeightMap(char* Image); //used to load in the height values from filebool LoadHeightMapFstream(char* Image); //just practicevoid RenderMap(); //Draws our Terrian};extern HeightMap hMap;#endifMy terrian.cpp : #include"AllFiles.h"HeightMap hMap;HeightMap::HeightMap(){//Reset the texture Coordinats texI = texJ =  texIplus1 = texJplus1 = 0;//Used to determine level of detail of the TerrainStepSize = 1;//Set the image size to 0, initiallysqrtImgSize = 0.0f;uInt i(0),j(0),k(0);//Create a Flat grid for our terrianfor(i = 0 ; i < vecMap.size(); i++) {for( j = 0; j< vecMap.size(); j++){vecMap[j][0] = i;vecMap[j][1] = sin(i*DEGTORAD); //The heightvecMap[j][2] = j;}}}bool HeightMap::LoadHeightMap(char *Image){FILE *pFile = NULL;long int FileSize = 0; //Hold the file sizepFile = fopen(Image,"rb"); //Read our image in binary modeif(!pFile){MessageBox(NULL,L"ERROR : File Was Not Read!",L"READING FILE...",MB_OK);return false;}fseek(pFile,0,SEEK_END); //set the cursor to the end of fileFileSize = ftell(pFile); //get the file sizerewind(pFile);//Set the cursor back to beginning vecHeight.resize(FileSize);  fread(&vecHeight[0],FileSize,1,pFile); //Read our value into the vector   if(vecHeight.size() != FileSize) //Check for proper allocation{MessageBox(NULL,L"ERROR : Unable to Distribute Data " ,L"EXTRACTING DATA",MB_OK);fclose(pFile);return false;}fclose(pFile); //Get the sqrt of img file so we can use it to convert our vector//size into a power of 2, which will match our image dimensionsqrtImgSize = sqrt((float)FileSize);//Check to see if the imgSize is power of 2******* could definetely be optimized******while(sqrtImgSize != 1) {if(sqrtImgSize % 2 == 0)sqrtImgSize /= 2;else{MessageBox(NULL,L"ERROR : Dimensions Not Supported",L"IMAGE DIMENSIONS",MB_OK);return false;}}//Reset our sqrtImgSize sqrtImgSize = sqrt((float)FileSize);//Match our Height vector with our grid vector in size and  set initial value to 0vecMap.resize(sqrtImgSize,vector< vector<float> > (sqrtImgSize,vector<float> (3,0) ) );//Insert the height into our grid//vecMap[j], where 0,2 is our//x and z coord and 1 is our height//value.for(uInt i =0; i< vecMap.size();i+=StepSize)for(uInt j =0; j<vecMap.size();j+=StepSize){vecMap[j][0] = i;vecMap[j][1] = (float)(vecHeight[j+i*vecMap.size()])/10.0;vecMap[j][2] = j;}return true;}//Not sure if this is correct. Haven't tried it outbool HeightMap::LoadHeightMapFstream(char *Image){char data;uInt FileSize;ifstream iFile(Image,std::ios::in);if(!Image){std::cerr << "Error : Failed to read the file\n";return false;}//Get the File SizeiFile.seekg(std::ios::end);FileSize = iFile.tellg();iFile.seekg(std::ios::beg);cout<<"FilseSize  "  <<FileSize<<endl;while(!iFile.eof()) //read Data{iFile >> data;vecHeight.push_back(data);cout<<data<<",";}printf("\n\n\n");for(uInt i = 0; i< vecHeight.size();i++)cout << vecHeight<<",";iFile.close();return true;}void HeightMap::RenderMap(){uInt i(0),j(0),k(0);float texI(0), texJ(0), texIplus1(0),texJplus1(0);glBegin(GL_QUADS);for(i = 0; i< vecMap.size()-2; i++){texI = i/(float)vecMap.size();texJ = j/(float)vecMap.size();texIplus1 = (i+1)/vecMap.size();texJplus1 = (j+1)/vecMap.size();for( j = 0; j<vecMap.size()-2; j++){ glTexCoord2f(texI,texJ);glVertex3f(vecMap[j][0],vecMap[j][1],vecMap[j][2]);glTexCoord2f(texIplus1,texJ);glVertex3f(vecMap[i+1][j][0],vecMap[i+1][j][1],vecMap[i+1][j][2]);glTexCoord2f(texIplus1,texJplus1);glVertex3f(vecMap[i+1][j+1][0],vecMap[i+1][j+1][1],vecMap[i+1][j+1][2]);   glTexCoord2f(texI,texJplus1);glVertex3f(vecMap[j+1][0],vecMap[j+1][1],vecMap[j+1][2]);}}glEnd();}And lastly here is my main.cpp :#include"Allfiles.h"GLuint window; //Hold the created window as a variable.float glx(41),gly(18),glz(43.5);GLvoid Reshape(int width, int height){glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0,(float)width/height,0.5f,200.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}GLvoid InitGL(){glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LEQUAL);glClearDepth(1.0f);glClearColor(0.0f,0.0f,1.0f,0.0f);glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);glEnable(GL_CULL_FACE);glCullFace(GL_FRONT);HeightMap();hMap.LoadHeightMap("h1.pbm");}GLvoid disp(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glTranslatef(0,0,-1.0);gluLookAt(glx,gly,glz,0,0,0,0,1,0);hMap.RenderMap(); glutPostRedisplay();glutSwapBuffers();}GLvoid specialKey( int key, int x, int y){switch(key){case GLUT_KEY_RIGHT : glx++;break;case GLUT_KEY_LEFT  : glx--;break;case GLUT_KEY_UP    :  gly++;break;case GLUT_KEY_DOWN  :  gly--;break;}cout<<glx<<","<<gly<<","<<glz<<endl;}GLvoid specialKeyRelease( int key, int x, int y){switch(key){case GLUT_KEY_RIGHT : case GLUT_KEY_LEFT  : case GLUT_KEY_UP    :  case GLUT_KEY_DOWN  :  break;}}GLvoid normKey(unsigned char key, int x, int y){switch(key){case 27 :case ' ' : glutDestroyWindow(window); exit(0); break; case 'w': glz++;break;case's':  glz--;break;}}GLvoid normKeyRelease(unsigned char key, int x, int y){switch(key){case 27 : glutDestroyWindow(window); exit(0); break; case ' ' : ; }}GLvoid mouseFunc(int button, int state, int x, int y ){switch(button){case GLUT_LEFT_BUTTON : if(state == GLUT_DOWN) printf("left key down\n"); elseif(state == GLUT_UP)  printf("left key up\n");break;case GLUT_RIGHT_BUTTON:if(state == GLUT_DOWN) printf("right key down\n"); elseif(state == GLUT_UP)  printf("right key up\n"); break;}}  int main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);glutInitWindowSize(800,450);window = glutCreateWindow("Terrian 3");InitGL();glutDisplayFunc(disp);glutIdleFunc(disp);glutReshapeFunc(Reshape);glutSpecialFunc(specialKey);glutSpecialUpFunc(specialKeyRelease);glutKeyboardFunc(normKey);glutKeyboardUpFunc(normKeyRelease);glutMouseFunc(mouseFunc); glutMainLoop();return EXIT_SUCCESS;}Here is a pic of it :http://img40.imageshack.us/img40/1369/terrian.jpgAlso I could use tri-strips but not sure how exactly to use it. When i try using it, it works but there is a T-triangle, in between each triangle , if you know what I mean.[Edited by - tnutty on May 27, 2009 3:27:58 PM]
  I added some new features like outlined,textured,or colored terrian.By using display list. Its much faster. But I believe it still could be faster.Tell me if you need to see the update on my code.   ;  Sorry for the quick reply, but VBOs will probably help with performance a lot.Search google for: OpenGL VBO   ;  Will try that tomorrow. Thanks. Just how much (approx) is "a lot",say in termsof FPS?   ;  depends on how big your buffer is, and how much ram you have on your video card.   ;  I don't have much ram, only 1mb.   ;  Not sure if I am using the step size correctly. It should determine the detailof the terrain but instead what its doing is that deleting the edges of the terrain. Any help on how to implement it correctly?Here is the relevant code : HeightMap::HeightMap(){//Reset the texture Coordinats texI = texJ =  texIplus1 = texJplus1 = 0;//Used to determine level of detail of the TerrainStepSize = 1;//Set the image size to 0, initiallysqrtImgSize = 0.0f;//Terrian Display list IDdlTerrian = 0; //Create a Flat grid for our terrianfor(uInt i = 0 ; i < vecMap.size(); i++) {for(uInt j = 0; j< vecMap.size(); j++){vecMap[j][0] = i;vecMap[j][1] = sin(i*DEGTORAD); //The heightvecMap[j][2] = j;}}}glNewList(hMap.dlTerrian,GL_COMPILE);glBegin(GL_QUADS);for(i = 0; i< vecMap.size()-2-StepSize; i++)for( j = 0; j<vecMap.size()-2-StepSize; j++){glVertex3f(vecMap[i+StepSize][j+StepSize][0],vecMap[i+StepSize][j+StepSize][1],vecMap[i+StepSize][j+StepSize][2]);glVertex3f(vecMap[i+1+StepSize][j+StepSize][0],vecMap[i+1+StepSize][j+StepSize][1],vecMap[i+1+StepSize][j+StepSize][2]);glVertex3f(vecMap[i+1+StepSize][j+1+StepSize][0],vecMap[i+1+StepSize][j+1+StepSize][1],vecMap[i+1+StepSize][j+1+StepSize][2]);glVertex3f(vecMap[i+StepSize][j+1+StepSize][0],vecMap[i+StepSize][j+1+StepSize][1],vecMap[i+StepSize][j+1+StepSize][2]);}glEnd();glEndList();;Quote:Original post by tnuttyI don't have much ram, only 1mb.[oh] You mean 1Gb, right? Or do you mean your program only uses 1Mb. Or are you programming on an embedded device (or just a really old computer)?Anyway, how fast is it now (i.e. frames per second)?   ;  I mean giga bytes. Without display list, its about ~8fps, with display list, its about ~27fps.   ;Quote:Original post by tnuttyJust how much (approx) is "a lot",say in terms of FPS?It isn't really recommended to use FPS as a measure of performance - average time per frame, and the variance thereof gives a much better idea of performance.That said, VBO will help a lot by any measure. Immediate mode (i.e glBegin/glEnd) has been deprecated for some years, due to its tendency to cause stalls in the GPU pipeline.   