Why Rotation along from center origin - c++

I'm facing a rotation problem a triangle that draw with opengl drawing primitive. I am using glut key function to rotate the triangle anticlockwise and clockwise by pressing E and R keys. And then also using glut special key function to translate the triangle x axis and y axis by pressing up down left right keys like this
But after translate this triangle to another position than press rotation key it's not rotate form it's last position where it stay. It's rotate from center origin how to fix this.
I don't know what i'm doing wrong.
Code:
#include<iostream>
#include <cstdlib>
#include<GL\freeglut.h>
using namespace std;
float posX = 0.0f;
float posY = 0.0f;
float movethis = 0.1f;
float angle = 0.0f;
void init(void) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, -1.0);
}
void drawFigure() {
glPointSize(4);
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
//Total 4 liens 9 vertex
glVertex2f(0.1f, 0.0f);
glVertex2f(-0.1f, 0.0f);
glVertex2f(0.0f, 0.1f);
glVertex2f(-0.1f, 0.0f);
glVertex2f(0.0f, 0.1f);
glVertex2f(0.1f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.0f, 0.1f);
glVertex2f(0.0f, 0.0f);
glEnd();
glBegin(GL_POINTS);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(0.0f, 0.1f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glEnd();
}
void SpecialKeys(int key, int xpos, int ypos) {
if (key == GLUT_KEY_UP) {
posY = posY + movethis;
}
else if (key == GLUT_KEY_DOWN) {
posY = posY - movethis;
}
else if (key == GLUT_KEY_RIGHT) {
posX = posX + movethis;
}
else if (key == GLUT_KEY_LEFT) {
posX = posX - movethis;
}
glutPostRedisplay();
}
void KeysFun(unsigned char key, int xpos, int ypos) {
if (key == 'e' || key == 'E') {
angle=angle+10;
}
else if (key == 'r' || key == 'R') {
angle=angle-10;
}
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glTranslatef(posX, posY, 0.0f);
drawFigure();
glFlush();
}
int main(int argc, char**argv) {
glutInit(&argc, argv);
glutInitWindowSize(600, 600);
glutInitWindowPosition(450, 50);
glutCreateWindow("Frame");
init();
glutDisplayFunc(display);
glutSpecialFunc(&SpecialKeys);
glutKeyboardFunc(&KeysFun);
glutMainLoop();
return EXIT_SUCCESS;
}

You have to read the order of operations in the matrix multiplication chain "backwards". You first have to rotate, then translate, but if you write it as
rotate
translate
vertices
data is traveling "upwards", so the vertices "see" first the translation, then the rotation. Solution: Flip the order of rotation and translation.

Related

Is there a way to cycle options in C++/GLUT

I am making a program that will cycle the color of a triangle with the up and down arrows. I am pretty new to GLUT and C++, so I can't figure out a way to "cycle" colors.
Here is the triangle code:
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( 0.0f, 0.0f, 10.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glColor3f(red, green, blue);
glBegin(GL_TRIANGLES);
glVertex3f(-2, 0, 0.0);
glVertex3f(2, 0, 0.0);
glVertex3f(2, 2, 0.0);
glEnd();
angle+=0.1f;
glutSwapBuffers();
}
and the input code:
void processSpecialKey(int key, int x, int y) {
switch(key) {
case GLUT_KEY_UP :
red += 1.0;
green += 0.0;
blue += 0.0; break;
case GLUT_KEY_DOWN :
red += 0.0;
green += 1.0;
blue += 0.0; break;
}
}

Opengl/Glut keyboard input trouble

i have an assignment to draw a pyramid onto the screen and then rotate after i press the 'k' key in my keyboard.
Everything works ok, except the rotation part as the program doesn't seem to be noting me pressing the key. The code goes as follows:
void changeSize(int w, int h) {
if(h == 0)
h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
// Set perspective
gluPerspective(45.0f ,ratio, 1.0f ,1000.0f);
// return to the model view matrix mode
glMatrixMode(GL_MODELVIEW);
}
void renderScene(void) {
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set the camera
glLoadIdentity();
gluLookAt(5.0,5.0,5.0,
0.0,0.0,0.0,
0.0f,2.0f,0.0f);
// put the geometric transformations here
// put drawing instructions here
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-100.0f, 0.0f, 0.0f);
glVertex3f( 100.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, -100.0f, 0.0f);
glVertex3f(0.0f, 100.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -100.0f);
glVertex3f(0.0f, 0.0f, 100.0f);
glEnd();
//triangulos da base
glBegin(GL_TRIANGLES);
glColor3f(255.0f,255.0f,255.0f);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glEnd();
//triangulos das faces
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
// End of frame
glutSwapBuffers();
}
// write function to process keyboard events
void rotate (unsigned char key, int x, int y) {
if (key == 'k')
glRotatef(45,1.0,1.0,0.0);
glutPostRedisplay();
}
int main(int argc, char **argv) {
// init GLUT and the window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(1200,1200);
glutCreateWindow("CG#DI-UM");
// Required callback registry
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
// put here the registration of the keyboard callbacks
glutKeyboardFunc(rotate);
// OpenGL settings
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
// enter GLUT's main cycle
glutMainLoop();
return 0;
}
It seems that my program is not noting me pressing the key even tho i am. Im tried only printing the keys that are being pressed, and that works, so i'm really lost here.
glRotatef seem to have no effect in rotate, because glLoadIdentity() is called at the begin of renderScene. Actually glRotatef changes the current matrix, but when the scene is rendered (renderScene), then glLoadIdentity() loads the Identity matrix to the current matrix.
Anyway it is a bad style to do changes to the current matrix in input event callbacks. Change states and values of variables in the input events and use the variables to set the current model view matrix before the scene is rendered in renderScene.
Add a global variable angle:
float angle = 0.0f;
Change the value of the variable in renderScene. e.g:
void rotate (unsigned char key, int x, int y) {
if (key == 'k') {
angle += 45.0f;
glutPostRedisplay();
}
}
And apply the rotation to the current matrix in renderScene:
void renderScene(void) {
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set the camera
glLoadIdentity();
gluLookAt(5.0,5.0,5.0,
0.0,0.0,0.0,
0.0f,2.0f,0.0f);
// apply rotation
glRotatef(angle, 1.0f, 1.0f, 0.0f);
// [...]
}

