c++ - switch between glOrtho and gluPerspective -


i'm working in little opengl - glut program (i'm totally noob), , i'm having many problems switching between gluperspective , glortho pressing key (for example 'p').

i've take screenhots illustrate problem... using gluperspective , glortho

and here's code...

#if defined(__apple__)     #include <opengl/opengl.h>     #include <glut/glut.h> #else     #include <gl/gl.h>     #include <gl/greeglut.h> #endif  #include <iostream> #include <math.h> #include "model.h"  using namespace std;  // actual vector representing camera\'s direction float lx = 0.0f,lz = -1.0f; // xz position of camera float x = 0.0f,z = 5.0f; // angle rotating triangle float angle = 0.0f; float fov = 45.0;  model m; model m1; model m2; model m3;  double maxx, maxy, maxz; double minx, miny, minz; double centx, centy, centz; double maxtam;  bool persp = true;  double min(double x, double y) {     if(x < y) return x;     return y; }  double max(double x, double y) {     if(x > y) return x;     return y; }  void setminmaxxyz(void) {     maxx = minx = m.vertices()[0];     maxy = miny = m.vertices()[1];     maxz = minz = m.vertices()[2];      for(int = 3; < m.vertices().size(); += 3) {         maxx = max(maxx,m.vertices()[i]);         minx = min(minx,m.vertices()[i]);          maxy = max(maxy,m.vertices()[i+1]);         miny = min(miny,m.vertices()[i+1]);          maxz = max(maxz,m.vertices()[i+2]);         minz = min(minz,m.vertices()[i+2]);     }      centx = ((maxx - minx)/2) + minx;     centy = ((maxy - miny)/2) + miny;     centz = ((maxz - minz)/2) + minz; }  void changeview(void) {     int w = glutget(glut_window_width);     int h = glutget(glut_window_height);     float ratio = w * 1.0 / h;      glmatrixmode(gl_projection);     glloadidentity();      if(persp) gluperspective(fov, ratio, 0.1f, 100.0f);     else glortho(-1,1,-1,1,-0.5,100.0);      glmatrixmode(gl_modelview); }  void changesize(int w, int h) {     // prevent divide zero, when window short     // (you cant make window of 0 width).     if(h == 0) h = 1;     if(w == 0) w = 1;      float ratio = w * 1.0 / h;      glmatrixmode(gl_projection);     glloadidentity();      if(persp) gluperspective(fov, ratio, 0.1f, 100.0f);     else glortho(-1,1,-1,1,0.1,100.0);      glviewport(0,0,w,h);      glmatrixmode(gl_modelview);      glutpostredisplay(); }  void renderscene(void) {     glclear(gl_color_buffer_bit | gl_depth_buffer_bit);      // reset transformations     glloadidentity();      // posicionament de la càmera     glulookat(  x, 1.0f, z,                 x+lx, 1.0f, z+lz,                 0.0f, 1.0f, 0.0f);      glclearcolor(0.5,0.5,1.0,1.0);      // dibuix terra     glcolor3f(0.0f, 255.0f, 0.0f);     glbegin(gl_quads);         glvertex3f(-5.0f, 0.0f, -5.0f);         glvertex3f(-5.0f, 0.0f, 5.0f);         glvertex3f( 5.0f, 0.0f, 5.0f);         glvertex3f( 5.0f, 0.0f, -5.0f);     glend();      // models .obj     (int = 0; < 3; ++i) {          float transx, transy, transz;         if(i == 0) {             m = m1;             transx = -1.25; transy = 0.5; transz = -2.0;          } else if(i == 1) {             m = m2;             transx = -0.5; transy = 0.5; transz = 2.5;          } else {             m = m3;             transx = 2.5; transy = 0.25; transz = -0.5;         }          setminmaxxyz();         maxtam = max(max(maxx - minx, maxy - miny), maxz - minz);          glpushmatrix();             gltranslated(-(centx / maxtam), -(centy / maxtam), -(centz / maxtam));             gltranslated(transx,transy,transz);             glscaled(1.0/maxtam,1.0/maxtam,1.0/maxtam);              glbegin(gl_triangles);                 for(int = 0; < m.faces().size(); i++){                     const face &f = m.faces()[i];                     glcolor3f(materials[f.mat].diffuse[0],materials[f.mat].diffuse[1],materials[f.mat].diffuse[2]);                      for(int j = 0; j < 3; j++)                         glvertex3dv(&m.vertices()[f.v[j]]);                     }             glend();         glpopmatrix();     }      glutswapbuffers(); }  void processkeys(unsigned char key, int x, int y) {     if (key == 27)         exit(0);      else if(key == 'p'){         persp = not persp;         changeview();     } }  void processspecialkeys(int key, int xx, int yy) {     float fraction = 0.1f;      switch (key) {         case glut_key_left :             angle -= 0.01f;             lx = sin(angle);             lz = -cos(angle);         break;          case glut_key_right :             angle += 0.01f;             lx = sin(angle);             lz = -cos(angle);         break;          case glut_key_up :             x += lx * fraction;             z += lz * fraction;         break;          case glut_key_down :             x -= lx * fraction;             z -= lz * fraction;         break;     } }  void idle(void) {     glutpostredisplay(); }  void iniview(void) {     int w = glutget(glut_window_width);     int h = glutget(glut_window_height);     float ratio = w * 1.0 / h;      glmatrixmode(gl_projection);     glloadidentity();      gluperspective(45.0f, ratio, 0.1f, 100.0f);     //glortho(-1,1,-1,1,0.01,1000);      glmatrixmode(gl_modelview);  }  int main(int argc, char **argv) {      // init glut creació finestra     glutinit(&argc, argv);     glutinitdisplaymode(glut_depth | glut_double | glut_rgba);     glutinitwindowposition(100,100);     glutinitwindowsize(800,800);     glutcreatewindow("idi: bloc 3 - càmeres perspectives");      // carregar models .obj     m1.load("model/legoman-assegut.obj");     m2.load("model/shaun_hastings.obj");     m3.load("model/porsche.obj");      // crides     glutdisplayfunc(renderscene);     glutreshapefunc(changesize);     glutidlefunc(idle);     glutkeyboardfunc(processkeys);     glutspecialfunc(processspecialkeys);      iniview();      // opengl init     glenable(gl_depth_test);      // loop     glutmainloop();      return 1; } 

it's supposed should see green "floor" in glortho view... i'm doing wrong??

ps. model objects .obj files delivered teacher.

edit:

finally, glortho works properly. now... i've got question, how can maximize window , (in glortho mode) image not deform??

in changesize() function... when i'm using gluperspective works fine, not glortho!!

the fact don't see floor orthographic projection expected given way set modelview matrix: happens because floor parallel xz plane - , view vector is, too.

// posicionament de la càmera glulookat(  x, 1.0f, z,             x+lx, 1.0f, z+lz,             0.0f, 1.0f, 0.0f); 

the first 3 arguments of glulookat xyz-components of camera position, next 3 arguments xyz of 'center of interest'. vector between 2 points view vector, , y-component 0, meaning parallel xz , floor.

if want see floor + orthographic projection, you'll have tilt camera looks down.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -