Results 1 to 7 of 7

Thread: having trouble with simple program

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    11

    having trouble with simple program

    im trying to get the square thats being drawn to move left or right when the r or s key is pressed, its not really working and i know the solution is simple im just not seeing it at the moment. heres the code.

    /*
    * Main.h
    * Core
    */
    #include <stdlib.h>
    #include <GLUT/glut.h>
    #include <OpenGL/gl.h>

    float x;

    /*
    * Main.cpp
    */

    #include "Main.h"

    void reshape(int width, int height)
    {
    glViewport(0,0,width,height);
    }

    void display(void)
    {
    glLoadIdentity();
    glPushMatrix();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //glLightfv(GL_LIGHT0, GL_POSITION, const GLfloat(30.0f, 30.0f, 30.0f));

    glBegin(GL_QUADS);
    glColor3f(0.0f , 0.0f, 1.0f);
    glVertex3f(-0.1f + x, -0.1f, 0.1f);

    glColor3f(0.0f , 1.0f, 0.0f);
    glVertex3f(-0.1f + x, 0.1f, 0.1f);

    glColor3f(1.0f , 0.0f, 0.0f);
    glVertex3f(0.1f + x, 0.1f, 0.1f);

    glColor3f(1.0f , 1.0f, 0.0f);
    glVertex3f(0.1f + x, -0.1f, 0.1f);
    glEnd();

    glFlush();
    }

    void keyboard (unsigned char key, int x, int y)
    {
    switch (key) {
    case 's':
    case 'S':
    x++;
    glutPostRedisplay();
    break;
    case 'r':
    case 'R':
    x++;
    glutPostRedisplay();
    break;
    case 27:
    exit(0);
    break;
    default:
    break;
    }
    }

    void idle(void)
    {
    glutPostRedisplay();
    }

    int main(int argc, char** argv)
    {
    glutInit(&amp;argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(640,480);

    glutCreateWindow("Core Engine Test");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);
    glutMainLoop();

    return 0;
    }
    [/B]

  2. #2
    Junior Member Newbie
    Join Date
    Aug 2009
    Posts
    7

    Re: having trouble with simple program

    Hm.. can you describe your problem ?
    You do not set initial value to X anywhere, also your keyboard function moves X always to ++ (right?).

    And you do not set matrixes at all (ok you load identitymatrix, but I think there should also be few calls to glOrtho/glFrustum/some other convenience function..
    you set viewport, but a box of size 0.2f*0.2f , at 0.1f units away from you (behind you?) is going to be small. and as you jump to the right 1.0f (everytime you press 's'/'S'/'r'/'R'), it is going to go at warp speed.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    11

    Re: having trouble with simple program

    im just trying to get the square to move and its not moving when i press either key. the speed and sizes i can fix later i just want the overall concept to start working

  4. #4
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: having trouble with simple program

    Quote Originally Posted by Craz1000
    im just trying to get the square to move and its not moving when i press either key. the speed and sizes i can fix later i just want the overall concept to start working
    It would help if you told us what you see on the screen when you run your program.
    Am I doing your homework for you?

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    11

    Re: having trouble with simple program

    .... a square

  6. #6
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: having trouble with simple program

    Quote Originally Posted by Craz1000
    .... a square
    How much of the window does the square cover? Can you do a triangle instead of a square?
    Am I doing your homework for you?

  7. #7
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: having trouble with simple program

    Your problem is that you have a global variable 'x' and a local variable 'x' in your Keyboard function. That's a conflict resulting in x always being set to zero by your keyboard function. An easy fix is to change the global variable to 'q'. So change 'x' to 'q' everywhere except in the line:

    void keyboard (unsigned char key, int x, int y)

    BTW - this is not an OpenGL or graphics problem. It is a basic programming problem. A problem like this is diagnosed by using a debugger to examine the value of x as the program runs, or by putting some print statements in the code to write out the value of x to the console window. If you had done that you would have seen that x was always 0.0, no matter how many times you pushed 'S' or 'R'. Thus your square was not moving. Good luck.
    Am I doing your homework for you?

Similar Threads

  1. New to Opengl, having trouble with program.
    By 3DozenWhales in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 07-25-2013, 10:14 AM
  2. Trouble with simple shooting range.
    By pcormier9988 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-22-2009, 06:22 AM
  3. trouble running a program
    By in forum OpenGL: User Software
    Replies: 0
    Last Post: 12-31-2002, 03:32 PM
  4. trouble running program ...please help.
    By c_anindya in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 11-12-2002, 10:07 AM
  5. Having trouble compiling a program
    By badhairday in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 03-12-2000, 07:49 AM

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