OpenGL/Glut: Making the camera rotate around X axis with arrow keys?

I'm trying to make the gluLookAt() function so that when I press the up & down key the camera moves around the X axis
I'm trying a method I saw at: http://www.lighthouse3d.com/tutorials/glut-tutorial/keyboard-example-moving-around-the-world/
but it's not working for me. Anyone knows an easier way? and where should I put the gluLookAt() so on myDisplay() func?
#include"glut.h"
#include<cmath>
#include<iostream>
using namespace std;
float xr = 0, yr = 0; //to control the object's movement from left to right
// actual vector representing the camera's direction
float lx = 0.0f, lz = -1.0f;
// XZ position of the camera
float x = 0.0f, z = 5.0f;
GLfloat angle = 0.0f;
int refreshmill = 1;
void timer(int value) { //to control the rotation of the object
glutPostRedisplay();
glutTimerFunc(refreshmill, timer, 0);
}
void myDisplay(void) {
//Circle One
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glBegin(GL_POLYGON);
for (int x = 0; x < 360; x++) {
theta = x * 3.142 / 180;
glVertex2f(150 * cos(theta)+xr,150 * sin(theta)+yr);
}
glEnd();
glPopMatrix();
//Circle Two
float theta2;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.5f, 0.0f, 0.0f); // rotation
glRotatef(angle, 0.0f, 0.0f, -0.5f); // rotation
glBegin(GL_POLYGON);
glColor3f(0, 0, 1);
for (int x = 0; x < 360; x++) {
theta2 = x * 3.142 / 180;
glVertex2f(150 + 15 * cos(theta2) + xr, 15 * sin(theta2) + yr);
}
glutSwapBuffers();
angle += 0.2; // rotation
glEnd();
glPopMatrix();
//Draw Star
glColor3ub(119, 193, 15);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glBegin(GL_POLYGON);
glVertex2d(15+xr, 60+yr);
glVertex2d(75+xr, 75+yr); //right peak
glVertex2d(15+xr, 90+yr);
glVertex2d(0+xr, 150+yr); //Up-peak Changed
glVertex2d(-15+xr, 90+yr);
glVertex2d(-75+xr,75+yr);
glVertex2d(-15+xr, 60+yr);
glVertex2d(0+xr,0+yr);
glEnd();
glPopMatrix();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
void renderScene(void) {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt(x, 1.0f, z,
x + lx, 1.0f, z + lz,
0.0f, 1.0f, 0.0f);
// Draw ground
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, -100.0f);
glEnd();
myDisplay();
glutSwapBuffers();
}
//Move to left or right
void keyboard(int key, int x, int y) {
float fraction = 0.1f;
switch (key) {
case GLUT_KEY_RIGHT:
xr++;
cout << x << endl;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
xr--;
cout << x << endl;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
angle -= 0.01f;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_DOWN:
angle += 0.01f;
lx = sin(angle);
lz = -cos(angle);
break;
}
}
void init() {
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-250, 250, -250, 250); //IMPORTANT- Define from negative to positive
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Homework: Circle");
// register callbacks
glutDisplayFunc(myDisplay);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutTimerFunc(0,timer,0);
glutSpecialFunc(keyboard);
// OpenGL init
init();
// enter GLUT event processing cycle
glutMainLoop();
}
First of all there should be only 1 display call back function:
int main(int argc, char** argv) {
// [...]
// glutDisplayFunc(myDisplay); <----- DELETE!!!
glutDisplayFunc(renderScene);
// glutIdleFunc(renderScene); <----- DELETE!!!
// [...]
}
Setup an an orthographic projection with an extended near and far plane. If the object is rotated around the X axis, it takes space in the 3 dimensions:
void init() {
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-250, 250, -250, 250, -250, 250); // <----
glMatrixMode(GL_MODELVIEW);
}
Add a variable anglaX which is changed in the keyboard event:
float angleX = 0.0f;
void keyboard(int key, int x, int y) {
switch (key) {
case GLUT_KEY_RIGHT: xr++; break;
case GLUT_KEY_LEFT: xr--; break;
case GLUT_KEY_UP: angleX -= 1.0f; break;
case GLUT_KEY_DOWN: angleX += 1.0f; break;
}
}
Rotate the model, after the view was set:
gluLookAt(x, 0.0f, z, x, 0.0f, z-1.0f, 0.0f, 1.0f, 0.0f);
glRotatef(angleX, 1, 0, 0);
Don't do any calls to glutSwapBuffers(), glFlush() and glutPostRedisplay(), except at the end of renderScene:
void timer(int value) { //to control the rotation of the object
// glutPostRedisplay(); <--- DELETE
glutTimerFunc(refreshmill, timer, 0);
}
void myDisplay(void) {
//Circle One
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glPushMatrix();
glBegin(GL_POLYGON);
for (int x = 0; x < 360; x++) {
theta = x * 3.142 / 180;
glVertex2f(150 * cos(theta)+xr,150 * sin(theta)+yr);
}
glEnd();
glPopMatrix();
//Circle Two
float theta2;
glPushMatrix();
glTranslatef(0.5f, 0.0f, 0.0f); // rotation
glRotatef(angle, 0.0f, 0.0f, -0.5f); // rotation
glBegin(GL_POLYGON);
glColor3f(0, 0, 1);
for (int x = 0; x < 360; x++) {
theta2 = x * 3.142 / 180;
glVertex2f(150 + 15 * cos(theta2) + xr, 15 * sin(theta2) + yr);
}
angle += 0.2; // rotation
glEnd();
glPopMatrix();
//Draw Star
glColor3ub(119, 193, 15);
glPushMatrix();
glBegin(GL_POLYGON);
glVertex2d(15+xr, 60+yr);
glVertex2d(75+xr, 75+yr); //right peak
glVertex2d(15+xr, 90+yr);
glVertex2d(0+xr, 150+yr); //Up-peak Changed
glVertex2d(-15+xr, 90+yr);
glVertex2d(-75+xr,75+yr);
glVertex2d(-15+xr, 60+yr);
glVertex2d(0+xr,0+yr);
glEnd();
glPopMatrix();
}
void renderScene(void) {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set the camera
gluLookAt(x, 0.0f, z, x, 0.0f, z-1.0f, 0.0f, 1.0f, 0.0f);
glRotatef(angleX, 1, 0, 0);
// Draw ground
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, -100.0f);
glEnd();
myDisplay();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
Further I recommend to use double buffering:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
You can init your camera with gluLookAt at init(), then rotate it when arrow keys pressed. If you want to rotate camera around it's local x axis, assume your initial modelview matrix is V, newly happened rotation around x axis is R, you need to set modelview matrix to R*V, not V*R.
case GLUT_KEY_UP:
GLfloat temp[16];
glGetFloatv(GL_MODELVIEW_MATRIX, temp);
glLoadIdentity();
glRotate(stepAngle, 1, 0, 0); // calculate stepAngle by your self
glMultMatrixf(temp);
break;
You don need to reset modelview matrix during rendering, view part is already done, make sure you restore it after rendering the whole scene:
glPushMatrix(GL_MODELVIEW_MATRIX)
// don't call glLoadIdentity() here, you don't need to reset view part.
...
...
glPopMatrix()

