Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: view the scene from all directions

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    8

    view the scene from all directions

    Hi all,

    I am a biginner in opengl, i want to write a program that i can show the environmet from all direction, i don't know how should i do?!!
    it's my code!

    #include <GL/gl.h>
    #include <GL/glut.h>
    #include <iostream>


    using namespace std;


    void init(void);
    void display(void);
    void changeSize(int w, int h);
    void Keyboard(unsigned char key, int x, int y);

    int fov = 50;

    int main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 300);
    glutCreateWindow("test");

    //glutReshapeFunc(changeSize);
    glutKeyboardFunc (Keyboard);
    glutDisplayFunc(display);

    init();

    glutMainLoop();
    }


    void init()
    {
    glClearColor(0.0,0.0,0.0,0.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(20.0, 1, 2.0, 40.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();View all Si
    gluLookAt(0, 17, 19,0, 0, 0,0, 1, 0);

    //glRotated(-45,0,0,1);
    glRotated(90,1,0,0);
    }


    void display()
    {
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fov, 1, 2.0, 40.0);

    // glMatrixMode(GL_MODELVIEW);
    // glLoadIdentity();
    // glRotated(45,0,1,0);

    glColor3f(1.0,0.0,0.0);
    glBegin(GL_QUADS);
    glVertex3f(-6.5f, -3.5f, -6.5f);
    glVertex3f(-6.5f, -3.5f, 6.5f);
    glVertex3f(6.5f, -3.5f, 6.5f);
    glVertex3f(6.5f, -3.5f, -6.5f);
    glEnd();

    glFlush();
    }


    void changeSize(int w, int h)
    {
    if(h == 0)
    h = 1;
    float ratio = 1.0* w / h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, w, h);
    gluPerspective(90,ratio,1,1000);
    }


    void Keyboard(unsigned char key, int x, int y)
    {
    switch (key)
    {
    case 43:
    fov += 5;
    break;//+
    case 45:
    fov -= 5;
    break;//-
    }

    glutPostRedisplay();
    }
    Last edited by new_beginner; 09-23-2013 at 06:38 AM.

  2. #2
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    8
    anyone can't help me! my exact mean is that i want to turn camera in around of my planar or for example a cube!?

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,213
    I'm not going to help you but I am going to give you some advice. What you've basically done here is given a code dump (without even using code tags) and said "somebody make this work for me". I don't know if that was your intention, but that's how it comes across, and that kind of question tends not to get a good reception anywhere. You need to at least show evidence that you've tried something, you need to give specifics of what you've tried, you need to give specifics of what didn't work when you tried it. Do that and you stand a much better chance of getting a helpful answer; otherwise the question is likely to stay ignored.

  4. #4
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    188
    ... i want to write a program that i can show the environmet from all direction, i don't know how should i do?!
    Have to agree with mhagain. One thing you've done right is post some code. That's a start. But you should also show that you've put in some time to try to solve the problem yourself. How do we know you even wrote this code? Maybe you just cut and paste someone else's code. If it doesn't look like you've put in any work, why would anyone put in the time to help you out. One thing someone should say on this forum, if it already hasn't been said. The reason for posting code is that it (should) be much easier to read than straight text. But that's ONLY true if you've used indenting. For example, my way of writing your 'Keyboard' routine is below. Others might do it differently, but I guarantee that their code would be easier to read than yours.

    Code :
    void Keyboard (unsigned char key, int x, int y)
    {
       switch (key)  {
          case 43:   fov += 5;  break;  //+
          case 45:   fov -= 5;  break;  //-
       }
     
       glutPostRedisplay();
     
    }

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    8
    Hi again,

    excuse me, i was so tired last night and i didn't write much explanation, by the way i tried to use code tags but i can't find it in above menu and i don't know what is code tags in this forum, also i tried to understood it with viewing page source but it's so much and i didn't have time to read all of html! by the way, also my original code has intenting but when i copied there in text box, format of it was changed!
    it's my code, it's result of my working for whole of the saturday! because it's my first try to write opengl code, may be i should explain more!
    i want to write a stack box game for my homework, i'm a master student and my research area is robotic, but i've taken geraphics course this semster but i don't have any expriences in opengl! and it takes time until i understand all things in opengl! for my homework we should create a floor planar with perspective projection that i wrote them, also we should have zoom in/out that also i wrote it with chaning FOV! and also we should show the sence from all direction, also i tried for it, but i am not sure, i think for it i should try with lookat function, but i don't know i should rotate the camera or just with changing coordination of camera i can solve it, i try with changing coordinates in lookat but i think it's so meaningless (also you can see in code i commented rotation afte lookat!!!)! i think there is sould be a logical method to solev it but i can't find it! so just i tried to ask for it from this forum! i apperciate it if anyone can help me or guide me to a online example about it! and i copied the code just because to clear my question! and also excuse me but i've writen code since 2006! and i can't say all becuase it's not possible someone say that i know all thing, but i know how to write code, and it's just my try to writing this program! by the way thank you for reading my question and your advices.
    Last edited by new_beginner; 09-23-2013 at 11:48 AM.

  6. #6
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    188
    The code tags are [ code] and [ /code], without the spaces. I've found that rotating the objects in the scene is much easier than messing with LookAt. Add two more global variables (like FOV) to your code. Call them xrot and yrot, both initialized to zero. Then add some more 'cases' to your Keyboard command which increase and decrease xrot and yrot. Next put two glRotatef commands in your 'Display' routine just above the 'glColor' command. Rotate around the x axis by xrot, and the the y axis by 'yrot'. Play with the order of the rotate commands until you get the behavior you want. Also uncomment glMatrixMode and glLoadIdentity. Let us know what happens.

    Did you know that you can write case 'x' in a switch command? You don't have to use the numbers that correspond to a key.
    Last edited by Carmine; 09-23-2013 at 03:35 PM.

  7. #7
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    8
    Quote Originally Posted by Carmine View Post
    The code tags are [ code] and [ /code], without the spaces. I've found that rotating the objects in the scene is much easier than messing with LookAt. Add two more global variables (like FOV) to your code. Call them xrot and yrot, both initialized to zero. Then add some more 'cases' to your Keyboard command which increase and decrease xrot and yrot. Next put two glRotatef commands in your 'Display' routine just above the 'glColor' command. Rotate around the x axis by xrot, and the the y axis by 'yrot'. Play with the order of the rotate commands until you get the behavior you want. Also uncomment glMatrixMode and glLoadIdentity. Let us know what happens.

    Did you know that you can write case 'x' in a switch command? You don't have to use the numbers that correspond to a key.

    thank you so much man, yes it's work! just now i have some questions, it's so good if you can help me again:
    1. i read some tutorials about glmatrixmode but i can't understand exactly when i should call it?!
    2. i put a variable in my display function that i want decrease it by a constant value in each step when i put a number for decreasing it, it works but when i define a static/global variable and i use it as a decreasing value, it doesn't work?!
    3. An important question, as i said i want to write a stack game, so when my first cube reach to floor, i should generate another cube, so i want to know how can i generate cube dynamically, i hope my explanatin would be clear!
    Last edited by new_beginner; 09-23-2013 at 05:52 PM.

  8. #8
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    495
    Quote Originally Posted by new_beginner View Post
    i read some tutorials about glmatrixmode but i can't understand exactly when i should call it
    All matrix operations affect the current matrix. glMatrixMode() controls which matrix (model-view, projection, texture or colour) is the current matrix. Most programs need to use the projection and model-view matrices. The projection matrix is typically set once (either prior to rendering each frame, or when the window size changes). The model-view matrix is often set for each object in the scene. The texture and colour matrices are less frequently used.

    Also, the whole "current matrix" system is deprecated or removed in "modern" OpenGL (OpenGL 3+ core profile, OpenGL ES 2+, WebGL). Matrices are just one of the types which can be used by shader variables.

    Quote Originally Posted by new_beginner View Post
    3. An important question, as i said i want to write a stack game, so when my first cube reach to floor, i should generate another cube, so i want to know how can i generate cube dynamically, i hope my explanatin would be clear!
    You need to create variables (e.g. an array) which hold information about the cubes (how many, their positions, etc), then generalise your rendering code to render "a cube" rather than "the cube".

    But this is more of a general "how do I program" question than "how do I use OpenGL".

  9. #9
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    8
    thank you man!
    upssss! just i need to "how do i use Opengl"!
    ok man, i did your method, there is another problem, now i have an array that keeps information about my cubes, but when i changed to another ones, my past cube is disapeared, indeed it is changed to new one! but i want to have seperate cubes! i mean i want to create a array of glsolidcube that each of them has own specicifaction or i need to a function to dynamically generate another cube! i find this link, but i am not sure is ti solution for me, or there is others solution!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •