Optimizing my OpenGL

  • Follow


Hi all,

I'm new to OpenGL and I'm hoping you can offer me some suggestions as
to how I might optimise my code.

My application involves displaying a torus whose radius corresponds to
the frequency of a note. The brightness of the torus is also
proportional to the amplitude of the note in question. A note has 5
properties. It has a onset time, i.e its start time. The rise time,
i.e. the time when it reaches an amplitude of 1. The sustain time, the
time when it will begin to fade out. And the release time, the time
when it stops playing (amplitude 0 in other words). And finally a
frequency, which relates to the radius of the torus.

The problem I'm having is that with a frame rate of 40 FPS, I am
already experiencing loading problems. I also notice some flickering in
my animation. Can anyone suggest to me where the main processor loading
problems reside? I wouldn't have expected to have problems displaying a
few torus object on the screen. Am I using double buffering correctly?

What improvements can you suggest in my code with regard to OpenGL?

I've tried removing the Blending and the Lighting, but the animation
looks primative when I do so.

Thanks for your help,

Barry.

#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#define MAX_DISPLAY_NOTES   	10
#define FRAMES_PER_SEC     	40.0f
#define NUM_OF_NOTES     	10

float gColor[5];
int gOn[5];
float gTime     = 0;        //global time
int numberOfTorus  =0; //number of torus being displayed at a point in
time

int winWidth = 768;
int winHeight = 768;

typedef struct
{
  double onset;
  double rise;
  double sustain;
  double release;

  double frequency;
}note_t;

note_t notes[NUM_OF_NOTES]=
{
  {2.4,  2.41,  3.0,  3.02,  5},
  {2.6,  2.61,  3.2,  3.22,  15},
  {2.8,  2.9,  3.4,  3.5,  25},
  {3.0,  3.1,  3.6,  3.7,  35},
  {3.2,  3.3,  3.8,  3.9,  45},
  {4.4,  4.5,  5.0,  5.1,  10},
  {4.6,  4.7,  5.2,  5.3,  15},
  {4.8,  4.9,  5.4,  5.5,  15},
  {5.0,  5.1,  5.6,  5.7,  15},
  {6.2,  6.21,  8.8,  8.83,  45},

};

note_t *noteStack[MAX_DISPLAY_NOTES];
long displayCount=0;
long lastNote=0;

void myTimer(int value)
{
  long i;

  note_t *tmpNoteStack[MAX_DISPLAY_NOTES];
  long tmpDisplayCount=displayCount;
  displayCount=0;

  for(i=0;i<tmpDisplayCount;i++)
  {
    if(gTime<noteStack[i]->release)
      tmpNoteStack[displayCount++]=noteStack[i];
  }

  for(i=lastNote;i<NUM_OF_NOTES;i++)
  {
    if(MAX_DISPLAY_NOTES==displayCount)
	break;

    if(gTime>=notes[i].onset&&(gTime+1/FRAMES_PER_SEC)>notes[i].onset)
    {
        tmpNoteStack[displayCount++] = &notes[i];
        lastNote++;
    }
    else if(gTime<notes[i].onset)
      break;

  }

  memcpy(noteStack,tmpNoteStack,sizeof(note_t*)*MAX_DISPLAY_NOTES);

  gTime += 1/FRAMES_PER_SEC;

  glutPostRedisplay();
  glutTimerFunc(1000/FRAMES_PER_SEC, myTimer, 1);
}

/* used to get current width and height of viewport */
void reshape(int wid, int ht)
{
  glViewport(0, 0, wid, ht);
  winWidth = wid;
  winHeight = ht;
}

GLfloat viewangle;

void redraw()
{
  float color;
  int i;
  /* clear stencil each time */


glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);

  for(i =0;i<displayCount;i++)
  {
      if(gTime<noteStack[i]->rise)
        color=
(gTime-noteStack[i]->onset)*(1/(noteStack[i]->rise-noteStack[i]->onset));
      else if(gTime<noteStack[i]->sustain)
        color= 1.0f;
      else if(gTime<=noteStack[i]->release)
        color=
(noteStack[i]->release-gTime)*(1/(noteStack[i]->release-noteStack[i]->sustain));
      else
        color=0;

      glColor3f(.0f, color, color);
      glutSolidTorus(2., noteStack[i]->frequency, 64, 128);
  }

  glutSwapBuffers();
}

