1

I'm new to openGL. Now,I want to draw a triangle in 3D with different color patterns filled in it. But I'm not getting the colors with unique layers. All the color are getting interpolated.

Also below I have attached the images for the expected output and obtained output for the above said process.

If I used glShadeModel (GL_SMOOTH);

The colors are getting interpolated as per the obtained output image.

If glShadeModel (GL_FLAT);

the color what I'm giving for the last vertex for example say Blue color, the whole triangle is filled with the blue color.

Can anyone please help me to sort out this issue . It will very helpful for me if I got any suggestions. Is vertex and fragment shaders are better way to sort out this problem ? I don't have enough knowledge on the vertex and fragment shaders. This is the method now I used to draw that traingle. Code:

glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3d(a1,b1,c1);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3d(a2,b2,c2);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3d(a3,b3,c3);
glEnd();
glFlush();

expected output

Obtained output

aadhithyan
  • 49
  • 10

3 Answers3

0

You have tried two different modes in OpenGL. In one of the modes, it will interpolate the color between the vertices of the polygon you draw. In the other mode, it will just use the same flat color for the entire polygon.

The reason you are getting the whole polygon filled with blue color is that you are asking OpenGL to draw a single, one-colored polygon.

Judging by your image of expected output, you will need to ask OpenGL to draw multiple one-colored polygons; one for each color. That is, you need to call glBegin/glEnd several times.

flodin
  • 5,215
  • 4
  • 26
  • 39
  • Thank You @flodin. Can you please tell me how to call glbegin and glend several times ? If you have any examples or any links , kindly share. Is it possible to draw it using primitive types like GL_POLYGON, GL_TRIANGLES, GL_TRIANGLE_FAN, instead of going for openGL ES 2.x, 3.x ?? – aadhithyan Jun 15 '18 at 07:16
  • I'm not sure what you are confused about. Surely you know how to call a function two times? Copy-paste your glBegin()/glEnd() chunk and modify it to render a second polygon where you want it. – flodin Jun 15 '18 at 14:13
  • I don't want second polygon. I just want to color a single polygon with more than 1 color as shown in expected output without using shaders. How to tell openGL to draw a triangle like that ? – aadhithyan Jun 15 '18 at 16:56
  • I understand, but you will need to construct it from multiple polygons, each with a solid color, that attach together and look like one polygon. – flodin Jun 16 '18 at 19:00
0

In terms of future-proofing new development, I would definitely consider going with OpenGL ES 2.x onwards and use shaders. OpenGL ES 1.x is effectively a dead-end technology, with some large limitations which the newer APIs solve.

In fragment shaders you can annotate input varyings with the flat keyword, which will toggle using the vertex value from the provoking vertex rather than interpolating from all contributing vertices. However this requires OpenGL ES 3.x onwards - it was not supported in OpenGL ES 2.x.

In OpenGL ES 2.x and older I think you effectively need to duplicate vertices - i.e. if all three inputs have the same value then the interpolation is effectively a no-op. This is obviously going to measurably increase the vertex processing cost.

solidpixel
  • 10,688
  • 1
  • 20
  • 33
  • Thank you for your valuable suggestions.If possible, can you please share some code or any useful links which will guide me in a proper manner to learn the OpenGL ES 3.x ? @solidpixel – aadhithyan Jun 15 '18 at 07:14
  • It's a huge topic, but just search for OpenGL ES 2.x or 3.x tutorials. There are plenty on the web. The "OpenGL ES 3.0 Programming Guide" is good too if you prefer a paper reference. – solidpixel Jun 15 '18 at 09:01
0

I have solved this problem and it's working fine for me. I haven't used any shader program.Please refer this question for answer. Fastest Method to compute Linear interpolation between 2 points in QT using OpenGL

aadhithyan
  • 49
  • 10
  • You could probably explain a few things here itself. – Harsh Wardhan Dec 19 '18 at 04:45
  • @HarshWardhan I have done the linear interpolation between the vertices and separated the intermediate points based on the colors. After separating, I have copied every points to vertex and color buffers and then used them to draw on screen. Since I'm dealing with more triangles, I have done this using **retained mode** in openGL for better performance instead of using immediate mode. – aadhithyan Dec 19 '18 at 06:03