C++ and GLUT smooth movement

I am new at using graphics with C++ and I have the following code from this
tutorial
here is the code
#include <windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <math.h>
float angle = 0.0f;
float lx = 0.0f, lz = -1.0f;
float x = 0.0f, z = 5.0f;
float deltaAngle = 0.0f;
float deltaMove = 0;
void drawSnowMan(){
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.75f, 0.0f);
glutSolidSphere(0.75f, 20, 20);
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.25f, 20, 20);
glPushMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
glTranslatef(0.5f, 0.10f, 0.18f);
glutSolidSphere(0.05f, 10, 10);
glTranslatef(-0.1f, 0.0f, 0.0f);
glutSolidSphere(0.05f, 10, 10);
glPopMatrix();
glColor3f(1.0f, 0.5f, 0.5f);
glutSolidCone(0.8f, 0.5f, 10, 2);
}
void changeSize(int w, int h){
if(h == 0)
h = 1;
float ratio = 1.0*w/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(45.0f, ratio, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
void computePos(float deltaMove){
x += deltaMove * lx * 0.1f;
z += deltaMove * lz * 0.1f;
}
void computeDir(float deltaAngle){
angle += deltaAngle;
lx = sin(angle/75.0);
lz = -cos(angle/75.0);
}
void pressKey(int key, int xx, int yy){
switch(key){
case GLUT_KEY_LEFT: deltaAngle = -0.5f; break;
case GLUT_KEY_RIGHT: deltaAngle = 0.5; break;
case GLUT_KEY_UP: deltaMove = 0.5f; break;
case GLUT_KEY_DOWN: deltaMove = -0.5f; break;
}
}
void relaseKey(int key, int x, int y){
switch(key){
case GLUT_KEY_LEFT:
case GLUT_KEY_RIGHT: deltaAngle = 0.0f; break;
case GLUT_KEY_UP:
case GLUT_KEY_DOWN: deltaMove = 0; break;
}
}
void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
if(deltaMove)
computePos(deltaMove);
if(deltaAngle)
computeDir(deltaAngle);
gluLookAt( x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f
);
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, -100.0f);
glEnd();
angle += 0.1f;
for(int i = -3; i < 3; i++){
for(int j = -3; j < 3; j++){
glPushMatrix();
glTranslatef(i*10.0,0, j*10.0);
drawSnowMan();
glPopMatrix();
}
}
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(320,320);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Lighthouse 3D");
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
glutSpecialFunc(pressKey);
glutIgnoreKeyRepeat(1);
glutSpecialUpFunc(relaseKey);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 1;
}
My problem is the following, when i try to turn to the left and i keep the left key pressed everything works perfectly fine. When i try to hit the left once the action is not smooth at all. In some cases if the button stroke is too fast it even turns to the opposite direction!
I assume you mean keyboard instead of mouse, in which case to have a "smoother" and "slower" feel you need to adjust your deltaAngle values, which might be too high.
This is only a temporary fix, since those values may affect other machines in different ways - some may be faster, some may be slower. You need to calculate the host machine's FPS in your render loop and multiply your delta values with it, to have the same performance on every computer. This is called time-based movement and is an essential feature of every modern graphics application.
This is exactly what is happening in the following tutorial, when a dot is moving too fast on the screen.
http://lazyfoo.net/SDL_tutorials/lesson32/index.php
A glutIdleFunc() will eat all your battery/CPU/GPU, use a glutTimerFunc() instead to redraw on a predictable, sane schedule.
Accumulate your input state between frames and apply it in your glutTimerFunc() instead of trying to do it right in your keyup/keydown callbacks.
No need for a glutResizeFunc(), do all that just before your draw.
You're using C++ so you should use the C++ versions of C headers, like <cmath> instead of <math.h>.
All together:
#include <GL/glut.h>
#include <cmath>
#include <map>
std::map< int, bool > keys;
void pressKey(int key, int xx, int yy)
{
keys[ key ] = true;
}
void relaseKey(int key, int x, int y)
{
keys[ key ] = false;
}
float angle = 0.0f;
float lx = 0.0f, lz = -1.0f;
float x = 0.0f, z = 5.0f;
void update()
{
const float angleStep = 0.5f;
if( keys[ GLUT_KEY_LEFT ] ) angle -= angleStep;
if( keys[ GLUT_KEY_RIGHT ] ) angle += angleStep;
const float moveStep = 0.5f;
float move = 0.0f;
if( keys[ GLUT_KEY_DOWN ] ) move -= moveStep;
if( keys[ GLUT_KEY_UP ] ) move += moveStep;
x += move * lx * 0.1f;
z += move * lz * 0.1f;
lx = sin(angle/75.0);
lz = -cos(angle/75.0);
}
void drawSnowMan()
{
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.75f, 0.0f);
glutSolidSphere(0.75f, 20, 20);
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.25f, 20, 20);
glPushMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
glTranslatef(0.5f, 0.10f, 0.18f);
glutSolidSphere(0.05f, 10, 10);
glTranslatef(-0.1f, 0.0f, 0.0f);
glutSolidSphere(0.05f, 10, 10);
glPopMatrix();
glColor3f(1.0f, 0.5f, 0.5f);
glutSolidCone(0.8f, 0.5f, 10, 2);
}
void renderScene(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const double w = glutGet( GLUT_WINDOW_WIDTH );
const double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective(45.0f, w / h, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt
(
x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f
);
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, 100.0f);
glVertex3f(100.0f, 0.0f, -100.0f);
glEnd();
for(int i = -3; i < 3; i++)
{
for(int j = -3; j < 3; j++)
{
glPushMatrix();
glTranslatef(i*10.0,0, j*10.0);
drawSnowMan();
glPopMatrix();
}
}
glutSwapBuffers();
}
void timer( int value )
{
update();
glutTimerFunc( 16, timer, 0 );
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(320,320);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Lighthouse 3D");
glutDisplayFunc(renderScene);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(relaseKey);
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 1;
}

