Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualscap3y

Posted 30 October 2012 - 08:17 AM

First up, what is your current code doing? Have you profiled to determine where the bottleneck is? Is it possible to post the loading code in a stand alone manner?

Sure, this the code I am using..
[source lang="cpp"]#include <windows.h>#include <gl/gl.h>#include <gl/glu.h>#include <gl/glut.h>#include <math.h>GLfloat lightPos[] = {-300.0f, 100.0f, 100.0f, 0.0f };// Define a constant for the value of PI#define GL_PI 3.1415f// Rotation amountsstatic GLfloat xRot = 0.0f;static GLfloat yRot = 0.0f;float vertex[3][3];# include &lt; stdio.h&gt;# include &lt; stdlib.h&gt;# include &lt; string.h&gt;struct group // Define a group which will take in the normal and vertices from the .stl files{ float normal[3]; float vertex1[3]; float vertex2[3]; float vertex3[3]; char unuse [2];};struct group facet[500000] ;int i,j,num;char filename[80];int numfacet[1];// Reduces a normal vector specified as a set of three coordinates,// to a unit normal vector of length one.void ReduceToUnit(float vector[3]){float length;// Calculate the length of the vectorlength = (float)sqrt((vector[0]*vector[0]) +(vector[1]*vector[1]) +(vector[2]*vector[2]));// Keep the program from blowing up by providing an exceptable// value for vectors that may calculated too close to zero.if(length == 0.0f)length = 1.0f;// Dividing each element by the length will result in a// unit normal vector.vector[0] /= length;vector[1] /= length;vector[2] /= length;}// Points p1, p2, & p3 specified in counter clock-wise ordervoid calcNormal(struct group *facet, float out[3]){ float v1[3],v2[3]; static const int x = 0; static const int y = 1; static const int z = 2; // Calculate two vectors from the three points v1[x] = (*facet).vertex1[x] -(*facet).vertex2[x]; v1[y] = (*facet).vertex1[y] - (*facet).vertex2[y]; v1[z] = (*facet).vertex1[z] - (*facet).vertex2[z]; v2[x] = (*facet).vertex2[x] - (*facet).vertex3[x]; v2[y] = (*facet).vertex2[y] - (*facet).vertex3[y]; v2[z] = (*facet).vertex2[z] - (*facet).vertex3[z]; // Take the cross product of the two vectors to get // the normal vector which will be stored in out out[x] = v1[y]*v2[z] - v1[z]*v2[y]; out[y] = v1[z]*v2[x] - v1[x]*v2[z]; out[z] = v1[x]*v2[y] - v1[y]*v2[x]; // Normalize the vector (shorten length to one) ReduceToUnit(out);}// Called to draw scenevoid RenderScene(void){ float normal[3]; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //glTranslatef(0.0f,0.0f,-500.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(-2.5,0.0,0.0); glPushMatrix(); glRotatef(xRot,1,0,0); glRotatef(yRot,0,1,0); glColor3ub(128, 128, 128); glBegin(GL_TRIANGLES); { for(j=0;j&lt;num;j++) { facet[j].vertex1; facet[j].vertex2; facet[j].vertex3; //float v[3][3]={ facet[j].vertexs[3][3]}; calcNormal(&facet[j],normal); glNormal3fv(normal); glVertex3fv(facet[j].vertex1); glVertex3fv(facet[j].vertex2); glVertex3fv(facet[j].vertex3); } } glEnd(); glPopMatrix(); glutSwapBuffers();}// This function does any needed initialization on the rendering// context.void SetupRC(){ GLfloat ambient[]={ 0.6f,0.6f,0.7f,1.0f}; GLfloat diffuse[]={0.7f,0.7f,0.7f,1.0f}; GLfloat spec[]={1.0f,1.0f,1.0f,1.0f}; GLfloat specma[]={1.0f,1.0f,1.0f,1.0f}; // Light values and coordinates glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); // Hidden surface removal glFrontFace(GL_CCW); // Counter clock-wise polygons face out glEnable(GL_CULL_FACE); // Do not calculate inside of jet // Enable lighting glEnable(GL_LIGHTING); // Setup and enable light 0 glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient); glLightfv(GL_LIGHT0,GL_AMBIENT,ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse); glLightfv(GL_LIGHT0,GL_SPECULAR,spec); // Enable color tracking glEnable(GL_LIGHT0); // Set Material properties to follow glColor values glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); glMaterialfv(GL_FRONT,GL_SPECULAR,specma); glMateriali(GL_FRONT,GL_SHININESS,128); // Light blue background glClearColor(0.0f, 0.0f, 0.0f, 1.0f );}/*void SpecialKeys(int key, int x, int y){ if(key == GLUT_KEY_UP) xRot-= 5.0f; if(key == GLUT_KEY_DOWN) xRot += 5.0f; if(key == GLUT_KEY_LEFT) yRot -= 5.0f; if(key == GLUT_KEY_RIGHT) yRot += 5.0f; //if(key &gt; 356.0f) // xRot = 0.0f; //if(key &lt; -1.0f) // xRot = 355.0f; // if(key &gt; 356.0f) // yRot = 0.0f; // if(key &lt; -1.0f) // yRot = 355.0f; // Refresh the Window glutPostRedisplay();}*/void ChangeSize(int w, int h){ // GLfloat nRange = 3.0f; /* GLfloat aspect; aspect = (GLfloat)w/(GLfloat)h; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(1000.0f,aspect,1.0,1000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); */ // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w &lt;= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, 2.0*(-nRange), 2.0*(nRange)); else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, 2.0*(-nRange), 2.0*(nRange)); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLightfv(GL_LIGHT0,GL_POSITION,lightPos);}int main(int argc, char* argv[]){ //stl loader FILE *binary; //FILE *out; if( (binary =fopen("Desktop\\QCAN_1-3-12-2-1107-5-4-7-8501-30000011050313402896800000046_20110519135647.stl","rb"))==NULL )////stl file location { printf("\nThe file could not be accessed. Are you sure of the location..?\n"); exit(1); } //out = fopen("C:\\my documents\\stlexperi3.txt","w"); fread(filename,sizeof(char),80,binary) ; fread(numfacet,sizeof(int),1,binary); num=numfacet[0]; for (j=0;j&lt;num;j++) { fread(facet[j].normal,sizeof(float),3,binary); fread(facet[j].vertex1,sizeof(float),3,binary); fread(facet[j].vertex2,sizeof(float),3,binary); fread(facet[j].vertex3,sizeof(float),3,binary); fread(facet[j].unuse,sizeof(char),2,binary); } glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500,500); // glutCreateWindow("Lighted Jet"); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutDisplayFunc(RenderScene); SetupRC(); glutMainLoop(); return 0;}[/source]

