Results 1 to 6 of 6

Thread: drawing triangle with gradient based on vertices

  1. #1
    Join Date
    Apr 2011
    Posts
    58
    Thanks
    1

    Default drawing triangle with gradient based on vertices

    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!

  2. #2
    Join Date
    Aug 2011
    Location
    Seoul, Korea
    Posts
    46
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawing triangle with gradient based on vertices

    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

  3. #3
    Join Date
    Apr 2011
    Posts
    58
    Thanks
    1

    Default Re: drawing triangle with gradient based on vertices

    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?

  4. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: drawing triangle with gradient based on vertices

    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?

  5. #5
    Join Date
    Aug 2011
    Location
    Seoul, Korea
    Posts
    46
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawing triangle with gradient based on vertices

    Quote Originally Posted by marc2050 View Post
    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

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: drawing triangle with gradient based on vertices

    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.

Similar Threads

  1. Replies: 2
    Last Post: 30th December 2010, 17:03
  2. Replies: 1
    Last Post: 24th December 2010, 00:15
  3. Replies: 3
    Last Post: 12th May 2010, 13:11
  4. Drawing a line with a linear gradient effect
    By toutarrive in forum Qt Programming
    Replies: 4
    Last Post: 15th February 2010, 07:42
  5. How to draw fast rectangle/triangle/circle
    By nileshsince1980 in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2008, 11:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.