PDA

View Full Version : contours on triangle polygons



acp693
04-30-2009, 09:24 AM
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
http://www.violini.de/pic2.JPG

lobbel
04-30-2009, 11:31 AM
Hello, for the beginning you could try to draw a quad
with glBegin(GL_QUADS) and set the texture coordinates



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



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.


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

ZbuffeR
04-30-2009, 11:40 AM
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));

acp693
04-30-2009, 12:33 PM
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.



call glClear(ior(GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT))
call glEnable(GL_TEXTURE_1D)
call glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTE R,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.

ZbuffeR
04-30-2009, 12:58 PM
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)

acp693
05-01-2009, 03:21 AM
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


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_FILTE R,GL_LINEAR)
call glTexParameterf(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR)
call glTexImage1D(GL_TEXTURE_1D,0,3,20,0,GL_RGB,GL_FLOA T,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()