COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Optimizing my OpenGL

• Email
• 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
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.

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

See related articles to this posting

```<bg_ie@yahoo.com> wrote in message
> 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
> 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.
>
>
> 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

1 Replies
105 Views

Similar Articles

12/12/2013 2:01:06 AM
page loaded in 5130 ms. (0)

Similar Artilces:

Optimal Gentoo Linux OpenGL Performance on an ATI Radeon X300
Hey there! I am developing a new kind of OpenGL-based 60 frames per second user-interface and want to tune my workstation for optimal OpenGL performance. I am using the latest Gentoo release with xorg-6.8.2-r4 on a Dell Optiplex GX280 Workstation equipped with a ATI R300-based card. lspci says: VGA compatible controller: ATI Technologies Inc RV370 5B60 [Radeon X300 (PCIE)] I am using the ati-drivers version 8.14 (the 8.16 are still marked unstable by Gentoo) and activated direct rendering through the fglrx driver. But not one double-buffer non-antialiased OpenGL-based (GLUT or GLX) demo/pr...

cl-opengl vs cl-opengl
For my own knowledge does anyone (Kenny?) know the differences between the cl-opengl that comes with Cello and the cl-opengl project on common-lisp.net. I'm looking into graphics packages and I would like to check out both Cello and OpenGL but it looks like I will be contending with a naming conflict. Why does Cello have it's own OpenGL bindings? Is that something that should be rectified? Is Cello compatible with the other cl-opengl? Sorry for all the questions, -dustin Dustin Withers wrote: > For my own knowledge does anyone (Kenny?) know the differences between > the cl-o...

how to to use the Optimization Toolbox to optimize a portfolio of stocks?
I’m trying to figure out how to to use the Optimization Toolbox to optimize a portfolio of stocks, let’s say the Dow30, based on the returns (1 year historical) and risks (stdev). This is quite easy to do in Excel, using Solver, and it is quite easy to do in Crystal Ball, using OptQuest. I’m not making a whole lot of progress here, trying to figure out how to do basically the same thing using Matlab. I found this: http://www.mathworks.com/help/finance/working-with-portfolio-constraints_bswwmte.html#bswwlk3-1 At a quick glance, I thought that would be helpful. I'm still not gettin...

Do you optimize?
So I've got this J2EE application (which for the sake of disclosure is all servlets and jsp, no ejb). And I find myself optimizing things at the "get rid of concatted strings" level (i.e. no "foo"+"bar"). I mention this to a friend who goes into a rant on how programmers shouldn't do this anymore because it just doesnt matter, the compiler does it all for you. Thoughts? Do you still optimize when you're writing "enterprise apps" (I'm ruling out the obvious cases of realtime systems, games, and others where size/speed optimizations cou...

optimization
Dear all, I have a question about heuristics that I hope someone could help me with. Imagine a general heuristic problem. In general, as one increases the number of variables that need to be optimized, the number of local optima in this optimization surface increases. Although this seems to make sense intuitively, I'm looking for papers/books/people addressing this issue. In particular, I'd like to know if this increase in the number of local optima is a general phenomenon in optimization problems. I'd appreciate any input you might have. Please send responses to my email: jas2...

optimization
Hi Jacob, i've problems with the following code: static BOOL LV_HitTest(HWND hwnd, LPPOINT lpPt){ RECT rc,rc2; INT firstItem, nViewItems, nItems; UINT row; static UINT col; LV_COLUMN lvc; firstItem=ListView_GetTopIndex(hwnd); nViewItems=ListView_GetCountPerPage(hwnd)+1; nItems=ListView_GetItemCount(hwnd); nViewItems=MIN(nItems-firstItem, nItems); for (row=0; row<nItems; row++){ col=0; while(ListView_GetColumn(hwnd, col, &lvc)){ col++; ListView_GetSubItemRect(hwnd, row, col, LVIR_BOUNDS, &rc); rc.right=rc.left+lvc.cx; if (PtInRect(&rc, *lpPt)){ ...

Porting from Win32 OpenGL code to non-Win32 OpenGL code
Hi, I've come across a pile of OpenGL tutorials which seem to be based in the Win32 arena and while 99% of the code will hold true for any platform, I'm at a loss of how to remove the likes of the following HDC hDC=NULL; HGLRC hRC=NULL; HWND hWnd=NULL; HINSTANCE hInstance; LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg,WPARAM wParam,LPARAM lParam) { switch (uMsg) { case WM_ACTIVATE: ...

Adventures in Optimization...Optimizing the stuff you didn't write!
The heart of Optimization is Benchmarking. Measure, measure, measure! So I found couple of really promising optimizations.... .... but they made hardly any difference. End to end, the process took just as many hours to within a few seconds. WHY!? The real elapsed time is not being chewed by my Ruby. It's been eaten by the things I'm using Ruby as a "Glue Language" to bind together. Measure, measure, measure... I love Ruby. It's so open. There is the profiler module. The Source code my disk, there to be tweaked. Pull in /usr/lib/ruby/1.8/profiler.rb Search and rep...

[ANN] AmanithVG second technology preview released (OpenVG over OpenGL and OpenGL|ES)
I am very glad to inform that the second AmanithVG Technology Preview as been released today. AmanithVG is a commercial implementation of OpenVG, the new application programming interface (API) for hardware accelerated 2D vector and raster graphics, created by the Khronos group. Differently from any other OpenVG implementation, AmanithVG is entirely built on top of OpenGL (from 1.1 to 2.0, using extensions where available) and OpenGL|ES (1.1 and 2.0) APIs This new technology preview includes: - Improved geometric kernel - VGU 1.0 implementation - A set of new amazing extensions, the ...

Professional seo services offer valuable seo services, seo optimization, website optimization services.
Outsourcing seo services is a leading outsourcing seo company support seo marketing services, seo consulting services, seo writing services, seo website editing and seo business report threw seo experts analyzes your web traffic using latest search engine optimization and seo tools technology for your business ranking. ...

http://dksoft.free.fr/mbbxg/main.htm Best regards Andre ...

Local optimization?
Hi Folks, It seems local optimization techniques are discussed in several undergraduate level books on compilers. I'd like to get a feel for how relevant they are these days. Are they only of historic interest or do any of them still make their way into modern compilers? If they're simply of historic interest, at what point did the move to their global counterparts take place (how recently could they be found in production compilers)? Cheers, John [It is my impression that even after you do all your whizzo global optimization, a local pass is still worthwhile to deal with redundant i...

An OpenGL++ Specification
I know this is slightly off topic but I am working on my graduate degree in graphics and was trying to locate a pdf copy of =93An OpenGL+ + Specification=94 written by P Birch, D Blythe, B Grantham, et. al. Haven=92t been able to find a copy anywhere - yet lots of other papers reference the document. Would anyone happen to have a copy they could forward me. Thanks -Ti ...

Troubleshooting OpenGL
I'm not sure if there is a better place to post this question so please let me know if you can think of one. I've been having problems running Call of Duty with my nVidia GeForce FX Go5200. I want to verify that my problem is not the OpenGL drivers or their configuration. Does anyone know of a utility or program that I could quickly verify OpenGL will work properly with my current settings? Thanks, Jason Well, I've determined that none of the OpenGL programs work at all. Some display errors and some just crash hard, but all in all none of them are able to display any graphic...

Interpreter Optimizations
Do any of the browsers do automatic optimizations? For example, - constant folding - branch folding - optree pruning - inlining of subroutines (under any conditions) I've been googling and cannot find anything on the topic. Also, if there are variations between the interpreters, is there a list of which interpreters do which optimizations? Thanks, Rob ...

Sql optimization
Hi all I'm trying to get a query to run a bit quicker and I know it can, but don't know how to let the optimizer know that. -- Andy Templeman <http://www.templeman.org.uk/> ...

Integer optimization
Hi! Is there any function that solves integer optimization problems other than the 'bintprog', which only focuses on binary ones, but is the only one I've found? example the problem: f = [-8; -5] A = [1 1 ; 9 5] ; b = [6; 45] [x, fval, exitflag,output] = bintprog(f,A,b) gives x = [1; 1] although that's very well within the limits and the optimal would probably be x = [5; 0]. > Is there any function that solves integer optimization problems other than the 'bintprog', which only focuses on binary ones, but is the only one I've found? A Google...

opengl version
How do I determine the version of OpenGL/GLU on my solaris box? On 16 Mar 2005 11:36:59 -0800 "rpond" <novahokie@gmail.com> wrote: > How do I determine the version of OpenGL/GLU on my solaris box? > pkginfo -l SUNWglrt -- Barbie - Prayers are like junkmail for Jesus I have seen things you lusers would not believe. I've seen Sun monitors on fire off the side of the multimedia lab. I've seen NTU lights glitter in the dark near the Mail Gate. All these things will be lost in time, like the root partition last week. Time to die. rpond wrote: > How do I det...

opengl languages
Could someone please tell me what languages work with OpenGL? I know C++ does, but I don't really want to buy Microsoft Visual C++. bob@coolgroups.com wrote: > Could someone please tell me what languages work with OpenGL? I know > C++ does, but I don't really want to buy Microsoft Visual C++. OCaml: http://www.ffconsultancy.com/products/ocaml_for_scientists/visualisation/ It is free, cross platform, very fast and supports lots of modern features (GC, HOFs, variant types, pattern matching etc.). If you don't want to buy software then don't use Windows. Download a...