OpenGL: Bildausschnitt festlegen (Ursprung Koordinatensystem)



  • Hallo,

    ich bin Neuling in Sachen OpenGL.
    Vorerst soll ich eine Grafik erzeugen.
    Das klappt auch wunderbar, nur erhalte ich ein Koordinatensystem, das von -1 bis +1 in x- und y-Richtung geht.
    D.h. meine Grafik muss von hand eingepasst werden (Punkte haben größere Koordinaten) und die Grafik befindet sich nur im oberen, rechten Quadranten.

    Wie kann ich erreichen, dass das Koordinatensystem von 0 bis 500 (in x- und y-Richtung) geht?

    #include "stdafx.h"
    #include "..\glut\glut.h"
    
    void drawpoints(void);
    void display(void);
    void init();
    
    void drawpoints(void)
    {
    	GLfloat vertices[3][2] = {{0, 0}, {250, 500}, {500,0}};
    	GLfloat P[2] = {100, 100};
    	int j = 0;
    
    	glBegin(GL_POINTS);
    
    	for (int i = 0; i < 100000; i++)
    	{
    		j = rand() % 3;
    		if (P[0] >= vertices[j][0])
    		{
    			P[0] = P[0] - abs((vertices[j][0] - P[0]) / 2);
    		}
    		else
    		{
    			P[0] = P[0] + abs((vertices[j][0] - P[0]) / 2);
    		}
    
    		if (P[1] >= vertices[j][1])
    		{
    			P[1] = P[1] - abs((vertices[j][1] - P[1]) / 2);
    		}
    		else
    		{
    			P[1] = P[1] + abs((vertices[j][1] - P[1]) / 2);
    		}
    		glVertex2f(P[0]/500, P[1]/500);
    	}
    
    	glEnd();
    }
    // ----------------------------------------------------------------------------
    // Anzeigefunktion (callback)
    // ----------------------------------------------------------------------------
    void display(void)
    {
    	glClear(GL_COLOR_BUFFER_BIT);
    
    	drawpoints();
    
    	glFlush();
    }
    
    // ----------------------------------------------------------------------------
    // Initialisierung
    // ----------------------------------------------------------------------------
    void init()
    {
    	glClearColor(0.0, 0.0, 0.0, 1.0);
    	glColor3f(1.0, 1.0, 1.0);
    }
    
    // ----------------------------------------------------------------------------
    // Hauptfunktion
    // ----------------------------------------------------------------------------
    int main(int argc, char** argv)
    {
    	glutInit(&argc,argv);
    
    	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    	glutInitWindowSize(500, 500);
    	glutInitWindowPosition(0, 0);
    
    	glutCreateWindow("Übungsblatt OpenGL 1 - 2");
    	glutDisplayFunc(display);
    	init();
    
    	glutMainLoop();
    }
    


  • Problem mit glOrtho(...) gelöst.


Anmelden zum Antworten