How to call method in OpenGlDemoClass (beginners question)


#1

Hi,

I’m using the JuceAudioDemoPlugin project and have added an OpenGLwindow to my window component in order to plot input dependent graphics - a spectogram. In the filtercomponent class I am calculating the Fourier Transform (using the FFTW3 library) from the input samples and I need to add the pointer of the FFT output array (magnitude) to another pointer in the OpenGLdemoclass(taken from the JuceDemo project) for plotting using a texture.

My problem now is as simple as this: how do set a pointer to the FFT output buffer inside the OpenGLDemoClass. In other words I have declared a float* pointer and need to set this from the outside. This I tried using the following method in the OpenGLDemoClass

void setArrayPointer(float *pointerA)
{
mags = pointerA;
}

Which I try to call from the “outside” using:

// Add a OpenGl canvas
addAndMakeVisible(demos = createOpenGLDemo());
demos->setBounds(0, 0, getWidth() - 4, getHeight() - (80));
demos->setArrayPointer(pointer); // pointer = &RealOutputFFTW3

But the Visual Studio prompt as an error:

c:\vstdevel\juceaudioplugin\demo\src\demoeditorcomponent.cpp(129) : error C2039: ‘setArrayPointer’ : is not a member of ‘juce::Component’

Since I’m not that strong with C++ yet I need some advice on how to do this!

Hope I’m making sense!

Thanks for reading
Thomas


#2

[quote=“stoltzo”]Hi,

I’m using the JuceAudioDemoPlugin project and have added an OpenGLwindow to my window component in order to plot input dependent graphics - a spectogram. In the filtercomponent class I am calculating the Fourier Transform (using the FFTW3 library) from the input samples and I need to add the pointer of the FFT output array (magnitude) to another pointer in the OpenGLdemoclass(taken from the JuceDemo project) for plotting using a texture.

My problem now is as simple as this: how do set a pointer to the FFT output buffer inside the OpenGLDemoClass. In other words I have declared a float* pointer and need to set this from the outside. This I tried using the following method in the OpenGLDemoClass

void setArrayPointer(float *pointerA)
{
mags = pointerA;
}

Which I try to call from the “outside” using:

// Add a OpenGl canvas
addAndMakeVisible(demos = createOpenGLDemo());
demos->setBounds(0, 0, getWidth() - 4, getHeight() - (80));
demos->setArrayPointer(pointer); // pointer = &RealOutputFFTW3

But the Visual Studio prompt as an error:

c:\vstdevel\juceaudioplugin\demo\src\demoeditorcomponent.cpp(129) : error C2039: ‘setArrayPointer’ : is not a member of ‘juce::Component’

Since I’m not that strong with C++ yet I need some advice on how to do this!

Hope I’m making sense!

Thanks for reading
Thomas[/quote]

I found a way to solve this.

I now use:

addAndMakeVisible(demos = createOpenGLDemo(pointer));

where I modified the createOpenGLDemo decleration:

Component* createOpenGLDemo(float* pointerA)
{
return new OpenGLDemo(pointerA);
}

Although I would like to know how to access a method inside the OpenGLDemoClass from the outside if possible…

Sorry for the noise :slight_smile:

Thomas


#3

if you are still using something like the following code to create your opengl class (this is based on line 191 of the jucedemo MainDemoWindow.cpp code)

then your returned demos object is actually a pointer to a Component rather than to your openGLComponent which has the setArrayPointer() function in it.

in a completely unsafe way you can see if the following works, just change your “outside” call to something like this:

Its generally unsafe as you are assuming that demos is actually an OpenGLDemo object. the best way would be for you to create a specific pointer to your OpenGLDemo object, rather than use the demos one like so:

then when you want to set the pointer just call

you will still be able to do all the same things to the gl object as you did with the demos object as the OpenGLDemo class inherits all of the Component attributes and methods.


#4

[quote=“colinbear”]if you are still using something like the following code to create your opengl class (this is based on line 191 of the jucedemo MainDemoWindow.cpp code)
…[/quote]

Hi,

Thank you so much for your reply - that was a very good help you gave.

Very much appreciated!

Thomas :slight_smile:


#5

[quote=“colinbear”]if you are still using something like the following code to create your opengl class (this is based on line 191 of the jucedemo MainDemoWindow.cpp code)

then your returned demos object is actually a pointer to a Component rather than to your openGLComponent which has the setArrayPointer() function in it.

in a completely unsafe way you can see if the following works, just change your “outside” call to something like this:

Its generally unsafe as you are assuming that demos is actually an OpenGLDemo object. the best way would be for you to create a specific pointer to your OpenGLDemo object, rather than use the demos one like so:

