Search code examples
copenglglut

Obtain a vertex after a transform in OpenGL?


I have the following piece of code:

#include <gl/glut.h>
#include <stdio.h>


GLfloat xf, yf, win;
GLint view_w, view_h;

//auxiliary vector
GLfloat aux[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
GLfloat vertices[768] = { 0.0 };
//count for aux vector
int count = 0;

//Meant to be used for drawing different shapes, store the number of vertecies for each primitive
int primitives[64] = {};

//Index
int vIndex = 0;
int pIndex = 0;

bool flag = false;

//Dedraws the scene using the vertices vector
void Redisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();     
    int j = 0;
        int k;
        for (int i = 0; i < pIndex; i++)
        {
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glBegin(GL_TRIANGLES);
            k = j;
            for(; j < k + 2*primitives[i];j+=2)
                glVertex2f(vertices[j], vertices[j + 1]);
            glEnd();
            glFlush();

        }
}

//Store vertices in the vertices vector
void storePrimitive(GLfloat *vector, int nVertex)
{
    for (int i = 0; i < nVertex * 2; i++)
        vertices[vIndex++] = vector[i];
    primitives[pIndex++] = nVertex;
}

//Draw scene
void draw(void)
{
    if (count > 5)
    {
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        //If flag is true, apply a transform
        if (flag)
        {
            glPushMatrix();
            glScalef(2, 2, 1);
        }
        glBegin(GL_TRIANGLES);
        glVertex2f(aux[0], aux[1]);
        glVertex2f(aux[2], aux[3]);
        glVertex2f(aux[4], aux[5]);
        storePrimitive(aux, 3);
        if (flag)
            glPopMatrix();
        glEnd();
        glFlush();
        count = 0;
        flag = false;
    }
}

void init(void)
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    xf = 50.0f;
    yf = 50.0f;
    win = 250.0f;
}

void resize(GLsizei w, GLsizei h)
{
    glViewport(0, 0, w, h);
    view_w = w;
    view_h = h;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-win, win, -win, win);
}

void keyboardFunc(unsigned char key, int x, int y)
{
    switch (key) {
    case 'r': //Clears the screen
        glClear(GL_COLOR_BUFFER_BIT);
        glFlush();
        break;
    case 'd': //Redraw scene
        Redisplay();
        break;
    case 't': //Apply a transform to the next primitive
        flag = true;
        break;
    }
}

void mouse(int button, int state, int x, int y)
{

    if (button == GLUT_LEFT_BUTTON)
        if (state == GLUT_DOWN) {        
            aux[count] = ((2 * win * x) / view_w) - win;
            count++;
            aux[count] = (((2 * win) * (y - view_h)) / -view_h) - win;
            count++;
        }
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(650, 600);
    glutInitWindowPosition(10, 10);
    glutCreateWindow("Example");
    glutDisplayFunc(draw);
    glutReshapeFunc(resize);
    glutKeyboardFunc(keyboardFunc);
    glutMouseFunc(mouse);
    init();
    glutMainLoop();
}

You can see that it is possible to apply a transform to a primitive in the draw function. What I want to do, however, is to store the vertices of the transformed primitive in the vertices vector. Is it possible to do so without resorting to manually applying the transform to each vertex?


Solution

  • Yes, it it possible using OpenGL's Transform Feedback. However, I see you are using old style OpenGL, and Transform Feedback is only available since OpenGL 3.0. The new OpenGL style is very very different. If you want to use TransformFeedback you would need to delete all your code and start from scratch.

    OP found the way to capture TransformFeedback from glBegin/End: https://www.glprogramming.com/red/chapter13.html#name2