Sign in to follow this  
gadekar_k

OpenGL drawing contour lines

Recommended Posts

gadekar_k    122
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 this post


Link to post
Share on other sites
lightbringer    1070
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 this post


Link to post
Share on other sites
gadekar_k    122
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 this post


Link to post
Share on other sites
lightbringer    1070
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 this post


Link to post
Share on other sites
Skiller    122
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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);

GL.glLoadIdentity();
#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 this post


Link to post
Share on other sites
gadekar_k    122
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);

GL.glLoadIdentity();
#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++)
{

InteropAPI.glBegin ( InteropAPI.GL_QUADS );


//====== 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 this post


Link to post
Share on other sites
Skiller    122
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 10
float 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 :)

BTW you should read this

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Thanks in advance.


-kiran

Share this post


Link to post
Share on other sites
LilBudyWizer    491
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 this post


Link to post
Share on other sites
gadekar_k    122
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.

Thanks in advance.

-Kiran

Share this post


Link to post
Share on other sites
LilBudyWizer    491
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
      Thanks,
       
  • Popular Now