| Tezelia (3) | |
|
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 (debugging) GLfloat Ballx = 0.0; GLfloat Bally = 0.0; GLfloat Speed = 3.0; //note that this needs gltools.h and gltools.cpp to be included in the shared directory #define IMAGE1 0 #define IMAGE2 1 #define IMAGE3 2 #define IMAGE4 3 #define IMAGE5 4 #define TEXTURE_COUNT 5 GLuint textures[TEXTURE_COUNT]; const char *textureFiles[TEXTURE_COUNT] = {"padhamsBG.tga", "grass_diff.tga","brick_texture.tga","brick_textur e.tga","brick_texture.tga"}; GLfloat mKa[4] = {0.11f,0.06f,0.11f,1.0f}; //ambient GLfloat mKd[4] = {0.43f,0.47f,0.54f,1.0f}; //diffuse GLfloat mKs[4] = {1.0f,1.0f,1.0f,1.0f}; //specular GLfloat mKe[4] = {0.5f,0.5f,0.0f,1.0f}; //emission // Useful lighting colour values GLfloat whiteLightBright[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat redLight[] = { 1.0f, 0.0f, 0.0f, 1.0f }; GLfloat whiteLightLessBright[] = { 0.8f, 0.8f, 0.8f, 1.0f }; GLfloat lightPos[] = { 100.0f, 100.0f, 50.0f, 1.0f }; GLint iWidth, iHeight, iComponents; GLenum eFormat; //------------------------------------// 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(); */ ///////////////////////////////////////////////////////////////////// backgroujnd glPushMatrix(); //rotate the quad slightly glTranslatef(0.0, -190.0, -350.0); // glRotatef(5.0, 0.0, 1.0, 0.0); glScalef(10.0,5.5,1.0); drawTexturedQuad(IMAGE1); glPopMatrix(); ///////////////////////////////////////////////////////// grass 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); glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes0); //set up texture parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //try these too // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD); // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); free(pBytes0); } //enable textures glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); // Hidden surface removal glFrontFace(GL_CCW);// Counter clock-wise polygons face out glEnable(GL_CULL_FACE); // Do not calculate inside // glCullFace(GL_FRONT_AND_BACK); // Enable lighting // glEnable(GL_LIGHTING); glEnable(GL_POINT_SMOOTH); // Setup and enable light 0 glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLightLe ssBright); glLightfv(GL_LIGHT0,GL_DIFFUSE,redLight); glLightfv(GL_LIGHT0,GL_POSITION,lightPos); glEnable(GL_LIGHT0); // Enable colour tracking glEnable(GL_COLOR_MATERIAL); // Set Material properties to follow glColor values glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); // Black blue background glClearColor(0.0f, 0.0f, 0.03f, 1.0f ); } void TimerFunc(int value) { glutSwapBuffers(); glutPostRedisplay(); glutTimerFunc(25, TimerFunc, 1); } void ChangeSize(int w, int h) { GLfloat fAspect; // Prevent a divide by zero if(h == 0) h = 1; // 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 } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(800, 600);// a 4:3 ratio glutCreateWindow("Textures Tutorial"); glutReshapeFunc(ChangeSize); glutDisplayFunc(RenderScene); glutTimerFunc(25, TimerFunc, 1); SetupRC(); glutMainLoop(); glutSpecialFunc(SpecialKeys); return 0; } | |
|
|
|
| cire (2355) | |
| 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. | |
|
|
|
| Tezelia (3) | |
|
"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? glPushMatrix(); glScalef (4.0, 4.0, 4.0); glTranslatef (Ballx, Bally-20.0, 50.0); ball(); glPopMatrix(); | |
|
|
|
| Tezelia (3) | |
|
Anybody? 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. | |
|
Last edited on
|
|