int
main(int argc, char **argv)
{

  static GLfloat matAmbient[]= { 0.1f, 0.1f, 1.0f, 1.0f };
  static GLfloat matSpecular[] = {0.0f, 0.0f, 1.0f , 1.0f};
  static GLfloat matDiffuse[] = { 0.0f, 0.0f, 1.0f, 1.0f };
  static GLfloat matShininess[] = { 128.0f };

  static GLfloat lightPos[] = {50.f, 30.f, 50.f, 1.0f};

  static GLfloat lightAmbient[]= { 0.25f, 0.25f, 0.25f, 1.0f };

  static GLfloat lightSpecular[] = {1.0f, 1.0f, 1.0f , 1.0f};
  static GLfloat lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  static GLfloat localAmbient[] = { 0.0f, 0.0f, 0.0, 1.0f };

  glutInit(&argc, argv);
  glutInitWindowSize(712, 712);
  glutInitDisplayMode(GLUT_STENCIL|GLUT_DEPTH|GLUT_DOUBLE);
  (void)glutCreateWindow("Window");
  glutDisplayFunc(redraw);

  glClearColor(.0f, .0f, .0f, .0f);
  glEnable(GL_DEPTH_TEST);

  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);

  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, localAmbient);

  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);

  glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

  glEnable ( GL_COLOR_MATERIAL ) ;

  glLightfv(GL_FRONT, GL_DIFFUSE, matDiffuse);

  glLightfv(GL_FRONT, GL_AMBIENT, matAmbient);
  glLightfv(GL_FRONT, GL_SPECULAR, matSpecular);
  glLightfv(GL_FRONT, GL_SHININESS, matShininess);

  glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

//////////////////////////////////////////////////////////////////////////

  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glHint(GL_POINT_SMOOTH, GL_NICEST);
  glHint(GL_LINE_SMOOTH, GL_NICEST);
  glHint(GL_POLYGON_SMOOTH, GL_NICEST);

  glEnable(GL_POINT_SMOOTH);
  glEnable(GL_LINE_SMOOTH);
  glEnable(GL_POLYGON_SMOOTH);

  glMatrixMode(GL_PROJECTION);
  glOrtho(-50., 50., -50., 50., -50., 50.);
  glMatrixMode(GL_MODELVIEW);

  glutTimerFunc(1000/FRAMES_PER_SEC,myTimer,0);
  glutMainLoop();

  return 0;             /* ANSI C requires main to return int. */
}

0
Reply bg_ie (117) 8/14/2006 7:35:53 PM

