Ich kapiere die Darstellung irgendwie nie, deswegen war ich mal so frei und habe einen Würfel dazugepackt, damit ich mich etwas orientieren kann. An der Linie kann ich mich nicht orientieren.
Ich habe die Tastatur etwas erweitert, weil die Maussteuerung mit etwas suspekt ist: q, e und 2 und x.
Ich verstehe das Kreuz als Zielkreuz? Dann muss es immer an der gleichen Stelle gezeichnet werden, nämlich einfach vor die Kamera.
Wenn Du etwas zeichnest, sagst Du, wo Du das jeweilge Teil vor die Kamera stellst - die Kamera selbst bewegt sich nie. Ich zeichne also erst den Kubus irgendwo in den Raum, lade dann die Identitätsmatrix und werfe das Kreuz damit vor die bisherige Sicht.
Code: Alles auswählen
#include <iostream>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
float RotAtX = 0.0f;
float RotAtY = 0.0f;
float lx = 0.0f;
float ly = 0.0f;
float lz = 0.0f;
float mx;
float my;
float oldx;
float oldy;
float GW;
float GH;
GLenum inf;
void WarpPointer(int val){
glutWarpPointer((GW / 2), (GH / 2)); //warp to screen middle
}
void HandleMouseMotion(int x, int y){ //handles the movement of the mouse
mx = x;
my = y;
if(mx != (GW / 2) && my != (GH / 2)){
if(mx < (GW / 2)){
if(mx < oldx){
RotAtY -= ((oldx - mx)*0.25);
glutPostRedisplay();
}
if(mx > oldx){
RotAtY -= ((mx - oldx)*0.25);
glutPostRedisplay();
}
}
else if(mx > (GW / 2)){
if(mx < oldx){
RotAtY += ((oldx - mx)*0.25);
glutPostRedisplay();
}
if(mx > oldx){
RotAtY += ((mx - oldx)*0.25);
glutPostRedisplay();
}
}
if(my < (GH / 2)){
if(my < oldy){
RotAtX += ((oldy - my)*0.25);
glutPostRedisplay();
}
if(my > oldy){
RotAtX += ((my - oldy)*0.25);
glutPostRedisplay();
}
}
else if(my > (GH / 2)){
if(my < oldy){
RotAtX -= ((oldy - my)*0.25);
glutPostRedisplay();
}
if(my > oldy){
RotAtX -= ((my - oldy)*0.25);
glutPostRedisplay();
}
}
}
oldx = mx;
oldy = my;
glutTimerFunc(1000, WarpPointer, 0); //warp every 10 sec
glutPostRedisplay(); //tells that window needs to be redisplayed
}
void Init(){ //Enables and sets funcs
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glutSetCursor(GLUT_CURSOR_NONE); //no Cursor
}
void resize(int w, int h){ //set the scene if resized
glViewport(0, 0, w, h);
GH = h;
GW = w;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 0.1, 200.0);
}
void handle_keyboard(unsigned char key, int x, int y){ //handle keyboard
if(key == 27){
exit(0);
}
else if(key == 'w'){
lz=lz+0.5f;
}
else if(key == 's'){
lz=lz-0.5f;
}
else if(key == 'a'){
lx=lx+0.5f;
}
else if(key == 'd'){
lx=lx-0.5f;
}
else if(key == ' '){
ly=ly-0.5f;
}
else if(key == 'c'){
ly=ly+0.5f;
}
else if(key == 'q'){
RotAtY+=0.5f;
}
else if(key == 'e'){
RotAtY-=0.5f;
}
else if(key == '2'){
RotAtX+=0.5f;
}
else if(key == 'x'){
RotAtX-=0.5f;
}
glutPostRedisplay(); //window need to be redisplayed
}
void drawCube()
{
glBegin( GL_QUAD_STRIP );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glEnd();
glBegin( GL_QUADS );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.5, -0.5, -0.5 );
glEnd();
}
void DrawScene(){ //draw the scene
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glRotatef(RotAtY, 0.0f, 1.0f, 0.0f);
glRotatef(RotAtX, 1.0f, 0.0f, 0.0f);
glTranslatef(lx, ly, lz -16 );
drawCube();
glLoadIdentity();
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-10.0f, 0.0f, -6.0f);
glVertex3f(10.f, 0.0f, -6.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, -10.0f, -6.0f);
glVertex3f(0.0f, 10.0f, -6.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -16.0f);
glVertex3f(0.0f, 0.0f, 4.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutGameModeString("1280x800:32@100"); //fullscreen options
if(glutGameModeGet(inf) != 0){
exit(1);
}
glutEnterGameMode(); //enter fullscreen
Init(); //enable modes
glutDisplayFunc(DrawScene);
glutKeyboardFunc(handle_keyboard);
glutReshapeFunc(resize);
glutPassiveMotionFunc(HandleMouseMotion);
glutMainLoop();
}