2012-03-28 8 views
3

Ich versuche, ein Objekt zu übersetzen, ohne glTranslate() zu verwenden. Ich habe im Internet gesucht und eine Funktion glMultMatrixf() gefunden. Ich wollte meine Übersetzungsmatrix mit dieser Funktion multiplizieren, aber ich konnte es nicht erreichen. Ich habe das Gefühl, dass ich mich logisch auf dem richtigen Weg befinde, aber ein Objekt auf diese Weise sogar möglich übersetzt?OpenGL-Übersetzung mit glMultMatrixf

Ich habe den Code mit OpenGL und C++ geschrieben. Hier ist der Code:

#include <iostream> 
#include <stdlib.h> 
#include <math.h> 

#ifdef __APPLE__ 
#include <OpenGL/OpenGL.h> 
#include <GLUT/glut.h> 
#else 
#include <GL/glut.h> 
#endif 

using namespace std; 

float _angle = 0; 

typedef GLfloat Matrix4x4 [4][4]; 
Matrix4x4 matTransl3D; 

/* Construct the 4 x 4 identity matrix. */ 
void matrix4x4SetIdentity (Matrix4x4 matIdent4x4) 
{ 
    GLint row, col; 

    for (row = 0; row < 4; row++) 
     for (col = 0; col < 4 ; col++) 
      matIdent4x4 [row][col] = (row == col); 
} 

void translate3D (GLfloat tx, GLfloat ty, GLfloat tz) 
{ 

    /* Initialize translation matrix to identity. */ 
    matrix4x4SetIdentity (matTransl3D); 

    matTransl3D [0][3] = tx; 
    matTransl3D [1][3] = ty; 
    matTransl3D [2][3] = tz; 

} 


void handleKeypress(unsigned char key, int x, int y) { 
    switch (key) { 
     case 27: //Escape key 
      exit(0); 
    } 
} 



void initRendering() 
{ 
    glEnable(GL_DEPTH_TEST); 
    glEnable(GL_NORMALIZE); 

} 

void handleResize(int w, int h) 
{ 
    glViewport(0, 0, w, h); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(45.0, (float)w/(float)h, 1.0, 200.0); 
} 

void drawScene() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_MODELVIEW); 


    glLoadIdentity(); 

    translate3D(0.0f, 0.0f, -50.0f); 
    glMultMatrixf(*matTransl3D); 




    glBegin(GL_TRIANGLE_FAN);  // draw triangle 

    glColor3f(1.0f,0.0f,0.0f); // set color to red 
    glVertex3f( 0.0f, 3.0f, 0.0f); 
    glColor3f(0.0f,1.0f,0.0f);     // set color to green 
    glVertex3f(-5.0f, -5.0f, 5.0f); 
    glColor3f(1.0f,1.0f,0.0f);     // set color to yellow 
    glVertex3f(5.0f, -5.0f, 5.0f); 
    glColor3f(0.0f,0.0f,1.0f);     // set color to blue 
    glVertex3f(5.0f, -5.0f, -5.0f); 
    glColor3f(1.0f,1.0f,1.0f);     // set color to white 
    glVertex3f(-5.0f, -5.0f, -5.0f); 
    glColor3f(0.0f,1.0f,0.0f);     // set color to green 
    glVertex3f(-5.0f, -5.0f, 5.0f); 
    glEnd(); 

    glBegin(GL_QUADS);  
    glColor3f(0.0f,1.0f,0.0f);     // set color to green 
    glVertex3f(-5.0f, -5.0f, 5.0f); 
    glColor3f(1.0f,1.0f,1.0f);     // set color to white 
    glVertex3f(-5.0f, -5.0f, -5.0f); 
    glColor3f(0.0f,0.0f,1.0f);     // set color to blue 
    glVertex3f(5.0f, -5.0f, -5.0f); 
    glColor3f(1.0f,1.0f,0.0f);     // set color to yellow 
    glVertex3f(5.0f, -5.0f, 5.0f); 
    glEnd(); 


    glutSwapBuffers(); 
} 

//Called every 25 milliseconds 
void update(int value) { 
    _angle += 1.0f; 
    if (_angle > 360) { 
     _angle -= 360; 
    } 
    glutPostRedisplay(); 
    glutTimerFunc(25, update, 0); 
} 

int main(int argc, char** argv) { 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
    glutInitWindowSize(400, 400); 

    glutCreateWindow("Translation 3d"); 
    initRendering(); 


    glutDisplayFunc(drawScene); 
    glutKeyboardFunc(handleKeypress); 
    glutReshapeFunc(handleResize); 
    glutTimerFunc(25, update, 0); 

    glutMainLoop(); 
    return 0; 
} 

Antwort

11

OpenGL nimmt Matrizen als Spaltenhaupt, das heißt die Indizierung

0 4 8 c 
1 5 9 d 
2 6 a e 
3 7 b f 

Jedoch Sie die Indizierung Ihrer Übersetzungsmatrix in der Reihenfolge

0 1 2 3 
4 5 6 7 
8 9 a b 
c d e f 
+0

Dank. krank versuchen Sie es – Perex19

+1

@ Perex19: BTW: Wenn Sie sich die Zeit des Schreibens alle Matrix-Operationen ersparen wollen, biete ich eine kleine Bibliothek für diese https://github.com/datenwolf/linmath.h – datenwolf

+0

Ill überprüfen Sie Ihren Link. Ich kann nicht glauben, dass es so einfach war. Danke – Perex19