PDA

View Full Version : drawing triangle with gradient based on vertices



marc2050
4th August 2011, 06:22
Hi.

I am trying to draw a triangle.
But the color of the triangle is based on the vertices.
There are 3 vertices in the triangle.
Each vertex has different color. e.g. vtx 1 = green, vtx 2 = red, vtx 3 = blue

Now, how do I fill the triangle with gradient color that comes from the color of these vertices?
(I know you can do something like in opengl, graphics programming using vertex shading or something like that, but I'm not into graphics but just want simple effect)

The idea is actually to form a picture that made up of joined triangles and color the picture as a whole so that it is a smooth image. (Again I know this is something from computer graphics. but I really not into that and have no time to study those graphics algorithm. just simple quick fix.)

Thanks!

Dong Back Kim
4th August 2011, 08:44
Hi there,

I'm a novice like you so can't really answer your question with details. However, here I have an idea how this can be done with out using OpenGL (you know with OpenGL this can be done within 5~10 minute).

In Qt, there is no gradient class supports what you want to achieve at once. However, since there is QLinearGradient, I guess you can use it three times for the three different color component against the same shape and perform logical composition of the three triangles you should be able to smoothly shaded RGB triangle. There is a good example in Qt Assistant. Look up "Image Composition Example".

Hope you get some hints =)

Regards,
Dong Back Kim

marc2050
4th August 2011, 09:07
Thanks.

I tried qlineargradient. I can use it for rect, and etc. But for triangle, it is tricky since there are 3 points to draw the lineargradient. How does it interpolate? I couldnt figure that out and cannot find any example (include the image composition example) nor anywhere else that show how this can be applied to triangle.
Any suggestion?

Cruz
4th August 2011, 09:14
QLinearGradient takes only two points. What happens if you use one of the triangle points as start, the center of the opposing line as stop and the set the gradient to the brush and draw the triangle?

Also, in your OP you said you want the whole image to be smooth. Does this include color smoothness? Because then why don't you apply a gradient to the entire background and just draw a triangle pattern on top of it?

Dong Back Kim
4th August 2011, 12:32
Thanks.

I tried qlineargradient. I can use it for rect, and etc. But for triangle, it is tricky since there are 3 points to draw the lineargradient. How does it interpolate? I couldnt figure that out and cannot find any example (include the image composition example) nor anywhere else that show how this can be applied to triangle.
Any suggestion?

Yeah it seems it would be bit tricky with QLinearGradient. Simple interpolation might be adequate with little bit of trigonometric calculation. (I know you want something simple but what you are asking isn't quite simple...)

1. Define the three points of the triangle
2. Prepare big enough rectangle matrix which would include the triangle you want to make. In this matrix, you will fill the calculation results of the interpolation.
3. Lets start red first. From the red point to towards the line of green and blue points (I assumed you are making RGB triangle), do the red value interpolation or red component linear gradient.
4. do the same thing for blue and green against the respect opposite lines.

Since each color component is linearly interpolated in each steps of 3 and 4, now you will have color smooth value within the triangle. I know this sounds very confusing... bit hard to explain without pen and paper =P

Hope this could help you anyhow.

Regards,
Dong Back Kim

SixDegrees
4th August 2011, 12:45
First, QLinearGradient is a one-dimensional gradient. You cannot use it to perform two-dimensional gradients, which is at least part of what you're asking. You'll have to implement such a thing yourself. There are a lot of ways to approach this; I have no idea which would be the best in your case.

Second, any sort of interpolation can only operate within the space enclosed by the interpolation points. That's the definition of interpolation. I have no idea what you mean when you're trying to apply this across several triangles, and I suspect you need to give more careful thought to what you are actually trying to achieve before proceeding.