Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7769603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:12:01+00:00 2026-06-01T16:12:01+00:00

All the tutorials I’ve seen always use a single object, like a triangle or

  • 0

All the tutorials I’ve seen always use a single object, like a triangle or cube. But it’s not clear to me how I can independently manipulate separate objects. I’ve seen some tutorials that refer to the fixed function pipeline and use pushmatrix and popmatrix, but with the programmable pipeline these functions are gone. Below are an init function and a draw function that draw a single triangle on the screen and rotates it about the Z axis. Can someone show me the code, or even pseudocode to add a second triangle and rotate it independently of the other triangle? Say around a different axis or in the opposite direction?

Here they are:

int Init(ESContext* esContext)
{
    UserData* userData = (UserData *)esContext->userData;
    const char *vShaderStr =
        "attribute vec4 vPosition;  \n"
        "uniform mat4 MVPMatrix;"
        "void main()                \n"
        "{                          \n"
        "   gl_Position = MVPMatrix * vPosition;\n"
        "}                          \n";

    const char *fShaderStr =
        "precision mediump float;   \n"
        "void main()                \n"
        "{                          \n"
        "   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n"
        "}                          \n";

    GLuint vertexShader;
    GLuint fragmentShader;
    GLuint programObject;
    GLint linked;
    GLfloat ratio = 320.0f/240.0f;

    vertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr);
    fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr);

    programObject = glCreateProgram();

    if (programObject == 0)
        return 0;

    glAttachShader(programObject, vertexShader);
    glAttachShader(programObject, fragmentShader);

    glBindAttribLocation(programObject, 0, "vPosition");
    glLinkProgram(programObject);
    glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &linked);

    if (!linked)
    {
        GLint infoLen = 0;
        glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);

        if (infoLen > 1)
        {
            char* infoLog = (char *)malloc(sizeof(char) * infoLen);
            glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);

            free(infoLog);
        }

        glDeleteProgram(programObject);
        return FALSE;
    }

    userData->programObject = programObject;

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    glViewport(0, 0, esContext->width, esContext->height);
    glClear(GL_COLOR_BUFFER_BIT);


    glUseProgram(userData->programObject);

    userData->angle = 0.0f;
    userData->start = time(NULL);
    userData->ProjMatrix = PVRTMat4::Perspective(ratio*2.0f, 2.0f, 3.0f, 7.0f, PVRTMat4::eClipspace::OGL, false, false);
    userData->ViewMatrix = PVRTMat4::LookAtLH(PVRTVec3(0.0f, 0.0f, -3.0f), PVRTVec3(0.0f, 0.0f, 0.0f), PVRTVec3(0.0f, 1.0f, 0.0f));
    return TRUE;
}



void Draw(ESContext *esContext)
{
    GLfloat vVertices[] = {0.0f, 0.5f, 0.0f,
                          -0.5f, -0.5f, 0.0f,
                           0.5f, -0.5f, 0.0f};

    GLint MVPHandle;
    double timelapse;

    PVRTMat4 MVPMatrix = PVRTMat4::Identity();
    UserData* userData = (UserData *)esContext->userData;

    timelapse = difftime(time(NULL), userData->start) * 1000;
    if(timelapse > 16.0f) //Maintain approx 60FPS
    {
        if (userData->angle > 360.0f)
        {
            userData->angle = 0.0f;
        }
        else
        {
            userData->angle += 0.1f;
        }
    }

    userData->ModelMatrix = PVRTMat4::RotationZ(userData->angle);

    MVPMatrix = userData->ViewMatrix * userData->ModelMatrix;
    MVPMatrix = userData->ProjMatrix * MVPMatrix;

    MVPHandle = glGetUniformLocation(userData->programObject, "MVPMatrix");
    glUniformMatrix4fv(MVPHandle, 1, FALSE, MVPMatrix.ptr());

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
    glEnableVertexAttribArray(0);

    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:12:02+00:00Added an answer on June 1, 2026 at 4:12 pm

    After drawing the first triangle, generate a new MVP matrix with your new desired rotation/position, upload it, and then draw the triangle a second time. You can change uniforms as many times as you want during the scene.

    This is similar to what push and pop are doing, they’re just changing the active matrix before drawing a given object.

    Example pseudocode:

    MMatrix = identity;
    MVPMatrix = VPMatrix * MMatrix;
    glUniformMatrix4fv(MVPHandle, 1, FALSE, MVPMatrix.ptr());
    glDrawArrays(GL_TRIANGLES, 0, 3); //draw triangle at 0,0,0
    
    MMatrix.translate(1,0,0);
    MVPMatrix = VPMatrix * MMatrix;
    glUniformMatrix4fv(MVPHandle, 1, FALSE, MVPMatrix.ptr());
    glDrawArrays(GL_TRIANGLES, 0, 3); //draw triangle at 1,0,0
    
    MMatrix.translate(1,0,0);
    MVPMatrix = VPMatrix * MMatrix;
    glUniformMatrix4fv(MVPHandle, 1, FALSE, MVPMatrix.ptr());
    glDrawArrays(GL_TRIANGLES, 0, 3); //draw triangle at 2,0,0
    
    ..repeat for as many objects as you want..
    

    This will leave you with three triangles, at (0,0,0), (1,0,0), and (2,0,0).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning RESTful Web Services and all the tutorials use javax.ws.rs.* package. But
This is very dumb - but I've seen all the tutorials and guides -
All the tutorials I have seen seem to use *.jsf , *.faces , or
I see all those tutorials about how one can use Javascript for input validating
I don't know why but in all tutorials I've seen there is DBObject.TableObject.Add(newObject); .
I've got all the tutorials up to beginner #5 translated and working, but I
I have seen tutorials all over the place for explaining how to get Code
I think all of the tutorials and stuff are great on blogs, but sometimes
In almost all tutorials of BackgroundWorker the reportProgress event is handled like this (this
I keep reading tutorials of how to use it in Silverlight Apps, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.