Results 1 to 2 of 2

Thread: openGL 3D room scene

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    2

    openGL 3D room scene

    Please, i'm trying to create a 3D table and a chair in a room, i want to implement the capabilities to switch camera too! please any help would be appreciated.
    Here is what i have so far .
    Thanks.



    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <GL/glut.h>

    void myInit(int *, char **);
    void mouseManager(int, int, int, int);
    void displayFunc(void);
    void idle(void);

    GLfloat dx=0.0, dy=0.0, dz=0.0, xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
    int spinning=0, res;

    float v[4][3]= {{0.0,0.0,1.0},{0.0,0.9428,-0.3333},
    {-0.8165,-0.4714,-0.3333},{0.8165,-0.4714,-0.3333}};

    GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
    GLfloat diffuse[] = {0.9, 0.8, 0.0, 1.0};
    GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};

    GLfloat global_ambient[] = {0.1, 0.1, 0.1, 1.0};

    GLfloat light0_ambient[] = {1.0, 0.0, 0.0, 1.0};
    GLfloat light0_diffuse[] = {1.0, 0.0, 0.0, 1.0};
    GLfloat light0_specular[] = {1.0, 1.0, 1.0, 1.0};
    GLfloat light0_position[] = {1.0, 2.0, 3.0, 1.0};


    int main(int argc, char **argv){

    myInit(&amp;argc, argv);

    glutDisplayFunc(displayFunc);
    glutMouseFunc(mouseManager);
    glutIdleFunc(idle);

    glutMainLoop(); //infinite loop
    }

    void myInit(int *argc, char **argv){
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100, 150);
    glutCreateWindow(argv[0]);

    glClearColor(1.0,1.0,1.0,0.0); // Set clear color to white

    glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light0_position);

    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);

    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);

    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glShadeModel(GL_FLAT);

    glEnable(GL_DEPTH_TEST);
    }


    void displayFunc(void){
    int i;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(40.0, // field of view
    640.0/480.0, // Aspect ratio
    1.0, // Near Z
    16.0 // Far Z
    );
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(0.0, 0.0, 6.0, // Eye is at (0, 0, 6)
    0.0, 0.0, 0.0, // center is at (0,0,0)
    0.0, 1.0, 0.0); // up is Y direction

    glRotatef(xAngle, 1.0, 0.0, 0.0);
    glRotatef(yAngle, 0.0, 1.0, 0.0);
    glRotatef(zAngle, 0.0, 0.0, 1.0);

    glBegin(GL_QUADS); //begin the four sided shape
    glColor3f(1.0, 0.0, 1.0);
    glVertex3f(-0.9, -0.9, 0.0); //first corner at -0.5, -0.5
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(-0.7, 0.7, 0.0); //second corner at -0.5, 0.5
    glColor3f(1.0, 1.0, 0.0);
    glVertex3f(0.9, 0.9, 0.0); //third corner at 0.5, 0.5
    glColor3f(0.0, 1.0, 1.0);
    glVertex3f(0.7, -0.7, 0.0); //fourth corner at 0.5, -0.5
    glEnd(); //end the shape we are currently working on
    glutSwapBuffers();

    }

    void mouseManager(int button, int state, int x, int y){

    switch(button){
    case GLUT_LEFT_BUTTON:
    if(state==GLUT_DOWN){
    dx=0.2;
    spinning=1;
    }
    else{
    dx=0.0;
    spinning=0;
    }
    break;

    case GLUT_RIGHT_BUTTON:
    if(state==GLUT_DOWN){
    dy=0.2;
    spinning=1;
    }
    else{
    dy=0.0;
    spinning=0;
    }
    break;

    case GLUT_MIDDLE_BUTTON:
    if(state==GLUT_DOWN){
    dz=0.2;
    spinning=1;
    }
    else{
    dz=0.0;
    spinning=0;
    }
    break;
    defaut:
    ;
    }

    displayFunc();
    }

    void idle(void){

    if(spinning){
    xAngle+=dx;
    yAngle+=dy;
    zAngle+=dz;
    };

    glutPostRedisplay();
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jul 2005
    Location
    Pennsylvannia
    Posts
    103

    Re: openGL 3D room scene

    Hi, Nikiben! :]

    Well maybe you could tell us, whats the problem with your code? Is it not working? Or are you not sure what to do next? If so.. what is it you want to do next?

    This board is for asking questions, and solving problems, but it is not a place where people give you lessons, or write code for you. Thats your job! ;]
    Striving for proficiency...

Similar Threads

  1. Building a room with openGL in Java
    By keselme in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 06-14-2016, 09:22 AM
  2. OpenGl 3D Room
    By Torrtuga in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 04-28-2015, 12:16 PM
  3. OpenGL room scene help
    By Nikiben in forum OpenGL: User Software
    Replies: 0
    Last Post: 11-26-2009, 04:13 PM
  4. Room design
    By ben1441 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 08-11-2003, 01:47 PM
  5. OpenGL beginner IRC chat room.
    By nexusone in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 09-26-2002, 01:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean