10.03.2019 - Round 1 results of our "A Love Letter For FreeBASIC" game dev competition have been published. Please be sure to check the results thread: http://games.freebasic.net/forum/index.php?topic=629.0. Don't forget that the competition is continuing with a round 2, lasting till 29th of April, 300 USD first prize. Stay tuned!

Author Topic: 3D rendering (software)  (Read 9439 times)

Brick Break

  • Forum Sage
  • *****
  • Posts: 405
    • View Profile
3D rendering (software)
« on: November 12, 2009, 08:02:54 PM »
I want to have the option to render stuff using FreeBASIC commands. I looked into it, and found a few useful formulas. Stuff like projection and rotation is easy, but I want to actually draw a scene to the screen. That means I need textured triangles. I know FreeBASIC doesn't have a command for that, so I know the textures need to be drawn to the buffer one pixel at a time. Google isn't very helpful. The closest to what I'm looking for is on Wikipedia, or at least the jumbled formula for it is.

ua is a texture coordinate, and u0 and u1 are endpoints(?) z is obviously the depth

Can anyone make sense of this? All I'm trying to do is make a complete software rendering engine on par with OpenGL.

KristopherWindsor

  • Forum Sage
  • *****
  • Posts: 363
  • The Thirsty Smiley
    • View Profile
    • Reddit/r/pics
    • Email
Re: 3D rendering (software)
« Reply #1 on: November 12, 2009, 08:55:54 PM »
Don't try to understand math or science on Wikipedia.
Without Wikipedia, I would think textbooks are poorly written.

Mitchell

  • Forum Howler
  • ****
  • Posts: 197
  • Rockin Geek
    • View Profile
    • Email
Re: 3D rendering (software)
« Reply #2 on: November 12, 2009, 09:16:09 PM »
You'll need a good sense of linear algebra and geometry for building a 3d renderer.

a is a parameter from 0 to 1.
u1 and u2 are end points (coordinate pairs). Not individual numbers. It just means you are doing the math on each component.
z is depth, of course
ua is the texture coordinate.

So to get the x component of ua, do the math on tha function, and use the x components every time you see a u.

For instance, if you had points a, b, and c in the form a = 2*b + c. Let's say b is (1,2) and c is (3,4). The x component of a is 2*1 + 3 = 5. The y component of a is 2*2+4=8. Thus your answer is (5,8) for 2*(1,2) + (3,4).
Never underestimate the destructive powers of somebody doing something new without having any clue of how to do it. :P

Mitchell

  • Forum Howler
  • ****
  • Posts: 197
  • Rockin Geek
    • View Profile
    • Email
Re: 3D rendering (software)
« Reply #3 on: November 12, 2009, 09:17:27 PM »
Don't try to understand math or science on Wikipedia.
Without Wikipedia, I would think textbooks are poorly written.

What's wrong with Wikipedia's math? Yeah, the articles more formal and advanced, but I don't see why that's poor on WP's part.
Never underestimate the destructive powers of somebody doing something new without having any clue of how to do it. :P

Brick Break

  • Forum Sage
  • *****
  • Posts: 405
    • View Profile
Re: 3D rendering (software)
« Reply #4 on: November 12, 2009, 09:40:02 PM »
Don't try to understand math or science on Wikipedia.
Without Wikipedia, I would think textbooks are poorly written.

What's wrong with Wikipedia's math? Yeah, the articles more formal and advanced, but I don't see why that's poor on WP's part.
It's poor on the Wikipedia community's part, as a form of retaliation against Wikipedia's shitty citation policy. If they didn't want everything politically correct and verifiable the articles might actually be good.

Mitchell

  • Forum Howler
  • ****
  • Posts: 197
  • Rockin Geek
    • View Profile
    • Email
Re: 3D rendering (software)
« Reply #5 on: November 12, 2009, 10:05:59 PM »
I pretty much went WTF when I first saw Wiki's math articles on bezier curves a while ago. But I got used to it after a while. Then again, I'm a math major. :P
Never underestimate the destructive powers of somebody doing something new without having any clue of how to do it. :P

Brick Break

  • Forum Sage
  • *****
  • Posts: 405
    • View Profile
Re: 3D rendering (software)
« Reply #6 on: November 14, 2009, 07:02:11 PM »
Well, Mitchell's explanation seems pretty good, only I can't even read half way through it because I know I'll never understand it. It's this kind of thing that makes me wish I were just using DarkBASIC again.

EDIT: Could someone perhaps give some example code to draw a triangle on the screen given three points and an additional three points for UV coordinates?
« Last Edit: November 14, 2009, 07:05:07 PM by Brick Break »

KristopherWindsor

  • Forum Sage
  • *****
  • Posts: 363
  • The Thirsty Smiley
    • View Profile
    • Reddit/r/pics
    • Email
