• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By tj8146
      I have attached my project in a .zip file if you wish to run it for yourself.
      I am making a simple 2d top-down game and I am trying to run my code to see if my window creation is working and to see if my timer is also working with it. Every time I run it though I get errors. And when I fix those errors, more come, then the same errors keep appearing. I end up just going round in circles.  Is there anyone who could help with this? 
       
      Errors when I build my code:
      1>Renderer.cpp 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2039: 'string': is not a member of 'std' 1>c:\program files (x86)\windows kits\10\include\10.0.16299.0\ucrt\stddef.h(18): note: see declaration of 'std' 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2061: syntax error: identifier 'string' 1>c:\users\documents\opengl\game\game\renderer.cpp(28): error C2511: 'bool Game::Rendering::initialize(int,int,bool,std::string)': overloaded member function not found in 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.h(9): note: see declaration of 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.cpp(35): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(36): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(43): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>Done building project "Game.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========  
       
      Renderer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include "Renderer.h" #include "Timer.h" #include <iostream> namespace Game { GLFWwindow* window; /* Initialize the library */ Rendering::Rendering() { mClock = new Clock; } Rendering::~Rendering() { shutdown(); } bool Rendering::initialize(uint width, uint height, bool fullscreen, std::string window_title) { if (!glfwInit()) { return -1; } /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } /* Make the window's context current */ glfwMakeContextCurrent(window); glViewport(0, 0, (GLsizei)width, (GLsizei)height); glOrtho(0, (GLsizei)width, (GLsizei)height, 0, 1, -1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glfwSwapInterval(1); glEnable(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_TEXTURE_2D); glLoadIdentity(); return true; } bool Rendering::render() { /* Loop until the user closes the window */ if (!glfwWindowShouldClose(window)) return false; /* Render here */ mClock->reset(); glfwPollEvents(); if (mClock->step()) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwSwapBuffers(window); mClock->update(); } return true; } void Rendering::shutdown() { glfwDestroyWindow(window); glfwTerminate(); } GLFWwindow* Rendering::getCurrentWindow() { return window; } } Renderer.h
      #pragma once namespace Game { class Clock; class Rendering { public: Rendering(); ~Rendering(); bool initialize(uint width, uint height, bool fullscreen, std::string window_title = "Rendering window"); void shutdown(); bool render(); GLFWwindow* getCurrentWindow(); private: GLFWwindow * window; Clock* mClock; }; } Timer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include <time.h> #include "Timer.h" namespace Game { Clock::Clock() : mTicksPerSecond(50), mSkipTics(1000 / mTicksPerSecond), mMaxFrameSkip(10), mLoops(0) { mLastTick = tick(); } Clock::~Clock() { } bool Clock::step() { if (tick() > mLastTick && mLoops < mMaxFrameSkip) return true; return false; } void Clock::reset() { mLoops = 0; } void Clock::update() { mLastTick += mSkipTics; mLoops++; } clock_t Clock::tick() { return clock(); } } TImer.h
      #pragma once #include "Common.h" namespace Game { class Clock { public: Clock(); ~Clock(); void update(); bool step(); void reset(); clock_t tick(); private: uint mTicksPerSecond; ufloat mSkipTics; uint mMaxFrameSkip; uint mLoops; uint mLastTick; }; } Common.h
      #pragma once #include <cstdio> #include <cstdlib> #include <ctime> #include <cstring> #include <cmath> #include <iostream> namespace Game { typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; typedef unsigned long ulong; typedef float ufloat; }  
      Game.zip
    • By lxjk
      Hi guys,
      There are many ways to do light culling in tile-based shading. I've been playing with this idea for a while, and just want to throw it out there.
      Because tile frustums are general small compared to light radius, I tried using cone test to reduce false positives introduced by commonly used sphere-frustum test.
      On top of that, I use distance to camera rather than depth for near/far test (aka. sliced by spheres).
      This method can be naturally extended to clustered light culling as well.
      The following image shows the general ideas

       
      Performance-wise I get around 15% improvement over sphere-frustum test. You can also see how a single light performs as the following: from left to right (1) standard rendering of a point light; then tiles passed the test of (2) sphere-frustum test; (3) cone test; (4) spherical-sliced cone test
       

       
      I put the details in my blog post (https://lxjk.github.io/2018/03/25/Improve-Tile-based-Light-Culling-with-Spherical-sliced-Cone.html), GLSL source code included!
       
      Eric
    • By Fadey Duh
      Good evening everyone!

      I was wondering if there is something equivalent of  GL_NV_blend_equation_advanced for AMD?
      Basically I'm trying to find more compatible version of it.

      Thank you!
    • By Jens Eckervogt
      Hello guys, 
       
      Please tell me! 
      How do I know? Why does wavefront not show for me?
      I already checked I have non errors yet.
      using OpenTK; using System.Collections.Generic; using System.IO; using System.Text; namespace Tutorial_08.net.sourceskyboxer { public class WaveFrontLoader { private static List<Vector3> inPositions; private static List<Vector2> inTexcoords; private static List<Vector3> inNormals; private static List<float> positions; private static List<float> texcoords; private static List<int> indices; public static RawModel LoadObjModel(string filename, Loader loader) { inPositions = new List<Vector3>(); inTexcoords = new List<Vector2>(); inNormals = new List<Vector3>(); positions = new List<float>(); texcoords = new List<float>(); indices = new List<int>(); int nextIdx = 0; using (var reader = new StreamReader(File.Open("Contents/" + filename + ".obj", FileMode.Open), Encoding.UTF8)) { string line = reader.ReadLine(); int i = reader.Read(); while (true) { string[] currentLine = line.Split(); if (currentLine[0] == "v") { Vector3 pos = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inPositions.Add(pos); if (currentLine[1] == "t") { Vector2 tex = new Vector2(float.Parse(currentLine[1]), float.Parse(currentLine[2])); inTexcoords.Add(tex); } if (currentLine[1] == "n") { Vector3 nom = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inNormals.Add(nom); } } if (currentLine[0] == "f") { Vector3 pos = inPositions[0]; positions.Add(pos.X); positions.Add(pos.Y); positions.Add(pos.Z); Vector2 tc = inTexcoords[0]; texcoords.Add(tc.X); texcoords.Add(tc.Y); indices.Add(nextIdx); ++nextIdx; } reader.Close(); return loader.loadToVAO(positions.ToArray(), texcoords.ToArray(), indices.ToArray()); } } } } } And It have tried other method but it can't show for me.  I am mad now. Because any OpenTK developers won't help me.
      Please help me how do I fix.

      And my download (mega.nz) should it is original but I tried no success...
      - Add blend source and png file here I have tried tried,.....  
       
      PS: Why is our community not active? I wait very longer. Stop to lie me!
      Thanks !
    • 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.
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL world to local rotations

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

In opengl I rotate objects in world space:

glRotatef(myObject.zRot, 0, 0, 1);
glRotatef(myObject.xRot, 1, 0, 0);
glRotatef(myObject.yRot, 0, 1, 0);


There is a way to find the object local angles of rotation?

Share this post


Link to post
Share on other sites
Advertisement

In opengl I rotate objects in world space:

glRotatef(myObject.zRot, 0, 0, 1);
glRotatef(myObject.xRot, 1, 0, 0);
glRotatef(myObject.yRot, 0, 1, 0);


There is a way to find the object local angles of rotation?


Look into quaternions. You can use that rotation matrix to extract a quat and then get eular angles (xyz).
There is a quaternion lib in my library.
There is also a function for extracting angles directly from a rotation matrix.

Share this post


Link to post
Share on other sites

In opengl I rotate objects in world space:

glRotatef(myObject.zRot, 0, 0, 1);
glRotatef(myObject.xRot, 1, 0, 0);
glRotatef(myObject.yRot, 0, 1, 0);


OpenGL does not work in world space. IF your modelview was still identity, the very first rotation will happen to in world space, but only because local and global are still the same. Every rotation after that is using the local coordinate system resulting from your previous transformations. If you want to use global coordinates, you have to change the order of the matrix multiplication (ie. do it manually, because OpenGL will always do modelview = rotationMatrix * modelview, when what you want is modelview = modelview * rotationMatrix; ).

Share this post


Link to post
Share on other sites

[quote name='Alessandro' timestamp='1318032168' post='4870317']
In opengl I rotate objects in world space:

glRotatef(myObject.zRot, 0, 0, 1);
glRotatef(myObject.xRot, 1, 0, 0);
glRotatef(myObject.yRot, 0, 1, 0);


OpenGL does not work in world space. IF your modelview was still identity, the very first rotation will happen to in world space, but only because local and global are still the same. Every rotation after that is using the local coordinate system resulting from your previous transformations. If you want to use global coordinates, you have to change the order of the matrix multiplication (ie. do it manually, because OpenGL will always do modelview = rotationMatrix * modelview, when what you want is modelview = modelview * rotationMatrix; ).
[/quote]

Sorry for the confusion. Yes opengl works in local space. I do have a quaternion library but I don't know the math of how would I go from local angles to world space, using matrices. Any chance to have some pseudo-code?

Share this post


Link to post
Share on other sites
Here is the code I came up with. I'm still not sure I'm extracting angles correctly from the final step.<br><br>



float xRot=45.0*DEG_TO_RAD;
float yRot=30.0*DEG_TO_RAD;
float zRot=22.0*DEG_TO_RAD;

// build rotateX matrix
matX= new float[16];
matX[5] = cosf(xRot);
matX[6] = -sinf(xRot);
matX[9] = -matX[6];
matX[10] = matX[5];

// build rotateY matrix
matY= new float[16];
matY[0] = cosf(yRot);
matY[2] = sinf(yRot);
matY[8] = -matY[2];
matY[10] = matY[0];

// build rotateZ matrix
matZ= new float[16];
matZ[0] = cosf(zRot);
matZ[1] = sinf(zRot);
matZ[4] = -matZ[1];
matZ[5] = matZ[0];

matXY=new float[16];
matXYZ=new float[16];

matXY= matrixMultiply(matX, matY);
matXYZ=matrixMultiply(matXY, matZ); //calculate final rotation matrix matXYZ

float *modelview;
modelview= new float[16];
glGetFloatv( GL_MODELVIEW_MATRIX, modelview ); // Get the current MODELVIEW matrix from OpenGL

float *worldRotMatrix;
worldRotMatrix=new float[16];
worldRotMatrix=matrixMultiply(modelview,matXYZ); // this should be the final rotation matrix, in world space

angle_y = -asin( worldRotMatrix[2]); /* Calculate Y-axis angle */
float C = cos( angle_y );
if ( fabs( C ) > 0.005 ) /* Gimball lock? */
{
tr_x = worldRotMatrix[10] / C; /* No, so get X-axis angle */
tr_y = -worldRotMatrix[6] / C;
angle_x = atan2( tr_y, tr_x ) ;
tr_x = worldRotMatrix[0] / C; /* Get Z-axis angle */
tr_y = -worldRotMatrix[1] / C;
angle_z = atan2( tr_y, tr_x ) ;
}
else /* Gimball lock has occurred */
{
angle_x = 0; /* Set X-axis angle to zero */
tr_x = worldRotMatrix[5]; /* And calculate Z-axis angle */
tr_y = worldRotMatrix[4];
angle_z = atan2( tr_y, tr_x ) ;
}




Share this post


Link to post
Share on other sites
Forgot to show the matrixMultiply function:


float* matrixMultiply(float* m1, float* m2)
{
float *finalMat;
finalMat=new float[16];
matrixIdentity(finalMat);
// Fisrt Column
finalMat[0] = m1[0]*m2[0] + m1[4]*m2[1] + m1[8]*m2[2] + m1[12]*m2[3];
finalMat[1] = m1[1]*m2[0] + m1[5]*m2[1] + m1[9]*m2[2] + m1[13]*m2[3];
finalMat[2] = m1[2]*m2[0] + m1[6]*m2[1] + m1[10]*m2[2] + m1[14]*m2[3];
finalMat[3] = m1[3]*m2[0] + m1[7]*m2[1] + m1[11]*m2[2] + m1[15]*m2[3];

// Second Column
finalMat[4] = m1[0]*m2[4] + m1[4]*m2[5] + m1[8]*m2[6] + m1[12]*m2[7];
finalMat[5] = m1[1]*m2[4] + m1[5]*m2[5] + m1[9]*m2[6] + m1[13]*m2[7];
finalMat[6] = m1[2]*m2[4] + m1[6]*m2[5] + m1[10]*m2[6] + m1[14]*m2[7];
finalMat[7] = m1[3]*m2[4] + m1[7]*m2[5] + m1[11]*m2[6] + m1[15]*m2[7];

// Third Column
finalMat[8] = m1[0]*m2[8] + m1[4]*m2[9] + m1[8]*m2[10] + m1[12]*m2[11];
finalMat[9] = m1[1]*m2[8] + m1[5]*m2[9] + m1[9]*m2[10] + m1[13]*m2[11];
finalMat[10] = m1[2]*m2[8] + m1[6]*m2[9] + m1[10]*m2[10] + m1[14]*m2[11];
finalMat[11] = m1[3]*m2[8] + m1[7]*m2[9] + m1[11]*m2[10] + m1[15]*m2[11];

// Fourth Column
finalMat[12] = m1[0]*m2[12] + m1[4]*m2[13] + m1[8]*m2[14] + m1[12]*m2[15];
finalMat[13] = m1[1]*m2[12] + m1[5]*m2[13] + m1[9]*m2[14] + m1[13]*m2[15];
finalMat[14] = m1[2]*m2[12] + m1[6]*m2[13] + m1[10]*m2[14] + m1[14]*m2[15];
finalMat[15] = m1[3]*m2[12] + m1[7]*m2[13] + m1[11]*m2[14] + m1[15]*m2[15];

return finalMat;
}

Share this post


Link to post
Share on other sites
If you understand the dot product and know how to use it to find angles between vectors:

After you do those 3 global rotations you get a matrix that shows the new x,y,z vectors for your object. So by drawing your object with the identity matrix it is probably aligned with the global vectors (right = x, y = top, forward = z or negative z). Those 3 vectors now equal the 3 columns in your matrix after doing global rotations. In otherwords, The identity matrix gives you x,y,z axis, what do they map to now: column0,1,2 of your matrix. So do a dot product between the old x and new x, old y and new y, old z and new z. And you can figure out that but.

I assume that is what you want, but you should be knowing the local rotations (yaw,pitch,roll). I do what you do, I align all models on the z axis, that way I can do z rotation first, and then the x,y rotations are the ones the figure out the heading direction of the object. I think I just dont understand your problem though.

Share this post


Link to post
Share on other sites
Sorry, instead of writing code perhaps I had to explain more in detail what I'm trying to do.
I have a simple opengl application where I load and display .obj files. The application allows also to position, scale and rotate those models using the mouse. All those transformations happen in local space.
The application allows to export the above data in a .txt file, with the following scheme:

obj file | xRot | yRot | zRot | xPos | yPos | zPos

Later on, in a separate, commercial application, I read this .txt file and I recreate the scene, loading sequentually the obj's files and applying the transformation values read from the .txt file.
Problem is, that the commercial application perform rotations in world space, while the angles that I "exported" are in local space.

So, what I'm trying to do is to convert those angles from local to world space so that the objects are properly rotated in the commercial application.

I hope I could explain myself better...

Share this post


Link to post
Share on other sites
You should be able to use the local xyz rotations to compute what the object would draw like relative to its own Identity/Local space, Then apply the transform that makes the object be a certain way globally. Should be able to just multiply both matrices. That would give you the final matrix it should look like to draw with given the vertex data in local space. With that matrix, you could just dot product all vectors in that matrix (column0,1,2) with the regular world space vectors (1,0,0 0,1,0, 0,0,1) with their new representations column0,1,2 and that would give you the angle that the object is drawing at relative to the regular world axis.

Share this post


Link to post
Share on other sites
Thanks dpadam450, so the pseudo-code would look like this:

1) build the object local space matrix
2) get the current MODELVIEW matrix from OpenGL
3) build final matrix multiplying the MODELVIEW by the local space matrix (and I think I arrived at this point in previous code I posted)
4) dot product the vectors defined by columns 0,1,2 of the final matrix, by the corresponding regular world space vectors (1,0,0 0,1,0, 0,0,1)
5) extract euler angles from this last resulting matrix.

Would that work? Any comments also from others?

Share this post


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

  • Advertisement