Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C++ and OpenLayer questions

This thread is locked; no one can reply to it. rss feed Print
C++ and OpenLayer questions
ngiacomelli
Member #5,114
October 2004

I have a few questions concerning C++ and OpenLayer (I'm new to both). I've got a working framework but now I have a few questions:

First of all, I want to ask about namespaces. I have a file called: render.cpp, that contains the main OpenLayer rendering code. In this file I declare the use of the ol namespace. In a header file included in render.cpp (render.h) I have a declaration for an OpenLayer TextRenderer (held within header guards).

When I try to compile this code, I get an error. By declaring the ol namespace again inside of render.h, this solves the problem. So what's the right thing to do, in this situation? Declare the namespace inside of render.h but outside of the header guards and ditch the namespace declaration inside of render.cpp or is it common to have both?

My other question is using OpenLayer along with standard OpenGL calls. I've read a few of the NeHe tutorials and was wondering how I should go about it? My plan was to get OpenLayer working nicely around my standard framework, and then work through NeHe tutorials to learn OpenGL. If someone could supply a clean way of doing this, I'd be very thankful.

Indeterminatus
Member #737
November 2000
avatar

To answer your first question:

Unless you are planning on extending OpenLayer, you are well advised not to declare the namespace ol, but rather to use it.

TextRenderer as such won't work, then, of course, because it's not in the global namespace, and you are not referencing it from its own namespace. The solution to your problem is to use ol::TextRenderer.

Another possible solution would be to put a "using namespace ol;" somewhere, but I do not recommend that one, as it ruins the namespace's original intent.

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

ngiacomelli
Member #5,114
October 2004

Whoops. I should explain, when I say I 'declared' the namespace, I was doing: 'using namespace ol'. ;D

Carrus85
Member #2,633
August 2002
avatar

It would help if you gave us a snippet of the offending code, but my guess is that you are trying to use openlayer functions in your render.h without prepending the proper namespace identifier (ol::*), hence why it compiles when you tell it to explictily include everything in the openlayer namespace into your current namespace (using namespace ol), but throws craploads of errors when you don't.

Indeterminatus
Member #737
November 2000
avatar

Alright, sorry then for the confusion. Mind to show some code, then? And the error message you were talking about might also be helpful ;)

edit: Damn, beaten :P

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

My other question is using OpenLayer along with standard OpenGL calls. I've read a few of the NeHe tutorials and was wondering how I should go about it? My plan was to get OpenLayer working nicely around my standard framework, and then work through NeHe tutorials to learn OpenGL. If someone could supply a clean way of doing this, I'd be very thankful.

You can use any OpenGL commands along with OL, but beware that a few functions might change the OpenGL state (the active color, for example)

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

ngiacomelli
Member #5,114
October 2004

A snippet from graphics.cpp:

1#include "./headers/application.h"
2#include "./headers/graphics.h"
3#include <string>
4 
5using namespace ol;
6using namespace std;
7 
8void Application::LoadGraphicData() {
9 
10 text.Load("Fonts/def.ttf", 16, 12, Rgba::WHITE );
11
12}
13 
14void Application::DestroyGraphicData() {
15
16 text.Destroy();
17 buffer.Destroy();
18
19}

From graphics.h:

1#ifndef _GRAPHICS_H_
2#define _GRAPHICS_H_
3 
4//////////////////////////////////////////////////////
5// Namespace declarations
6//////////////////////////////////////////////////////
7 
8using namespace ol;
9 
10//////////////////////////////////////////////////////
11// OpenLayer declarations
12//////////////////////////////////////////////////////
13 
14GfxRend gfx;
15TextRenderer text;
16Bitmap buffer;
17 
18#endif

Code works fine but should I really need to use: using namespace ol twice?!

Here's some code from a NeHe tutorial. I was wondering how I'd go about integrating it with my rendering code (also below):

1glBegin(GL_TRIANGLES); // Start Drawing The Pyramid
2 glColor3f(1.0f,0.0f,0.0f); // Red
3 glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
4 glColor3f(0.0f,1.0f,0.0f); // Green
5 glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
6 glColor3f(0.0f,0.0f,1.0f); // Blue
7 glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)
8 glColor3f(1.0f,0.0f,0.0f); // Red
9 glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
10 glColor3f(0.0f,0.0f,1.0f); // Blue
11 glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)
12 glColor3f(0.0f,1.0f,0.0f); // Green
13 glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right)
14 glColor3f(1.0f,0.0f,0.0f); // Red
15 glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
16 glColor3f(0.0f,1.0f,0.0f); // Green
17 glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back)
18 glColor3f(0.0f,0.0f,1.0f); // Blue
19 glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back)
20 glColor3f(1.0f,0.0f,0.0f); // Red
21 glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
22 glColor3f(0.0f,0.0f,1.0f); // Blue
23 glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)
24 glColor3f(0.0f,1.0f,0.0f); // Green
25 glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)
26 glEnd(); // Done Drawing The Pyramid

void Application::Render() {
      
      Transforms::ResetTransforms();
      Transforms::SetColorChannels( Rgba::WHITE );

      GfxRend::RefreshScreen();     
      
}

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

Here's some code from a NeHe tutorial. I was wondering how I'd go about integrating it with my rendering code (also below):

1void Application::Render() {
2
3 Transforms::ResetTransforms();
4 Transforms::SetColorChannels( Rgba::WHITE );
5
6 glDisable( GL_TEXTURE_2D ); // OL 1.93 makes sure that the texturing settings aren't changed, //
7 // but OL2.0 does that only if you compile it with OL_NO_STATE_CHANGE for performance reasons //
8 glBegin(GL_TRIANGLES); // Start Drawing The Pyramid
9 glColor3f(1.0f,0.0f,0.0f); // Red
10 glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
11 glColor3f(0.0f,1.0f,0.0f); // Green
12 glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
13 glColor3f(0.0f,0.0f,1.0f); // Blue
14 glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)
15 glColor3f(1.0f,0.0f,0.0f); // Red
16 glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
17 glColor3f(0.0f,0.0f,1.0f); // Blue
18 glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)
19 glColor3f(0.0f,1.0f,0.0f); // Green
20 glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right)
21 glColor3f(1.0f,0.0f,0.0f); // Red
22 glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
23 glColor3f(0.0f,1.0f,0.0f); // Green
24 glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back)
25 glColor3f(0.0f,0.0f,1.0f); // Blue
26 glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back)
27 glColor3f(1.0f,0.0f,0.0f); // Red
28 glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
29 glColor3f(0.0f,0.0f,1.0f); // Blue
30 glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)
31 glColor3f(0.0f,1.0f,0.0f); // Green
32 glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)
33 glEnd(); // Done Drawing The Pyramid
34 GfxRend::RefreshScreen();
35
36}

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

ngiacomelli
Member #5,114
October 2004

How odd, I just get a black screen. No crash, it's just not rendered.

Go to: