your right, it was fairly easy once i realized how opengl works with the code
i posted my code below and am wondering if you experts have any hints or suggestions for me? everything works but maybe i didn’t code it in the most efficient way?
// ***************************************************
// ** Creates 3 Boxes **
// ***************************************************
// used later when the FALCON is connected
//#include <windows.h>
//#include "glut.h"
//#include <math.h>
//#include "haptics.h"
#include <stdlib.h>
#include <GL/openglut.h>
#include <math.h>
//#include <GL/glut.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <cstdlib>
// removes the console window
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
using namespace std;
int w, h; // width and height of main window
int pos = 0; // position of the two moveable gripper fingers
GLfloat x1, yy1, z1, x2, y2, z2, x3, y3, z3;
// if the window size is changed, the objects inside will be resized as well
void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
double ratio = w * 1.0 / h; // changed 'float' to 'double' due to warning
glMatrixMode(GL_PROJECTION); // Use the Projection Matrix
glLoadIdentity(); // Reset Matrix
glViewport(0, 0, w, h); // Set the viewport to be the entire window
gluPerspective(45, ratio, 0.1, 100); // Set the correct perspective.
glMatrixMode(GL_MODELVIEW); // Get Back to the Modelview
}
// renders any text used
void renderBitmapString (float x,
float y,
float z,
void *font,
char *string) {
char *c;
glLoadIdentity(); // reset matrix
glRasterPos3f(x, y, z);
for (c = string; *c != '\0'; c++) {
glutBitmapCharacter(font, *c);
}
}
void restorePerspectiveProjection() {
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // restore previous projection matrix
glMatrixMode(GL_MODELVIEW); // get back to modelview mode
}
void setOrthographicProjection() {
glMatrixMode(GL_PROJECTION); // switch to projection mode
// save previous matrix which contains the
glPushMatrix(); // settings for the perspective projection.
glLoadIdentity(); // reset matrix
gluOrtho2D(0, w, h, 0); // set a 2D orthographic projection
glMatrixMode(GL_MODELVIEW); // switch back to modelview mode
}
void drawGrippers() {
// declares for sphere
GLdouble radius = 0.2;
GLint slices = 30;
GLint stacks = 30;
GLfloat r = 1.0f;
GLfloat g = 1.0f;
GLfloat b = 0.0f;
// draw three spheres
glLoadIdentity();
glTranslatef(x1, yy1, z1);
glColor3f(r, g, b);
glutSolidSphere(radius, slices, stacks); // top sphere
glLoadIdentity();
glTranslatef(x2, y2, z2);
glColor3f(r, g, b);
glutSolidSphere(radius, slices, stacks); // top sphere
glLoadIdentity();
glTranslatef(x3, y3, z3);
glColor3f(r, g, b);
glutSolidSphere(radius, slices, stacks); // top sphere
glColor3f(1.0f, 1.0f, 1.0f);
}
// any permanent text or lines are placed on the screen
void screenLabels() {
GLfloat r = 0.0f;
GLfloat g = 0.0f;
GLfloat b = 0.0f;
GLfloat r1 = 1.0f;
GLfloat b1 = 1.0f;
// draws all 'limit lines'
glLoadIdentity();
glBegin(GL_LINES);
glVertex3f(-0.21f, 1.22f, -5.0f); // top sphere, iniPOS, TOP line
glVertex3f(0.21f, 1.22f, -5.0f);
glVertex3f(-0.21f, 1.22f, -5.0f); // top sphere, iniPOS, LEFT line
glVertex3f(-0.21f, 0.8f, -5.0f);
glVertex3f(0.21f, 1.22f, -5.0f); // top sphere, iniPOS, RIGHT line
glVertex3f(0.21f, 0.8f, -5.0f);
glVertex3f(-1.22f, -0.29f, -5.0f); // left sphere, iniPOS, TOP line
glVertex3f(-0.8f, -0.29f, -5.0f);
glVertex3f(-1.22f, -0.29f, -5.0f); // left sphere, iniPOS, LEFT line
glVertex3f(-1.22f, -0.7f, -5.0f);
glVertex3f(1.22f, -0.29f, -5.0f); // right sphere, iniPOS, TOP line
glVertex3f(0.8f, -0.29f, -5.0f);
glVertex3f(1.22f, -0.29f, -5.0f); // right sphere, iniPOS, RIGHT line
glVertex3f(1.22f, -0.7f, -5.0f);
glVertex3f(-0.29f, -1.22f, -5.0f); // left sphere, altPOS, RIGHT line
glVertex3f(-0.29f, -0.97f, -5.0f);
glVertex3f(-0.29f, -1.22f, -5.0f); // left sphere, altPOS, BOTTOM line
glVertex3f(-0.71f, -1.22f, -5.0f);
glVertex3f(0.29f, -1.22f, -5.0f); // right sphere, altPOS, LEFT line
glVertex3f(0.29f, -0.97f, -5.0f);
glVertex3f(0.29f, -1.22f, -5.0f); // right sphere, altPOS, BOTTOM line
glVertex3f(0.7f, -1.22f, -5.0f);
glVertex3f(-0.21f, 0.09f, -5.0f); // top sphere, gripCLOSE, BOTTOM line
glVertex3f(0.21f, 0.09f, -5.0f);
glVertex3f(-0.3f, -0.2f, -5.0f); // left sphere, gripCLOSE, DIAGONAL line
glVertex3f(0.0f, -0.5f, -5.0f);
glVertex3f(0.3f, -0.2f, -5.0f); // right sphere, gripCLOSE, DIAGONAL line
glVertex3f(0.0f, -0.5f, -5.0f);
glEnd();
renderBitmapString(-0.08f, 1.3f , -5.0f, GLUT_BITMAP_HELVETICA_18, "'X'");
renderBitmapString(-1.5f, -0.5f , -5.0f, GLUT_BITMAP_HELVETICA_18, "'Y'");
renderBitmapString(1.4f, -0.5f , -5.0f, GLUT_BITMAP_HELVETICA_18, "'Z'");
renderBitmapString(-2.0f, 1.9f , -5.0f, GLUT_BITMAP_HELVETICA_18, "Simulation of Falcon / Gripper Movements");
if ((pos == 0) || (pos == 1) || (pos == 42)) {
glColor3f(r1, g, b);
renderBitmapString(-0.47f, -0.1f , -5.0f,
GLUT_BITMAP_TIMES_ROMAN_24, "Initial Position!");
}
else if ((pos == 2) || (pos == 32)){
glColor3f(r1, g, b);
renderBitmapString(-0.55f, -0.1f , -5.0f,
GLUT_BITMAP_TIMES_ROMAN_24, "Alternate Position!");
}
else if (pos == 31) {
glColor3f(r, g, b1);
renderBitmapString(-0.9f, -0.1f , -5.0f,
GLUT_BITMAP_TIMES_ROMAN_24, "Moving to Alternate Position...");
}
else if (pos == 41) {
glColor3f(r, g, b1);
renderBitmapString(-0.8f, -0.1f , -5.0f,
GLUT_BITMAP_TIMES_ROMAN_24, "Moving to Initial Position...");
}
else if (pos == 51) {
glColor3f(r, g, b1);
renderBitmapString(-0.52f, -0.1f , -5.0f,
GLUT_BITMAP_TIMES_ROMAN_24, "Closing Grippers...");
}
else if (pos == 61) {
glColor3f(r, g, b1);
renderBitmapString(-0.6f, -0.1f , -5.0f,
GLUT_BITMAP_TIMES_ROMAN_24, "Opening Grippers...");
}
else if (pos == 52) {
glColor3f(r1, g, b);
renderBitmapString(-0.6f, -0.1f , -5.0f,
GLUT_BITMAP_TIMES_ROMAN_24, "Grippers are Closed!");
}
else if (pos == 62) {
glColor3f(r1, g, b);
renderBitmapString(-0.58f, -0.1f , -5.0f,
GLUT_BITMAP_TIMES_ROMAN_24, "Grippers are Open!");
}
glColor3f(1.0f, 1.0f, 1.0f);
}
// creates three boxes and text
void renderScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen
drawGrippers();
screenLabels();
glutSwapBuffers();
}
void instructions() {
system("cls");
cout << endl << endl << endl << " press 'i' at anytime for instructions" << endl;
cout << " press 'esc' at anytime to EXIT" << endl << endl;
cout << " press '1' = INITIAL position" << endl;
cout << " press '2' = ALTERNATE position" << endl;
cout << " press '3' = MOVE grippers DOWN" << endl;
cout << " press '4' = MOVE grippers UP" << endl;
cout << " press '5' = CLOSE grippers" << endl;
cout << " press '6' = OPEN grippers" << endl;
}
// checking keyboard for key presses
void glutKeyboard(unsigned char key, int x, int y) {
GLfloat stepALT = 0.01f;
GLfloat stepGripper = 0.01f;
static bool inited = true;
if (key == 27) { // esc key
exit(0);
}
// the 'y' and 'z' axis of the gripper can move into different
// positions: INITIAL position, ALTERNATE position, move DOWN
// & move UP
// INITIAL position
else if (key == '1') {
renderBitmapString(-1.0f, -2.0f , -5.0f, GLUT_BITMAP_HELVETICA_18, "TEST");
pos = 1;
x2 = -1.0f; // left sphere
y2 = -0.5f;
x3 = 1.0f; // right sphere
y3 = -0.5f;
glutPostRedisplay();
}
// ALTERNATE position
else if (key == '2') {
pos = 2;
x2 = -0.5f; // left sphere
y2 = -1.0f;
x3 = 0.5f; // right sphere
y3 = -1.0f;
glutPostRedisplay();
}
// DOWN to ALTERNATE position
else if (key == '3') {
// left cube
if ((x2 <= -0.5) && (y2 >= -1.0)) {
x2 += stepALT;
y2 -= stepALT;
pos = 31;
}
else {
pos = 32;
}
// right cube
if ((x3 >= 0.5) && (y3 >= -1.0)) {
x3 -= stepALT;
y3 -= stepALT;
}
glutPostRedisplay();
}
// UP to INITIAL position
else if (key == '4') {
// left cube
if (( x2 >= -1.0 && y2 <= -0.5)) {
x2 += -stepALT;
y2 -= -stepALT;
pos = 41;
}
else {
pos = 42;
}
// right cube
if (( x3 <= 1.0 && y3 <= -0.5)) {
x3 += stepALT;
y3 += stepALT;
}
}
// CLOSE grippers
else if (key == '5') {
// top cube
if (yy1 >= 0.3) {
//x1 += -stepGripper;
yy1 -= stepGripper;
pos = 51;
}
else {
pos = 52;
}
// left cube
if (x2 <= -0.3) {
x2 += stepGripper;
//y2 -= -stepGripper;
}
// right cube
if (x3 >= 0.3) {
x3 -= stepGripper;
//y3 += stepGripper;
}
}
// OPEN grippers
else if (key == '6') {
// top cube
if (yy1 <= 1.0) {
//x1 += -stepGripper;
yy1 += stepGripper;
pos = 61;
}
else {
pos = 62;
}
// left cube
if (x2 >= -1.0) {
x2 -= stepGripper;
//y2 -= -stepGripper;
}
// right cube
if (x3 <= 1.0) {
x3 += stepGripper;
//y3 += stepGripper;
}
}
else if (key == 'i') {
instructions();
}
glutPostRedisplay();
}
int main(int argc, char **argv) {
// initialize variables
x1 = 0.0f; // top sphere
yy1 = 1.0f;
z1 = -5.0f;
x2 = -1.0f; // left sphere
y2 = -0.5f;
z2 = -5.0f;
x3 = 1.0f; // right sphere
y3 = -0.5f;
z3 = -5.0f;
// init GLUT, create Window and init Keyboard
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(10,10);
glutInitWindowSize(800, 650);
glutCreateWindow("THREE BOXES DEMO");
glutKeyboardFunc(glutKeyboard);
instructions();
// register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
// enter GLUT event processing cycle
glutMainLoop();
return 0;
}
thanks for making it this far down
how can i get scroll bars so as to reduce the length of a post?