how to change integer values of glLookat view elements with keyboard? - c++

I am making a room and i want to change the values of lookat because i want to move my camera up,down,right,left,forward and backward inside my room.
My code is given below with comments:
#include <iostream>
using namespace std;
#include "glut.h"
#include "GL/gl.h"
//---------------------------------------------------------------------
static int all =0;
static int allD =0;
static int a=0;
static int d=0;
static int o=0;
static float w=0;
static float s=0;
//---------------------------------------------------------------------
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}
//---------------------------------------------------------------------
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_DEPTH);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//glDisable(GL_DEPTH_TEST);
glPushMatrix();
glRotatef(a,0,1,0);
glRotatef(d,0,1,0);
//back wall glPushMatrix();
glTranslatef(0,0,-2);
glColor3f(0.5,1,0);
glScalef (50.0, 20.0, 2);
glutSolidCube (0.5);
glPopMatrix();
//left wall glPushMatrix();
glTranslatef(-12,0,4.5);
glColor3f(1,1,0);
glScalef (2, 20, 25);
glutSolidCube (0.5);
glPopMatrix();
//floor glPushMatrix();
glTranslatef(0,-5,4.5);
glColor3f(0.5,1,1);
glScalef (50, 0.2, 25);
glutSolidCube (0.5);
glPopMatrix();
//roof glPushMatrix();
glTranslatef(0,4.5,4.5);
glColor3f(0,0,1);
glScalef (50, 2, 25);
glutSolidCube (0.5);
glPopMatrix();
//right wall glPushMatrix();
glTranslatef(12,0,4.5);
glColor3f(0.8,0.2,1);
glScalef (2, 20, 25);
glutSolidCube (0.5);
glPopMatrix();
////front wall 1 //glPushMatrix();
// glTranslatef(-2,0,9);
// glColor3f(0.5,0,0);
// glScalef (12.0, 20.0, 2);
// glutSolidCube (0.5);
//glPopMatrix();
////front wall 2 //glPushMatrix();
// glTranslatef(2.5,2.5,9);
// glColor3f(0.5,0,0);
// glScalef (10.0, 10.0, 2);
// glutSolidCube (0.5);
//glPopMatrix();
////Door //glPushMatrix();
// glTranslatef(0.8,-2,9);
// glRotatef(o,0,1,0);
// glColor3f(0.5,0.5,0.5);
// glScalef (1.0, 8.0, 0.2);
// glutSolidCube (0.5);
//glPushMatrix();
// glTranslatef(1.7,0,0);
// glColor3f(0.5,0.5,0.5);
// glScalef (6.0, 1.5, 0.2);
// glutSolidCube (0.5);
//glPopMatrix();
//glPopMatrix();
//table glPopMatrix();
glutPostRedisplay();
glutSwapBuffers();
}
//---------------------------------------------------------------------
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
//glFrustum(-2,2,-2,2,2.5,20);
//glOrtho(-2,2,-2,2,2.5,20);
gluLookAt (0.0, 0.0, 25, w, s, 0.0, 0.0, 1.0, 0.0);
//i made it 'w' and 's' but its not working. glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//---------------------------------------------------------------------
void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 'a': a = (a - 1) % 360;
glutPostRedisplay();
break;
case 'd': d = (d + 1) % 360;
glutPostRedisplay();
break;
case 'o': o = (o + 1) % 90;
glutPostRedisplay();
break;
case 'w': w = (w + 0.1) ;
glutPostRedisplay();
break;
case 's': s = (s - 0.1);
glutPostRedisplay();
break;
default: break;
}
}
//---------------------------------------------------------------------
void SpecialKeys(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_RIGHT : all = (all + 10) % 360;
glutPostRedisplay();
break;
case GLUT_KEY_UP : allD = (allD - 10) % 360;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT : all = (all - 10) % 360;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN : allD = (allD + 10) % 360;
glutPostRedisplay();
break;
default: break;
}
}
//---------------------------------------------------------------------
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (700, 500);
glutInitWindowPosition (0,0);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutSpecialFunc(SpecialKeys);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
//