<bg_ie@yahoo.com> wrote in message 
news:1155584153.465181.82940@m73g2000cwd.googlegroups.com...
> Hi all,
>
> I'm new to OpenGL and I'm hoping you can offer me some suggestions as
> to how I might optimise my code.
>
> My application involves displaying a torus whose radius corresponds to
> the frequency of a note. The brightness of the torus is also
> proportional to the amplitude of the note in question. A note has 5
> properties. It has a onset time, i.e its start time. The rise time,
> i.e. the time when it reaches an amplitude of 1. The sustain time, the
> time when it will begin to fade out. And the release time, the time
> when it stops playing (amplitude 0 in other words). And finally a
> frequency, which relates to the radius of the torus.
>
> The problem I'm having is that with a frame rate of 40 FPS, I am
> already experiencing loading problems. I also notice some flickering in
> my animation. Can anyone suggest to me where the main processor loading
> problems reside? I wouldn't have expected to have problems displaying a
> few torus object on the screen. Am I using double buffering correctly?
>
> What improvements can you suggest in my code with regard to OpenGL?
>
> I've tried removing the Blending and the Lighting, but the animation
> looks primative when I do so.
>
> Thanks for your help,
>
> Barry.
>
> #include <GL/glut.h>
> #include <math.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define MAX_DISPLAY_NOTES   10
> #define FRAMES_PER_SEC     40.0f
> #define NUM_OF_NOTES     10
>
> float gColor[5];
> int gOn[5];
> float gTime     = 0;        //global time
> int numberOfTorus  =0; //number of torus being displayed at a point in
> time
>
> int winWidth = 768;
> int winHeight = 768;
>
> typedef struct
> {
>  double onset;
>  double rise;
>  double sustain;
>  double release;
>
>  double frequency;
> }note_t;
>
> note_t notes[NUM_OF_NOTES]=
> {
>  {2.4,  2.41,  3.0,  3.02,  5},
>  {2.6,  2.61,  3.2,  3.22,  15},
>  {2.8,  2.9,  3.4,  3.5,  25},
>  {3.0,  3.1,  3.6,  3.7,  35},
>  {3.2,  3.3,  3.8,  3.9,  45},
>  {4.4,  4.5,  5.0,  5.1,  10},
>  {4.6,  4.7,  5.2,  5.3,  15},
>  {4.8,  4.9,  5.4,  5.5,  15},
>  {5.0,  5.1,  5.6,  5.7,  15},
>  {6.2,  6.21,  8.8,  8.83,  45},
>
> };
>
> note_t *noteStack[MAX_DISPLAY_NOTES];
> long displayCount=0;
> long lastNote=0;
>
> void myTimer(int value)
> {
>  long i;
>
>  note_t *tmpNoteStack[MAX_DISPLAY_NOTES];
>  long tmpDisplayCount=displayCount;
>  displayCount=0;
>
>  for(i=0;i<tmpDisplayCount;i++)
>  {
>    if(gTime<noteStack[i]->release)
>      tmpNoteStack[displayCount++]=noteStack[i];
>  }
>
>  for(i=lastNote;i<NUM_OF_NOTES;i++)
>  {
>    if(MAX_DISPLAY_NOTES==displayCount)
> break;
>
>    if(gTime>=notes[i].onset&&(gTime+1/FRAMES_PER_SEC)>notes[i].onset)
>    {
>        tmpNoteStack[displayCount++] = &notes[i];
>        lastNote++;
>    }
>    else if(gTime<notes[i].onset)
>      break;
>
>  }
>
>  memcpy(noteStack,tmpNoteStack,sizeof(note_t*)*MAX_DISPLAY_NOTES);
>
>  gTime += 1/FRAMES_PER_SEC;
>
>  glutPostRedisplay();
>  glutTimerFunc(1000/FRAMES_PER_SEC, myTimer, 1);
> }
>
> /* used to get current width and height of viewport */
> void reshape(int wid, int ht)
> {
>  glViewport(0, 0, wid, ht);
>  winWidth = wid;
>  winHeight = ht;
> }
>
> GLfloat viewangle;
>
> void redraw()
> {
>  float color;
>  int i;
>  /* clear stencil each time */
>
>
> glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
>
>  for(i =0;i<displayCount;i++)
>  {
>      if(gTime<noteStack[i]->rise)
>        color=
> (gTime-noteStack[i]->onset)*(1/(noteStack[i]->rise-noteStack[i]->onset));
>      else if(gTime<noteStack[i]->sustain)
>        color= 1.0f;
>      else if(gTime<=noteStack[i]->release)
>        color=
> (noteStack[i]->release-gTime)*(1/(noteStack[i]->release-noteStack[i]->sustain));
>      else
>        color=0;
>
>      glColor3f(.0f, color, color);
>      glutSolidTorus(2., noteStack[i]->frequency, 64, 128);
>  }
>
>  glutSwapBuffers();
> }
>
> int
> main(int argc, char **argv)
> {
>
>  static GLfloat matAmbient[]= { 0.1f, 0.1f, 1.0f, 1.0f };
>  static GLfloat matSpecular[] = {0.0f, 0.0f, 1.0f , 1.0f};
>  static GLfloat matDiffuse[] = { 0.0f, 0.0f, 1.0f, 1.0f };
>  static GLfloat matShininess[] = { 128.0f };
>
>  static GLfloat lightPos[] = {50.f, 30.f, 50.f, 1.0f};
>
>  static GLfloat lightAmbient[]= { 0.25f, 0.25f, 0.25f, 1.0f };
>
>  static GLfloat lightSpecular[] = {1.0f, 1.0f, 1.0f , 1.0f};
>  static GLfloat lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
>  static GLfloat localAmbient[] = { 0.0f, 0.0f, 0.0, 1.0f };
>
>  glutInit(&argc, argv);
>  glutInitWindowSize(712, 712);
>  glutInitDisplayMode(GLUT_STENCIL|GLUT_DEPTH|GLUT_DOUBLE);
>  (void)glutCreateWindow("Window");
>  glutDisplayFunc(redraw);
>
>  glClearColor(.0f, .0f, .0f, .0f);
>  glEnable(GL_DEPTH_TEST);
>
>  glEnable(GL_LIGHTING);
>  glEnable(GL_LIGHT0);
>
>  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, localAmbient);
>
>  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
>
>  glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
>  glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
>  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
>
>  glEnable ( GL_COLOR_MATERIAL ) ;
>
>  glLightfv(GL_FRONT, GL_DIFFUSE, matDiffuse);
>
>  glLightfv(GL_FRONT, GL_AMBIENT, matAmbient);
>  glLightfv(GL_FRONT, GL_SPECULAR, matSpecular);
>  glLightfv(GL_FRONT, GL_SHININESS, matShininess);
>
>  glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
>
> //////////////////////////////////////////////////////////////////////////
>
>  glEnable(GL_BLEND);
>  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
>
>  glHint(GL_POINT_SMOOTH, GL_NICEST);
>  glHint(GL_LINE_SMOOTH, GL_NICEST);
>  glHint(GL_POLYGON_SMOOTH, GL_NICEST);
>
>  glEnable(GL_POINT_SMOOTH);
>  glEnable(GL_LINE_SMOOTH);
>  glEnable(GL_POLYGON_SMOOTH);
>
>  glMatrixMode(GL_PROJECTION);
>  glOrtho(-50., 50., -50., 50., -50., 50.);
>  glMatrixMode(GL_MODELVIEW);
>
>  glutTimerFunc(1000/FRAMES_PER_SEC,myTimer,0);
>  glutMainLoop();
>
>  return 0;             /* ANSI C requires main to return int. */
> }
>

Disable polygon smooth and stencil & see if your FPS picks up. Neither does 
much./anything for you here

jbw


0
Reply jbwest 8/15/2006 1:40:01 AM


1 Replies
86 Views

(page loaded in 0.059 seconds)

Similiar Articles:













7/29/2012 6:53:37 AM


Reply: