1

I'm trying "rotation algorithm" in opngl but it's not working I'm getting a blank page when I run my program . should I put POINT* verts or Point verts[6] is there something wrong with my code?

void rotate(POINT* verts, GLint nverts, POINT fixedv, GLdouble theta) {
     POINT newverts[6]; //POINT fixedv
    GLint k;


    for (k = 0; k < nverts; k++) {
        newverts[k].x = fixedv.x + (verts[k].x - fixedv.x) * cos(theta) - (verts[k].y - fixedv.y) * sin(theta);
        newverts[k].y = fixedv.y + (verts[k].x - fixedv.x) * sin(theta) + (verts[k].y - fixedv.y) * cos(theta);
        newverts[k].x = (verts[k].x) * cos(theta) - (verts[k].y) * sin(theta);
        newverts[k].y = (verts[k].x) * sin(theta) + (verts[k].y) * cos(theta);
    }
    glBegin(GL_TRIANGLE_FAN);
    for (k = 0; k < nverts; k++)
        glVertex2f(newverts[k].x, newverts[k].y);
    glEnd();
    glFlush();
}

display code:

void display() {
    glColor3f(r, g, b);
    if (check == 3) {
        double theta = 3.14 * 0.5;

        POINT verts[6],fixedpivot;
        fixedpivot.x = x;
        fixedpivot.y = y;

        verts[0].x = x + 25;
        verts[0].y = y + 50;
        verts[1].x = x;
        verts[1].y = y;
        verts[2].x = x+50;
        verts[2].y = y;
        verts[3].x = x + 25;
        verts[3].y = y + 50;
        verts[4].x = x + 50;
        verts[4].y = y + 100;
        verts[5].x = x;
        verts[5].y = y + 100;
    
        glClear(GL_COLOR_BUFFER_BIT);
        

    
        
        glColor3f(r, g, b);
        rotate(verts, 6, fixedpivot, theta);
        glFlush();
Spektre
  • 49,595
  • 11
  • 110
  • 380
zouzou
  • 31
  • 7
  • 1
    why are you rotating each point twice? first with center and then without ... rem-out the last 2 `newverts[k].? = ` lines. Also why rotate also renders (that is non-standard)? Also have you heard about [matrices](https://stackoverflow.com/a/28084380/2521214)? You can simply use `glMatrixmode/glPushmatrix/glPopmatrix/glRotatef/glTranslatef` without your rotation loop... simply just draw original polygon data. Also you can feed the `verts[]={ 0,1, 2,3, 4,5, ... }` directly while declaration no need to assign each element. – Spektre Jul 07 '20 at 10:53
  • 1
    Also rewriting your point to have `float[2]` instead of `x,y` or along with it (union) allows to use `glVertex2fv(verts[i].xy);` which is faster and less code to write. – Spektre Jul 07 '20 at 10:56

0 Answers0