You need to re-execute the gluLookAt() function whenever you update the values you're passing to it.
A cleaner way to execute this part of your code would be:
static int W = 0;
static int H = 0;
static int S = 0;
//---------------------------------------------------------------------
void updateCamera() {
gluLookAt (0.0, 0.0, 25, W, S, 0.0, 0.0, 1.0, 0.0);
}
//---------------------------------------------------------------------
void reshape (int w, int h)
{
W = w;
H = h;
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
//glFrustum(-2,2,-2,2,2.5,20);
//glOrtho(-2,2,-2,2,2.5,20);
updateCamera();
glLoadIdentity();
}
//---------------------------------------------------------------------
void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
a = (a - 1) % 360;
break;
case 'd':
d = (d + 1) % 360;
break;
case 'o':
o = (o + 1) % 90;
break;
case 'w':
W = (W + 0.1) ;
updateCamera();
break;
case 's':
S = (S - 0.1);
updateCamera();
break;
default: break;
}
glutPostRedisplay();
}
In general, it's a good idea to abstract out the calls you make often and/or from different places so that you aren't copy-pasting and making future updates or bug fixes more difficult.

Related

Why in OpenGL my camera rotates 180 degrees every other time?

I want to do something like a 3d-shooter.
The calculations seems to be correct, but it is works every other time with a spread with 180 degree rotation on every call glutPostRedisplay(). I understood that thanks to that red line.
I do this with such IDE: Code Blocks / Qt Creator under Linux(Ubuntu x64).
main.cpp
#include "Functions.h"
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(wnd_width, wnd_height);
glutInitWindowPosition(300, 100);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
glutCreateWindow("OpenGL my");
glutDisplayFunc(display);
glutIdleFunc(Idle);
glutSpecialFunc(KeyPressed);
GLdouble aspect = wnd_width/wnd_height;
gluPerspective(90, aspect, 0.1, 10);
glTranslatef(0, -0.3, 0);
glutMainLoop();
return 0;
}
Functions.h
#include <GL/glut.h>
#include <math.h>
#include <iostream>
int wnd_width=1300;
int wnd_height=900;
float pos_x=0, pos_z=0.1;
float angle = 0;
float speed = 0.1;
void DrawFloor(){
glVertex3d(1, 0, 2.5);
glVertex3d(1, 0, 0);
glVertex3d(-1, 0, 0);
glVertex3d(-1, 0, 2.5);
}
void DrawWall(float x, float width, float height){
glVertex3f(x, height, 0);
glVertex3f(x, height, width);
glVertex3f(x, 0, width);
glVertex3f(x, 0, 0);
}
void DrawLine(){
glVertex3f(0, 0.1, -1);
glVertex3f(0, 0.1, 1);
}
void KeyPressed(int key, int x, int y){
switch (key) {
case GLUT_KEY_UP: {
//pos_x = speed * cos(3.14*angle/180);
//pos_z = speed * sin(3.14*angle/180);
pos_z+=0.1;
break;
}
case GLUT_KEY_DOWN: {
pos_x = speed*cos(angle);
pos_z = speed*sin(angle);
break;
}
case GLUT_KEY_LEFT: {
angle += 1;
pos_x = speed * cos(3.14 * angle/180);
pos_z = speed * sin(3.14 * angle/180);
break;
}
case GLUT_KEY_RIGHT: {
angle -= 3;
pos_x = speed * cos(3.14 * angle/180);
pos_z = speed * sin(3.14 * angle/180);
break;
}
}
std::cout<<"x: "<<pos_x<<'\t';
std::cout<<"z: "<<pos_z<<'\n';
glutPostRedisplay();
}
void display(){
glClearColor(0.6, 0.8, 1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gluLookAt(pos_x, 0, pos_z, pos_x, 0, pos_z+0.2, 0, 1, 0);
glBegin(GL_QUADS);
glColor3f(0, 0, 0.7);
DrawFloor();
glColor3f(0, 0.8, 0.1);
DrawWall(-0.5, 2, 0.7);
DrawWall(0.5, 2, 0.7);
glEnd();
glLineWidth(2);
glColor3f(0.7, 0.2, 0.2);
glBegin(GL_LINES);
DrawLine();
glEnd();
glutSwapBuffers();
}
void Idle(){
//pos_z+=0.01;
//glutPostRedisplay();
}
You're using the fixed function stack, so you'll have to learn what glMatrixMode does and how transformation matrices are managed in those old ways.
Anyway, consider this a possible implementation of glMatrixMode
void glMatrixMode(GLenum m)
{
switch(m){
case GL_MODELVIEW: ctx->M = &ctx->matrix_modelview; break;
case GL_PROJECTION: ctx->M = &ctx->matrix_projection; break;
case GL_TEXTURE: ctx->M = &ctx->matrix_texture; break;
case GL_COLOR: ctx->M = &ctx->matrix_color; break;
}
}
On other words, there's a (context) global matrix selected with glMatrixMode, that's subsequently used for all folloing matrix manipulations.
Hence, glMatrixMode is not some form of initialization, but something you use to switch (often several times) during rendering a frame! Your use of it in the main function is kind of pointless. You must use it in your drawing function.
Furthermore, every matrix manipulation multiplies on top, of what's currently in ctx->M. gluLookAt is normally used with a identity matrix. If you don't reset you identity, that gluLookAt will work relative to the look-at done previously. So what you want is this:
void display(){
glClearColor(0.6, 0.8, 1., 1.); /* <<<----* */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTIOM);
glLoadIdentity();
setup_projection_here();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(pos_x, 0, pos_z, pos_x, 0, pos_z+0.2, 0, 1, 0);
glBegin(GL_QUADS);
/* ... */
glutSwapBuffers();
}
(*) Also you normally want to clear to alpha = 1 on the main framebuffer, unless you're planning on drawing window-translucent imagery. With alpha = 0 things might look correct in the window, but if a screenshot is taken, or some screen recording or sharing is used, it may cause undesired transparence.

Want to move one 2d object and not the other

I have to make a bowling game in openGL. This is the code I have so far. What it does that it draws a ball and is moved accordingly when an arrow key is pressed.
So far, I have that ball moving, that is fine. What I want to do that other point I have created, that should not be moved. Because, when that ball reaches to that point, it should be drop or something I will make that obstacle is dropped.
The code is written in Eclipse IDE.
#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
#include <stdio.h> /* printf, scanf, puts, NULL */
float posX = 0, posY = -0.1, posZ = 0;
GLfloat rotation = 90.0;
double x, y, angle;
#define PI 3.1415926535898
GLint circle_points = 50;
void reshape(int width, int heigth) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//clip the windows so its shortest side is 2.0
if (width < heigth) {
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) heigth / (GLfloat) width,
2.0 * (GLfloat) heigth / (GLfloat) width, 2.0, 2.0);
} else {
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) width / (GLfloat) heigth,
2.0 * (GLfloat) width / (GLfloat) heigth, 2.0, 2.0);
}
// set viewport to use the entire new window
glViewport(0, 0, width, heigth);
}
void circ() {
glColor3f(0.0, 0.0, 1.0);
glPointSize(11.0);
glBegin(GL_POINTS);
glVertex3f(0.1, 0.1, 0.0);
glEnd();
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 300; i++) {
angle = 2 * PI * i / 300;
x = cos(angle) / 20;
y = sin(angle) / 20;
glVertex2d(x, y);
}
glEnd();
}
void display() {
//Clear Window
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(posX, posY, posZ);
circ();
glPopMatrix();
glFlush();
}
void init() {
// set clear color to black
glClearColor(1.0, 1.0, 1.0, 0.0);
// set fill color to white
glColor3f(1.0, 1.0, 1.0);
//This is the default view and these statements could be removed
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
}
float move_unit = 0.02f;
void keyboardown(int key, int x, int y) {
switch (key) {
case GLUT_KEY_RIGHT:
posX += move_unit;
break;
case GLUT_KEY_LEFT:
posX -= move_unit;
break;
case GLUT_KEY_UP:
posY += move_unit;
break;
case GLUT_KEY_DOWN:
posY -= move_unit;
break;
default:
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
//initialize mode and open a windows in upper left corner of screen
//Windows tittle is name of program
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Practice 1");
glutDisplayFunc(display);
init();
glutSpecialFunc(keyboardown);
glutMainLoop();
}
Modern graphics APIs simulating finite state machine. That means that before Draw calls you must fully configure (or leave default) graphics pipeline "machine":
SetStates(); // Configure pipeline state: set geometry, textures, matrices, etc.
Begin();
Draw(); // Render frame according to current pipeline configuration (state)
End(); // Swap screen buffers
In case of many objects, you can just wrap all stuff with for loop:
for( each_object )
{
SetStates(); // current object's vertex/index buffer, texture, matrices, etc.
Begin();
Draw();
End();
}
Not very efficient. Next step of improvement might include: frustum culling, instancing, vertex buffers merging, texture atlases, draw calls sorting, etc.
BTW, consider using Vertex Buffer Objects (VBOs), instead of Begin/glVertex2d/End which is deprecated
Try this:
#include <GL/glut.h>
#include <cmath>
float posX = 0, posY = -0.1, posZ = 0;
GLfloat rotation = 90.0;
double x, y, angle;
#define PI 3.1415926535898
GLint circle_points = 50;
void point()
{
glColor3f(0.0, 0.0, 1.0);
glPointSize(11.0);
glBegin(GL_POINTS);
glVertex3f(0.1, 0.1, 0.0);
glEnd();
}
void circ()
{
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i <= 300; i++)
{
angle = 2 * PI * i / 300;
x = cos(angle) / 20;
y = sin(angle) / 20;
glVertex2d(x, y);
}
glEnd();
}
void display()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
point();
glPopMatrix();
glPushMatrix();
glTranslatef(posX, posY, posZ);
circ();
glPopMatrix();
glutSwapBuffers();
}
float move_unit = 0.02f;
void keyboardown(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_RIGHT:
posX += move_unit;
break;
case GLUT_KEY_LEFT:
posX -= move_unit;
break;
case GLUT_KEY_UP:
posY += move_unit;
break;
case GLUT_KEY_DOWN:
posY -= move_unit;
break;
default:
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Practice 1");
glutDisplayFunc(display);
glutSpecialFunc(keyboardown);
glutMainLoop();
}