then when you want to set the pointer just call

you will still be able to do all the same things to the gl object as you did with the demos object as the OpenGLDemo class inherits all of the Component attributes and methods.[/quote]

Hi Colinbear,

I tried to follow your advice on how to make an instance of the createOpenGLDemo() class from the Juce Demo.

This is how it is done from Jules:

addAndMakeVisible(demos = createOpenGLDemo());


Component* createOpenGLDemo()
{
return new OpenGLDemo();
}

I would like to do it the way you suggest but I haven’t succeeded :frowning: and I’m getting frustrated since I’ve feel I have tried all possibilities…

To recall you recipy:

[b][i]Its generally unsafe as you are assuming that demos is actually an OpenGLDemo object. the best way would be for you to create a specific pointer to your OpenGLDemo object, rather than use the demos one like so:

Code:
OpenGLDemo *gl = new OpenGLDemo();

then when you want to set the pointer just call

Code:
gl->setArrayPointer(pointer);[/i][/b]

Doing it your way leads to the following “poor” error message in Visual Studio Express C++

…\src\DemoEditorComponent.cpp(104) : error C2061: syntax error : identifier ‘OpenGLDemo’

Do you have any suggestions?

The essential code lines from my project are presented below:

[i]DemoEditorComponent::DemoEditorComponent (DemoJuceFilter* const ownerFilter)
: AudioFilterEditor (ownerFilter)
{
static ShinyLookAndFeel* shinyLook = 0;

if (shinyLook == 0)
    shinyLook = new ShinyLookAndFeel();

LookAndFeel::setDefaultLookAndFeel (shinyLook);

// create our gain slider..
addAndMakeVisible (gainSlider = new Slider (T("gain")));
gainSlider->addChangeListener (this);
gainSlider->setRange (0.0, 1.0, 0.01);

addAndMakeVisible (kneeSlider = new Slider (T("knee")));
kneeSlider->addChangeListener (this);
kneeSlider->setRange (0.0, 1.0, 0.01);

addAndMakeVisible (OpenGlZoomSlider = new Slider (T("OpenGlZoomSlider")));
OpenGlZoomSlider->addChangeListener (this);
OpenGlZoomSlider->setRange (0.0, 1.5, 0.01);

// get the gain parameter from the filter and use it to set up our slider
gainSlider->setValue (ownerFilter->getParameter (0), false);
kneeSlider->setValue (ownerFilter->getParameter (0), false);

// create and add the midi keyboard component..
//addAndMakeVisible (midiKeyboard 
//   = new MidiKeyboardComponent (ownerFilter->keyboardState, 
//                                MidiKeyboardComponent::horizontalKeyboard));

// add a label that will display the current timecode and status..
//addAndMakeVisible (infoLabel = new Label (String::empty, String::empty));

// add the triangular resizer component for the bottom-right of the UI
addAndMakeVisible (resizer = new ResizableCornerComponent (this));
resizer->setResizeLimits (150, 150, 1024,1024);

// set our component's initial size to be the last one that was stored in the filter's settings
//setSize (ownerFilter->lastUIWidth, 
//         ownerFilter->lastUIHeight);
setSize (winWidth, winHeight);

// register ourselves with the filter - it will use its ChangeBroadcaster base 
// class to tell us when something has changed, and this will call our changeListenerCallback()
// method.
ownerFilter->addChangeListener (this);

// Add a OpenGl canvas
//addAndMakeVisible(demos = createOpenGLDemo());

OpenGLDemo *gl = new OpenGLDemo();

}[/i]


#6

you’ll need to include “OpenGLDemo.h” from maindemowindow.cpp if you haven’t already…


#7

Hi Daniel,

Thanks for your support. The OpenGLDemo.h doesn’t exist (I’ve started using the JuceAudioPlugin project and have imported the opengl component from the JuceDemo - in the jucedemo the opengl.cpp has not a h-file associated - I don’t know why the opengldemo can live without one but it works for some reason. I believe that the “Component* createOpenGLDemo(float* pointerA);” in the jucedemo_headers.h file makes it reachable for other classes).

What I would like to achieve later is to assign gui buttons to variables of he OpenGl class - how can this be done in a “nice” way?

If you would like to see the project I can mail it to you! Currently the VST plugin show a 512 point fft over time using a opengl texture.

thanks
Thomas


#8

Hi Daniel,

