PDA

View Full Version : having trouble with simple program



Craz1000
12-09-2009, 07:47 PM
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]

Mikael Korpela
12-10-2009, 05:22 AM
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.

Craz1000
12-10-2009, 05:43 PM
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

MaxH
12-10-2009, 05:57 PM
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.

Craz1000
12-10-2009, 06:03 PM
.... a square

MaxH
12-11-2009, 12:17 AM
.... a square How much of the window does the square cover? Can you do a triangle instead of a square?

MaxH
12-11-2009, 12:55 PM
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.