It looks like you're new here. If you want to get involved, click one of these buttons!

- 141.2K All Categories
- 103.9K Programming Languages
- 6.5K Assembler Developer
- 1.9K Basic
- 40K C and C++
- 2.9K C#
- 7.9K Delphi and Kylix
- 4 Haskell
- 9.7K Java
- 4.1K Pascal
- 1.3K Perl
- 2K PHP
- 552 Python
- 37 Ruby
- 4.4K VB.NET
- 1.6K VBA
- 20.9K Visual Basic
- 2.6K Game programming
- 317 Console programming
- 93 DirectX Game dev
- 1 Minecraft
- 112 Newbie Game Programmers
- 2 Oculus Rift
- 9K Applications
- 1.8K Computer Graphics
- 746 Computer Hardware
- 3.5K Database & SQL
- 535 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 258 XML Development
- 3.3K Classifieds
- 199 Co-operative Projects
- 199 For sale
- 190 FreeLance Software City
- 1.9K Jobs Available
- 605 Jobs Wanted
- 212 Wanted
- 2.9K Microsoft .NET
- 1.8K ASP.NET
- 1.1K .NET General
- 3.4K Miscellaneous
- 7 Join the Team
- 356 Comments on this site
- 71 Computer Emulators
- 2.1K General programming
- 187 New programming languages
- 640 Off topic board
- 226 Mobile & Wireless
- 98 Android
- 126 Palm Pilot
- 340 Multimedia
- 156 Demo programming
- 184 MP3 programming
- Bash scripts
- 27 Cloud Computing
- 53 FreeBSD
- 1.7K LINUX programming
- 371 MS-DOS
- Shell scripting
- 321 Windows CE & Pocket PC
- 4.1K Windows programming
- 944 Software Development
- 417 Algorithms
- 68 Object Orientation
- 92 Project Management
- 95 Quality & Testing
- 271 Security
- 7.7K WEB-Development
- 1.8K Active Server Pages
- 62 AJAX
- 6 Bootstrap Themes
- 55 CGI Development
- 28 ColdFusion
- 224 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 2.2K JavaScript
- 37 JQuery
- 309 WEB Servers
- 157 WEB-Services / SOAP

Sephiroth
Fayetteville, NC, USAMember Posts: **1,035** ✭

in 3D Graphics

OK, I have a simple clipping function up that also sets values based on where the vertex is based on the player's view. Now I can get z (the distance form the player) and y (above/below the player) easily, but how do I calculate x? I also found out that i cannot use indirection floating point values, so how can I point x, y, and z in the function to the memory location of the three actual variables being clipped and drawn?

[code]

bool CheckClipPlane(float x, float y, float z)

{

int distance;

distance = sqrt(((x - player.xpos) * (x - player.xpos)) + ((y - player.ypos) * (y - player.ypos)) + ((z - player.zpos) * (z - player.zpos)));

if(distance > 512)

return false;

if(x < (player.xpos + cos((player.heading - 45) * pi / 180.0) * distance) || z < (player.zpos - sin((player.heading - 45) * pi / 180.0) * distance))

return false;

if(x > (player.xpos + cos((player.heading + 45) * pi / 180.0) * distance) || z > (player.zpos + sin((player.heading + 45) * pi / 180.0) * distance))

return false;

//Now set x y z to how they appear in the FOV

z = distance; //How far out the vertex is

y -= player.ypos; //How far above or below the player the vertex is

// x = ?; //How do you do x???

return true;

}

[/code]

Now you would call the function with say "if(CheckClipPlane(x_m, y_m, z_m))". Now if it is too far to be seen, or the vertex is outside the player's FOV, it will return false and go on to examine the next triangle. If the vertex IS in the FOV, I need to have x_m, y_m, and z_m modified so that when I send the data to OpenGL it'll draw it as if the player really was moving around. Thanks for the help!

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

[code]

bool CheckClipPlane(float x, float y, float z)