Re: 3D rendering (software)
« Reply #7 on: November 16, 2009, 01:33:29 AM »
Don't try to understand math or science on Wikipedia.
Without Wikipedia, I would think textbooks are poorly written.

What's wrong with Wikipedia's math? Yeah, the articles more formal and advanced, but I don't see why that's poor on WP's part.

Each paragraph of an article may be a completely new topic. And to understand each topic / paragraph, you need to know certain things. They won't tell you if you're supposed to know said certain things from high school, or general knowledge, or some college course you haven't taken. The articles are concise so that if you don't understand a certain sentence, the entire article is worthless. The chosen variable names, etc., might be different from what you were taught in a textbook, making it extra hard to interpret.
And you might think that you can just recursively click on links within the article to reach / find / read the prerequisites for the article, but it doesn't work, because there are links to a lot of pages you don't need to read.

Also, for example, I recently tried to find out how to calculate the cross product of two 3D edges on WP, and the simple / standard formula was not on there.

Brick Break

  • Forum Sage
  • *****
  • Posts: 405
    • View Profile
Re: 3D rendering (software)
« Reply #8 on: November 16, 2009, 06:57:05 PM »
Don't try to understand math or science on Wikipedia.
Without Wikipedia, I would think textbooks are poorly written.

What's wrong with Wikipedia's math? Yeah, the articles more formal and advanced, but I don't see why that's poor on WP's part.

Each paragraph of an article may be a completely new topic. And to understand each topic / paragraph, you need to know certain things. They won't tell you if you're supposed to know said certain things from high school, or general knowledge, or some college course you haven't taken. The articles are concise so that if you don't understand a certain sentence, the entire article is worthless. The chosen variable names, etc., might be different from what you were taught in a textbook, making it extra hard to interpret.
And you might think that you can just recursively click on links within the article to reach / find / read the prerequisites for the article, but it doesn't work, because there are links to a lot of pages you don't need to read.

Also, for example, I recently tried to find out how to calculate the cross product of two 3D edges on WP, and the simple / standard formula was not on there.

So you've experimented with this stuff too? Do you know what the formulas are? Would it be possible for you to give us a textured triangle function?

KristopherWindsor

  • Forum Sage
  • *****
  • Posts: 363
  • The Thirsty Smiley
    • View Profile
    • Reddit/r/pics
    • Email
Re: 3D rendering (software)
« Reply #9 on: November 17, 2009, 05:12:38 PM »
So you've experimented with this stuff too? Do you know what the formulas are? Would it be possible for you to give us a textured triangle function?

I tried a very long time ago. Then I learned linear algebra and OpenGL. :D

Brick Break

  • Forum Sage
  • *****
  • Posts: 405
    • View Profile
Re: 3D rendering (software)
« Reply #10 on: November 17, 2009, 05:24:10 PM »
So you've experimented with this stuff too? Do you know what the formulas are? Would it be possible for you to give us a textured triangle function?

I tried a very long time ago. Then I learned linear algebra and OpenGL. :D
OpenGL is platform-specific! BASIC code is not! Out with it!

Dr_D

  • Forum Sage
  • *****
  • Posts: 257
    • Yahoo Instant Messenger - dr_davenstein
    • View Profile
    • Dr_D's apps
    • Email
Re: 3D rendering (software)
« Reply #11 on: November 26, 2009, 12:29:08 PM »
OpenGL isn't platform specific. DirectX, not that is. :p

ecxjoe

  • Novice
  • ***
  • Posts: 78
    • View Profile
    • Delta Code
Re: 3D rendering (software)
« Reply #12 on: May 02, 2010, 09:28:41 AM »
I've written a textured triangle function before. All you need to make sense of is interpolation, scaling textures onto arbitrary-sized surfaces. If you can do this with rectangles (such as sprites), then you can do it with triangles.

For a sprite (psuedo code):

Code: [Select]
scalex = renderW / spriteW: scaley = renderH / spriteH
FOR y = 0 TO renderH-1
    FOR x = 0 TO renderW-1
        PSET (x, y), sprite(x*scalex, y*scaleY)
    NEXT x
NEXT y

If you can make sense of that, then you can apply the same thing to a triangle algorithm. Though it's a little more complicated, because a triangle as varying lengths of width, so as you move down the triangle you have to recalculate a new horizontal scale for each line of the triangle you draw. Once you get that going then it's not too hard to work out the other quirks.

Mitchell

  • Forum Howler
  • ****
  • Posts: 197
  • Rockin Geek
    • View Profile
    • Email
Re: 3D rendering (software)
« Reply #13 on: May 05, 2010, 10:07:04 AM »
You may have a problem with that since interpolation of pixels across the triangle is not the same as the 3d vertex of the texture being mapped to the screen, so you can get a skew according to which direction you're rendering. ie if you draw pixels horizontally, you will be skewed horizontally and vice versa for vertically.

Wiki Link
Never underestimate the destructive powers of somebody doing something new without having any clue of how to do it. :P