openGL Problem
-
Hallo,
also ich möchte vorweg sagen das ich noch ein Anfänger in OpenGl bin.
Ich frage mich schon seit einiger Zeit warum folgendes bei mir passiert:also ich erstelle mir zb. ein Viereck
glColor3f(1.0f,0.0f,0.0f); // ROT
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);nun sehe ich das Viereck. Aber wenn ich nun auf der Z-Achse Vor und Zurück bewege ändert sich nichts an der Größe.
Wenn ich durchfliege dann seh ich natürlich nichts mehr. Ab einer gewissen Entfernung verschwindet es plotzlich.Sollte es nicht größer/kleiner werden? da ich mich nun näher dran/weit weg befinde?
Ich sag schonmal Danke fürs lesen
-
Wie hast du denn die Projektionsmatrix aufgesetzt?
-
glMatrixMode(GL_MODELVIEW );
muss man da noch mehr machen?
-
Ja, wenn du den gewünschten Effekt haben willst brauchst du eine perspektivische Projektion. Zeig mal die komplette Initialisierungsequenz von deinem Programm.
-
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { _control87(MCW_EM, MCW_EM); Application->OnIdle = IdleLoop; } void __fastcall TForm1::FormCreate(TObject *Sender) { hdc = GetDC(Handle); SetPixelFormatDescriptor(); hrc = wglCreateContext(hdc); wglMakeCurrent(hdc, hrc); SetupRC(); } void __fastcall TForm1::IdleLoop(TObject*, bool& done) { done = false; RenderGLScene(); SwapBuffers(hdc); } void __fastcall TForm1::SetupRC() { glEnable( GL_DEPTH_TEST ); // Enable texture mapping glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Set the blending function for translucency glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This will clear the background color to black glClearDepth(1.0); // Enables clearing of the depth buffer glDepthFunc(GL_LESS); // The type of depth test to do glEnable(GL_DEPTH_TEST); // Enables depth testing glShadeModel(GL_SMOOTH); // Enables smooth color shading glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really nice perspective calculations //glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void __fastcall TForm1::SetPixelFormatDescriptor() { PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 24, 0,0,0,0,0,0, 0,0, 0,0,0,0,0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 }; PixelFormat = ChoosePixelFormat(hdc, &pfd); SetPixelFormat(hdc, PixelFormat, &pfd); }
entschuldige bitte die Unordnung
-
Du setzt nirgends die Projektionsmatrix! Das könntest du z.B. dort tun wo dein "Resize-Event" des Fensters angestoßen wird. In etwa so:
void Resize( GLsizei width, GLsizei height ) { if ( height == 0 ) { return; } glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); gluPerspective( 45.0f, ( GLdouble )width / height, 3.0, 100.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); }
-
Hopala hab vergessen den Resice zu posten:
void __fastcall TForm1::FormResize(TObject *Sender) { GLfloat nRange = 200.0f; glViewport(0, 0, ClientWidth, ClientHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (ClientWidth <= ClientHeight) glOrtho(-nRange, nRange, -nRange*ClientHeight/ClientWidth, nRange*ClientHeight/ClientWidth, -nRange, nRange); else glOrtho(-nRange*ClientWidth/ClientHeight, nRange*ClientWidth/ClientHeight, -nRange, nRange, -nRange, nRange); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
-
Habs rausgefunden:
gluPerspective(70.0,1,.3,10.0);
glTranslatef(x,y,z);
glBegin(GL_TRIANGLES);ich hatte das glTranslate noch vor der gluPerspective im Code.
Vielen Dank für die Hilfe