{

int distance;

distance = sqrt(((x - player.xpos) * (x - player.xpos)) + ((y - player.ypos) * (y - player.ypos)) + ((z - player.zpos) * (z - player.zpos)));

if(distance > 512)

return false;

if(x < (player.xpos + cos((player.heading - 45) * pi / 180.0) * distance) || z < (player.zpos - sin((player.heading - 45) * pi / 180.0) * distance))

return false;

if(x > (player.xpos + cos((player.heading + 45) * pi / 180.0) * distance) || z > (player.zpos + sin((player.heading + 45) * pi / 180.0) * distance))

return false;

//Now set x y z to how they appear in the FOV

z = distance; //How far out the vertex is

y -= player.ypos; //How far above or below the player the vertex is

// x = ?; //How do you do x???

return true;

}

[/code]

Now you would call the function with say "if(CheckClipPlane(x_m, y_m, z_m))". Now if it is too far to be seen, or the vertex is outside the player's FOV, it will return false and go on to examine the next triangle. If the vertex IS in the FOV, I need to have x_m, y_m, and z_m modified so that when I send the data to OpenGL it'll draw it as if the player really was moving around. Thanks for the help!

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

Terms of use / Privacy statement / Publisher: Lars Hagelin

Programmers Heaven articles / Programmers Heaven files / Programmers Heaven uploaded content / Programmers Heaven C Sharp ebook / Operated by CommunityHeaven

© 1997-2017 Programmersheaven.com - All rights reserved.

## Comments

7: [code]

: bool CheckClipPlane(float x, float y, float z)

: {

: int distance;

:

: distance = sqrt(((x - player.xpos) * (x - player.xpos)) + ((y - player.ypos) * (y - player.ypos)) + ((z - player.zpos) * (z - player.zpos)));

:

: if(distance > 512)

: return false;

:

: if(x < (player.xpos + cos((player.heading - 45) * pi / 180.0) * distance) || z < (player.zpos - sin((player.heading - 45) * pi / 180.0) * distance))

: return false;

:

: if(x > (player.xpos + cos((player.heading + 45) * pi / 180.0) * distance) || z > (player.zpos + sin((player.heading + 45) * pi / 180.0) * distance))

: return false;

:

: //Now set x y z to how they appear in the FOV

: z = distance; //How far out the vertex is

: y -= player.ypos; //How far above or below the player the vertex is

: // x = ?; //How do you do x???

:

: return true;

: }

: [/code]

: Now you would call the function with say "if(CheckClipPlane(x_m, y_m, z_m))". Now if it is too far to be seen, or the vertex is outside the player's FOV, it will return false and go on to examine the next triangle. If the vertex IS in the FOV, I need to have x_m, y_m, and z_m modified so that when I send the data to OpenGL it'll draw it as if the player really was moving around. Thanks for the help!

: -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

:

If you're planning to do something serious that is not a good approach.Matrices is what you should use(almost every 3d game uses them).The position and orientation of an object in 3d space can be represented either by a 3x3 matrix and a position vector, or a 4x4 matrix(last row it's the position vector). I've chosen the 3x3 matrix and the pos. vector.The rows of the matrix represent in order: the RIGHT vector, the UP vector and the last row is the FORWARD vector. But keep in mind that theese vector's must be normalized(ie. their length must be 1) and there must be 90 deg between them.Now let's say the camera orientation matrix is M ,the camera pos is P ,the world vertex is V and the camera space vertex(the one you need to find out) is cV.You'll have:

cV.x=M.RIGHT*(V-P)

cV.y=M.UP*(V-P)

cV.z=M.FORWARD*(V-P)

where v1*v2 =v1.x*v2.x+v1.y*v2.y+v1.z*v2.z , v1, v2 vectors

And that's all folks, I hope I didn't write this for nothing

1,035✭: cV.x=M.RIGHT*(V-P)

: cV.y=M.UP*(V-P)

: cV.z=M.FORWARD*(V-P)

: where v1*v2 =v1.x*v2.x+v1.y*v2.y+v1.z*v2.z , v1, v2 vectors

: And that's all folks, I hope I didn't write this for nothing

:

Hey I REALLY appreciate the info. I knew I had to use matrices somehow but wasn't sure exactly how they were used. However, i am still not clear on a few things. What do you mean by "normalized"? Also, what's with the 90deg deal? You mean 90deg between the traingle's vertices? And finally, is there any advantage to using a 4x4 or 3x3 matrix? I mean, is one faster than the other? I would like my engine to run on slightly older machines (hopefully down to p2/300) because a lot of people I know still have 200mhz-400mhz machines which are relatively new, but technology has just sky-rocketed this year! Thanks again for the help and I'll see what I can do with what I have here. Oh and if you could direct me to a site where I can actually see some source using the matrices or show me some yourself I would be indebted to you.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

71,035✭:

The tutorial showed me basically what I know, but I am still unclear on how to get what goes into the matrix. After that, what do I do with the matrix? I am a visual learner and math was my weakest subject in HS and now in college. I took algebra three times in HS, although i later found out that one of the first two teachers I had was fired due to lack of her own knowledge in the subject... haha! Is there a site where I can actually [b]see[/b] some of this source? I have looked for this "Frog Engine" breifly but cannot seem to find it. Oh, and your engine isn't half bad! I've figured out how to draw polys/objects and rotate, zoom, and flip them, but the 3D first person perspective is still throwing me. Do you yourself have any tutorials on how to do all this that show some of the actual calculations? Thanks for your help, E_on, I REALLY appreciate it.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

1,035✭x=10

y=5

z=7

sqrt(x * x + y * y + z * z) = 13.something

The only way i can get that formula to work is to plug 0's and 1's into the x, y, and z values. And as much reading as I have done (almost two and a half hours), I am not any closer to figuring this out. A lot of people can write tutorials, but I need to SEE the code to understand it. One other thing is that I don't think I've seen anything on calculating what's in view. If this math is the actual drawing stuff, i do NOT need it. I simply pass 3 vertices to OpenGL and IT z-buffers and draws them. All I need to do is find out what vertices are in the player's FOV to send to OpenGL. Here is how this works:

[code]

glBegin(GL_TRIANGLES);

glNormal3f(0.0f, 0.0f, 1.0f);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(5, 5, 5);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(0, 0, 5);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3f(10, 0, 5);

glEnd();

[/code]

This example will draw a triangle with the top being at 5, 5, 5, the left bottom being at 0, 0, 5, and the right bottom being at 10, 0, 5. As you can see, all I need are the traingles that are in the player's sight, no matter what angle he is looking, and the proper degree of rotation to send to glRotate3f(). Then glRotate3f can properly rotate the vertices and I can send that data to glVertex3f! How do I do that? Thanks for the help, E_on.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

7x=10

y=5

z=7

sqrt(x * x + y * y + z * z) = sqrt(174) = 13.something

1 / 13.some = 0.076...

x*0.076...=0.76...

y*0.076...=0.381...

z*0.076...=0.533...

and now we have sqrt(x * x + y * y + z * z) = sqrt(1) = 1

I'm sorry you had such "untalented" math teachers. I don't know any OpenGL stuff so I can't help you with it.

1,035✭: x=10

: y=5

: z=7

: sqrt(x * x + y * y + z * z) = sqrt(174) = 13.something

: 1 / 13.some = 0.076...

: x*0.076...=0.76...

: y*0.076...=0.381...

: z*0.076...=0.533...

: and now we have sqrt(x * x + y * y + z * z) = sqrt(1) = 1

: I'm sorry you had such "untalented" math teachers. I don't know any OpenGL stuff so I can't help you with it.

:

Ah so I had to multiply the results by the coordinates! That's why I kept getting all kinds of screwy errors. As for rotation, I've figured out how to rotate stuff easily. Now with this maybe I can also make it relative to the camera. Thanks for all of your help E_on, I owe ya' one.