Thanks for your support. The OpenGLDemo.h doesn’t exist (I’ve started using the JuceAudioPlugin project and have imported the opengl component from the JuceDemo - in the jucedemo the opengl.cpp has not a h-file associated - I don’t know why the opengldemo can live without one but it works for some reason. I believe that the “Component* createOpenGLDemo(float* pointerA);” in the jucedemo_headers.h file makes it reachable for other classes).

What I would like to achieve later is to assign gui buttons to variables of he OpenGl class - how can this be done in a “nice” way?

If you would like to see the project I can mail it to you! Currently the VST plugin show a 512 point fft over time using a opengl texture.

thanks
Thomas[/quote]

oops. sorry. my mistake. You’re right, there is no opengldemo.h and therefore the OpenGLDemo class can not, as it stands, be used outside of the opengldemo.cpp

As it currently is, nothing outiside the scope of opengldemo.cpp needs to access anything specific to the OpenGLDemo class, so Jules has not made it a publically accesible class (ie. he hasn’t put the class declaration in an .h file that can be included by other files). However if you want to call member functions of OpenGLDemo from another part of the application (ie. to set your FFT data) then the other parts of the program need to know about OpenGLDemo.

The way colinbear suggested is the correct way to go about achieving what you want to do, but you’ll need to expose the OpenGLDemo class to other cpp files by moving the class declaration into a new header file. i.e. create an opengldemo.h, move the OpenglDemo class declaration to the new h file (actually you could just move everything from the .cpp file to the new h file), then include the new h file from maindemowindow.cpp, and from opengldemo.cpp (and from whereever else needs it)

Remember, the way JuceDemo has been coded is not necessarily the best approach for your audio plugin, so don’t be afraid to take a different approach, and change the code, if it suits your needs better.

Hope that helps!


#9

Hi Daniel,

Again, thanks a lot for your reply…I’ll look into it right away.

Thomas :slight_smile:


#10

Hi,

I tried to follow your advice but the code I ended up with cannot be compiled :frowning:

I tried decompose the Opengl.cpp into a *.cpp and a *.h file for reasons described in the above.

The error VS Express C++ provides me:

JUCE! Linking to: jucelib_static.lib
…\src\OpenGL.cpp(51) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
…\src\OpenGL.cpp(51) : error C2556: ‘int DemoOpenGLCanvas::resized(void)’ : overloaded function differs only by return type from 'void DemoOpenGLCanvas::resized(void)'
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(84) : see declaration of ‘DemoOpenGLCanvas::resized’
…\src\OpenGL.cpp(51) : error C2371: ‘DemoOpenGLCanvas::resized’ : redefinition; different basic types
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(84) : see declaration of ‘DemoOpenGLCanvas::resized’
…\src\OpenGL.cpp(86) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
…\src\OpenGL.cpp(86) : error C2556: ‘int DemoOpenGLCanvas::mouseDrag(const juce::MouseEvent &)’ : overloaded function differs only by return type from 'void DemoOpenGLCanvas::mouseDrag(const juce::MouseEvent &)'
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(85) : see declaration of ‘DemoOpenGLCanvas::mouseDrag’
…\src\OpenGL.cpp(86) : error C2371: ‘DemoOpenGLCanvas::mouseDrag’ : redefinition; different basic types
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(85) : see declaration of ‘DemoOpenGLCanvas::mouseDrag’
…\src\OpenGL.cpp(93) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
…\src\OpenGL.cpp(93) : error C2556: ‘int DemoOpenGLCanvas::renderOpenGL(void)’ : overloaded function differs only by return type from 'void DemoOpenGLCanvas::renderOpenGL(void)'
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(86) : see declaration of ‘DemoOpenGLCanvas::renderOpenGL’
…\src\OpenGL.cpp(93) : error C2371: ‘DemoOpenGLCanvas::renderOpenGL’ : redefinition; different basic types
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(86) : see declaration of ‘DemoOpenGLCanvas::renderOpenGL’
…\src\OpenGL.cpp(112) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
…\src\OpenGL.cpp(112) : error C2556: ‘int DemoOpenGLCanvas::timerCallback(void)’ : overloaded function differs only by return type from 'void DemoOpenGLCanvas::timerCallback(void)'
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(87) : see declaration of ‘DemoOpenGLCanvas::timerCallback’
…\src\OpenGL.cpp(112) : error C2371: ‘DemoOpenGLCanvas::timerCallback’ : redefinition; different basic types
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(87) : see declaration of ‘DemoOpenGLCanvas::timerCallback’
…\src\OpenGL.cpp(159) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
…\src\OpenGL.cpp(159) : error C2556: ‘int DemoOpenGLCanvas::setArrayPointer(float *)’ : overloaded function differs only by return type from 'void DemoOpenGLCanvas::setArrayPointer(float *)'
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(88) : see declaration of ‘DemoOpenGLCanvas::setArrayPointer’
…\src\OpenGL.cpp(159) : error C2371: ‘DemoOpenGLCanvas::setArrayPointer’ : redefinition; different basic types
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(88) : see declaration of ‘DemoOpenGLCanvas::setArrayPointer’
…\src\OpenGL.cpp(167) : error C2011: ‘OpenGLDemo’ : ‘class’ type redefinition
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(33) : see declaration of ‘OpenGLDemo’
…\src\OpenGL.cpp(197) : error C2027: use of undefined type 'OpenGLDemo’
c:\vstdevel\juceaudioplugin\demo\src\OpenGL.h(33) : see declaration of ‘OpenGLDemo’
…\src\OpenGL.cpp(197) : fatal error C1903: unable to recover from previous error(s); stopping compilation

