Skip to main content
GameDev.net gamedev.net
🔒 Locked

Keyboard Input problem

Started by horizon981 Jun 5, 2006 at 11:43 AM 1 replies 450+ views
Original Post
horizon981
horizon981
I was originally studying lighting. I made a blue sphere that was lit at the top to make it appear more REAL. Then I thought it would be cool if I could see the lights move. Unfortunately, the light positions are fixed (as far as i know). So, the code below tries to move the blue sphere to observe light reflections from various angles. The Problem? I use two variables x and y to store the translational values and increase or decrease them depending on the key pressed. Well, nothing is happening. You can see that i use cout<

//This is the common framework required for all GLUT programs

#include <gl/glut.h>
#include <windows.h>
#define STEP 0.05
#include <iostream>
using namespace std;

GLint Width, Height; 

GLfloat x=0.0f,y=0.0f;

GLfloat diffused[]={0.0f,0.9f,0.1f,1.0f};
GLfloat ambient[]={1.0f,1.0f,1.0f,1.0f};
GLfloat lightpos[]={0.0f,1.0f,1.0f,1.0f};


void RenderScene()
{
	
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glTranslatef(x,y,0.0f);
		
	glColor3f(0.0f,0.0f,1.0f);
	glutSolidSphere(0.5f,20,20);

	glutSwapBuffers();
}

void SetupRC()
{

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_CULL_FACE);

	glLightfv(GL_LIGHT0,GL_DIFFUSE,diffused);
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient);
	glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
	glEnable(GL_LIGHT0);
	
	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT,GL_AMBIENT);
	glClearColor(0.0f,0.0f,0.0f,1.0f);
	
}

void TimerFunction(int value)
{
	
	glutPostRedisplay(); //Refresh the screen
    glutTimerFunc(33,TimerFunction,1);
}


void ChangeSize(int width,int height) 
{
    /* Save the new width and height */
    Width  = width;
    Height = height;

    /* Reset the viewport... */
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
	gluPerspective(45.0, (float)width / (float)height, 0.1, 1000.0);

    glMatrixMode(GL_MODELVIEW);
}


void SpecialFunction(int key,int x,int y)
{
	cout<<x<<","<<y<<" ";

	if(key == GLUT_KEY_UP)
		y += STEP;
	if(key == GLUT_KEY_DOWN)
		y -= STEP;
	if(key == GLUT_KEY_RIGHT)
		x += STEP;
	if(key == GLUT_KEY_LEFT)
		x -= STEP;

	
	glutPostRedisplay();
}


int main()
{

	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutCreateWindow("Simple");
	glutDisplayFunc(RenderScene);
    glutTimerFunc(33,TimerFunction,1);      
	glutSpecialFunc(SpecialFunction);

	SetupRC();
	glutMainLoop();

	return 0;
}


Caste
Caste
Hey,
if you take a look at the names of your variables you'll see that it's logic that you get this error.
Your SpecialFunction has two arguments called x and y, and you have two global variables called the same. But because of namespaces within the SpecialFunction you can only acces the arguments though.
This should work:
...//Renamed argument variables here:void SpecialFunction(int key,int mouse_x,int mouse_y){	cout<<x<<","<<y<<" ";	if(key == GLUT_KEY_UP)		y += STEP;	if(key == GLUT_KEY_DOWN)		y -= STEP;	if(key == GLUT_KEY_RIGHT)		x += STEP;	if(key == GLUT_KEY_LEFT)		x -= STEP;		glutPostRedisplay();}...
horizon981
horizon981
Hey, thanks a lot!
But I still have to ask myself: When will I stop making these STUPID mistakes???

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.