PDA

View Full Version : Moving objects without using pushMatrix or popMatrix



Isabel
05-16-2016, 03:46 PM
Hello, i would like some help please, im making a program where i got 2 objects (a solidSphere and wireTorus), the program has specified letters (u = move up, d = move down, l = move left, k = move right) where only 1 figure its suppose to move (wich it doesnt happen, the 2 objects move), i require to move only 1 object without using the methods "pushMatrix" and "popMatrix", any ideas? Thank you in advance. Here is my code:



#include <GL/glut.h>
#include <stdio.h>
void display(void)
{

glClearColor(1.0, 1.0, 1.0, 0.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(1.0,0.0,1.0) ;
glutSolidSphere(.30,20,20) ;

glColor3f(0.0,0.0,1.0);
glutWireTorus(0.25,0.75, 28, 28);


glutSwapBuffers();
}

void reshape(int width, int height)
{

glViewport(0, 0, width, height);


glMatrixMode(GL_PROJECTION); //afecta la perspectiva de la proyección

glLoadIdentity();

gluPerspective(60.0, (GLfloat)height / (GLfloat)width, 1.0, 128.0);


glMatrixMode(GL_MODELVIEW);


glLoadIdentity();



gluLookAt(0.0, 1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}


void keyboard(char opcion) {

switch (opcion)
{

case 'u':

glTranslatef(0.0f,0.1f,0.0f);
break;

case 'd':
glTranslatef(0.0f,-0.1f,0.0f);
break;

case 'l':
glTranslatef(-0.1f,0.0f,0.0f);
break;

case 'k':
glTranslatef(0.1f,0.0f,0.0f);
break;


case 'f':
if (glIsEnabled(GL_CULL_FACE))
glDisable(GL_CULL_FACE);
else
glEnable(GL_CULL_FACE);

break;


case 'r':

glRotatef(1.0,1.0,0.0,0.0);
break;

case 't':


glRotatef(1.0,0.0,1.0,0.0);
break;
case 'e':
exit(0);
break;

case 'w':
glRotatef(10,0.0f,0.0f,-1.0f);
break;

case 'x':
glRotatef(10,0.0f,0.0f,1.0f);
break;


}


glutPostRedisplay();


}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(512, 512);
glutInitWindowPosition(20, 20);
glutCreateWindow("TRANSFORMACIONES");

// void display(void)
glutDisplayFunc(display);
// void reshape(int width, int height)
glutReshapeFunc(reshape);
// void keyboard(char opcion)
glutKeyboardFunc(keyboard);


glutMainLoop();
return 0;
}

Carmine
05-17-2016, 01:10 AM
Help us to help you. Put your code inside [ code] and [ /code] tags.
Make it as easy to read as possible. Use indentation.
There are too many blank lines and commented out lines.
For example .....




void keyboard (char opcion)
{
short kul = glIsEnabled(GL_CULL_FACE);
static float a = 0.1, b = 10.0;

switch (opcion) {

case 'u': glTranslatef ( 0, a, 0); break;
case 'd': glTranslatef ( 0, -a, 0); break;
case 'l': glTranslatef (-a, 0, 0); break;
case 'k': glTranslatef ( a, 0, 0); break;

case 'f': if (kul) glDisable(GL_CULL_FACE);
else glEnable (GL_CULL_FACE); break;

case 'r': glRotatef (1, 1, 0, 0); break;
case 't': glRotatef (a, 0, 1, 0); break;
case 'w': glRotatef (b, 0, 0, -1); break;
case 'x': glRotatef (b, 0, 0, 1); break;

case 'e': exit(0); break;
}

glutPostRedisplay();
}


int main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize (512, 512);
glutInitWindowPosition ( 20, 20);
glutCreateWindow ("TRANSFORMACIONES");
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop ();

return 0;
}