Jump to content
  • Advertisement

DavidJr

Member
  • Content Count

    24
  • Joined

  • Last visited

Community Reputation

148 Neutral

About DavidJr

  • Rank
    Member
  1. DavidJr

    3D Model Terrain Collision

    Thank you guys for all of your replies. I see some choices here. First, to use height map. The reason I use model as terrain because I read height map technique requires random terrain, am I right? Second, to use collision library Third, as I read somewhere I can bake height map from model. Seems the easiest and the best is to use heightmap.
  2. I tried to search on Google about 3D model terrain collision but I still don't get the answer I want. I'd like to ask what method usually used to achieve this collision.   I have terrain (it has cave), let say it is an OBJ file and my entity will use collision box to detect the terrain collision. Is the collision basic different with terrain height collision?   Lot of people recommend to use a library, but I don't want to, I'd like to try to implement the collision myself. If you guys have better advice, I'd really appreciate it. Thank you before.
  3. Dave Hunt, sorry for really late reply. Got so busy this week.     When I used array, I didn't set cName[32] to '\0'. Here is the way I call fread using array. fread(cName, sizeof (char), 32, pIn);   Definitely my array was out of bound, because I only had 32. Recently, I tried to change the array size to 33. Still crashed. Using pointer instead of array works fine. Perhaps I'm wrong?   Thank you very much so far, Dave Hunt.
  4. Very often the devil's in the ellipsis. It's hard to say what happened without knowing what "..." is.   memory allocation   pMesh->pName = (char*)malloc(sizeof (char) * 33); fread(pMesh->pName,...); pMesh->pName[32] = '\0';
  5. Hello guys, I think I've found the problem. Yesterday I tried to comment those codes, the error appears when I tried to read the mesh name. I've char array in my dynamic struct, tried to change the char array to char pointer and allocate the char pointer fixed the problem.   e.g: typedef struct Mesh {     char cName[32]; } Mesh;   fread(pMesh->cName, ....);   -> typedef struct Mesh {    char *pName; } Mesh;   ...allocating the char fread(pMesh->pName,...); ...string terminate   Thank you very much for all of your help :) Btw, I'm curious why this happened?
  6.     No, the buffer reads correctly IMO. Take a look :) for (int j = 0; j < pMesh->iTr; ++j) {      fread(&pMesh->pVe[j], sizeof (fVec3), 1, pIn);      fread(&pMesh->pVn[j], sizeof (fVec3), 1, pIn);      fread(&pMesh->pVt[j], sizeof (fVec2), 1, pIn);      fread(&pMesh->pIndex[j].x, sizeof (int), 1, pIn); }
  7. Hi, when I try to read mesh file, I got error on the second loop, especially when the parser calls malloc for the second time. When I try different model with more mesh, e.g: 4 mesh, in mesh loop on count 3 it crashed. The VS tells me it can be heap corruption and there's "Critical error detected c0000374" in the output. Here is the code: for (int i = 0; i < iMesh; ++i) {    Mesh *pMesh = &pModel->pMesh[i];    fread(&pMesh->iTr, sizeof (int), 1, pIn);   pVe = (fVec3*)malloc(sizeof (fVec3) * pMesh->iTr);   pVn = (fVec3*)malloc(sizeof (fVec3) * pMesh->iTr);   pVt = (fVec2*)malloc(sizeof (fVec2) * pMesh->iTr);   pIndex = (fVec2*)malloc(sizeof (fVec2) * pMesh->iTr);   for (int j = 0; j < pMesh->iTr; ++j) {   //fread etc.   }   // OpenGL stuffs, buffer data   free(pVe);   free(pVt);   free(pVn); } Is this memory leak? What did I do wrong? Please help, I'm really struck here I appreciate any reply, thanks before
  8. DavidJr

    GLSL Animation

    Hey! I could get it working now! The problem is how I send the uniform is wrong, I change from matrix to 2 vec4 uniform. Here is the way I send it atm: float *pQuat, *pTrans; // allocation and storing the value to pQuat and pTrans Shader_LoadUniform4fv(CurrentShader, "GL_Q", pModel->iBone, &(pModel->pQuat[0])); Shader_LoadUniform4fv(CurrentShader, "GL_T", pModel->iBone, &(pModel->pTrans[0])); But I don't really like it, I like the way how I send before like this (the fQuat and fTrans are in Bone Struct): float fQuat[4], fTrans[4]; // allocation and storing the value to fQuat and fTrans Shader_LoadUniform4fv(CurrentShader, "GL_Q", pModel->iBone, &(pModel->pBone[0].fQuat[0])); Shader_LoadUniform4fv(CurrentShader, "GL_T", pModel->iBone, &(pModel->pBone[0].fTrans[0])); Any solution with the second one?
  9. DavidJr

    GLSL Animation

    Okay I figured out that my bone index in vertex shader was corrupt and I got it fixed by changing it to float type and cast it to integer. The default position now is messed and still the vertex won't move and keeps staying in that strange position. The black object is the animated object, it keeps in that shape whenever the frame changes. The black object should look like this I'm kinda stuck here, please help. Thank you so much
  10. Hello, I've succeed trying to animate mesh in immediate mode and now i'm trying to convert them to GLSL. But I got trouble animating it in GLSL. It seems like the uniform matrix values are 0 which results the model in default reference pose.   This is the vertex shader: #version 400 core in vec3 GL_Vertex; in vec2 GL_TexCoord; in vec3 GL_Normal; in int GL_Bone; out vec2 GL_TexCoord0; uniform mat4 m_Perspective, m_View, m_Model; uniform mat2x4 m_QT[100]; vec3 Rotate_Vector(in vec4 q, in vec3 v) {     return v + 2.0*cross(cross(v, q.xyz) + q.w*v, q.xyz); } void main() {     vec4 q = vec4(m_QT[GL_Bone][0][0], m_QT[GL_Bone][0][1], m_QT[GL_Bone][0][2], m_QT[GL_Bone][0][3]);     vec3 t = vec3(m_QT[GL_Bone][0][0], m_QT[GL_Bone][1][1], m_QT[GL_Bone][1][2]);     vec3 vVertex = Rotate_Vector(q, GL_Vertex) + t;          mat4 mModelView = m_View * m_Model;     gl_Position = m_Perspective * mModelView * vec4(vVertex, 1.0);          GL_TexCoord0 = GL_TexCoord; } I use quaternion and a vector translation and convert them to matrix 2x4 and send them to shader. Try to debug in GLSL like this: if (vVertex == GL_Vertex) {     vVertex = vec3(0.0, 0.0, 0.0); } It does not display anything which means the output of rotate_vector + t is 0. I assume the uniform matrix is 0.   I have checked the values before send them to shader, they are fine. I think there's something wrong with my code. Here is how I send the matrix to the shader: Shader_LoadUniformMatrix2x4fv(iShader, "m_QT", pModel->iBone, GL_FALSE, &(pModel->pBone[0].m[0][0])); I store the matrix in float m[2][4] array and the first row is the quaternion and the second row is the vector, and the column order is x, y, z, w. Also when I buffer the vbo, I set it to GL_STATIC_DRAW.   Thanks before
  11. DavidJr

    Quaternion Skinning

    Hello, I manage to bind the default pose and store them in local Quaternion-Translation. How to deform the bone and the mesh after I interpolate two quaternion? I interpolate the quaternions by SLERPing the quaternions.   I manage to deform the bone but the position is in accurate, and try to deform the mesh, it is completely messed up.   This is what I try after slerping the quaternions as qTransform and vTransform.   For Joints: qLocal = qWorld * qTransform vLocal = vWorld + vTransform   if has root qLocal = qLocal.parent * qLocal.child, rotate the vLocal by the qLocal.Parent and then add it to vLocal.   For mesh: Rotate the vertex data by the joint ID and add the result to the vertex data.   Result:   Real position:   Any suggestion?
  12. DavidJr

    Quaternion Skinning

    Thanks for the reply HappyCoder. Tried what you said but not all of the joints got the correct position.   example: Joint 0 has parent -1 (root) Joint 1 has parent 0 Joint 2 has parent 1 Joint 3 has parent 2   However, every bone that connected to the root gets the correct position while the others don't. In the example case, the joint 0 and joint 1 get the correct position.             pAnim.pBones[i].qWorld = Quaternion_FromEuler(pAnim.pBones[i].Rot);         pAnim.pBones[i].vWorld.x = pAnim.pBones[i].Pos[0];         pAnim.pBones[i].vWorld.y = pAnim.pBones[i].Pos[1];         pAnim.pBones[i].vWorld.z = pAnim.pBones[i].Pos[2];                  int iRoot = pAnim.pBones[i].Parent;         // Rotation         pAnim.pBones[i].qLocal = pAnim.pBones[i].qWorld;                 // Translation         pAnim.pBones[i].vLocal = pAnim.pBones[i].vWorld;         if (iRoot != -1)         {             //pAnim.pBones[i].qLocal = Quaternion_Multiply(pAnim.pBones[iRoot].qLocal, pAnim.pBones[i].qLocal);             pAnim.pBones[i].vLocal = Vector_Rotate(pAnim.pBones[i].vLocal, pAnim.pBones[iRoot].qLocal);         } Vector_Rotate uses formula v'=qpq'. Please help.
  13. DavidJr

    Quaternion Skinning

    Hello, I'm using Quaternion-Translation to animate my model, I don't want to use matrix. Each bone has vec3 rotation and vec3 position. I mean if I use matrix, it's just simply multiply matrix local parent with matrix local child to get the relative position, what about quaternion? I know Quaternion is just a representation of rotation. I stored the rotation in quaternion and the position in vec3, how to get the default bind position depending the joint's parent and child? I've tried to multiply the parent quaternion with child quaternion and then sum the child position with parent position. And this is what I got: [attachment=29875:Quaternion.jpg] While it should be like this: [attachment=29876:Real.jpg] Thanks
  14. Hello, I've been struggling with this for a month. I did skeletal animation in deprecated way and I wanna move on with Shader, how to do this with shader? The skeleton has 3 axis angles (x,y,z) and 3 positions, here is the way I did: 1. Built joint transformation matrices 2. Get the rotation between 2 axis rotations & positions 3. Set those rotations to matrix and store the positions in matrix 4. SLERP the quaternion 5. Convert the slerp output quaternion to Matrix 4x4 6. Multiply the matrix with pre built joint transformation matrix 7. Multiply the child matrix with parent matrix   The problems when I convert these to shader are, instead of sending the bone matrices to shader, I convert the multiplied child & parent matrix to quaternion and send it to shader and convert them again to matrix in GPU then multiply it with the vertices & weight. However, to get the vertices going, I have to send the pre built matrices which I don't want to. Is there any method to transform the vertex without sending the pre built transformation matrices? What is the correct step of interpolating the joints? I don't want to send matrices but quaternion instead.   Thank you. for (int i = 0; i < pMesh[mesh].VertNum; ++i)     {         memcpy(pOut[i].Pos, pMesh[mesh].pVertex[i].Pos, sizeof (Vertex));         float tmp[3], vert[3];         // mGlobalSkeleton & mGlobal must have to be sent to the GPU too in order to make the vertices work         VectorITransform(pMesh[mesh].pVertex[i].Pos, pAnim.pBones[pMesh[mesh].pVertex[i].BoneID].mGlobalSkeleton, tmp);         VectorTransform(tmp, pAnim.pBones[pMesh[mesh].pVertex[i].BoneID].mGlobal, vert);         pOut[i].Pos[0] = vert[0] * 1.0;         pOut[i].Pos[1] = vert[1] * 1.0;         pOut[i].Pos[2] = vert[2] * 1.0;     } Here is my shader: #version 400 compatibility in vec3 vPosition; in vec2 vTexCoord; in vec3 vNormal; in ivec4 vIndex; uniform mat2x4 mQuat[100]; mat4 Quaternion_ToMatrix(in mat2x4 mIn) {     float sqw = mIn[0][0]*mIn[0][0];     float sqx = mIn[0][1]*mIn[0][1];     float sqy = mIn[0][2]*mIn[0][2];     float sqz = mIn[0][3]*mIn[0][3];     // invs (inverse square length) is only required if quaternion is not already normalised     float fInv = 1.0 / (sqx + sqy + sqz + sqw);          mat4x4 mOut;          mOut[0][0] = (sqx - sqy - sqz + sqw) * fInv; // since sqw + sqx + sqy + sqz =1/invs*invs     mOut[1][1] = (-sqx + sqy - sqz + sqw) * fInv;     mOut[2][2] = (-sqx - sqy + sqz + sqw) * fInv;     mOut[1][0] = 2.0 * (mIn[0][1]*mIn[0][2] + mIn[0][3]*mIn[0][0]) * fInv;     mOut[0][1] = 2.0 * (mIn[0][1]*mIn[0][2] - mIn[0][3]*mIn[0][0]) * fInv;     mOut[2][0] = 2.0 * (mIn[0][1]*mIn[0][3] - mIn[0][2]*mIn[0][0]) * fInv;     mOut[0][2] = 2.0 * (mIn[0][1]*mIn[0][3] + mIn[0][2]*mIn[0][0]) * fInv;          mOut[2][1] = 2.0 * (mIn[0][2]*mIn[0][3] + mIn[0][1]*mIn[0][0]) * fInv;     mOut[1][2] = 2.0 * (mIn[0][2]*mIn[0][3] - mIn[0][1]*mIn[0][0]) * fInv;          mOut[3][0] = 0.0;     mOut[3][1] = 0.0;     mOut[3][2] = 0.0;          mOut[0][3] = mIn[1][0];     mOut[1][3] = mIn[1][1];     mOut[2][3] = mIn[1][2];     mOut[3][3] = mIn[1][3];          return mOut; } mat2x4 mTemp; void main() {     mat4 mMat = Quaternion_ToMatrix(mQuat[vIndex.x]);          vec4 vVertex = mMat * vec4(vPosition, 1.0);     gl_Position = gl_ModelViewProjectionMatrix * vVertex; }
  15. Thank you for your replies before     I just realize it, thank you.     Thank you for reminding me, I forgot that I have to add terminating character.     I did print those data, and they all are correct.     Btw I didn't use fscanf anymore. This is the code I've fixed as you suggested about the allocations, are they correct? Because I still get reading violation access       pLevel[iLevel].iMesh = iVal;     //pLevel[iLevel].pMesh = (SMesh*) malloc(sizeof (SMesh) * iVal);     pLevel[iLevel].pMesh = new SMesh[iVal];     for (int i = 0; i < pLevel[iLevel].iMesh; ++i)     {         //pLevel[iLevel].pMesh[i].pName = (char*) malloc(sizeof (char) * 32);         //pLevel[iLevel].pMesh[i].mMat.pTexture = (char*) malloc(sizeof (char) * 32);         //pLevel[iLevel].pMesh[i].pName = new char[32];         //pLevel[iLevel].pMesh[i].mMat.pTexture = new char[32];         fread(cLine, sizeof (char), 32, pIn);         //pLevel[iLevel].pMesh[i].pName = (char*) malloc(strlen(cLine));         pLevel[iLevel].pMesh[i].pName = new char[strlen(cLine)+1];         strncpy(pLevel[iLevel].pMesh[i].pName, cLine, strlen(cLine));         pLevel[iLevel].pMesh[i].pName[strlen(cLine)] = '\0';         fread(cLine, sizeof (char), 32, pIn);         //pLevel[iLevel].pMesh[i].mMat.pTexture = (char*) malloc(strlen(cLine));         pLevel[iLevel].pMesh[i].mMat.pTexture = new char[strlen(cLine)+1];         strncpy(pLevel[iLevel].pMesh[i].mMat.pTexture, cLine, strlen(cLine));         pLevel[iLevel].pMesh[i].mMat.pTexture[strlen(cLine)] = '\0';         fread(&pLevel[iLevel].pMesh[i].mMat.iSphere, sizeof (int), 1, pIn);         fread(&pLevel[iLevel].pMesh[i].mMat.fTransparancy, sizeof (float), 1, pIn);                  pLevel[iLevel].pMesh[i].mMat.iID = TextureLoad(cLine);                  fread(&iVal, sizeof (int), 1, pIn);         pLevel[iLevel].pMesh[i].iVe = iVal;         //pLevel[iLevel].pMesh[i].pVe = (SVec3Df*) malloc(sizeof (SVec3Df) * iVal);         //pLevel[iLevel].pMesh[i].pVt = (SVec2Df*) malloc(sizeof (SVec2Df) * iVal);         pLevel[iLevel].pMesh[i].pVe = new SVec3Df[iVal];         pLevel[iLevel].pMesh[i].pVt = new SVec2Df[iVal];         for (int j = 0; j < iVal; ++j)         {             fread(&pLevel[iLevel].pMesh[i].pVe[j], sizeof (float), 3, pIn);             fread(&pLevel[iLevel].pMesh[i].pVt[j], sizeof (float), 2, pIn);         }                  fread(&iVal, sizeof (int), 1, pIn);         pLevel[iLevel].pMesh[i].iVn = iVal;         //pLevel[iLevel].pMesh[i].pVn = (SVec3Df*) malloc(iVal * sizeof (SVec3Df));         pLevel[iLevel].pMesh[i].pVn = new SVec3Df[iVal];         for (int j = 0; j < iVal; ++j)         {             fread(&pLevel[iLevel].pMesh[i].pVn[j], sizeof (float), 3, pIn);             //printf("%f %f %f\n", pLevel[iLevel].pMesh[i].pVn[j].fX, pLevel[iLevel].pMesh[i].pVn[j].fY, pLevel[iLevel].pMesh[i].pVn[j].fZ);         }                  fread(&iVal, sizeof (int), 1, pIn);         pLevel[iLevel].pMesh[i].iTri = iVal * 3;         //pLevel[iLevel].pMesh[i].pTri = (SVec3Di*) malloc(sizeof (SVec3Di) * iVal);         pLevel[iLevel].pMesh[i].pTri = new SVec3Di[iVal];         for (int j = 0; j < iVal; ++j)         {             fread(&pLevel[iLevel].pMesh[i].pTri[j], sizeof (int), 3, pIn);             //printf("%d %d %d\n", pLevel[iLevel].pMesh[i].pTri[j].iX, pLevel[iLevel].pMesh[i].pTri[j].iY, pLevel[iLevel].pMesh[i].pTri[j].iZ);         }     }
  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!