Transformation Problems

Started by
4 comments, last by NightCreature83 12 years, 9 months ago
could someone please help, i am finding it difficult to work around moving individual models, i can only move the whole scene. i cant seem to work around moving models that have been selected....Any ideas please

this is my display method....the translate enable me to drag the whole scene(which is not exactly what i want...which is moving a selected object)....seems placing a translate in this function moves the camera instead of the selected models....



void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // This clear the background color to dark blue
glMatrixMode(GL_MODELVIEW); // Modeling transformation
glPushMatrix(); glLoadIdentity(); // Initialize the model matrix as identity
glTranslatef(mouse1.GetX(), mouse1.GetY(), 0.0);
drawModel(GL_RENDER);
glPopMatrix();
glFlush();
glutSwapBuffers();

}


this is where the selection is done,i have tried every scenario i could but i cant get the selected model to move individually, or how i could include the pushMatrix,gltranslate and popmatrix....to achieve what i want....thank you.


mouse(int button, int state, int x, int y)
{
GLuint selectBuf[SIZE];
GLint hits;
GLint viewport[4];

mouse1.SetState(button, state);
mouse1.SetNewPos(x, y);
printf("Mouse %s At %d %d\n", (state == GLUT_DOWN) ? "Down" : "Up", x, y);

if (button == GLUT_LEFT_BUTTON && glutGetModifiers() & GLUT_ACTIVE_SHIFT)
button = GLUT_MIDDLE_BUTTON;



if(button==GLUT_LEFT_BUTTON && state== GLUT_DOWN)
{

glGetIntegerv(GL_VIEWPORT,viewport);
glSelectBuffer(SIZE, selectBuf);
glRenderMode(GL_SELECT);

glInitNames();
glPushName(-1);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport);
gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,0.1f,10000.0f);

drawModel(GL_SELECT);


glMatrixMode (GL_PROJECTION);

glPopMatrix();
glFlush();

hits = glRenderMode(GL_RENDER);

processHits(hits, selectBuf);

glutPostRedisplay();



}

}



thank you for your help
Advertisement

could someone please help, i am finding it difficult to work around moving individual models, i can only move the whole scene. i cant seem to work around moving models that have been selected....Any ideas please

this is my display method....the translate enable me to drag the whole scene(which is not exactly what i want...which is moving a selected object)....seems placing a translate in this function moves the camera instead of the selected models....



void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // This clear the background color to dark blue
glMatrixMode(GL_MODELVIEW); // Modeling transformation
glPushMatrix(); glLoadIdentity(); // Initialize the model matrix as identity
glTranslatef(mouse1.GetX(), mouse1.GetY(), 0.0);
drawModel(GL_RENDER);
glPopMatrix();
glFlush();
glutSwapBuffers();

}


this is where the selection is done,i have tried every scenario i could but i cant get the selected model to move individually, or how i could include the pushMatrix,gltranslate and popmatrix....to achieve what i want....thank you.


mouse(int button, int state, int x, int y)
{
GLuint selectBuf[SIZE];
GLint hits;
GLint viewport[4];

mouse1.SetState(button, state);
mouse1.SetNewPos(x, y);
printf("Mouse %s At %d %d\n", (state == GLUT_DOWN) ? "Down" : "Up", x, y);

if (button == GLUT_LEFT_BUTTON && glutGetModifiers() & GLUT_ACTIVE_SHIFT)
button = GLUT_MIDDLE_BUTTON;



if(button==GLUT_LEFT_BUTTON && state== GLUT_DOWN)
{

glGetIntegerv(GL_VIEWPORT,viewport);
glSelectBuffer(SIZE, selectBuf);
glRenderMode(GL_SELECT);

glInitNames();
glPushName(-1);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport);
gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,0.1f,10000.0f);

drawModel(GL_SELECT);


glMatrixMode (GL_PROJECTION);

glPopMatrix();
glFlush();

hits = glRenderMode(GL_RENDER);

processHits(hits, selectBuf);