glulookat() - move camera with keyboard( OpenGL)

I am trying to move the camera with the use of the keyboard. I am calling the glulookat function in the draw method as follows:
gluLookAt(posx,posy,posz,lookx,looky,lookz,upx,upy,upz);
Also I have, for example, for moving the position of the camera on the X axis the following code:
void keyboard(unsigned char key, int x, int y) {
switch(key) {
case 'w' :
break;
case 'a' :
posx-=1.0;
break;
case 's' :
break;
case 'd' :
posx+=1.0;
break;
}
}
The posx,posy,posz,lookx,looky,lookz,upx,upy,upz variables are declared as global double variables. I have tried to initialize them in two ways: when declaring them, as well as in the init() method, but with no success. The camera isn't moving, although the program receives the keyboard input properly, as I have tested this aspect separately. Any ideas of what I am doing wrong?
EDIT: I provided the main code for a better understanding:
Outer_space* space;
Light* sceneLight;
Light* sceneLight2;
Light* sceneLight3;
Satelite* satelite1;
double posx=35.0,posy=35.0,posz=35.0,lookx=0,looky=1,lookz=0,upx=0,upy=0,upz=-1;
void init(void) {
//random number generator
srand(time(NULL));
space = new Outer_space(12, 12,12, new Vector3D(0.5f, 0.7f, 0.9f));
sceneLight = new Light(0, 0);
sceneLight->setTranslation(new Vector3D(0, 20, 0));
sceneLight->setConstantAttenuation(0.09f);
sceneLight->setLinearAttenuation(0.08f);
sceneLight2 = new Light(1, 0);
sceneLight2->setTranslation(new Vector3D(20, 0, 0));
sceneLight2->setConstantAttenuation(0.09f);
sceneLight2->setLinearAttenuation(0.08f);
sceneLight3 = new Light(2, 0);
sceneLight3->setTranslation(new Vector3D(0, 0, 20));
sceneLight3->setConstantAttenuation(0.09f);
sceneLight3->setLinearAttenuation(0.08f);
satelite1 = new Satelite(2,new Vector3D(0.2f,0.3f,0.5f));
satelite1->setTranslation(new Vector3D(10,10,10));
satelite1->setRotation(new Vector3D(-90, 0, 0));
satelite1->setScale(new Vector3D(10, 10, 10));
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
}
void draw(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(0,20,0, 0, 0, 0, 0, 1, 0);
gluLookAt(posx,posy,posz,lookx,looky,lookz,upx,upy,upz);
space->draw();
sceneLight->draw();
sceneLight2->draw();
sceneLight3->draw();
satelite1->draw();
glPushMatrix();
glRasterPos3f(-8.5, 4, -6);
glutSwapBuffers();
}
void update(void){
glutPostRedisplay();
}
void resize(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
GLfloat aspect = (GLfloat)w / (GLfloat)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, aspect, 1.0, 60);
}
void keyboard(unsigned char key, int x, int y) {
switch (key)
{
case 'w' :
break;
case 'a' :
posx-=1.0;
break;
case 's' :
break;
case 'd' :
posx+=1.0;
break;
}
}
void specialKeyboard(int key, int x, int y) {
switch (key)
{
case GLUT_KEY_RIGHT:
posx+=1;
break;
case GLUT_KEY_LEFT:
posx-=1;
break;
case GLUT_KEY_UP:
break;
case GLUT_KEY_DOWN:
break;
}
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Asteroid Escape");
init();
glutIdleFunc(update);
glutDisplayFunc(draw);
glutReshapeFunc(resize);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeyboard);
glutMainLoop();
return 0;
}
EDIT2:
I have commented out the glPushMatrix(); call from the draw() method and now the camera seems to be moving. What is the explanation?
glPushMatrix is supposed to be followed by glPopMatrix.
It seems that you overflow the matrix stack, did you check glGetError result ?
Besides, jour call to glPushMatrix seems pretty useless like this, what do you expect it to do ?

