2017-03-05 1 views
1

J'ai dessiné un cercle dans OpenGL. Ce que je voudrais faire, c'est en montrer une partie à plusieurs reprises. Par exemple, au lieu de montrer seulement un cercle, montrant 4 de demi-cercle en continu. Je pensais que c'était possible en ayant deux boucles for et en utilisant un glViewport (paramètres) à l'intérieur. Mais je n'ai pas réussi à le faire.Répétition d'une image plusieurs fois dans OpengGL

J'ai mon code ici, les boucles for sont au début de la fonction circle(). J'apprécierais que quelqu'un puisse m'aider à ce sujet.

#include <Windows.h> 
#include <iostream> 
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
#include <GL/glut.h> 
#include <GL/gl.h> 
#include <GL/glu.h> 
#include <fstream> 


using namespace std; 

int pntX1, pntY1, r; 

void myInit() 
{ 
    glClearColor(1.0, 1.0, 1.0, 0.0); 
    glColor3f(0.0f, 0.0f, 0.0f); 
    glPointSize(4.0); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0.0, 101.0, 0.0, 101.0); 
} 

void drawPolyLineFile(char * fileName) 
{ 
    fstream inStream; 
    inStream.open(fileName, ios::in); 
    if (inStream.fail()) 
     return; 
    glClear(GL_COLOR_BUFFER_BIT); 
    GLint numpolys, numlines, x, y; 
    inStream >> numpolys; 

    for (int j = 0; j < numpolys; j++) 
    { 
     inStream >> numlines; 
     glBegin(GL_LINE_STRIP); 
     for (int i = 0; i < numlines; i++) 
     { 
      inStream >> x >> y; 
      glVertex2i(x, y); 
     } 
     glEnd(); 
    } 
    glFlush(); 
    inStream.close(); 
} 

void writePixel(GLint x, GLint y) 
{ 
    glBegin(GL_POINTS); 
    glVertex2i(x + pntX1, y + pntY1); 
    glEnd(); 
} 

void circlePoints(int x, int y) { 
    writePixel(x, y); 
    writePixel(y, x); 
    writePixel(y, -x); 
    writePixel(x, -y); 
    writePixel(-x, -y); 
    writePixel(-y, -x); 
    writePixel(-y, x); 
    writePixel(-x, y); 
} 

void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top) { 
    glClearColor(1.0, 1.0, 1.0, 0.0); 
    glColor3f(0.0f, 0.0f, 0.0f); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(left, right, bottom, top); 
} 

void setViewPort(GLint left, GLint right, GLint bottom, GLint top) 
{ 
    glViewport(left, bottom, right - left, top - bottom); 
} 
void Circle() { 
    //int x1=100,y1=100,r=50; 
    //int x=0,y=r; 
    //int d = 3/2 - r; 

    //glViewport(50, 50, 50, 50); 
    /* 
    setWindow(0.0, 101.0, 0.0, 101.0); 
    for (int i = 0; i<1; i++) 
     for (int j = 0; j < 1; j++) 
     { 
      //glViewport(i * 50 +, j * 50, 50, 50); 
      glViewport(50, 50, 50, 50); 
      drawPolyLineFile("dino.dat"); 
     } 
    */ 

    //glViewport(0, 0, 0, 0); 

    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1, 0, 0); 

    int x = 0; 
    int r = 50; 
    int y = r; 
    int h = (1 - r); 
    int deltaE = 3; 
    int deltaSE = (-2)*r + 5; 

    circlePoints(x, y); 

    while (y > x) { 

     if (h<0) { 
      h += deltaE; 
      deltaE += 2; 
      deltaSE += 2; 
     } 
     else { 
      h += deltaSE; 
      deltaE += 2; 
      deltaSE += 4; 
      y--; 
     } 
     x++; 
     circlePoints(x, y); 
    } 

} 

void Display() { 
    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(0.0, 0.0, 0.0); 
    glPointSize(1.0); 

    Circle(); 

    glFlush(); 
} 


int main(int argc, char *argv[]) 
{ 
    pntX1 = 50; pntY1 = 50; r = 50; 

    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(101, 101.0); 
    glutInitWindowPosition(100, 150); 
    glutCreateWindow("My circles"); 


    //glViewport(0, 0, 100, 100); 

    /* 
    setWindow(0, 500.0, 0, 500.0); 
    for(int i=0; i<2; i++) 
     for(int j=0; j<2; j++) 
     { 
     glViewport(i*250, j*250, 250, 250); 
     //drawPolylineFile("circles.dat"); 
     } 
     */ 


    glutDisplayFunc(Display); 
    myInit(); 

    glutMainLoop(); 
    return 0; 
} 

Répondre

0

changer Viewport est pas ce que vous avez besoin utiliser des matrices au lieu:

for (i=0;i<N;i++) // N is number of objects you want to render 
{ 
glMatrixMode(GL_MODELVIEW); 
glPushMatrix(); 
glTranslate3(dx[i],dy[i],dz[i]); // dx,dy,dz is offset of object i where you want to draw it 
// render i-th object occurrence 
glPopMatrix(); 
} 

espérons aussi que vous savez que vous pouvez dessiner cercle avec GL_LINE_LOOP et GL_TRIANGLE_FAN primitives beaucoup plus rapide.

Ne changez pas glViewPort car ce n'est pas ce que vous pensez! Il mappe votre espace d'écran et si vous le changez pendant le rendu, alors vous invalidez la profondeur et les tampons auxiliaires de sorte que vous ne pouvez plus utiliser de trucs avancés.

Pour plus d'informations, voir: