views:

449

answers:

2

This is probably a silly mistake but I cant see it?! I have classes defining geometry and classes that render that geometry. Right now it is basic triangles and colours for each vertex.

Here is the code defining said geometry objects data:

CGeometry* g = new CGeometry();
g->vertexes = new double[3*3];

g->vertexes[0] = 0;
g->vertexes[1] = 0;
g->vertexes[2] = 0;

g->vertexes[3] = 100;
g->vertexes[4] = 100;
g->vertexes[5] = 0;

g->vertexes[6] = 100;
g->vertexes[7] = 0;
g->vertexes[8] = 0;

g->colors = new double[12];

g->colors[0] = 1;
g->colors[1] = 1;
g->colors[2] = 0;
g->colors[3] = 1;

g->colors[4] = 1;
g->colors[5] = 0;
g->colors[6] = 1;
g->colors[7] = 0;

g->colors[8] = 0;
g->colors[9] = 1;
g->colors[10] = 1;
g->colors[11] = 0;

And here is the code that renders said data:

CGeometry* g = object->geometry;

int j = object->endIndex - object->startIndex;
double* vertexes = g->vertexes;
double* colors = g->colors;

glBegin(GL_TRIANGLES);
{
 for(int i = 0; i < j; i++){
  int coord = object->startIndex+i;
  int colorind = coord*4;

  double r,g,b,a;
  r = colors[colorind];
  g = colors[colorind+1];
  b = colors[colorind+2];
  a = colors[colorind+3];

  glColor4d( r,g,b,a);
  glVertex3d( vertexes[coord*3],
     vertexes[coord*3+1],
     vertexes[coord*3+2]);
 }
}
glEnd();

Yet regardless of what I put my triangle is always yellow, or the value of the first color in the colors array. I have gone into the debugger and checked the values on each individual loop iteration and the values of the r g b and a variables do infact change accordingly and are not always yellow, yet the result is a yellow triangle.

Yet if I put the following nabbed from the neheGL tutorials:

glClearColor(0.1f,0.1f,0.1f,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();         // Reset The Current Modelview Matrix
//glTranslatef(1.5f,0.0f,0.0f);      // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_TRIANGLES);        // Drawing Using Triangles
 glColor3f(1.0f,0.0f,0.0f);      // Set The Color To Red
 glVertex3f( 0.0f, 1.0f, 0.0f);     // Top
 glColor3f(0.0f,1.0f,0.0f);      // Set The Color To Green
 glVertex3f(-1.0f,-1.0f, 0.0f);     // Bottom Left
 glColor3f(0.0f,0.0f,1.0f);      // Set The Color To Blue
 glVertex3f( 1.0f,-1.0f, 0.0f);     // Bottom Right
glEnd();           // Finished Drawing The Triangle
glTranslatef(160.0f,0.0f,0.0f);      // Move Right 3 Units
glColor3f(0.5f,0.5f,1.0f);       // Set The Color To Blue One Time Only
glBegin(GL_QUADS);         // Draw A Quad
 glVertex3f(-1.0f, 1.0f, 0.0f);     // Top Left
 glVertex3f( 1.0f, 1.0f, 0.0f);     // Top Right
 glVertex3f( 1.0f,-1.0f, 0.0f);     // Bottom Right
 glVertex3f(-1.0f,-1.0f, 0.0f);     // Bottom Left
glEnd();

I get a nice blended triangle with 3 colours from each vertex

+2  A: 

The alpha values of your second and third color are set to zero, so they are fully transparent. The first color has alpha=1 and is the only one that can be seen in the resulting triangle...

sth
Tom J Nowell
On top of that using glColor3d not glColor4d has no effect so alpha is not the issue.
Tom J Nowell
Hmm, it seemed to fit the problem so nicely... But I don't know OpenGL, so no idea where the real problem could be then.
sth
There's actually *glColor4d* in your code, mind posting the code that's causing the problems? Since in this case, I believe that sth is right.
arul
Besides that, you should always define your colors correctly, no matter if the alpha is currently ignored because of the function chosen to select it, it might change in the future.
arul
glColor3d glColor3f glColor4f etc make no difference.
Tom J Nowell
At the moment I have a set of structures, but the code at the moment iterates over those structures but could do so if just handed a CGeometry array as the others are purely structural: http://pastebin.com/f37eff695 which is then rendered by this method: http://pastebin.com/m56f9b4d9
Tom J Nowell
+4  A: 

Actually, I think I got it: you're only seeing a small portion of the bottom left corner of your triangle. You need to move away from it to view it entirely: your coordinates are too large.

Renaud Bompuis
indeed it was, reducing the vertex values from 100->1 resulted in the triangle I expected
Tom J Nowell