I’m going to give color coding a try but can’t find any examples of this in my redbook. I have a few questions relating to the code below. Basically, I’m still trying to solve being able to highlight each individual triangle as the mouse hovers over it.
Previous ideas on how to do this … frame rendering loop:
- clear depth/color-buffer
… I do this in my display - render color-coded
… I don’t know what this means. How is it color-coded? Before I draw the triangle, all I do is choose a unique color? - read color of pixel of mouse w/ glReadPixel()
- clear depth/color buffer
… Is all the above done in SELECT render mode? - render model.
… And then I switch to RENDER mode here?
My stuff looks like this …
public void display(GLAutoDrawable drawable) {
// ...
switch (getRenderState()) {
case ModelConstants.UPDATE: // RENDER Mode
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Projection transformation.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(-winWidth * getZoomFactor(), winWidth * getZoomFactor(),
-winHeight * getZoomFactor(), winHeight * getZoomFactor(), 100, -100);
// reset the modelview matrix
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
// Viewing transformation.
glu.gluLookAt(centroid.getX(), centroid.getY(), centroid.getMax_Z(),
centroid.getX(), centroid.getY(), centroid.getZ(),
0.0, 1.0, 0.0);
// Modeling tranformation.
arcBall.getTransform().get(transf);
gl.glMultMatrixf(transf);
// Draw the model.
{
// eventually, calls drawByRegion below
// to draw model.
drawModel(drawable);
}
break;
case ModelConstants.SELECT: // SELECT Mode
// ... handle selection and picking
Added GL_COLOR_ARRAY here.
If each triangle has its own color (all 3 vertices have the same color), doesn’t that mean I need to draw by triangle using glDrawElements? I thought that’s part of what color-coding would move away from to help speed things up? Just a bit confused here. This is my code where I draw my model and works fine. Uses selection on groups of elements, but not per element.
public void drawByRegions(GL gl, int index, int activeId, Color color) {
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_COLOR_ARRAY);
// ...
vElementsBuffer[index].rewind();
// highlights groups of elements.
gl.glLoadName(index);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, vElementsBuffer[index]);
gl.glDrawArrays(GL.GL_TRIANGLES, 0, vElementsBuffer[index].capacity() / 3);
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_COLOR_ARRAY);
return;
}