# Manipulating RGB for mesh vertices

I need to do the following:

Say the maximum displacement of a node is A. I want to use this value to manipulate the colour (red for largest , blue for lowest, interpolation of colours in between)for every vertices on my surface.

Currently I can only use this:

glColor3f(Displacement1/A,0,1 - (Displacement1/A));
glVertex3d(x1,y1,z1);

This will only give me shade of red to blue.

Can anyone help me to use colours so as to describe the distribution/ analysis of displacements of nodes on the surface.

Thanks

If I understand you correctly, by “interpolation of colors” you
would like to pass by green, yellow,… for instance.

You actually try to achieve a mapping for a scalar value (displacement) to a visual representation (color).

With a displacement normalized between 0 and 1, the right solution is to use a 1D texture where you define/build the mapping from each scalar value to a color value.

For each vertex, assign a texture coordinate equal to its displacement.

During rasterization the texture coordinate will be interpolated.
The 1D texture will be sampled with this interpolated texture coordinate.

How do you create a 1D texture? And how does value from 0 to 1 map to the correct colour?

Are there any examples i can refer to? I’m sorry about this but i’m not a fluent opengl programmer.

If you know how to create a 2D texture, it is pretty much the same for a 1D texture: use glTexImage1D instead of glTexImage2D to send the texture from the RAM to the VRAM, use target
http://www.glprogramming.com/red/chapter09.html
or this tutorial:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06

The 1D texture is just a 1D array where each element is a color.
Choose the size of your texture to be the number of key colors your scalar values can be mapped to, say 4 (BTW, only a power-of-two size works for old OpenGL version)

When you define the array, you define the mapping from the scalar (index) to color, you define the texture:

at index=0, rgb=(1,0,0) red
at index=1, rgb=(1,1,0) yellow
at index=2, rgb=(0,1,0) green
at index=3, rgb=(0,0,1) blue

If (in your mind ) you divide the index by 3, you have some
scalar value in the [0,1] range:
index=0 scalar=0
index=1 scalar=1/3
index=2 scalar=2/3
index=3 scalar=1

To be short (and not exact), during rasterization, OpenGL will use the texture coordinate it interpolated to pick the color of the closest scalar (GL_NEAREST) or to blend the colors of the two closest scalars (GL_LINEAR).

At each vertex, you call
glTexCoord1f(displacement); // value between 0 and 1
glVertex3d(x1,y1,z1);

Thanks.

Ive looked at the examples and I assumed this:

glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 4, 0,
GL_RGB, GL_UNSIGNED_BYTE, textureArray);

you said:

When you define the array, you define the mapping from the scalar (index) to color, you define the texture:

at index=0, rgb=(1,0,0) red
at index=1, rgb=(1,1,0) yellow
at index=2, rgb=(0,1,0) green
at index=3, rgb=(0,0,1) blue

Question:

How do I set these in the array (textureArray)?
as in: textureArray= ??

You said
If (in your mind ) you divide the index by 3, you have some
scalar value in the [0,1] range:
index=0 scalar=0
index=1 scalar=1/3
index=2 scalar=2/3
index=3 scalar=1

At each vertex, you call
glTexCoord1f(displacement); // value between 0 and 1
glVertex3d(x1,y1,z1)

Question: If the index is from 0-4, how can the gltexcoord1f recognise 0 - 1 only?

UPDATE:

Hi, Ive tried coding this to understand. Please check if it is correct based on what your explanation?

{

static unsigned char textureArray =

{

``````{ 1, 0, 0 }, /* Red */
{1, 1, 0 }, /* Yellow */
{ 0, 1, 0 }, /* Green */
{ 0, 0, 1 }  /* Blue */
``````

};

glNewList(textureList = glGenLists(1), GL_COMPILE);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR);
glTexImage1D(GL_TEXTURE_1D, 0, 3, 4, 0, GL_RGB,
GL_UNSIGNED_BYTE,textureArray);
glEndList();
}

void Init (Void)
{

//What else can be included here to make sure texture works???

}

void Display(void)
{

glEnable(GL_TEXTURE_1D);
glCallList(textureList);

glTexCoord1f(displacement); // value between 0 and 1
glVertex3d(x1,y1,z1);

}

One error, and one note on performance that I notice:

Error: You need a glEnable(GL_TEXTURE_1D) before your calls to glTexParamateri()

Performance Note: You only need to place your texture setup in a display list if you will lose that state due to some other rendering function. Otherwise you can just set it once.

Otherwise, that looks good to me.

I’ve incorporated the codes into my program and i can’t see the coloured texture…everything is black now Maybe it has something to do with

i) i declare textureList as GLuint. Is that correct|??

GLuint textureList;

ii) my previous settings as well… please check the init setting below:

``````  glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glClearColor(1,1,1,1);
glMatrixMode(GL_MODELVIEW);
glLightfv(GL_LIGHT0, GL_POSITION, direction);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
GLfloat mat_d[] = {0.1, 0.5, 0.8, 1.0};
GLfloat mat_s[] = {0.1, 0.1, 0.1, 0.1 };
GLfloat low_sh[] = {5.0};
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_d);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_s);
glMaterialfv(GL_FRONT, GL_SHININESS, low_sh);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_1D);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_DECAL);

glPolygonMode(GL_FRONT, GL_FILL);
glEnable(GL_NORMALIZE);
``````

SOS. Thanks

Ah, you’re using lighting too… I haven’t used fixed-function lighting since I was first learning OpenGL. There are some specific material settings that are needed to make it all work right, IIRC. Someone with more fixed-function experience than I have would know them.

you set this :
glEnable(GL_COLOR_MATERIAL);
It means the material used will be the one defined by the glColor* calls. In your case, you do not want this.
Replace with this:
glDisable(GL_COLOR_MATERIAL);
and it should be better.

how to draw a sequence of circles whose centres are located along the circumference of other circle

Shreyu,