Hi, I’m new around here, but I could see that many people around here knows a lot about threading in OpenGL. I have this slight problem using threads in a GLUT based program.
You see I use GLVU provided by UNC as a platform for my GLUT program, since I usually use GLUT, I don’t think that device context or pixel format descriptors is a necessity or should ever be shared between windows and all.
I only used threading to calculate some other process in the background. Now here is my little problem.
In a simple program that I have just done, I generated 5000 sphere randomly in 3D Space.I tried using 3 threads, 1 thread for generating the random position of the sphere in 3D Space which run once in the beginning of the program, 1 thread is used to calculate the View Frustum planes for the use of Frustum Culling, and 1 last thread for generating a list of sphere visible in View Frustum.
Here’s the problem, why is it that if I turned on my thread for Frustum Calculations and List Generation the FPS drops instead of climbing. I thought that threading was supposed to make an application runs faster, but obviously been in this situation, I was wrong.
I tried many ways like to bypass the thread if it already runs etc. But it doesn’t change anything. Any ways I can counter this? I would really appreciate any suggestions. By the way, here is the display part of my code.
Please HELP!!!
void MyDisplay(void){
glutSetWindow(MainWindow.GetWindowID());
int i;
MainWindow.BeginFrame();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if(UseViewFrustum->get_int_val()){
if(ThreadedViewFrustum->get_int_val()){
// if(ReCalculateFrustum){
MyThreads[1] = CreateThread(NULL,128,UpdateFrustum,NULL,NULL,NULL);
// ReCalculateFrustum = false;
// }
}
else{
// ReCalculateFrustum = true;
float ProjectionMatrix[16];
float ModelViewMatrix[16];
MainWindow.GetProjectionMatrix(ProjectionMatrix);
MainWindow.GetModelviewMatrix(ModelViewMatrix);
MyFrustum.CalculateFrustum(ProjectionMatrix,ModelViewMatrix);
}
if(UseVisibility->get_int_val()){
// if(UpdateVisibility){
MyThreads[2] = CreateThread(NULL,0,UpdateVisibleSphere,NULL,NULL,NULL);
// UpdateVisibility = false;
// }
for(i=0;i<VisibleCount;i++){
glPushMatrix();
glTranslatef(VisibleSphere[i].x,VisibleSphere[i].y,VisibleSphere[i].z);
glutSolidSphere(0.3,30,30);
glPopMatrix();
}
}
else{
for(i=0;i<SPHERECOUNT;i++){
if(MyFrustum.PointInFrustum(SpherePosition[i])){
glPushMatrix();
glTranslatef(SpherePosition[i].x,SpherePosition[i].y,SpherePosition[i].z);
glutSolidSphere(0.3,30,30);
glPopMatrix();
}
}
}
}
else{
for(i=0;i<SPHERECOUNT;i++){
glPushMatrix();
glTranslatef(SpherePosition[i].x,SpherePosition[i].y,SpherePosition[i].z);
glutSolidSphere(0.3,30,30);
glPopMatrix();
}
}
MainWindow.DrawFPS();
MainWindow.EndFrame();
}