Results 1 to 6 of 6

Thread: contours on triangle polygons

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    3

    contours on triangle polygons

    Hello, This is my first post here, I only started playing with Opengl a few days ago.

    My project involves animating a structure built from triangles. To test ideas I started with just one triangle, I've been successful with producing a triangle in the orientation I want, and can spin it round and such. Now I want to color the triangles as in the Pic so that the coloring corosponds to the z value. I know I have to use a texture laid on the triangle polygon, and produce an array of twenty rgb colors, but apart from that I don't know where to start.
    Any help would be gratefuilly received.

    Albert

  2. #2
    Senior Member Regular Contributor
    Join Date
    Sep 2008
    Posts
    139

    Re: contours on triangle polygons

    Hello, for the beginning you could try to draw a quad
    with glBegin(GL_QUADS) and set the texture coordinates

    Code :
    Shader.SetUniform(1D Texture)
    Shader.Enable()
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f,0.0f); glVertex2f(0.0f,0.0f);
    glTexCoord2f(1.0f,0.0f); glVertex2f(1.0f,0.0f);
    glTexCoord2f(1.0f,1.0f); glVertex2f(1.0f,1.0f);
    glTexCoord2f(0.0f,1.0f); glVertex2f(0.0f,1.0f);
    glEnd();
    Shader.End()

    Actually you can not access a position in a fragment program. but you you have your texture coordinates in your fragment
    program. So you can use them as a position.
    So you can do some if in your shader.

    for example

    Code :
    float4 fp(float2 texcoord: TEXCOORD0) : COLOR0
    {
         float4 color = float4(0.0f,0.0f,0.0f,1.0f);
         if(texcoord.x > 0.2f &amp;&amp; texcoord.x < 0.35f)
         {
              color = float4(1.0f,0.0f,0.0f,1.0f);
         }
         else if(texcoord.x >= 0.35f &amp;&amp; texcoord < 0.5f
         {
              color = float4(1.0f,0.5f,0.0f,1.0f);
         }
         ...
         ...
         return color;
    }

    But you could also use a 1D Texture as lookup table in you shader. Then you can change the color values dynamicaly.

    Create a 1D Texture and start from blue and go over to red, like in your amplitude and then in you shader do something like this.
    Code :
    float4 fp(uniform sampler1D lookup, float2 texcoord: TEXCOORD0, float) : COLOR0
    {
         float4 color = tex1D(lookup, texcoord.x);
         return color;
    }
    In the shader above, the texoord are the texture coordinates from your quad. So when you change you amplitude values or range or whatever you only need to change the 1D texture upload it again to gpu emmory and set set the uniform value again.

    hope that helped a bit
    lobbel

  3. #3
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: contours on triangle polygons

    This can be done using 1D texturing.
    convert your z scale into [0;1] range, and set it as texture coordinate for each vertex :
    float s = (z+0.3)/0.6;
    glTexCoord1f(s);

    and to disable bilinear filtering of the texture :
    glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST));

  4. #4
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    3

    Re: contours on triangle polygons

    Thank you both,
    This is part of my code, I don't know how I ascociate a color to the texture, at the moment this code produces a uniform white triangle. The language is Fortran.

    Code :
          call glClear(ior(GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT))
          call glEnable(GL_TEXTURE_1D)
          call glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
          call glPushMatrix ()
     
          call glBegin(GL_TRIANGLES)
     
          call glTexCoord1f(1.0)
          call glVertex3f(-12.0, -8.0, 1.0)
          call glTexCoord1f(0.5)
          call glVertex3f( 0.0, 12.0, 0.0)
          call glTexCoord1f(0.0)
          call glVertex3f( 12.0,  -8.0, -1.0)
     
          call glEnd()
          call glPopMatrix ()
          call glFlush()
          call swap_opengl_buffers()

    Thanks again.

  5. #5
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: contours on triangle polygons

    white means something is wrong with the texture, disabling it.
    A classic is to forget mipmaps, they are needed by default.
    try without mipmaps by adding :
    call glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST)

  6. #6
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    3

    Re: contours on triangle polygons

    Thanks for the help, but I've still got no texture, just a white triangle, here's my modified code . Can anyone point me in the right direction, I now have the colors loaded in a global variable arrcol, dimension 20x3 holding RGB values between 0.0 and 1.0, Thanks again. Albert

    Code :
    call glClear(ior(GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT))
          call glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
          call glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
          call glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP)
          call glTexParameterf(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
          call glTexParameterf(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
          call glTexImage1D(GL_TEXTURE_1D,0,3,20,0,GL_RGB,GL_FLOAT,arrcol) !comment, arrcol holds twenty colours as RGB values
          call glEnable(GL_TEXTURE_GEN_S)
          call glEnable(GL_TEXTURE_1D)
          call glPushMatrix ()
     
          call glBegin(GL_TRIANGLES)!
     
          call glTexCoord1f(1.0)
          call glVertex3f(-12.0, -8.0, 1.0)
          call glTexCoord1f(0.5)
          call glVertex3f( 0.0, 12.0, 0.0)
          call glTexCoord1f(0.0)
          call glVertex3f( 12.0,  -8.0, -1.0)
     
          call glEnd()
          call glPopMatrix ()
          call glFlush()
          call swap_opengl_buffers()

Similar Threads

  1. draw contours
    By appdeveloper in forum OpenGL: General
    Replies: 0
    Last Post: 10-15-2009, 04:04 AM
  2. Contours
    By moonwalkercs in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-21-2006, 11:12 AM
  3. contours and smoothing
    By cru in forum OpenGL: General
    Replies: 1
    Last Post: 08-18-2004, 10:03 PM
  4. Texture contours........
    By StudentFYP in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-22-2001, 11:04 AM
  5. triangle fans and polygons
    By smart_idiot in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-16-2000, 08:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean