I have a problem to scale an object without changing translated position. I tried to apply translate and scaling using keybind for my car but when i click the keybind for scaling, only my body repositioned it self but the car's tire scaled at the fixed position i translated. Below is my code:
#include <windows.h>
#include <gl/glut.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
const float steps = 100;
const float angle = 3.1415926 * 2.0 / steps;
float m = 0;
float s = 1;
void wheel(float x, float y)
{
float w;
glBegin(GL_POLYGON);
glColor3ub(0, 0, 0);
for (int i = 0; i < 360; i++)
{
w = (i*3.1416 / 180);
glVertex2f(x + 0.04 * s * cos(w), y + 0.04 * s * sin(w));
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3ub(48, 51, 64);
glVertex2f(-1, 1); glColor3ub(48, 51, 64);
glVertex2f(1, 1); glColor3ub(11, 11, 16);
glVertex2f(1, -0.1); glColor3ub(11, 11, 16);
glVertex2f(-1, -0.1);
glEnd();
//[2] Floor
glBegin(GL_QUADS);
glColor3ub(51, 25, 0);
glVertex2f(-1, -0.1); glColor3ub(51, 25, 0);
glVertex2f(1, -0.1); glColor3ub(102, 51, 0);
glVertex2f(1, -1); glColor3ub(102, 51, 0);
glVertex2f(-1, -1);
glEnd();
//[3-1] Road
glBegin(GL_QUADS);
glColor3ub(26, 26, 26);
glVertex2f(-1, -0.2); glColor3ub(26, 26, 26);
glVertex2f(1, -0.2); glColor3ub(40, 40, 40);
glVertex2f(1, -0.9); glColor3ub(40, 40, 40);
glVertex2f(-1, -0.9);
glEnd();
//[3-2] Road
glBegin(GL_QUADS);
glColor3ub(248, 210, 16);
glVertex2f(-0.9, -0.52); glColor3ub(248, 210, 16);
glVertex2f(-0.7, -0.52); glColor3ub(248, 210, 16);
glVertex2f(-0.7, -0.58); glColor3ub(248, 210, 16);
glVertex2f(-0.9, -0.58);
glEnd();
//[4] Star
glBegin(GL_POLYGON);
glEnd();
//[5] Building
glBegin(GL_POLYGON);
glEnd();
//[6] Car
glBegin(GL_POLYGON);
glColor3ub(65, 105, 225);
glVertex2f(m + (s * 0.50), s * -0.7);
glVertex2f(m + (s * 0.52), s * -0.6);
glVertex2f(m + (s * 0.80), s * -0.6);
glVertex2f(m + (s * 0.86), s * -0.7);
glVertex2f(m + (s * 0.86), s * -0.8);
glVertex2f(m + (s * 0.44), s * -0.8);
glVertex2f(m + (s * 0.44), s * -0.7);
glVertex2f(m + (s * 0.50), s * -0.7);
glEnd();
wheel(m + 0.52, -0.8);
wheel(m + 0.76, -0.8);
glFlush();
}
void key(unsigned char key, int x, int y) {
switch (key)
{
case 'A':
case 'a':
m = m - 0.1;
break;
case 'D':
case 'd':
m = m + 0.1;
break;
case 'Q':
case 'q':
s = s + 0.1;
break;
case 'E':
case 'e':
s = s - 0.1;
break;
}
glutPostRedisplay();
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1000, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("Moving Car");
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutMainLoop();
return 0;
}
I don't know how to make it scale at a fixed position after translating, I did try applying translate in the scaling part but it doesn't work.