Gaming Homebrew Project OpenGl glTranslatef() only moves object on the z axis.

A_stream

Member
OP
Newcomer
Joined
Apr 20, 2023
Messages
13
Trophies
0
Age
29
XP
265
Country
Bulgaria
Good Day/Night. I'm not sure if I can ask in here, but I'm having a little problem with my prototype game.
So I'm trying to learn OpenGl using GLUT with CodeBlocks, and everything is going well, like, it renders a cube that I can move, but It only moves on the z axis. And I don't know what to do, because I checked everywhere to see if I did something wrong, but there's nothing (I even tried chatGPT to generate me code but it's the same). So I came here to ask if anyone here has any clue on how to fix this, It will be of great help!
Here's the code:
[ICODE] [CODE=c]/* * GLUT Shapes Demo * * Written by Nigel Stewart November 2003 * * This program is test harness for the sphere, cone * and torus shapes in GLUT. * * This was edited by the creator of this game. * Nigel Stewart's code was used as a foundation. */ #ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut.h> #endif #include <stdlib.h> #include <math.h> #define PI 3.14159265358979323846 GLfloat x = 1.0f; GLfloat y = -2.0f; GLfloat z = -7.0f; GLfloat Rotation = 0.0f; GLfloat moveDirectionX = 0.0f; GLfloat moveDirectionZ = -1.0f; // Initial movement direction /* GLUT callback Handlers */ static void resize(int width, int height) { const float ar = (float) width / (float) height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity() ; } static void display(void) { const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; const double a = t*90.0; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3d(0,0,1); glPushMatrix(); glTranslatef(x, y, z); glRotatef(Rotation, 0, 1, 0); glutSolidCube(3); glPopMatrix(); glutSwapBuffers(); } static void key(unsigned char key, int x, int y) { switch (key) { case 27 : exit(0); break; case 'w': z--; break; case 's': z++; break; case 'a': x++; break; case 'd': x--; break; } glutPostRedisplay(); } static void idle(void) { glutPostRedisplay(); } const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f }; const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f }; const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f }; const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat high_shininess[] = { 100.0f }; /* Program entry point */ int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitWindowSize(640,480); glutInitWindowPosition(10,10); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Gemu"); glutReshapeFunc(resize); glutDisplayFunc(display); glutKeyboardFunc(key); glutIdleFunc(idle); glClearColor(1,1,1,1); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); glutMainLoop(); return EXIT_SUCCESS; }[/CODE]
[/ICODE]
 
Last edited by A_stream,

catlover007

Developer
Developer
Joined
Oct 23, 2015
Messages
722
Trophies
1
XP
3,968
Country
Germany
1. please mark your code as code, it makes it soo much easier to read (see the picture how to do it)
2. the issue is probably in this function: static void key(unsigned char key, int x, int y)

the x and y parameter shadow your global x and y variables because they have the same name (i.e. you access them instead of your global x and y variable). Thus you modify the parameters not your global x and y variables.

3. Learning old fashioned OpenGL 1.x is nowdays really not that good of an idea anymore. It doesn't really have anything to do with how GPUs nowdays work, it is only provided through a compability layer of the driver for old applications.

Either try learning modern OpenGL as they call it (https://learnopengl.com/ and https://open.gl/ are good resources) or maybe take a long at https://www.raylib.com/ It works a bit similar to fixed pipeline OpenGL, but you can do a lot more with it.

1713653946697.png
 

A_stream

Member
OP
Newcomer
Joined
Apr 20, 2023
Messages
13
Trophies
0
Age
29
XP
265
Country
Bulgaria
1. please mark your code as code, it makes it soo much easier to read (see the picture how to do it)
2. the issue is probably in this function: static void key(unsigned char key, int x, int y)

the x and y parameter shadow your global x and y variables because they have the same name (i.e. you access them instead of your global x and y variable). Thus you modify the parameters not your global x and y variables.

3. Learning old fashioned OpenGL 1.x is nowdays really not that good of an idea anymore. It doesn't really have anything to do with how GPUs nowdays work, it is only provided through a compability layer of the driver for old applications.

Either try learning modern OpenGL as they call it (https://learnopengl.com/ and https://open.gl/ are good resources) or maybe take a long at https://www.raylib.com/ It works a bit similar to fixed pipeline OpenGL, but you can do a lot more with it.

View attachment 432818
Damn. MY HERO! It works!!!
1. Thanks for showing me, how to make the code more readable(will keep that in mind for the future).
2. Thanks for clearing that up, with the keyboard.
3. Yeah I tried to use modern OpenGl tutorials, but for some reason I couldn't set them up for vsCode or CodeBlocks and I decided to just use the plain old OpenGl template on CodeBlocks and for now it's doing it's job, also don't get me wrong I will try and setup maybe the GLFW template or do something else that uses modern tools. As for Raylib, I tried learning it and was doing a lot of progress in understanding it, but one day it decided to not get my mouse input at all, then decided that It will not draw any objects on screen (not even billboards), then I reinstalled the library but it still didn't work, so I decided to give up.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
  • Quincy @ Quincy:
    Usually when such a big title leaks the Temp will be the first to report about it (going off of historical reports here, Pokemon SV being the latest one I can recall seeing pop up here)
  • K3Nv2 @ K3Nv2:
    I still like how a freaking mp3 file hacks webos all that security defeated by text yet again
  • BigOnYa @ BigOnYa:
    They have simulators for everything nowdays, cray cray. How about a sim that shows you playing the Switch.
  • K3Nv2 @ K3Nv2:
    That's called yuzu
    +1
  • BigOnYa @ BigOnYa:
    I want a 120hz 4k tv but crazy how more expensive the 120hz over the 60hz are. Or even more crazy is the price of 8k's.
  • K3Nv2 @ K3Nv2:
    No real point since movies are 30fps
  • BigOnYa @ BigOnYa:
    Not a big movie buff, more of a gamer tbh. And Series X is 120hz 8k ready, but yea only 120hz 4k games out right now, but thinking of in the future.
  • K3Nv2 @ K3Nv2:
    Mostly why you never see TV manufacturers going post 60hz
  • BigOnYa @ BigOnYa:
    I only watch tv when i goto bed, it puts me to sleep, and I have a nas drive filled w my fav shows so i can watch them in order, commercial free. I usually watch Married w Children, or South Park
  • K3Nv2 @ K3Nv2:
    Stremio ruined my need for nas
  • BigOnYa @ BigOnYa:
    I stream from Nas to firestick, one on every tv, and use Kodi. I'm happy w it, plays everything. (I pirate/torrent shows/movies on pc, and put on nas)
  • K3Nv2 @ K3Nv2:
    Kodi repost are still pretty popular
  • BigOnYa @ BigOnYa:
    What the hell is Kodi reposts? what do you mean, or "Wut?" -xdqwerty
  • K3Nv2 @ K3Nv2:
    Google them basically web crawlers to movie sites
  • BigOnYa @ BigOnYa:
    oh you mean the 3rd party apps on Kodi, yea i know what you mean, yea there are still a few cool ones, in fact watched the new planet of the apes movie other night w wifey thru one, was good pic surprisingly, not a cam
  • BigOnYa @ BigOnYa:
    Damn, only $2.06 and free shipping. Gotta cost more for them to ship than $2.06
    +1
  • BigOnYa @ BigOnYa:
    I got my Dad a firestick for Xmas and showed him those 3rd party sites on Kodi, he loves it, all he watches anymore. He said he has got 3 letters from AT&T already about pirating, but he says f them, let them shut my internet off (He wants out of his AT&T contract anyways)
  • K3Nv2 @ K3Nv2:
    That's where stremio comes to play never got a letter about it
  • BigOnYa @ BigOnYa:
    I just use a VPN, even give him my login and password so can use it also, and he refuses, he's funny.
  • BigOnYa @ BigOnYa:
    I had to find and get him an old style flip phone even without text, cause thats what he wanted. No text, no internet, only phone calls. Old, old school.
  • Psionic Roshambo @ Psionic Roshambo:
    @BigOnYa, Lol I bought a new USB card reader thing on AliExpress last month for I think like 87 cents. Free shipping from China... It arrived it works and honestly I don't understand how it was so cheap.
    +1
    Psionic Roshambo @ Psionic Roshambo: @BigOnYa, Lol I bought a new USB card reader thing on AliExpress last month for I think like 87... +1