Basically I placed my textures and everything on the window and now I want my sphere or the ball to move left or right, I have the special key code, etc but for some reason still my ball isn't moving left or right!?
Why is this happening please check my code
I WILL HIGHLIGHT THE IMPORTANT MOVEMENT RELATED CODES IN BOLD
//Windows - uncomment the includes below
#include "shared/gltools.h" //OpenGL toolkit - in the local
shared folder
#include <math.h> //These were included in
glm.cpp so I kept them here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "shared/glm.h"
#include <iostream> //Needed for console output
//------------------------------------//
void drawTexturedQuad(int image)
{
//add some lighting normals for each vertex
//draw the texture on the front
glEnable(GL_TEXTURE_2D);
// glFrontFace(GL_CW); //use glFrontFace(GL_CW) to texture the other side - not needed
here as we set this elsewhere
glColor3f(0.8, 0.8, 0.8);
glEnable( GL_TEXTURE_2D );
//bind the texture
glBindTexture(GL_TEXTURE_2D, textures[image]);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0,0.0);
glVertex3f(-50.0, 0.0,100.0);
glTexCoord3f(1.0,0.0,0.0);
glVertex3f(50.0, 0.0,100.0);
glTexCoord2f(1.0,1.0);
glVertex3f(50.0, 100.0,100.0);
glTexCoord2f(0.0,1.0);
glVertex3f(-50.0, 100.0,100.0);
glEnd();
glDisable( GL_TEXTURE_2D );
}
// Called to draw scene
//FUNCTION FOR MY BALL
void ball(){
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
//specify the vertices (points in 3D space) of the shape - note that these are 2D points
glutSolidSphere( 5.0, 20.0, 20.0);
glEnd();
glFlush();
}
void SpecialKeys(int key, int x, int y) // a function for the movement key - car moves
{
if(key == GLUT_KEY_UP){
Bally = Bally + Speed;
}
if(key == GLUT_KEY_DOWN){
Bally = Bally - Speed;
}
if(key == GLUT_KEY_LEFT){
Ballx = Ballx - Speed;
}
if(key == GLUT_KEY_RIGHT){
Ballx = Ballx + Speed;
}
glutPostRedisplay();
}
void RenderScene(void)
{
// Clear the window with current clearing colour
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
//rotate and then translate the quad
glScalef(4,4,4);
glTranslatef(0.0, -20.0, 50.0);
//try setting to -185.0 to see the back of the quad
ball();
glPopMatrix();
// a test point
//use for locating points in your view
/* glEnable(GL_POINT_SMOOTH);
glPointSize(10.0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex3f(0.0f, 0.0f, 100.0f);
glEnd();
*/
glPushMatrix();
//rotate and then translate the quad
glTranslatef(-10.0, -300.0, 50.0);
glRotatef(-70.0, 1.0, 0.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(8.0,7.0,1.5);
drawTexturedQuad(IMAGE2);
glPopMatrix();
////////////////////////////////////////////////////////////// main wall
glPushMatrix();
//rotate and then translate the quad
glTranslatef(-5.0, -20.0, 0.0);
//glRotatef(-45,0.0 ,1.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(2.4,0.5,1.0);
drawTexturedQuad(IMAGE3);
glPopMatrix();
////////////////////////////////////////////////////////////// left brick
glPushMatrix();
//rotate and then translate the quad
glTranslatef(-290.0, -35.0, 50.0);
glRotatef(100.0, 0.0 ,1.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(4.0,0.88,1.0);
drawTexturedQuad(IMAGE4);
glPopMatrix();
///////////////// right brick
glPushMatrix();
//rotate and then translate the quad
glTranslatef(310.0, -35.0, 50.0);
glRotatef(-90, 0.0 ,1.0, 0.0);
//try setting to -185.0 to see the back of the quad
glScalef(4.0,0.88,1.0);
drawTexturedQuad(IMAGE5);
glPopMatrix();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
//textures
GLuint texture;
// allocate a texture name
glGenTextures( 1, &texture );
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
//make sure any TGA has no alpha channel - photoshop is a good converter to targa
(TGA)
//the gltLoadTGA method is found in gltools.cpp and is from the OpenGL SuperBible
book
//there are quite a few ways of loading images
// Load textures in a for loop
glGenTextures(TEXTURE_COUNT, textures);
//this puts the texture into OpenGL texture memory
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
for(int iLoop = 0; iLoop < TEXTURE_COUNT; iLoop++)
{
// Bind to next texture object
glBindTexture(GL_TEXTURE_2D, textures[iLoop]);
// Load texture data, set filter and wrap modes
//note that gltLoadTGA is in the glm.cpp file and not a built-in openGL function
pBytes0 = gltLoadTGA(textureFiles[iLoop],&iWidth, &iHeight,
&iComponents, &eFormat);
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW);// Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Calculate aspect ratio of the window
fAspect = (GLfloat)w/(GLfloat)h;
// Set the perspective coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// field of view of 45 degrees, near and far planes 1.0 and 1000
//that znear and zfar should typically have a ratio of 1000:1 to make sorting out z
depth easier for the GPU
gluPerspective(55.0f, fAspect, 1.0, 1000.0);
// Modelview matrix reset
glMatrixMode(GL_MODELVIEW);
//pull the eye position back to view the scene
gluLookAt(0.00,0.00,400.0,//eye
0.00,0.00,0.00,//centre
0.00,1.00,0.00);//up
}
Where is ballX and ballY actually taken into account when drawing the ball? Aside from initializing them and changing them when keys are pressed (which probably isn't what you meant to do,) you access them nowhere else in the code.
"Where is ballX and ballY actually taken into account when drawing the ball? Aside from initializing them and changing them when keys are pressed (which probably isn't what you meant to do,) you access them nowhere else in the code."
k so I did this as well
and it's still not working
for my first assignment i made an object and moved it, for this I just did the same thing and can't move the ball.. the only difference is I have textures as well in this assignment, can that be affecting anything?
It's been a week and I still haven't solved this problem i absolutely hate programming and opengl i just want to get this assignment over and done with, i've had enough.
i declared ballX and Bally in the translation code for the ball
glTranslatef (Ballx, Bally-20.0, 50.0)
But it still ain't moving.
I want the ball to move left and right with arrow keys, once I figure this out I will move the ball forward with the z key. but on z axis... Logically it's easy but for some reason when it comes to programming it, it's so hard
You should just be able to right
if ball (variable) == key > right.... ball move right by x (the integer the ball has to move by) but no it has to be complicated.