-1

I'm currently working on a basic GUI that create and draw a robot in a 3d space, I'm using OpenGL and freeglut to deal with the 3d part.

Until last week, I was ignoring all the perspective stuff like 'gluLookAt' or 'gluPerspective' ...

Now, I would like to add those things in order to get basic camera movement (rotation, zoom, translation) with user input.

But i'm stuck cause whenever I try to add the perspective part to my code, I'm not able to get my beautiful robot anymore.

here's my current code :

void drawScene(void) {
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glMatrixMode(GL_MODELVIEW);
glColor3f(0.0f, 0.0f, 0.0f);
ortho();
robot.draw(); // only sone basic lines and quads
glLoadIdentity();
sprintf(title, "robot creation link:%i/joint:%i", robot.linkNumber,       robot.jointNumber);
glutSetWindowTitle(title);
glFlush();
}


int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE|GLUT_MULTISAMPLE);
glutInitWindowPosition(0,0);
glutInitWindowSize(1360,768);
glEnable(GL_MULTISAMPLE_ARB | GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
id = glutCreateWindow("robot creation");

glutDisplayFunc (drawScene);
glutKeyboardFunc(keyboardHandler);
glutSpecialFunc (specialKeyHandler);
glutMouseFunc   (mouseHandler);
glutReshapeFunc (reshapeHandler);

glutMainLoop();
return 0;
}

I wonder if my code need to be completly re-done to work properly with such things or if I'm not using them properly.

Atm I've tried to add this after the window creation :

glViewport(0, 0, 1360, 768);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(180.0f,1360.0f/768.0f,0.1f,1000.0f);

and this in the drawScene function after the drawing part :

gluLookAt(
    10.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f
);

I know I'm facing the object because I can see a dot in the center of the screen that came from the robot.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Did you remove the `ortho()` call from the first version of the code? Otherwise you're setting two projections. – Reto Koradi May 11 '15 at 13:59
  • ortho is a simple function that draw x, y and z axis, this is not a existing function in opengl. – Benjamin fraquet May 11 '15 at 14:12
  • I know it's not an OpenGL function. But I thought it might set up an orthographic projection. Before posting the code here, it's best if you remove all code that is not needed to reproduce the problem. – Reto Koradi May 11 '15 at 14:27
  • If you still see a point that should be your robot, most probably you are just to far away to see its details. Move your camera closer to the robot (1.0f, 0.0f, 0.0f) if the robot is positioned at the center and see if the problem still exists. Using perspective will make objects that are far away appear smaller. – mikyra May 11 '15 at 17:57
  • @Benjaminfraquet: "and this in the drawScene function after the drawing part". That's conceptually wrong. You have to set the matrices (like any other state) before the draw calls to have any effect. Now it is not clear where you excatly put it in relation to that strange after-drawing `glLoadIdentity()`. – derhass May 11 '15 at 18:32
  • @Benjaminfraquet: There are some other issues with this code. THere are some GL calls (`glEnable()`, `glDepthFunc()`) _before_ GLUT had a achance to create a context, which is totally undefined behavior and not going to work. Furthermore, the `glEnable` call is completely wrong, as those enums are not single bits, so using the bitwise OR of sifferent enum values will result in total garbage. – derhass May 11 '15 at 18:34

1 Answers1

0

You have both matrix modes (model view and projection. It is better to activate one. For gmu perspective try something like gluPerspective(170, 1.33, 0.00001, 1000); or put the camera closer to check if you can see a difference in the object. If you are not able to see the object your matrices are overwriting each other. You can check their values by:Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX, modelMatrix); Gl.glGetDoublev(Gl.GL_PROJECTION_MATRIX, projMatrix);.

Another option is also gluunproject which is easier to work than look at function (in my opinion)

Good Luck
  • 1,104
  • 5
  • 12