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++] = ¬es[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++] = ¬es[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: Optimizing glCopyPixels or glDrawPixels - comp.graphics.api.opengl ...Hi, Which is faster, glCopyPixels or glDrawPixels? And is there a way to make it faster like if I used power of 2 dimensions of my rectangle? Can I q... Shaders and Optimization - comp.graphics.api.openglHi, I read somewhere that current GPUs are optimizing the shader ... shadow mapping shader optimization - comp.graphics.api.opengl ... Hi, in order to soft my shadows ... glReadPixels question - comp.graphics.api.openglHi All, i am trying to save my opengl generated picture in a file. I am using the ... Optimizing glCopyPixels or glDrawPixels - comp.graphics.api.opengl ... One last ... comp.graphics.api.opengl - page 10This is page 10 of the comp.graphics.api.opengl group which contains 7086 articles. ... viewports .... same texture 3 157 (7/9/2003 4:07:34 PM) Hello, I'm trying to optimize my ... SPEED TEST: DirectX9 vs openGL - comp.graphics.api.opengl ...I know both DirectX and openGL, and I like them both, a lot, for different reasons. ... my version of the two, post the source, and let the gurus from each side optimize the ... rewrite glRotate? - comp.graphics.api.openglBut OpenGL is the other way round. My brain is too small to understand "why" the reverse order? ... It would've been more, let's say, inviting for developers to "optimize ... PBuffer usage samples - comp.graphics.api.openglHi, I'm looking for some OpenGL INT32 ARGB PBuffer ... Optimizing glCopyPixels or glDrawPixels - comp.graphics.api ... is another window overlapping the bottom left of my ... Strategy suggestion? - comp.graphics.api.openglStrategy suggestion? - comp.graphics.api.opengl... mind is ... on the ... I'm optimizing the inner most loop of my script. I need to convert month name to month number. speed up software emulated OpenGL - comp.graphics.api.opengl ...Now, my programs do not need a lot of graphics and they run quite ... comp.graphics.api.opengl - page 23 Optimize speed drawing models 11 67 (7/31 ... is there an API to get the memory usage of my program - comp.unix ...Texture memory usage - comp.graphics.api.opengl System-API to get current ... memory consumption - comp.unix.solaris I'd like to optimize memory usage of my program. Optimizing OpenTK/OpenGL code in C# - OpenGL - GameDev.netOptimizing OpenTK/OpenGL code in C# - posted in OpenGL: Hey guys, I am working on a project in C# using OpenTK. I am using it in Windowed mode, meaning there are ... Optimizing glTexImage2D - OpenGL - GameDev.netOptimizing glTexImage2D - posted in OpenGL: I am using 24-bit bitmaps only, and I want to optimize their use with glTexImage2D. The extensions on my card only list ... 7/29/2012 6:53:37 AM
|