• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By codelyoko373
      I wasn't sure if this would be the right place for a topic like this so sorry if it isn't.
      I'm currently working on a project for Uni using FreeGLUT to make a simple solar system simulation. I've got to the point where I've implemented all the planets and have used a Scene Graph to link them all together. The issue I'm having with now though is basically the planets and moons orbit correctly at their own orbit speeds.
      I'm not really experienced with using matrices for stuff like this so It's likely why I can't figure out how exactly to get it working. This is where I'm applying the transformation matrices, as well as pushing and popping them. This is within the Render function that every planet including the sun and moons will have and run.
      if (tag != "Sun") { glRotatef(orbitAngle, orbitRotation.X, orbitRotation.Y, orbitRotation.Z); } glPushMatrix(); glTranslatef(position.X, position.Y, position.Z); glRotatef(rotationAngle, rotation.X, rotation.Y, rotation.Z); glScalef(scale.X, scale.Y, scale.Z); glDrawElements(GL_TRIANGLES, mesh->indiceCount, GL_UNSIGNED_SHORT, mesh->indices); if (tag != "Sun") { glPopMatrix(); } The "If(tag != "Sun")" parts are my attempts are getting the planets to orbit correctly though it likely isn't the way I'm meant to be doing it. So I was wondering if someone would be able to help me? As I really don't have an idea on what I would do to get it working. Using the if statement is truthfully the closest I've got to it working but there are still weird effects like the planets orbiting faster then they should depending on the number of planets actually be updated/rendered.
    • By Jens Eckervogt
      Hello everyone, 
      I have problem with texture
      using System; using OpenTK; using OpenTK.Input; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; using System.Drawing; using System.Reflection; namespace Tutorial_05 { class Game : GameWindow { private static int WIDTH = 1200; private static int HEIGHT = 720; private static KeyboardState keyState; private int vaoID; private int vboID; private int iboID; private Vector3[] vertices = { new Vector3(-0.5f, 0.5f, 0.0f), // V0 new Vector3(-0.5f, -0.5f, 0.0f), // V1 new Vector3(0.5f, -0.5f, 0.0f), // V2 new Vector3(0.5f, 0.5f, 0.0f) // V3 }; private Vector2[] texcoords = { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) }; private int[] indices = { 0, 1, 3, 3, 1, 2 }; private string vertsrc = @"#version 450 core in vec3 position; in vec2 textureCoords; out vec2 pass_textureCoords; void main(void) { gl_Position = vec4(position, 1.0); pass_textureCoords = textureCoords; }"; private string fragsrc = @"#version 450 core in vec2 pass_textureCoords; out vec4 out_color; uniform sampler2D textureSampler; void main(void) { out_color = texture(textureSampler, pass_textureCoords); }"; private int programID; private int vertexShaderID; private int fragmentShaderID; private int textureID; private Bitmap texsrc; public Game() : base(WIDTH, HEIGHT, GraphicsMode.Default, "Tutorial 05 - Texturing", GameWindowFlags.Default, DisplayDevice.Default, 4, 5, GraphicsContextFlags.Default) { } protected override void OnLoad(EventArgs e) { base.OnLoad(e); CursorVisible = true; GL.GenVertexArrays(1, out vaoID); GL.BindVertexArray(vaoID); GL.GenBuffers(1, out vboID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw); GL.GenBuffers(1, out iboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw); vertexShaderID = GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(vertexShaderID, vertsrc); GL.CompileShader(vertexShaderID); fragmentShaderID = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(fragmentShaderID, fragsrc); GL.CompileShader(fragmentShaderID); programID = GL.CreateProgram(); GL.AttachShader(programID, vertexShaderID); GL.AttachShader(programID, fragmentShaderID); GL.LinkProgram(programID); // Loading texture from embedded resource texsrc = new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial_05.example.png")); textureID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, textureID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texsrc.Width, texsrc.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); System.Drawing.Imaging.BitmapData bitmap_data = texsrc.LockBits(new Rectangle(0, 0, texsrc.Width, texsrc.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, texsrc.Width, texsrc.Height, PixelFormat.Bgra, PixelType.UnsignedByte, bitmap_data.Scan0); texsrc.UnlockBits(bitmap_data); GL.Enable(EnableCap.Texture2D); GL.BufferData(BufferTarget.TextureBuffer, (IntPtr)(texcoords.Length * Vector2.SizeInBytes), texcoords, BufferUsageHint.StaticDraw); GL.BindAttribLocation(programID, 0, "position"); GL.BindAttribLocation(programID, 1, "textureCoords"); } protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(0, 0, ClientRectangle.Width, ClientRectangle.Height); } protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Key.Escape)) { Exit(); } } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); // Prepare for background GL.Clear(ClearBufferMask.ColorBufferBit); GL.ClearColor(Color4.Red); // Draw traingles GL.EnableVertexAttribArray(0); GL.EnableVertexAttribArray(1); GL.BindVertexArray(vaoID); GL.UseProgram(programID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture3D, textureID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID); GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0); GL.DisableVertexAttribArray(0); GL.DisableVertexAttribArray(1); SwapBuffers(); } protected override void OnClosed(EventArgs e) { base.OnClosed(e); GL.DeleteVertexArray(vaoID); GL.DeleteBuffer(vboID); } } } I can not remember where do I add GL.Uniform2();
    • By Jens Eckervogt
      Hello everyone
      For @80bserver8 nice job - I have found Google search. How did you port from Javascript WebGL to C# OpenTK.?
      I have been searched Google but it shows f***ing Unity 3D. I really want know how do I understand I want start with OpenTK But I want know where is porting of Javascript and C#?
       
      Thanks!
    • By mike44
      Hi
      I draw in a OpenGL framebuffer. All is fine but it eats FPS (frames per second), hence I wonder if I could execute the framebuffer drawing only every 5-10th loop or so?
      Many thanks
       
    • By cebugdev
      hi all,
      how to implement this type of effect ? 
      Also what is this effect called? this is considered volumetric lighting?
      what are the options of doing this? 
      a. billboard? but i want this to have the 3D effect that when we rotate the camera we can still have that 3d feel.
      b. a transparent 3d mesh? and we can animate it as well?
      need your expert advise.
       
      additional:
      2. how to implement things like fireball projectile (shot from a monster) (billboard texture or a 3d mesh)?
       
      Note: im using OpenGL ES 2.0 on mobile. 
       
      thanks!
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL How do I swap Y and Z axis with D3DXMATRIX?

This topic is 1880 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

I seem to be having problem exporting camera with 3ds max.
The object is sitting horizontally in max, now the exported object is standing upright in dx9
I believe the y-z axis is wrong. How do I flip it around? I mean what matrix can I multiply with this wrong matrix?

///
float mat[4][4];
Matrix3 invTM;
int persp;
float hither;
float yon;
D3DXMATRIX m_d3dWorldXform; // your app world transform matrix
D3DXMATRIX m_d3dViewXform; // your app view transform matrix
D3DXMATRIX m_d3dProjXform; // your app projection transform matrix
Interface *ip2 = GetCOREInterface();
ViewExp * pView = ip2->GetActiveViewport(); // Get the viewport in question
GraphicsWindow *gw = pView->getGW(); // Get the GraphicsWindow context

gw->getCameraMatrix( mat, &invTM, &persp, &hither, &yon); // getting these values to work with ... see above for their types
float oneOverDepth = 1.0f / (yon - hither);

// Set the Direct3D Camera View Position and Camera Projection Transforms.
//
// The first matrix is the full projection transformation matrix that
// converts World Coordinates into NPC. This means that the matrix is the
// product of the Camera View Position transformation matrix and the Camera
// Projection matrix. The second matrix is the inverse of the Camera View
// Position transformation matrix so if we multiply this second matrix by
// the first, we get the Camera Projection matrix. If we take the inverse
// of the second matrix, we get the Camera View Position matrix.
//
// The Camera View Position transformation converts World coordinates into
// Camera View Position coordinates where the camera is located at the
// origin. We have been given the inverse of the Camera View Position
// matrix so the first step is to take the inverse of this transform to
// obtain the Camera View Position matrix.
// General conversion from 3ds max coords to Direct3D coords:
//
// 3ds max: (Up, Front, Right) == (+Z, +Y, +X)
//
// Direct3D: (Up, Front, Right) == (+Y, +Z, +X)
//
// Conversion from 3ds max to Direct3D coords:
//
// 3ds max * conversion matrix = Direct3D
//
// [ x y z w ] * | +1 0 0 0 | = [ X Y Z W ]
// | 0 0 +1 0 |
// | 0 +1 0 0 |
// | 0 0 0 +1 |
//
// The View transform below accomplishes this. The standard View transform
// received makes the rotation about the X axis because the assumption was
// to transform to RH coords with the XY plane being the vertical plane
// instead of the XZ plane. The negation of the the Z column does the RH
// to LH flip. Thus, the View transform makes the transition from RH 3ds
// max coords to LH Direct3D coords.
Matrix3 camTM = Inverse(invTM);

// We now have an affine matrix (4x3) with no perspective column (it is
// understood to be (0, 0, 0, 1)). We add the fourth column and flip the
// Z-axis because Direct3D uses a left-handed coordinate system and MAX
// uses a right-handed coordinate system.
// Copy the affine view matrix data
int ki, kj;
MRow *pcvm = camTM.GetAddr();
for (ki = 0; ki < 4; ki++) {
for (kj = 0; kj < 3; kj++) {
m_d3dViewXform.m[ki][kj] = pcvm[ki][kj];
}
}
// Assign the fourth column (perspective terms)
m_d3dViewXform.m[0][3] = m_d3dViewXform.m[1][3] = m_d3dViewXform.m[2][3] = 0.0f;
m_d3dViewXform.m[3][3] = 1.0f;

// Scale the Z-axis (third column) by -1 to flip to left-handed Direct3D
// coordinate system
for (ki = 0; ki < 4; ki++) {
m_d3dViewXform.m[ki][2] *= -1.0f;
}

// Calculate the Direct3D Camera Projection transformation matrix.
//
// First, multiply the MAX full projection matrix by the inverse of the MAX
// Camera View Position matrix to obtain the MAX Camera Projection matrix.
//
// This gives us a correct Direct3D Camera Projection matrix except for the
// lower right quadrant.
//
MRow *pa = invTM.GetAddr();
for (ki = 0; ki < 4; ki++) {
float val = (float)(ki==3);
for (kj = 0; kj < 4; kj++) {
m_d3dProjXform.m[ki][kj] = pa[ki][0] * mat[0][kj] +
pa[ki][1] * mat[1][kj] +
pa[ki][2] * mat[2][kj] +
val * mat[3][kj];
}
}

// Now calculate the lower right quadrant of the Camera Projection matrix
// using the facts that MAX uses an NPC Z-axis range of +1 to -1 whereas
// Direct3D uses an NPC Z-axis range of zero to +1.
//
// For ease of reference, the general forms of the Direct3D Projection
// matrix for perspective and orthographic projections are given below.
//
// Please note that the matrices are specified in row-major order. This
// means that the translate terms are located in the fourth row and the
// projection terms in the fourth column. This is consistent with the way
// MAX, Direct3D, and OpenGL all handle matrices. Even though the OpenGL
// documentation is in column-major form, the OpenGL code is designed to
// handle matrix operations in row-major form.

if (persp) {

// Perspective projection. The general form of the Direct3D Camera
// Projection matrix is:
//
// | 2n/(r-l) 0 0 0 |
// | 0 2n/(t-b) 0 0 |
// | (r+l)/(r-l) (t+b)/(t-b) f/(f-n) 1 |
// | 0 0 -fn/(f-n) 0 |
//
// Construct the lower right four terms correctly for Direct3D.
//
m_d3dProjXform.m[2][2] = yon*oneOverDepth;
m_d3dProjXform.m[2][3] = 1.0f;
m_d3dProjXform.m[3][2] = -(yon*hither*oneOverDepth);
m_d3dProjXform.m[3][3] = 0.0f;

} else {

// Orthographic projection. The general form of the Direct3D Camera
// Projection matrix is:
//
// | 2/(r-l) 0 0 0 |
// | 0 2/(t-b) 0 0 |
// | 0 0 1/(f-n) 0 |
// | (r+l)/(r-l) (t+b)/(t-b) -n/(f-n) 1 |
//
// Construct the lower right four terms correctly for Direct3D.
//
m_d3dProjXform.m[2][2] = oneOverDepth;
m_d3dProjXform.m[2][3] = 0.0f;
m_d3dProjXform.m[3][2] = -(hither*oneOverDepth);
m_d3dProjXform.m[3][3] = 1.0f;
}


Thanks
Jack Edited by lucky6969b

Share this post


Link to post
Share on other sites
Advertisement
Depending on whether you use a left- or a right-handed coordinate system, you'd need to do something like this:

first pitch the model backwards: D3DXMatrixRotationYawPitchRoll(&matrixPitch, 0.f, PI * .5f, 0.f);
then flip it along the z-axis when needed: D3DXMatrixScaling(&matrixScale, 1.f, 1.f, -1.f);
then concatenate all of them together: matrixPitch * matrixScale * matrixWorld * matrixView * matrixProjection

Of course it would be better to correct a mesh' orientation directly after loading it, not only until you render it.

Share this post


Link to post
Share on other sites
You can always do it while exporting, I think it's easier. When You export .FBX model you can go to Advanced Options -> Axis Conversion, in .OBJ you can chcek (or uncheck)convert yz-axis, most of model exporters got this option.

Share this post


Link to post
Share on other sites
Hi eppo, thanks for your help. I am actually following the typical article found on the net. I think you have read it too.
Basically, it should be correct except I didn't export the world transformation of the object. So would that be the root cause of the problem?
Thanks
Jack

Share this post


Link to post
Share on other sites
How do you feel that it's the view transformation problem or the projection transformation problem?
If the definition of projection matrix is to transforming the view frustum to cuboid shape, it won't do any harm to the camera, will it?
So it must be the view transformation problem.

Share this post


Link to post
Share on other sites
How do I dismantle the up-right-view vectors of the transformation?


// Scale the Z-axis (third column) by -1 to flip to left-handed Direct3D
// coordinate system
for (ki = 0; ki < 4; ki++) {
m_d3dViewXform.m[ki][2] *= -1.0f;
}


Can this simple operation change it from right-handed to left-handed?
Thanks
Jack Edited by lucky6969b

Share this post


Link to post
Share on other sites
I probably should have been clearer on this: the problem isn't really in the object's world-xfrm, it's in the mesh vertices themselves. You either correct this by applying a pre-transformation when rendering or by simply swapping vertex components during loading; D3DXVECTOR3(vec3DS.x, vec3DS.z, (-)vec3DS.y).

Share this post


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

  • Advertisement