# OpenGL drawing contour lines

## Recommended Posts

Hi All, I am trying to draw a surface chart using opengl. I am able to draw it properly but i just wondering how will be possible for me to draw the contouyr lines for the surface chart. Just not able to found the right API for doing so? Can any buddy able to give me any hint regarding the same? Thanks in advance. -kiran

##### Share on other sites
Could you give more detail, maybe a pic of a specific example of the effect you're trying to achieve?

If by contour lines you mean something like a heightmap, then I think you'll have to improvise by processing your internal data and plotting points or short line segments. Or you might find that using evaluators can help you, but I wouldn't know where to start with those.

##### Share on other sites
Hi,
yeah you are correct i want to draw height map where i want to identify each section with different color. I am currently using GL_QUARDS mode to connect the point which are closer to each other.
So i am just wondering there if there will be any api avaliable out of the box where i can use it directly to paint the section with the required color directly.

##### Share on other sites
If the regions are convex (that's a pretty big if), then you probably could use GL_POLYGON to connect them (set different color for each polygon) and position the lower regions further away into the screen to prevent overwrite. (You've probably already thought of that with your quad method.)

##### Share on other sites
If you aren't too concerned with speed and just want it implemented quickly I have one solution for you:

1. Create a clipping plane at the bottom of the heightmap.
2. Set color to contour color.
3. Render heightmap.
3. Move clipping plane up as high as you want your contour line.
4. Set color to nomal.
5. Render heightmap.
6. Move clipping plane up to the base of your next contour line.
7. Repeat step 2 to step 6 for each contour.

Easy to implement, looks good, but probably very slow :)

##### Share on other sites
Hi Skiller,
Thank you very much for your reply it would be a great help if you could provide me with some code snippet for drawing the contour regions.

Following is the code i am using to draw the surface chart

glDraw()
{
#region Matrix SetUp
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

#endregion

GL.glPushMatrix();

#region SurfaceReleated
//====== Set the polygon filling mode
InteropAPI.glPolygonMode( InteropAPI.GL_FRONT_AND_BACK, InteropAPI.GL_FILL );
//====== Image grid sizes
int nx = m_xSize-1,
nz = m_zSize-1;

int i = 0;
for (int z=0; z <

##### Share on other sites
Hi Skiller,
Please find below the code snippet i am using to draw the Surface chart
void glDraw()
{
#region Matrix SetUp
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

#endregion

GL.glPushMatrix();

#region SurfaceReleated
//====== Set the polygon filling mode
InteropAPI.glPolygonMode( InteropAPI.GL_FRONT_AND_BACK, InteropAPI.GL_FILL );
//====== Image grid sizes
int nx = m_xSize-1,
nz = m_zSize-1;

int i = 0;
for (int z=0; z < nz; z++, i++)
{

//====== Turn on the primitive connection mode (connected)
//====== The strip of connected quads begins here
for (int x=0; x < nx; x++, i++)
{
// i, j, k, n — 4 indices of a quad
// Counter Clockwise direction

int j = i + m_xSize, // Other vertices indices
k = j+1,
n = i+1;

//=== Get coordinates of 4 vertices

//====== Vertices are given in counter clockwise direction order

InteropAPI.glVertex3f (xi, yi, -zi);

InteropAPI.glVertex3f (xj, yj, -zj);

InteropAPI.glVertex3f (xk, yk, -zk);

InteropAPI.glVertex3f (xn, yn, -zn);
}
//====== Close block of GL_QUAD_STRIP commands
InteropAPI.glEnd();
}

#endregion SurfaceReleated

GL.glPopMatrix();
GL.glFlush();
}

##### Share on other sites
My guess is it would look something like this, tho it looks like you have wrapped up your gl commands so you'd need to do that to this code too.

#define MAX_CONTOURS 10float clipHeight = 0.0f;glColor4f(0.0f, 1.0f, 0.0f, 1.0f);for (int c = 0; c < MAX_CONTOURS; ++c) {	double clipPlane[4] = {0.0f,1.0f, 0.0f, 0.0f};		//Define Clip plane	glPushMatrix();	glTranslatef(0.0f, clipHeight, 0.0f);			// Set position of clip plane	glEnable(GL_CLIP_PLANE0);				//Enable clip plane	glClipPlane(GL_CLIP_PLANE0, clipPlane);			//Create clip plane	glPopMatrix();	//Insert code between "#region SurfaceReleated" and "#endregion SurfaceReleated" here	//Will create green contour 0.1f high every 10.0f units	clipHeight += 0.1f;	glColor4f(0.0f, 1.0f, 0.0f, 1.0f);	if (c % 2 == 1) {		clipHeight += 9.8f;		glColor4f(1.0f, 1.0f, 1.0f, 1.0f);	}}

Havn't checked it but that should work. You might need to turn of writing to the depth buffer for it to work right tho, and you'll probably wan to disable the clipping plane at some point too.
It should give you some place to start though :)

And dont forget this will probably run pretty slow :(

A better way I just thaught of would be using a pixel shader, would be a little harder to implement but it would be MUCH faster :)

##### Share on other sites
Hi Skiller,
Thanks a lot for your reply, but still i am not able to do draw the height map.
I think my logic is not exactly fitting with ur code.Can it be possible for me to specify the color for a particular region so that anything drawn in that area will get filled with the color what i have already set.

-kiran

##### Share on other sites
You can use glTexGen and a 1D texture to shade the heightmap according to height. The 1D texture would generally be a color gradiant. Since it's 1D you have one component, the s component. If you want to shade by height then the parameters are basically (0,1,0,0). That would make y translate directly to the s component. Chances are your y's don't run from 0 to 1 though. Rather they go from min to max and you want to map that to 0 to 1. So you want (0,1/(max-min),0,-min/(max-min)). So for (x,max,z,1) you get 0*x + max/(max-min) + 0*z - min/(max-min) = (max-min) / (max-min) = 1 and for (x,min,z,1) you get 0*x + min/(max-min) + 0*z - min/(max-min) = (min-min) / (max-min) = 0.

##### Share on other sites
Hi LilBudyWizer,
I have tried with glTexGen and a 1D texture but still it is not working for me.
Can you please provide me with some working code for this.as per as my profile is concern I am begginer in opengl programming.

-Kiran

##### Share on other sites
The Redbook has an example of using contours. The example is quite a ways down in Chapter 9 which is what the link it to. The section and subsection is "Automatic Texture-Coordinate Generation" and "Creating Contours" so searching on either of those will take you right to it. It's only about 100 lines which you can cut and paste. The write up before and after it should give enough information to figure out what you play with.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627668
• Total Posts
2978541
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 10
• 10
• 12
• 22