Author Topic: Triangle multicolored in OpenGl (Rosetta code)  (Read 4557 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Triangle multicolored in OpenGl (Rosetta code)
« on: April 12, 2021, 05:19:19 pm »
Hi QB64coders
another task for Rosetta Code in QB64

Code: QB64: [Select]
  1. 'Task
  2. 'Display a smooth shaded triangle with OpenGL.
  3. Screen _NewImage(600, 600, 32)
  4. glInput = 0
  5.  
  6.  
  7. Sub _GL ()
  8.     If glInit = 0 Then
  9.         _glViewport 1, 1, 600, 600
  10.         _glClearColor 0, 0, 0, 1
  11.         glInit = -1
  12.     End If
  13.     _glClear _GL_COLOR_BUFFER_BIT
  14.     _glBegin _GL_TRIANGLES
  15.     _glColor4f 1, 0, 0, 1
  16.     _glVertex2f -1, -1
  17.     _glColor4f 0, 1, 0, 1
  18.     _glVertex2f 0, 0
  19.     _glColor4f 0, 0, 1, 1
  20.     _glVertex2f 1, -1
  21.     _glEnd
Programming isn't difficult, only it's  consuming time and coffee

Offline AndyA

  • Newbie
  • Posts: 73
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #1 on: April 12, 2021, 05:47:09 pm »
The Rosetta Code task name is 'OpenGL'.   ( https://rosettacode.org/wiki/OpenGL)

Had a hard time finding what it is we're supposed to be looking for. Did you already submit the code to Rosetta Code?  I didn't see a 'QB64' entry.

I dunno, maybe a SLEEP: SYSTEM at the end instead DO:WHILE loop, to keep CPU usage to a minimum.

By the way, nice, concise, clean code for the triangle.


Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #2 on: April 12, 2021, 07:26:29 pm »
Hi AndyA
thanks to evaluate my task.

Yes maybe better to post the link of the task! I'll do next time.

Quote
Did you already submit the code to Rosetta Code?  I didn't see a 'QB64' entry
I've posted before here to show the task to QB64 friends. I'll post soon if there is no better code to show.
Programming isn't difficult, only it's  consuming time and coffee

Offline AndyA

  • Newbie
  • Posts: 73
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #3 on: April 12, 2021, 09:43:00 pm »
TempodiBasic,

Just a silly question, as I know nothing about OpenGL, is there a way to make the triangle cover the whole 600x600 screen?

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #4 on: April 13, 2021, 06:02:21 pm »
@AndyA
the OpenGl graphic system has its own cohordinates and its primitive graphic functions....
the range of Y cohordinates is -1(bottom) +1 top --> the Y increases from bottom to top while in the standard graphic it goes from top to bottom
the range of x cohordinates is -1 (left) +1(right)
the center of the screen is 0,0
as you can see here.
https://raw.githubusercontent.com/AshishKingdom/OpenGL-Tutorials/gh-pages/images/first-triangle/coordinate_system.png
A first good tutorial is that of Ashish at this web address https://ashishkingdom.github.io/OpenGL-Tutorials/

A short answer is  change the 2nd vertex2f from 0,0 to 0,1   (vertex2f x,y)
Programming isn't difficult, only it's  consuming time and coffee

Offline AndyA

  • Newbie
  • Posts: 73
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #5 on: April 13, 2021, 07:25:59 pm »
Yep, that's what I was trying to figure out. It's just like the X/Y axis I learned in school vs. the computer screen coordinates.

Thanks for the solution and the link to Ashish's OpenGL tutorial page.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #6 on: April 14, 2021, 01:51:11 pm »
It would be nice to work with any 3 given points (that fit on screen) and any 3 colors for a Rosetta post.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #7 on: April 15, 2021, 06:11:57 pm »
@bplus
IMHO you can have two way to got this....

the first is simpler
make at random the 3 vertex cohordinates in the range of -1 to +1 for the X and Y
 make at random the RGBA components of color in the range of 0 to +1 (0-255)
and you got it
as here
Code: QB64: [Select]
  1. 'Task
  2. 'Display a smooth shaded triangle with OpenGL changing vertex's values
  3. ' and color's values.
  4. Screen _NewImage(600, 600, 32)
  5. Dim Shared glInit As Integer, GlNew As Integer
  6. glInput = 0
  7.     If GlNew > 100 Then GlNew = 0 Else GlNew = GlNew + 1
  8.  
  9.  
  10. Sub _GL ()
  11.     Static r1, r2, r3, g1, g2, g3, b1, b2, b3, x1, x2, x3, y1, y2, y3
  12.     If glInit = 0 Then
  13.         _glViewport 1, 1, 600, 600
  14.         _glClearColor 0, 0, 0, 1
  15.         glInit = -1
  16.     End If
  17.     _glClear _GL_COLOR_BUFFER_BIT
  18.  
  19.     If GlNew = 1 Then
  20.         r1 = Rnd * 1.0
  21.         r2 = Rnd * 1.0
  22.         r3 = Rnd * 1.0
  23.         g1 = Rnd * 1.0
  24.         g2 = Rnd * 1.0
  25.         g3 = Rnd * 1.0
  26.         b1 = Rnd * 1.0
  27.         b2 = Rnd * 1.0
  28.         b3 = Rnd * 1.0
  29.  
  30.         x1 = (Rnd * 2.0) - 1
  31.         y1 = (Rnd * 2.0) - 1
  32.         x2 = (Rnd * 2.0) - 1
  33.         y2 = (Rnd * 2.0) - 1
  34.         x3 = (Rnd * 2.0) - 1
  35.         y3 = (Rnd * 2.0) - 1
  36.     End If
  37.  
  38.     _glBegin _GL_TRIANGLES
  39.     _glColor4f r1, g1, b1, 1
  40.     _glVertex2f x1, y1
  41.     _glColor4f r2, g2, b2, 1
  42.     _glVertex2f x2, y2
  43.     _glColor4f r3, g3, b3, 1
  44.     _glVertex2f x3, y3
  45.     _glEnd

the second way is to port the GL part of code from this demo in which I use a very fine your program to show the other two graphic output : normal or starndard graphic an the OpenGl graphic while in your original work you have greatly mastered the Maptriangles mode. https://www.qb64.org/forum/index.php?topic=3334.msg126287#msg126287


Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #8 on: April 15, 2021, 07:16:14 pm »
@TempodiBasic Good changes! Could the switching to a new triangle be smoothed out maybe with _delay or _limit?

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #9 on: April 16, 2021, 03:59:57 am »
@bplus
you're right! Using _delay can do a smoother changing of the aspect of the triangle!
here a way to get this:
Code: QB64: [Select]
  1. 'Task
  2. 'Display a smooth shaded triangle with OpenGL changing vertex's values
  3. ' and color's values.
  4. Screen _NewImage(600, 600, 32)
  5. Dim Shared glInit As Integer, GlNew As Integer
  6. glInput = 0
  7.     If GlNew = 100 Then GlNew = 0 Else GlNew = GlNew + 1
  8.     _Delay .002
  9.  
  10.  
  11. Sub _GL ()
  12.     Static r1, r2, r3, g1, g2, g3, b1, b2, b3, x1, x2, x3, y1, y2, y3
  13.     If glInit = 0 Then
  14.         _glViewport 1, 1, 600, 600
  15.         _glClearColor 0, 0, 0, 1
  16.         glInit = -1
  17.     End If
  18.     _glClear _GL_COLOR_BUFFER_BIT
  19.  
  20.     If GlNew = 1 Then
  21.         r1 = Rnd * 1.0
  22.         r2 = Rnd * 1.0
  23.         r3 = Rnd * 1.0
  24.         g1 = Rnd * 1.0
  25.         g2 = Rnd * 1.0
  26.         g3 = Rnd * 1.0
  27.         b1 = Rnd * 1.0
  28.         b2 = Rnd * 1.0
  29.         b3 = Rnd * 1.0
  30.  
  31.         x1 = (Rnd * 2.0) - 1
  32.         y1 = (Rnd * 2.0) - 1
  33.         x2 = (Rnd * 2.0) - 1
  34.         y2 = (Rnd * 2.0) - 1
  35.         x3 = (Rnd * 2.0) - 1
  36.         y3 = (Rnd * 2.0) - 1
  37.     End If
  38.  
  39.     _glBegin _GL_TRIANGLES
  40.     _glColor4f r1, g1, b1, 1
  41.     _glVertex2f x1, y1
  42.     _glColor4f r2, g2, b2, 1
  43.     _glVertex2f x2, y2
  44.     _glColor4f r3, g3, b3, 1
  45.     _glVertex2f x3, y3
  46.     _glEnd
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #10 on: April 16, 2021, 01:24:47 pm »
Hmm... that GL stuff seems to be working under it's own clock???

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Triangle multicolored in OpenGl (Rosetta code)
« Reply #11 on: April 17, 2021, 05:40:07 pm »
@bplus
in QB64 Opengl is a parallel process to the ordinary code. However the GPU is another chip parallel to the CPU.
Programming isn't difficult, only it's  consuming time and coffee