OpenGL - how to connect affine transform functions with events from the program

I am a beginner at using openGL.
I have used a program which I found over the internet to draw a cube on the screen and translate, scale and rotate it according to certain keyboard strokes.
Bellow, I have attached the code for doing this:
#define RADDEG 57.29577951f
float XUP[3] = {1,0,0}, XUN[3] = {-1, 0, 0},
YUP[3] = {0,1,0}, YUN[3] = { 0,-1, 0},
ZUP[3] = {0,0,1}, ZUN[3] = { 0, 0,-1},
ORG[3] = {0,0,0};
GLfloat viewangle = 0, tippangle = 0, traj[120][3];
GLfloat d[3] = {0.1, 0.1, 0.1};
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
GLfloat scaleF = 0.2;
//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
// Use arrow keys to rotate entire scene !!!
void Special_Keys (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT : viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP : tippangle -= 5; break;
case GLUT_KEY_DOWN : tippangle += 5; break;
default: printf (" Special key %c == %d\n", key, key);
}
glutPostRedisplay();
}
//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
void Keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 'j' : d[0] += 0.1; break;
case 'k' : d[0] -= 0.1; break;
case 'n' : d[1] += 0.1; break;
case 'm' : d[1] -= 0.1; break;
//case 'l' : d[2] += 0.1; break;
case 'z' : xAngle += 5; break;
case 'x' : yAngle += 5; break;
case 'c' : zAngle += 5; break;
case 'q' : scaleF += 0.1; break;
case 'w' : scaleF -= 0.1; break;
default: cout<< "Redo a valid keystroke;"<<endl;
}
glutPostRedisplay();
}
//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
void Triad (void)
{
glColor3f (1.0, 1.0, 1.0);
glBegin (GL_LINES);
glVertex3fv (ORG); glVertex3fv (XUP);
glVertex3fv (ORG); glVertex3fv (YUP);
glVertex3fv (ORG); glVertex3fv (ZUP);
glEnd ();
glRasterPos3f (1.1, 0.0, 0.0);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'X');
glRasterPos3f (0.0, 1.1, 0.0);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'Y');
glRasterPos3f (0.0, 0.0, 1.1);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'Z');
}
//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
void Draw_Box (void)
{
glBegin (GL_QUADS);
glColor3f(1,0,0);
glVertex3f(1,1,1);
glVertex3f(-1,1,1);
glVertex3f(-1,-1,1);
glVertex3f(1,-1,1);
glColor3f(0,1,1);
glVertex3f(1,1,-1);
glVertex3f(-1,1,-1);
glVertex3f(-1,-1,-1);
glVertex3f(1,-1,-1);
glColor3f(0,1,0);
glVertex3f(1,1,1);
glVertex3f(1,-1,1);
glVertex3f(1,-1,-1);
glVertex3f(1,1,-1);
glColor3f(1,0,1);
glVertex3f(-1,1,1);
glVertex3f(-1,-1,1);
glVertex3f(-1,-1,-1);
glVertex3f(-1,1,-1);
glColor3f(0,0,1);
glVertex3f(1,1,1);
glVertex3f(-1,1,1);
glVertex3f(-1,1,-1);
glVertex3f(1,1,-1);
glColor3f(1,1,0);
glVertex3f(1,-1,1);
glVertex3f(-1,-1,1);
glVertex3f(-1,-1,-1);
glVertex3f(1,-1,-1);
glEnd();
}
//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
void redraw (void)
{
int v;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_DEPTH_TEST);
glLoadIdentity ();
glTranslatef (0, 0, -3);
glRotatef (tippangle, 1,0,0); // Up and down arrow keys 'tip' view.
glRotatef (viewangle, 0,1,0); // Right/left arrow keys 'turn' view.
glDisable (GL_LIGHTING);
Triad ();
glPushMatrix ();
glTranslatef (d[0], d[1], d[2]); // Move box down X axis.
glScalef (scaleF, scaleF, scaleF);
glRotatef (zAngle, 0,0,1);
glRotatef (yAngle, 0,1,0);
glRotatef (xAngle, 1,0,0);
Draw_Box ();
glPopMatrix ();
glutSwapBuffers();
}
//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
int main (int argc, char **argv)
{
glutInit (&argc, argv);
glutInitWindowSize (900, 600);
glutInitWindowPosition (300, 300);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow ("Orbital Font Demo");
glutDisplayFunc ( redraw );
glutKeyboardFunc ( Keyboard );
//glutSpecialFunc (Special_Keys);
glClearColor (0.1, 0.0, 0.1, 1.0);
glMatrixMode (GL_PROJECTION);
gluPerspective (60, 1.5, 1, 10);
glMatrixMode (GL_MODELVIEW);
glutPostRedisplay();
glutMainLoop ();
return 1;
}
//---+----3----+----2----+----1----+---<>---+----1----+----2----+----3----+----4
The thing is that I want to copy this code into another C++ program where I am using openCV libraries to connect my VGA camera. Based on the movements performed in front of the camera, I am classifying the performed movements using a SVM model.
I want to use the output of the SVM model which is basically a integer value and pass it to the openGL code in order to move the cube in the window.
In the above mentioned code, this procedure is performed by using keystrokes, and implicitly the glKeyboardFunc function. What functions should I use in order to connect the output of the SVM model to the redraw function of the above mentioned code?
You should use glutIdle to check whether there is a new frame. If there is you should update textures with new image using glTexSubImage2D*.
*You should use a texture to display a custom image.