#1scap3y

Posted 30 October 2012 - 05:46 AM

First up, what is your current code doing? Have you profiled to determine where the bottleneck is? Is it possible to post the loading code in a stand alone manner?


Sure, this the code I am using..


[source lang="cpp"]#include <windows.h>#include <gl/gl.h>#include <gl/glu.h>#include <gl/glut.h>#include <math.h>GLfloat lightPos[] = {-300.0f, 100.0f, 100.0f, 0.0f };// Define a constant for the value of PI#define GL_PI 3.1415f// Rotation amountsstatic GLfloat xRot = 0.0f;static GLfloat yRot = 0.0f;float vertex[3][3];# include &lt; stdio.h&gt;# include &lt; stdlib.h&gt;# include &lt; string.h&gt;struct group // Define a group which will take in the normal and vertices from the .stl files{ float normal[3]; float vertex1[3]; float vertex2[3]; float vertex3[3]; char unuse [2];};struct group facet[500000] ;int i,j,num;char filename[80];int numfacet[1];// Reduces a normal vector specified as a set of three coordinates,// to a unit normal vector of length one.void ReduceToUnit(float vector[3]){float length;// Calculate the length of the vector length = (float)sqrt((vector[0]*vector[0]) + (vector[1]*vector[1]) +(vector[2]*vector[2]));// Keep the program from blowing up by providing an exceptable// value for vectors that may calculated too close to zero.if(length == 0.0f)length = 1.0f;// Dividing each element by the length will result in a// unit normal vector.vector[0] /= length;vector[1] /= length;vector[2] /= length;}// Points p1, p2, & p3 specified in counter clock-wise ordervoid calcNormal(struct group *facet, float out[3]){ float v1[3],v2[3]; static const int x = 0; static const int y = 1; static const int z = 2; // Calculate two vectors from the three points v1[x] = (*facet).vertex1[x] -(*facet).vertex2[x]; v1[y] = (*facet).vertex1[y] - (*facet).vertex2[y]; v1[z] = (*facet).vertex1[z] - (*facet).vertex2[z]; v2[x] = (*facet).vertex2[x] - (*facet).vertex3[x]; v2[y] = (*facet).vertex2[y] - (*facet).vertex3[y]; v2[z] = (*facet).vertex2[z] - (*facet).vertex3[z]; // Take the cross product of the two vectors to get // the normal vector which will be stored in out out[x] = v1[y]*v2[z] - v1[z]*v2[y]; out[y] = v1[z]*v2[x] - v1[x]*v2[z]; out[z] = v1[x]*v2[y] - v1[y]*v2[x]; // Normalize the vector (shorten length to one) ReduceToUnit(out);}// Called to draw scenevoid RenderScene(void){ float normal[3]; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //glTranslatef(0.0f,0.0f,-500.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(-2.5,0.0,0.0); glPushMatrix(); glRotatef(xRot,1,0,0); glRotatef(yRot,0,1,0); glColor3ub(128, 128, 128); glBegin(GL_TRIANGLES); { for(j=0;j&lt;num;j++) { facet[j].vertex1; facet[j].vertex2; facet[j].vertex3; //float v[3][3]={ facet[j].vertexs[3][3]}; calcNormal(&facet[j],normal); glNormal3fv(normal); glVertex3fv(facet[j].vertex1); glVertex3fv(facet[j].vertex2); glVertex3fv(facet[j].vertex3); } } glEnd(); glPopMatrix(); glutSwapBuffers();}// This function does any needed initialization on the rendering// context. void SetupRC(){ GLfloat ambient[]={ 0.6f,0.6f,0.7f,1.0f}; GLfloat diffuse[]={0.7f,0.7f,0.7f,1.0f}; GLfloat spec[]={1.0f,1.0f,1.0f,1.0f}; GLfloat specma[]={1.0f,1.0f,1.0f,1.0f}; // Light values and coordinates glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); // Hidden surface removal glFrontFace(GL_CCW); // Counter clock-wise polygons face out glEnable(GL_CULL_FACE); // Do not calculate inside of jet // Enable lighting glEnable(GL_LIGHTING); // Setup and enable light 0 glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient); glLightfv(GL_LIGHT0,GL_AMBIENT,ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse); glLightfv(GL_LIGHT0,GL_SPECULAR,spec); // Enable color tracking glEnable(GL_LIGHT0); // Set Material properties to follow glColor values glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); glMaterialfv(GL_FRONT,GL_SPECULAR,specma); glMateriali(GL_FRONT,GL_SHININESS,128); // Light blue background glClearColor(0.0f, 0.0f, 0.0f, 1.0f );}/*void SpecialKeys(int key, int x, int y){ if(key == GLUT_KEY_UP) xRot-= 5.0f; if(key == GLUT_KEY_DOWN) xRot += 5.0f; if(key == GLUT_KEY_LEFT) yRot -= 5.0f; if(key == GLUT_KEY_RIGHT) yRot += 5.0f; //if(key &gt; 356.0f) // xRot = 0.0f; //if(key &lt; -1.0f) // xRot = 355.0f; // if(key &gt; 356.0f) // yRot = 0.0f; // if(key &lt; -1.0f) // yRot = 355.0f; // Refresh the Window glutPostRedisplay();}*/void ChangeSize(int w, int h){ // GLfloat nRange = 3.0f; /* GLfloat aspect; aspect = (GLfloat)w/(GLfloat)h; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(1000.0f,aspect,1.0,1000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); */ // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w &lt;= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, 2.0*(-nRange), 2.0*(nRange)); else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, 2.0*(-nRange), 2.0*(nRange)); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLightfv(GL_LIGHT0,GL_POSITION,lightPos);}int main(int argc, char* argv[]){ //stl loader FILE *binary; //FILE *out; if( (binary =fopen("Desktop\\QCAN_1-3-12-2-1107-5-4-7-8501-30000011050313402896800000046_20110519135647.stl","rb"))==NULL )////stl file location { printf("\nThe file could not be accessed. Are you sure of the location..?\n"); exit(1); } //out = fopen("C:\\my documents\\stlexperi3.txt","w"); fread(filename,sizeof(char),80,binary) ; fread(numfacet,sizeof(int),1,binary); num=numfacet[0]; for (j=0;j&lt;num;j++) { fread(facet[j].normal,sizeof(float),3,binary); fread(facet[j].vertex1,sizeof(float),3,binary); fread(facet[j].vertex2,sizeof(float),3,binary); fread(facet[j].vertex3,sizeof(float),3,binary); fread(facet[j].unuse,sizeof(char),2,binary); } glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500,500); // glutCreateWindow("Lighted Jet"); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutDisplayFunc(RenderScene); SetupRC(); glutMainLoop(); return 0;}[/source]

PARTNERS