Jump to content
  • Advertisement
Sign in to follow this  
LorinAtzberger

OpenGL Some issues from trying to develop a small engine

This topic is 3441 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I'm trying to develop a small 3D engine in C# using OpenGL. The issues I write here are not API specific but I just tough it would be good to say what I use. First of all I developed the engine around the Entity ideea. A mesh is a entity, a Pivot is a entity and Camera is a Entity. These are all I have up till now. They have 3 vectors: Position, Rotation and Scale(used only on meshes). I managed to rotate the camera using the mouse and it works well. I'm trying to now to make a Move method that takes a vector as a parameter that will move the entity in the direction it's facing. For instance I'd like to use Move(new GVec3(1,0,0)) to move the camera forward. Right now if I call this it moves my camera but not twards were it's facing. I tried using some matrix transformations but I took those from an example that inverted the z parameter and I'm not sure where in the function should I modify the sign. I think it would be better to talk with someone here and clarify this before I continue. Next issue is that I'm trying to do some raycast picking ( I need more informations such as x,y,z of the interersection, the triangle, the normal of the triangle, the surface etc so that's why I'm not using GL_SELECT). I drew some lines to show me visually how the projected line is positioned. It works well when the camera is at 0,0,0 but after that when I move it backwards the point I click on the screen is no longer the point it shows me. The point it shows me is more and more closer to the center of the screen. Not sure where I go wrong here. Also if I'm going to test everything with raycast then I guess I'll have to use each objects position and rotation and even scale when doing the test. However I have no ideea on how to modify the data to use these informations. I tried looking at some 3D engine's source code but they are very complex and I don't understand a lot from what they do. Could anyone be so nice to help me with these things? Thank you! EDIT: Oh I also know C++ VB and BlitzMax so you can basically write code in any language and I'll understand.

Share this post


Link to post
Share on other sites
Advertisement

public void Move(GVec3 p)
{
//Pos = Pos + p;
GMatrix m = new GMatrix();
m.LoadIdentity();
m.Rotate(Rot);
m.Translate(p);
Pos.x += m.grid[3, 0];
Pos.y += m.grid[3, 1];
Pos.z += m.grid[3, 2];
}


This should in theory do the trick right?
Somehow it doesn't. It only works when the rotation is 0,0,0


public void Translate(GVec3 v)
{
grid[3, 0] = grid[0, 0] * v.x + grid[1, 0] * v.y + grid[2, 0] * v.z + grid[3, 0];
grid[3, 1] = grid[0, 1] * v.x + grid[1, 1] * v.y + grid[2, 1] * v.z + grid[3, 1];
grid[3, 2] = grid[0, 2] * v.x + grid[1, 2] * v.y + grid[2, 2] * v.z + grid[3, 2];
}
public void Scale(GVec3 v)
{
grid[0, 0] = grid[0, 0] * v.x;
grid[0, 1] = grid[0, 1] * v.x;
grid[0, 2] = grid[0, 2] * v.x;

grid[1, 0] = grid[1, 0] * v.y;
grid[1, 1] = grid[1, 1] * v.y;
grid[1, 2] = grid[1, 2] * v.y;

grid[2, 0] = grid[2, 0] * v.z;
grid[2, 1] = grid[2, 1] * v.z;
grid[2, 2] = grid[2, 2] * v.z;
}
public void Rotate(GVec3 v)
{
float cos_ang,sin_ang;


cos_ang=(float)Math.Cos(v.y);
sin_ang = (float)Math.Sin(v.y);

float m00 = grid[0,0]*cos_ang + grid[2,0]*-sin_ang;
float m01 = grid[0,1]*cos_ang + grid[2,1]*-sin_ang;
float m02 = grid[0,2]*cos_ang + grid[2,2]*-sin_ang;

grid[2,0] = grid[0,0]*sin_ang + grid[2,0]*cos_ang;
grid[2,1] = grid[0,1]*sin_ang + grid[2,1]*cos_ang;
grid[2,2] = grid[0,2]*sin_ang + grid[2,2]*cos_ang;

grid[0,0]=m00;
grid[0,1]=m01;
grid[0,2]=m02;

cos_ang = (float)Math.Cos(v.x);
sin_ang = (float)Math.Sin(v.x);

float m10 = grid[1,0]*cos_ang + grid[2,0]*sin_ang;
float m11 = grid[1,1]*cos_ang + grid[2,1]*sin_ang;
float m12 = grid[1,2]*cos_ang + grid[2,2]*sin_ang;

grid[2,0] = grid[1,0]*-sin_ang + grid[2,0]*cos_ang;
grid[2,1] = grid[1,1]*-sin_ang + grid[2,1]*cos_ang;
grid[2,2] = grid[1,2]*-sin_ang + grid[2,2]*cos_ang;

grid[1,0]=m10;
grid[1,1]=m11;
grid[1,2]=m12;

cos_ang = (float)Math.Cos(v.z);
sin_ang = (float)Math.Sin(v.z);

m00 = grid[0,0]*cos_ang + grid[1,0]*sin_ang;
m01 = grid[0,1]*cos_ang + grid[1,1]*sin_ang;
m02 = grid[0,2]*cos_ang + grid[1,2]*sin_ang;

grid[1,0] = grid[0,0]*-sin_ang + grid[1,0]*cos_ang;
grid[1,1] = grid[0,1]*-sin_ang + grid[1,1]*cos_ang;
grid[1,2] = grid[0,2]*-sin_ang + grid[1,2]*cos_ang;

grid[0,0]=m00;
grid[0,1]=m01;
grid[0, 2] = m02;
}


This might explain my methods

[Edited by - LorinAtzberger on March 17, 2009 9:42:17 AM]

Share this post


Link to post
Share on other sites
Usually you store the axis (orientation) along with the position of each entity. The x axis (right vector) as (1,0,0), the y axis (up vector) as (0,1,0) and the z axis (direction) as (0,0,1). Then, to move an entity about a scalar s you simply add s * entity.direction to entity.position.

Each time you rotate your entity simply update the entity's orientation.

Share this post


Link to post
Share on other sites
Yes this should work. But what if I want to use the z of the movement (for let's say strafing). How should I do that?
And by the way if the directions is 0 then it won't move.

Share this post


Link to post
Share on other sites
Your direction shouldn't be zero since it's a vector and not a position.

To strafe, you want to move along the right vector.

Share this post


Link to post
Share on other sites
I solved that problem. I translated the matrix from a language that uses degrees for sin/cos. C# uses radians. Now that I fixed that I have this set of functions that work perfectly:


public void PositionEntity(GVec3 p)
{
this.Pos.x = p.x;
this.Pos.y = p.y;
this.Pos.z = -p.z;

}
public void Move(GVec3 p)
{
GVec3 np = new GVec3();
np.x = p.x;
np.y = p.y;
np.z = -p.z;
GMatrix mat = new GMatrix();
mat.LoadIdentity();
mat.RotateY(Rot.y);
mat.RotateX(Rot.x);
mat.RotateZ(Rot.z);
mat.Translate(np);

Pos.x += mat.grid[3, 0];
Pos.y += mat.grid[3, 1];
Pos.z += mat.grid[3, 2];
}
public void Turn(GVec3 p)
{
Rot.x += p.x;
Rot.y += p.y;
Rot.z += p.z;

}
public void ScaleEntity(GVec3 p)
{
Scale = Scale * p;
}


Also now that this is working I'm thinking of keeping the object I'm testing the raycast on in the center and rotating then translating the line by - the objects rotation and position. This should work and so I won't have to move each vertext to test it :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!