glutPostRedisplay();



}

}



thank you for your help


You should stick the translation in your DrawModel function on a per model basis other wise it will effect all models drawn by the main render loop

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion




ObjLoad ("reflector2.3ds","texture.bmp", 10.0, 20.0, -500.0, 900,0,0);

ObjLoad ("mast2.3ds","texture.bmp", 20.0, 10.0, -500.0, 900,0,0);

ObjLoad ("receiver4.3ds","texture.bmp", 0.0, 0.0, -500.0, 900,0,0);

ObjLoad ("connector.3ds","texture.bmp", -10.0, 10.0, -500.0, 900,0,0);

ObjLoad ("chesspawn.3ds","texture.bmp", -20.0, 10.0, -500.0, 900,0,0);

ObjLoad ("stand2.3ds","texture.bmp", -30.0, 10.0, -500.0, 900,0,0);




or could i still further put the translation in the draw function, wil it still be okay

glTranslatef(locationlist[0],
locationlist[1],
locationlist[2]);






void drawModel(GLenum mode)

{

int i,j;

for (i=0;i<obj_qty;i++)

{

glPushMatrix(); // We save the current matrix

glTranslatef(locationlist[0],
locationlist[1],
locationlist[2]);






glMultMatrixf(&object.matrix[0][0]); // Now let's multiply the object matrix by the identity-first matrix


if (object.id_texture!=-1)

{

glBindTexture(GL_TEXTURE_2D, object.id_texture); // We set the active texture

glEnable(GL_TEXTURE_2D); // Texture mapping ON

}

else

glDisable(GL_TEXTURE_2D); // Texture mapping OFF

if(mode == GL_SELECT) glLoadName(i);

glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)

for (j=0;j<object.polygons_qty;j++)

{

//----------------- FIRST VERTEX -----------------

//Normal coordinates of the first vertex

glNormal3f( object.normal[ object.polygon[j].a ].x,

object.normal[ object.polygon[j].a ].y,

object.normal[ object.polygon[j].a ].z);

// Texture coordinates of the first vertex

glTexCoord2f( object.mapcoord[ object.polygon[j].a ].u,

object.mapcoord[ object.polygon[j].a ].v);

// Coordinates of the first vertex

glVertex3f( object.vertex[ object.polygon[j].a ].x,

object.vertex[ object.polygon[j].a ].y,

object.vertex[ object.polygon[j].a ].z);




thanks for your help




ObjLoad ("reflector2.3ds","texture.bmp", 10.0, 20.0, -500.0, 900,0,0);

ObjLoad ("mast2.3ds","texture.bmp", 20.0, 10.0, -500.0, 900,0,0);

ObjLoad ("receiver4.3ds","texture.bmp", 0.0, 0.0, -500.0, 900,0,0);

ObjLoad ("connector.3ds","texture.bmp", -10.0, 10.0, -500.0, 900,0,0);

ObjLoad ("chesspawn.3ds","texture.bmp", -20.0, 10.0, -500.0, 900,0,0);

ObjLoad ("stand2.3ds","texture.bmp", -30.0, 10.0, -500.0, 900,0,0);




or could i still further put the translation in the draw function, wil it still be okay

glTranslatef(locationlist[0],
locationlist[1],
locationlist[2]);






void drawModel(GLenum mode)

{

int i,j;

for (i=0;i<obj_qty;i++)

{

glPushMatrix(); // We save the current matrix

glTranslatef(locationlist[0],
locationlist[1],
locationlist[2]);






glMultMatrixf(&object.matrix[0][0]); // Now let's multiply the object matrix by the identity-first matrix


if (object.id_texture!=-1)

{

glBindTexture(GL_TEXTURE_2D, object.id_texture); // We set the active texture

glEnable(GL_TEXTURE_2D); // Texture mapping ON

}

else

glDisable(GL_TEXTURE_2D); // Texture mapping OFF

if(mode == GL_SELECT) glLoadName(i);

glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)

for (j=0;j<object.polygons_qty;j++)

{

//----------------- FIRST VERTEX -----------------

//Normal coordinates of the first vertex

glNormal3f( object.normal[ object.polygon[j].a ].x,

object.normal[ object.polygon[j].a ].y,

object.normal[ object.polygon[j].a ].z);

// Texture coordinates of the first vertex

glTexCoord2f( object.mapcoord[ object.polygon[j].a ].u,

object.mapcoord[ object.polygon[j].a ].v);

// Coordinates of the first vertex

glVertex3f( object.vertex[ object.polygon[j].a ].x,

object.vertex[ object.polygon[j].a ].y,

object.vertex[ object.polygon[j].a ].z);




thanks for your help



That should be fine as long as the matrices for the selected object is different from the rest, like the rest has a transform of identity and the selected one has a different one.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion





ObjLoad ("reflector2.3ds","texture.bmp", 10.0, 20.0, -500.0, 900,0,0);

ObjLoad ("mast2.3ds","texture.bmp", 20.0, 10.0, -500.0, 900,0,0);

ObjLoad ("receiver4.3ds","texture.bmp", 0.0, 0.0, -500.0, 900,0,0);

ObjLoad ("connector.3ds","texture.bmp", -10.0, 10.0, -500.0, 900,0,0);

ObjLoad ("chesspawn.3ds","texture.bmp", -20.0, 10.0, -500.0, 900,0,0);

ObjLoad ("stand2.3ds","texture.bmp", -30.0, 10.0, -500.0, 900,0,0);




or could i still further put the translation in the draw function, wil it still be okay

glTranslatef(locationlist[0],
locationlist[1],
locationlist[2]);






void drawModel(GLenum mode)

{

int i,j;

for (i=0;i<obj_qty;i++)

{

glPushMatrix(); // We save the current matrix

glTranslatef(locationlist[0],
locationlist[1],
locationlist[2]);






glMultMatrixf(&object.matrix[0][0]); // Now let's multiply the object matrix by the identity-first matrix


if (object.id_texture!=-1)

{

glBindTexture(GL_TEXTURE_2D, object.id_texture); // We set the active texture

glEnable(GL_TEXTURE_2D); // Texture mapping ON

}

else

glDisable(GL_TEXTURE_2D); // Texture mapping OFF

if(mode == GL_SELECT) glLoadName(i);

glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)

for (j=0;j<object.polygons_qty;j++)

{

//----------------- FIRST VERTEX -----------------

//Normal coordinates of the first vertex

glNormal3f( object.normal[ object.polygon[j].a ].x,

object.normal[ object.polygon[j].a ].y,

object.normal[ object.polygon[j].a ].z);

// Texture coordinates of the first vertex

glTexCoord2f( object.mapcoord[ object.polygon[j].a ].u,

object.mapcoord[ object.polygon[j].a ].v);

// Coordinates of the first vertex

glVertex3f( object.vertex[ object.polygon[j].a ].x,

object.vertex[ object.polygon[j].a ].y,

object.vertex[ object.polygon[j].a ].z);




thanks for your help



thanks alot i did see some improved changes, i am trying to achieve a drag object property in my program, i had in place of gltranslate used ObjTranslate(&object[obj_control],mouse1.GetX(), mouse1.GetY(), 0.0); in my program earlier to help me achieve dragging....only for my objects to disappear (probably translating the selected objects to locations not in the scene).......which brought me to feel my algorithm was wrong....i tested it with the function (without using my drag algorithm)ObjTranslate(&object[obj_control],0.0, 0.0, -1); the model moved a step on each click.

could you please give me an idea how i could incorporate dragging of objects in my program or an algorithm i could use...i am really bad at maths


thank you very much



I don't know GL as well as I should anymore but what you would like to do is the following.




Setup Projection and view matrices

Foreach model in modelsList

{

setup the world matrix of this model

draw the model itself

}




I have no idea what your object and all the arrays stand for keep your code simple it will help you understand what you are doing or trying to do without seeing the code for it.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement