GithubHelp home page GithubHelp logo

kplanisphere / global-modeling-matrix Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 682 KB

Proyecto 6 - Graficación

Home Page: https://linktr.ee/planisphere.kgz

C++ 100.00%
3d-transformations computer-graphics cpp data-visualization opengl rotation scaling translation global-modeling-matrix

global-modeling-matrix's Introduction

Global Modeling Matrix

Description

This project, completed as part of the course work at the Benemérita Universidad Autónoma de Puebla, focuses on implementing a global modeling matrix to manage 3D transformations for multiple objects. The objective is to use a combination of rotation, scaling, and translation to manipulate the position and orientation of 3D objects in space.

Overview

OpenGL uses transformation matrices to manage the position, rotation, and scaling of objects. This project demonstrates how to use a global modeling matrix to apply transformations efficiently to multiple objects. The implementation includes operations for rotation, scaling, and translation, and leverages a stack to handle multiple transformation states.

Objectives

  • Implement a global modeling matrix for 3D transformations using OpenGL.
  • Apply learned concepts to rotate, scale, and translate multiple 3D objects.
  • Develop an understanding of transformation matrices and their applications in computer graphics.

Key Features

  • Initialization: Set up the OpenGL environment and window properties.
  • Transformation Functions: Implement functions for rotating, scaling, and translating objects.
  • Matrix Operations: Utilize matrix multiplication for applying transformations.
  • Global Modeling Matrix: Manage transformations using a global modeling matrix.
  • Animation Loop: Continuously apply transformations to animate 3D objects.

Project Structure

The project includes the following main components:

Initialization

This function sets up the OpenGL environment, defining the color of the window and the projection parameters.

void init(void) {
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)WIDTH / HEIGHT, ZNEAR, ZFAR);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(EYE_X, EYE_Y, EYE_Z, CENTER_X, CENTER_Y, CENTER_Z, UP_X, UP_Y, UP_Z);
    glClearColor(0, 0, 0, 0);
}

Global Modeling Matrix

This function manages the global modeling matrix and applies transformations.

void Operaciones3D::GlobalModelingMatrix(char eje, float theta, float sx, float sy, float sz, float tx, float ty, float tz) {
    LoadIdentity(A);
    switch (eje) {
        case 'X':
            rotateX(DegToRad(theta));
            break;
        case 'Y':
            rotateY(DegToRad(theta));
            break;
        case 'Z':
            rotateZ(DegToRad(theta));
            break;
    }
    translate(tx, ty, tz);
    MultM(T, A, A);
    Escalado(sx, sy, sz);
    MultM(E, A, A);
}

Main Function

This function initializes the graphics window and starts the main loop.

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowPosition(50, 50);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutCreateWindow("Global Modeling Matrix | Jesus Huerta Aguilar");
    init();
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutKeyboardFunc(keys);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

Display Function

This function handles the rendering of objects using the global modeling matrix.

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    drawAxis();
    glColor3f(1.0f, 1.0f, 1.0f);
    S.ShowStage();
    glutSwapBuffers();
}

Transformation Functions

These functions define the rotation, scaling, and translation matrices and multiply them with the transformation matrix.

void translate(float dx, float dy, float dz) {
    float T[4][4] = {
        {1, 0, 0, dx},
        {0, 1, 0, dy},
        {0, 0, 1, dz},
        {0, 0, 0, 1}
    };
    MultM(R, T, A);
}

void rotateX(float theta) {
    float R[4][4] = {
        {1, 0, 0, 0},
        {0, cos(theta), -sin(theta), 0},
        {0, sin(theta), cos(theta), 0},
        {0, 0, 0, 1}
    };
    MultM(T, R, A);
}

void rotateY(float theta) {
    float R[4][4] = {
        {cos(theta), 0, sin(theta), 0},
        {0, 1, 0, 0},
        {-sin(theta), 0, cos(theta), 0},
        {0, 0, 0, 1}
    };
    MultM(T, R, A);
}

void rotateZ(float theta) {
    float R[4][4] = {
        {cos(theta), -sin(theta), 0, 0},
        {sin(theta), cos(theta), 0, 0},
        {0, 0, 1, 0},
        {0, 0, 0, 1}
    };
    MultM(T, R, A);
}

Matrix Multiplication Function

This function multiplies two 4x4 matrices.

void MultM(float A[4][4], float B[4][4], float C[4][4]) {
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            C[i][j] = 0;
            for (int k = 0; k < 4; k++) {
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }
}

Execution

The project initializes a graphical window and applies global transformations to the pyramid. The transformations are continuous, providing a dynamic visual representation of the 3D modeling matrix operations.

Pressing "1"

Pressing "Q"

Pressing "A"

global-modeling-matrix's People

Contributors

kplanisphere avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.