how can I stop an object from rotating while other parts rotate?

I've just drawn a 3 dof robot with 3 joints which are revoloute , i've drawn a shere to use as an obstacle which should be constant without any moving but as the gripper of the robot starts to move my obstacle moves too, which shouldnt,2nd problem is : i want to turn arond my robot i did this by arrow keys theres no error but it dosent work.here is my code:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#define VIEW_TURN_RATE 10
#define GLUT
#define GLUT_KEY
#define GLUT_SPEC
float t1=30;
float t2=45;
float t3=45;
float t4=0;
float t5=0;
float l=1.5;
float h=0.2*l;
double m=0.2;
double n=0.2;
double p=0.2;
float eyex,eyey=0;
float eyez=6;
float lx,ly,lz=0;
int turn = 0, turn1 = 0;
void TurnRight(void)
{
turn = (turn - VIEW_TURN_RATE) % 360;
}
void TurnLeft(void)
{
turn = (turn + VIEW_TURN_RATE) % 360;
}
void TurnForwards(void)
{
turn1 = (turn1 - VIEW_TURN_RATE) % 360;
}
void TurnBackwards(void)
{
turn1 = (turn1 + VIEW_TURN_RATE) % 360;
}
void myidlefunc()
{
if(t1<100)
t1+=2;
else
{
if(t2<150)
t2+=2;
else
{
if(t3<150)
t3+=2;
else
{
if(t4<150)
t4+=2;
else
{
if(t5<120)
t5+=2;
else
{
glutIdleFunc(0);
}
}
}
}
}
glutPostRedisplay();
}
#ifdef GLUT
#ifdef GLUT_SPEC
void
special(int key, int x, int y)
{
int i = 0;
printf("this is special keyboard: %d \n",key);
switch (key) {
/* start of view position functions */
case 'r':{
TurnRight();
i++;
}
break;
case GLUT_KEY_LEFT:{
TurnLeft();
i++;
}
break;
case GLUT_KEY_DOWN:{
TurnForwards();
i++;
}
break;
case GLUT_KEY_UP:{
TurnBackwards();
i++;
}
break;
/* end of view postions functions */
}
if (i)
glutPostRedisplay();
}
#endif
#ifdef GLUT_KEY
void keyboard(unsigned char c,int x,int y)
{
switch(c)
{
case 27:
exit(0);
break;
case 's':
if (t1 < 50) {
t1++;}
break;
case 'S':
//if (t1 < 80) {
t1--;
//}
break;
case 'q':
glutIdleFunc(myidlefunc);
break;
case 'f':
if (t2 < 160) {
t2++;}
break;
case 'F':
t2--;
break;
case 'g':
t3++;
break;
case 'h':
t4++;
break;
case 'k':
t5++;
break;
}
glutPostRedisplay();
}
#endif
#endif
void drawFloor(void)
{
glPushMatrix ();
glBegin(GL_QUADS);
glColor3f(0.0,0.8,0.8);
glVertex3f(-20.0, 0.0, 100.0);
glVertex3f(20.0, 0.0, 100.0);
glVertex3f(20.0, 0.0, -100.0);
glVertex3f(-20.0, 0.0, -100.0);
glEnd();
glPopMatrix ();
glEnable(GL_LIGHTING);
}
void Base(float l)
{
glPushMatrix();
//glColor3f(0.4,0.6,0.5);
glScaled(1.0*l,0.2*l,1.0*l);
glutSolidCube(1);
glPopMatrix();
}
void obst(float radius, int slices, int stacks)
{
glTranslated (0,-0.5,0.0);
glPushMatrix ();
glutSolidSphere (radius,slices,stacks);
glPopMatrix ();
}
void arm(float l,float r,int slices,int stacks,float rotat)
{
float d;
d=0.2*l;
glTranslated (0,l/2+d/2,0);
glPushMatrix ();
glScaled (d/l,1,d/l);
glutSolidCube (l);
glPopMatrix ();
glTranslated (0,l/2,0);
glRotated (rotat,0,0,1);
glPushMatrix ();
glutSolidSphere (r,slices,stacks);
glPopMatrix ();
}
void Base2(float l,float r,int slices,int stacks,float rotat)
{
float d;
d=0.2*l;
glTranslated (d/2,d/2,d/2);
glPushMatrix ();
glScaled (d,d/2,d);
glutSolidCube (2);
glPopMatrix ();
}
void hand(float d,float x)
{
glColor3f(0.0,0.4,0.3);
glRotated(t5,0,1,0);
glPushMatrix ();
glTranslated(0,-d/2+x/2,0);
glScaled(1,x/d,1);
glutSolidCube (d);
glPopMatrix();
glPushMatrix ();
glTranslated(-d/2+x/2,x/2,0);
glScaled (x/(d-x),1,d/(d-x));
glutSolidCube(d-x);
glPopMatrix ();
glPushMatrix ();
glTranslated (d/2-x/2,x/2,0);
glScaled (x/(d-x),1,d/(d-x));
glutSolidCube(d-x);
glPopMatrix ();
}
void display()
{
float l,T,r,x,d;
double radius,height;
int slices, stacks;
l=1.5;
T=30;
d=0.2*l;
x=0.2*d;
r=d/2;
radius=1.0;
height=3;
slices=1000;
stacks=202;
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glPushMatrix ();
//lightning start here
glEnable(GL_LIGHT0);
GLfloat light0_pos []={lx,ly+1,lz+1,1};
GLfloat light0_diffuse []={1,0,0,0};
glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse);
GLfloat light0_Ambient []={0,1,1,0};
glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_Ambient);
gluLookAt(0.2,0.8,0.0,5.0,3.0,5.0,0.0,0.1,0.0);
glLightfv(GL_LIGHT0,GL_POSITION,light0_pos);
glScaled(0.2,0.2,0.2);
glRotated(90,0.0,1.0,0.0);
/* Draw "bottom" of floor in blue. */
glColor3f(0.0, 0.8, 0.8);
drawFloor();
//base
glColor3f(0.0,0.0,0.0);
Base(2);
//base2
glColor3f(1.0,0.0,0.0);
Base2(l,r,60,60,t1);
//arm1
glColor3f(1.0,0.0,0.8);
arm(l,r,60,60,t1);
glColor3f(0.0,1.0,0.0);
arm(l,r,60,60,t2);
glColor3f(1.0,0.8,0.5);
arm(l,r,60,60,t3);
//hand
glTranslated (0,d/2+r,0);
hand(d,x);
//obst
glTranslated (-1,3,0);
glColor3f(0.0,0.0,1.0);
obst(radius,slices,stacks);
//glRotated(0.0,0.0,1.0,0.0);
glPopMatrix ();
glFlush();
glutSwapBuffers();
}
void reshape(int width,int height)
{
float aspect=(float)width/(float)height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,width,height);
//glOrtho(-aspect,aspect,-1,+1,-1,+1);
gluPerspective(60.0,0.0,0.0,8);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
//glTranslatef(0.0, 0.0, 0.0); /* viewing transform */
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutInitWindowPosition(0,0);
glutInitWindowSize(500,500);
static int window=glutCreateWindow("OPenGL!");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
#ifdef GLUT_SPEC
glutSpecialFunc(special);
#endif
glShadeModel(GL_FLAT);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glutMainLoop();
exit(0);
}
As you are rendering the obstacle last, your modelview matrix has been the recipient of all the transforms up to that point. You need to manage your modelview matrix more carefully using glPushMatrix() and glPopMatrix(). I haven't tested but try a push just before //arm 1 and pop just before your //obst.