Skip to content

Instantly share code, notes, and snippets.

@fenrig
Created April 14, 2015 15:56
Show Gist options
  • Save fenrig/91c0ff8aa500601db900 to your computer and use it in GitHub Desktop.
Save fenrig/91c0ff8aa500601db900 to your computer and use it in GitHub Desktop.
#include <GL/glut.h>
#define DEBUG 1
// display globals
unsigned int height = 400, width = 400;
int xmin = -100, xmax = 100, ymin = -100, ymax = 100, nearVal = 1, farVal = 100;
// camera globals
float x = 5.0f, y = 0.0f, z = 5.0f;
void init(void){
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void winReshapeFcn(GLint newWidth, GLint newHeight){
height = newHeight;
width = newWidth;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(xmin, xmax, ymin, ymax, nearVal, farVal);
}
void displayFcn(void){
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x, y, z,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
// x-as RED
glColor3f(1, 0, 0);
glBegin(GL_LINE_STRIP);
glVertex3f(0, 0, 0);
glVertex3f(xmax, 0, 0);
glEnd();
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0xAAAA);
glBegin(GL_LINE_STRIP);
glVertex3f(0, 0, 0);
glVertex3f(xmin, 0, 0);
glEnd();
glDisable(GL_LINE_STIPPLE);
// y-as GREEN
glColor3f(0, 1, 0);
glBegin(GL_LINE_STRIP);
glVertex3f(0, 0, 0);
glVertex3f(0, ymax, 0);
glEnd();
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0xAAAA);
glBegin(GL_LINE_STRIP);
glVertex3f(0, 0, 0);
glVertex3f(0, ymin, 0);
glEnd();
glDisable(GL_LINE_STIPPLE);
// z-as BLUE
glColor3f(0, 0, 1);
glBegin(GL_LINE_STRIP);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, farVal);
glEnd();
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0xAAAA);
glBegin(GL_LINE_STRIP);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, -farVal);
glEnd();
glDisable(GL_LINE_STIPPLE);
glColor3f(1, 1, 0);
//glutSolidSphere(5, 20, 20);
glFlush();
return;
}
void processSpecialKeys(int key, int xx, int yy) {
if(DEBUG)
printf("processSpecialKeys()\n");
float fraction = 0.1f;
switch (key) {
case GLUT_KEY_LEFT :
x -= fraction;
break;
case GLUT_KEY_RIGHT :
x += fraction;
break;
case GLUT_KEY_UP :
z += fraction;
break;
case GLUT_KEY_DOWN :
z -= fraction;
break;
}
if(DEBUG)
printf("x: %f - y: %f - z: %f\n", x, y, z);
glutPostRedisplay();
return;
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(height, width);
glutCreateWindow("ballen");
init();
// display
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
// keyboard
glutSpecialFunc(processSpecialKeys);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment