GLSL heightmap coloring (OpenGL)

Hi, i recently made a heightmap, it uses a triangle strip /row, and I try to figure out how can i color the triangles one by one.
So, for example I have a color array of 9:
1
2
3
[0.0f,0.2f,0.6f //a triangles first vertex
 0.0f,0.4f,0.3f //second vertex
 0.0f,0.7f,0.2f] //third vertex 

and i want to use it to color all triangles with it.
My heightmap coords are in a vector:
 
vector<GLfloat> hmapcrds [x1,h1,z1; x2,h2,z2;...]
like this:
c1 c2 c3 c4 c5 //row 1
c6 c7 c8 c9 c10 //row 2 coordinates

index array:c6->c1->c7->c2 ...cn->c(n-rwosize)->c(n+1)->cn->c(n-rwosize+1)

i draw my elements with a VAO and drawelements/row; (i tried drawrange but didnt work)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
        ...
	glGenVertexArrays(1,&VertArrObj);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBuffer);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(GLuint)*v_heightIndexes.sie(),&v_heightIndexes[0],GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
	glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*v_heightTerrain.size(),&v_heightTerrain[0],GL_STATIC_DRAW);

	glBindVertexArray(VertArrObj);

	glEnableVertexAttribArray(0); //"vertex" attrib location
        glEnableVertexAttribArray(1); //"color" attrib location

	glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
	glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);

	glBindBuffer(GL_ARRAY_BUFFER,colorBuffer);
	glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,0);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBuffer);

	glBindVertexArray(0);
}...

void p_renderHeightMap()
{
         glBindVertexArray(VertArrObj);
	 for(unsigned long indRow=0;indRow<indexRows;indRow++)
	{glDrawElements(GL_TRIANGLE_STRIP,indexRange,GL_UNSIGNED_INT,(void*)(sizeof(unsigned long)*indexRange*indRow));
}


I think it want to index my color array too, so how can i do that it only reads the 3 colors from my array, the repeat it /vertex/triangle till the end of drawing.

The shaders work fine, i changed the map to a triangle so it applied these colors, but with the map "I see only darkness before me".
with my working shader without color "in" - height coloring: http://kepfeltoltes.hu/view/130827/1094341850Untitled_www.kepfeltoltes.hu_.png

(I also want to do texturing per triangle.)
Last edited on
how can i do that it only reads the 3 colors from my array, the repeat it /vertex/triangle till the end of drawing.


There's some confusion here. The color is part of the vertex. You can't use the color from one vertex in another vertex... just as you can't use the position of one vertex in another vertex.

If you want your vertexes to have a color attribute, then they must all have color attributes. You cannot have them "share" a single color attribute.

You are using the term "vertex" as if it were synonymous with "position", which is not the case. A vertex is a collection of information to represent a single point. A "position" is just one part of the information is contains.


You might be able to create a 1D texture with color lookups that you can use to avoid having to add a color attribute to your vertexes... but that would be more trouble than it's worth.

but with the map "I see only darkness before me".


Hard to say what's going wrong without also seeing the shader code.
Thanks for the reply, now I understand vertex attributes, I read about this and also I figured out how to texture without another VBO, in the shader using the vertex position attrib divided by the tiling factor.
Topic archived. No new replies allowed.