My *.h file content is:

[i]#ifdef _WIN32

// #include <GL/glx.h>

//OpenGL screen size
const int screenWidth = 512;
const int screenHeight = 512;

//Texture dimensions
const int texWidth = NBINS/2; // Frequency axis
const int texHeight = 256; // Time axis

// ---------------------------------------------------------------------------
// Class 1
// ---------------------------------------------------------------------------
class OpenGLDemo : public Component,
public ChangeBroadcaster
{
public:
// Declare Variables here…
//==============================================================================
OpenGLDemo();
~OpenGLDemo();
//==============================================================================
void resized();

private:

};

// ---------------------------------------------------------------------------
// Class 2
// ---------------------------------------------------------------------------
class DemoOpenGLCanvas : public OpenGLComponent,
public Timer
{
public:
// ------------- FFTW specific -------------
int indx; // variable in for loops
float mags[NBINS]; // array for FFTW output data
float* outputBuffer;
float* pointerA;
float* window;
double* in; // pointer for FFTW input data (double or complex)

fftw_complex *out;		// pointer for complex fftw output data
fftw_plan p;			// plan 

float rotx, roty,rotz;
int   freq_indx;
int	  time_indx;
float maxValue;
float minValue;

// Pointer to texture bitmap
unsigned char *myTexture;		// pointer to texture bitmap

// Colormap
int colorMap[256][3];

// Declare Variables here..
//==============================================================================
DemoOpenGLCanvas();
~DemoOpenGLCanvas();
//==============================================================================

void resized();
void mouseDrag (const MouseEvent&);
void renderOpenGL();
void timerCallback();
void setArrayPointer(float* Apointer);

private:

};[/i]

and the *.cpp content for the resized() member function is:

[i]DemoOpenGLCanvas::resized()
{
if (makeCurrentContextActive())
{
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )

	// Texture definitions
	myTexture = (byte *) malloc(texWidth*texHeight * 3); // actual texture

	memset(myTexture, 0, texWidth*texHeight*3);

	glViewport(0,0,screenWidth,screenHeight);			// Reset The Current Viewport

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)screenWidth/(GLfloat)screenHeight,0.1f,2548.0f);

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

	glGenTextures(1, (GLuint*)myTexture);				// Create The Texture
	glBindTexture(GL_TEXTURE_2D, (GLuint)myTexture);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, myTexture);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}

}[/i]

Anyone can pin point what I have done wrong here?

Thomas


#11

those “int assumed” errors are becuase you haven’t specifi9ed a return type in the function definition

eg. you’ve written

[quote]DemoOpenGLCanvas::resized()
{
[/quote]

instead of

[quote]void DemoOpenGLCanvas::resized()
{
[/quote]

if no return type is specified, the compiler assumes “int” (which is a bit wierd, i know… but its just the way it is) so then you get other errors sabout it differing from the declared functions in the .h file only by return type. which isn’t allowed.
So the first step is to fix all those functions so they have the correct return type…


#12

[quote=“daniel78”]those “int assumed” errors are becuase you haven’t specifi9ed a return type in the function definition

eg. you’ve written

[quote]DemoOpenGLCanvas::resized()
{
[/quote]

instead of

[quote]void DemoOpenGLCanvas::resized()
{
[/quote]

if no return type is specified, the compiler assumes “int” (which is a bit wierd, i know… but its just the way it is) so then you get other errors sabout it differing from the declared functions in the .h file only by return type. which isn’t allowed.
So the first step is to fix all those functions so they have the correct return type…[/quote]

Daniel, you are todays hero! Everything works nicely now which means that I can start assigning buttons - yahoooooo.

Again, thanks for your patience…

Cheers
Thomas


#13