Opengl translation with glMultMatrixf

Im trying to translate an object without using glTranslate(). I have searched through web and found a function glMultMatrixf(). I wanted to multiply my translation matrix using this function but I couldn't accomplished it. I feel like I'm on the correct path logically but is translating an object this way even possible?
I have written the code using opengl and c++. Here is the code:
#include <iostream>
#include <stdlib.h>
#include <math.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
float _angle = 0;
typedef GLfloat Matrix4x4 [4][4];
Matrix4x4 matTransl3D;
/* Construct the 4 x 4 identity matrix. */
void matrix4x4SetIdentity (Matrix4x4 matIdent4x4)
{
GLint row, col;
for (row = 0; row < 4; row++)
for (col = 0; col < 4 ; col++)
matIdent4x4 [row][col] = (row == col);
}
void translate3D (GLfloat tx, GLfloat ty, GLfloat tz)
{
/* Initialize translation matrix to identity. */
matrix4x4SetIdentity (matTransl3D);
matTransl3D [0][3] = tx;
matTransl3D [1][3] = ty;
matTransl3D [2][3] = tz;
}
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
void handleResize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
}
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
translate3D(0.0f, 0.0f, -50.0f);
glMultMatrixf(*matTransl3D);
glBegin(GL_TRIANGLE_FAN); // draw triangle
glColor3f(1.0f,0.0f,0.0f); // set color to red
glVertex3f( 0.0f, 3.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f); // set color to green
glVertex3f(-5.0f, -5.0f, 5.0f);
glColor3f(1.0f,1.0f,0.0f); // set color to yellow
glVertex3f( 5.0f, -5.0f, 5.0f);
glColor3f(0.0f,0.0f,1.0f); // set color to blue
glVertex3f( 5.0f, -5.0f, -5.0f);
glColor3f(1.0f,1.0f,1.0f); // set color to white
glVertex3f( -5.0f, -5.0f, -5.0f);
glColor3f(0.0f,1.0f,0.0f); // set color to green
glVertex3f(-5.0f, -5.0f, 5.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f); // set color to green
glVertex3f(-5.0f, -5.0f, 5.0f);
glColor3f(1.0f,1.0f,1.0f); // set color to white
glVertex3f( -5.0f, -5.0f, -5.0f);
glColor3f(0.0f,0.0f,1.0f); // set color to blue
glVertex3f( 5.0f, -5.0f, -5.0f);
glColor3f(1.0f,1.0f,0.0f); // set color to yellow
glVertex3f( 5.0f, -5.0f, 5.0f);
glEnd();
glutSwapBuffers();
}
//Called every 25 milliseconds
void update(int value) {
_angle += 1.0f;
if (_angle > 360) {
_angle -= 360;
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Translation 3d");
initRendering();
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);
glutMainLoop();
return 0;
}
OpenGL takes matrices as column major, i.e. the indexing is
0 4 8 c
1 5 9 d
2 6 a e
3 7 b f
However you're indexing your translation matrix in the order
0 1 2 3
4 5 6 7
8 9 a b
c d e f