Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 顶点/索引缓冲区与OpenGL 3.3的混淆_C++_Opengl 3 - Fatal编程技术网

C++ 顶点/索引缓冲区与OpenGL 3.3的混淆

C++ 顶点/索引缓冲区与OpenGL 3.3的混淆,c++,opengl-3,C++,Opengl 3,我正在尝试使用OpenGL 3.3渲染3D立方体。我的代码基本上是基于 这是我的密码: // Attempt to make a cube in OpenGL 3.3, using GLEW and GLFW #include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <cmath> // Include GLEW (op

我正在尝试使用OpenGL 3.3渲染3D立方体。我的代码基本上是基于

这是我的密码:

// Attempt to make a cube in OpenGL 3.3, using GLEW and GLFW
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cmath>
// Include GLEW (openGL Extension Wrangler)
#define GLEW_STATIC
#include <GL/glew.h>
// Include GLFW (openGL FrameWork)
#include <GL/glfw.h>
// Define this helper macro to get an array position
#define BUFFER_OFFSET(i) ((char *)NULL + (i))

static const double PI = 3.14159265358979323846;

float radians(float inDeg)
{
    return inDeg * PI/180.0 ;
}

struct Vertex
{ // 64 bytes = 16 x 4 bytes per float
    GLfloat x, y, z ;         // position coordinates
    GLfloat nx, ny, nz ;      // normal coordinates
    GLfloat r, g, b, a ;      // color coordinates for vertex shading
    GLfloat s0, t0 ;          // s and t are the standard texture coordinates
    GLfloat s1, t1 ;          // (just used as padding)
    GLfloat s2, t2 ;          // (just used as padding)
} ;
// Vertex Attributes
GLuint      vertexSize       = sizeof( Vertex ) ;
GLuint      positionOffset   = 0 ;
GLuint      colorOffset      = 24 ;
// Vertex Data - stored dynamically, see BuildCube()
Vertex     *vertexData ;
GLuint     *indexData  ;
GLuint      vertexDataLength = 8  ;
GLuint      indexDataLength  = 36 ;

struct Matrix
{ // 64 bytes = 16 x 4 bytes per float
    GLfloat m[16] ;
} ;
// Matrix Data
Matrix      ModelMatrix ;
Matrix      ViewMatrix  ;
Matrix      ProjMatrix  ;

Matrix IdentityMatrix(void)
{   // set the diagonal values to one
    Matrix out ;
    out.m[0]  =  1.0 ; out.m[1]  =  0.0 ; out.m[2]  =  0.0 ; out.m[3]  =  0.0 ;
    out.m[4]  =  0.0 ; out.m[5]  =  1.0 ; out.m[6]  =  0.0 ; out.m[7]  =  0.0 ;
    out.m[8]  =  0.0 ; out.m[9]  =  0.0 ; out.m[10] =  1.0 ; out.m[11] =  0.0 ;
    out.m[12] =  0.0 ; out.m[13] =  0.0 ; out.m[14] =  0.0 ; out.m[15] =  1.0 ;
    return out ;
}

Matrix ZeroMatrix(void)
{   // set all values to zero
    Matrix out ;
    out.m[0]  =  0.0 ; out.m[1]  =  0.0 ; out.m[2]  =  0.0 ; out.m[3]  =  0.0 ;
    out.m[4]  =  0.0 ; out.m[5]  =  0.0 ; out.m[6]  =  0.0 ; out.m[7]  =  0.0 ;
    out.m[8]  =  0.0 ; out.m[9]  =  0.0 ; out.m[10] =  0.0 ; out.m[11] =  0.0 ;
    out.m[12] =  0.0 ; out.m[13] =  0.0 ; out.m[14] =  0.0 ; out.m[15] =  0.0 ;
    return out ;
}

Matrix Multiply(const Matrix *m1, const Matrix *m2)
{
    // Since these are note REALLY matrixes, we can get away with a shortcut
    Matrix out = IdentityMatrix() ;
    GLuint row, column, row_offset ;
    for (row = 0; row < 4; row++)
    {
        row_offset = row * 4 ;
        for (column = 0; column < 4; column++)
        {
            out.m[row_offset + column] =
            (m1->m[row_offset + 0] * m2->m[column + 0]) +
            (m1->m[row_offset + 1] * m2->m[column + 4]) +
            (m1->m[row_offset + 2] * m2->m[column + 8]) +
            (m1->m[row_offset + 3] * m2->m[column + 12]) ;
        }
    }
    return out ;
}

void Translate(Matrix &inMat, GLfloat dx, GLfloat dy, GLfloat dz)
{
    Matrix TM = IdentityMatrix() ;
    TM.m[3]  =  dx ;
    TM.m[7]  =  dy ;
    TM.m[11] =  dz ;
    inMat = Multiply(&inMat, &TM) ;
}

void Rotate(Matrix &inMat, GLfloat xAng, GLfloat yAng, GLfloat zAng)
{   // rotates a vector or point around the origin at the specified angles
    // create the half-angle values in radians
    GLfloat rad_x_ang = radians(xAng)/2.0 * -1.0 ;      // multiply by -1 to make the rotation right-handed
    GLfloat rad_y_ang = radians(yAng)/2.0        ;
    GLfloat rad_z_ang = radians(zAng)/2.0 ;      // multiply by -1 to make the rotation right-handed
    // compute sin and cos values, so they're not repeated a LOT
    GLfloat cosX = cos(rad_x_ang) ;
    GLfloat sinX = sin(rad_x_ang) ;
    GLfloat cosY = cos(rad_y_ang) ;
    GLfloat sinY = sin(rad_y_ang) ;
    GLfloat cosZ = cos(rad_z_ang) ;
    GLfloat sinZ = sin(rad_z_ang) ;
    // create quaternion vector: Q
    GLfloat q0 = cosZ * cosY * cosX + sinZ * sinY * sinX ;
    GLfloat q1 = sinZ * cosY * cosX - cosZ * sinY * sinX ;
    GLfloat q2 = cosZ * sinY * cosX + sinZ * cosY * sinX ;
    GLfloat q3 = cosZ * cosY * sinX - sinZ * sinY * cosX ;
    // create rotation matrix
    Matrix RM ;
    RM.m[0]  = q0*q0+q1*q1+q2*q2+q3*q3 ; RM.m[1]  = 0.0                     ; RM.m[2]  = 0.0                     ; RM.m[3]  = 0.0                     ;
    RM.m[4]  = 0.0                     ; RM.m[5]  = q0*q0-q1*q1-q2*q2+q3*q3 ; RM.m[6]  = 2*q2*q3 - 2*q0*q1       ; RM.m[7]  = 2*q1*q3 + 2*q0*q2       ;
    RM.m[8]  = 0.0                     ; RM.m[9]  = 2*q2*q3 + 2*q0*q1       ; RM.m[10] = q0*q0-q1*q1+q2*q2-q3*q3 ; RM.m[11] = 2*q1*q2 - 2*q0*q3       ;
    RM.m[12] = 0.0                     ; RM.m[13] = 2*q1*q3 - 2*q0*q2       ; RM.m[14] = 2*q1*q2 + 2*q0*q3       ; RM.m[15] = q0*q0+q1*q1-q2*q2-q3*q3 ;
    // multiply the new rotational matrix with the current incoming matrix
    inMat = Multiply(&inMat, &RM) ;
}

const GLchar* VertexShader =
{
    "#version 330\n"\
    "attribute vec3 in_Position;\n"\
    "attribute vec4 in_Color;\n"\
    "uniform   mat4 ModelMatrix;\n"\
    "uniform   mat4 ViewMatrix;\n"\
    "uniform   mat4 ProjMatrix ;\n"\
    "out       vec4 ex_Color;\n"\
    "void main(void)\n"\
    "{\n"\
    "   gl_Position = (ProjMatrix * ViewMatrix * ModelMatrix) * vec4(in_Position, 1.0);\n"\
    "   ex_Color    = in_Color;\n"\
    "}\n"
};

const GLchar* FragmentShader =
{
    "#version 330\n"\
    "in  vec4 ex_Color ;\n"\
    "out vec4 out_Color ;\n"\
    "void main(void)\n"\
    "{\n"\
    "   out_Color = ex_Color ;\n"\
    "}\n"
};

// Shader Attribute IDs
GLuint      attribute_in_Position ;
GLuint      attribute_in_Color ;
GLuint      uniform_ModelMatrix ;
GLuint      uniform_ViewMatrix ;
GLuint      uniform_ProjMatrix ;
// OpenGL Object IDs
GLuint      VertShaderID ;
GLuint      FragShaderID ;
GLuint      GLSLProgID ;
GLuint      vaoID ;
GLuint      vboID ;
GLuint      iboID ;

// Forward declare functions
void        BuildCube(void) ;
void        CreateShaders(void) ;
void        DestroyShaders(void) ;
void        InitStuff(void) ;
bool        DrawStuff(int) ;
void        KillStuff(void) ;

void BuildCube(void)
{
    vertexData = new Vertex[8] ;   // create a dynamic memory array of 8 vertices
    indexData  = new GLuint[36] ;
    // specify only 8 vertices (ignore texture and normals for now)
    vertexData[0].x  =  0.5 ; vertexData[0].y  =  0.5 ; vertexData[0].z  =  0.5 ;
    vertexData[1].x  = -0.5 ; vertexData[1].y  =  0.5 ; vertexData[1].z  =  0.5 ;
    vertexData[2].x  = -0.5 ; vertexData[2].y  = -0.5 ; vertexData[2].z  =  0.5 ;
    vertexData[3].x  =  0.5 ; vertexData[3].y  = -0.5 ; vertexData[3].z  =  0.5 ;
    vertexData[4].x  =  0.5 ; vertexData[4].y  =  0.5 ; vertexData[4].z  = -0.5 ;
    vertexData[5].x  = -0.5 ; vertexData[5].y  =  0.5 ; vertexData[5].z  = -0.5 ;
    vertexData[6].x  = -0.5 ; vertexData[6].y  = -0.5 ; vertexData[6].z  = -0.5 ;
    vertexData[7].x  =  0.5 ; vertexData[7].y  = -0.5 ; vertexData[7].z  = -0.5 ;
    // specify all triangles
    indexData[0]  = 0 ; indexData[1]  = 1 ; indexData[2]  = 2 ;  // top
    indexData[3]  = 2 ; indexData[4]  = 3 ; indexData[5]  = 0 ;
    indexData[6]  = 4 ; indexData[7]  = 6 ; indexData[8]  = 5 ;  // bottom
    indexData[9]  = 6 ; indexData[10] = 4 ; indexData[11] = 7 ;
    indexData[12] = 2 ; indexData[13] = 1 ; indexData[14] = 5 ;  // left
    indexData[15] = 5 ; indexData[16] = 6 ; indexData[17] = 2 ;
    indexData[18] = 0 ; indexData[19] = 3 ; indexData[20] = 4 ;  // right
    indexData[21] = 3 ; indexData[22] = 7 ; indexData[23] = 4 ;
    indexData[24] = 1 ; indexData[25] = 0 ; indexData[26] = 5 ;  // front
    indexData[27] = 0 ; indexData[28] = 4 ; indexData[29] = 5 ;
    indexData[30] = 3 ; indexData[31] = 2 ; indexData[32] = 6 ;  // back
    indexData[33] = 6 ; indexData[34] = 7 ; indexData[35] = 3 ;
    // specify the colors
    for (unsigned int ii = 0; ii < 8; ii++)
    {   //
        vertexData[ii].r =  vertexData[ii].x + 0.5 ;
        vertexData[ii].g =  vertexData[ii].y + 0.5 ;
        vertexData[ii].b =  vertexData[ii].z + 0.5 ;
        vertexData[ii].a =  1.0 ;
    }
    // end BuildCube()
}

void CreateShaders(void)
{
    GLenum ErrorCheckValue = glGetError();
    // establish vertex shader
    VertShaderID = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(VertShaderID, 1, &VertexShader, NULL);
    glCompileShader(VertShaderID);
    // establish fragment shader
    FragShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(FragShaderID, 1, &FragmentShader, NULL);
    glCompileShader(FragShaderID);
    // set the GLSL program ID
    GLSLProgID = glCreateProgram();
        // link the vertex and fragment shaders
        glAttachShader(GLSLProgID, VertShaderID);
        glAttachShader(GLSLProgID, FragShaderID);
    glLinkProgram(GLSLProgID);
    glUseProgram(GLSLProgID);
    // check for errors before continuing
    ErrorCheckValue = glGetError();
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
            stderr,
            "ERROR: Could not create the shaders: %s \n",
            gluErrorString(ErrorCheckValue)
        );
        exit(-1);
    }
}

void DestroyShaders(void)
{
    if (!glfwGetWindowParam(GLFW_OPENED)) { return ; }
    GLenum ErrorCheckValue = glGetError();
    glUseProgram(0);
    glDetachShader(GLSLProgID, VertShaderID);
    glDetachShader(GLSLProgID, FragShaderID);
    glDeleteShader(FragShaderID);
    glDeleteShader(VertShaderID);
    glDeleteProgram(GLSLProgID);
    // check for errors last
    ErrorCheckValue = glGetError();
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
            stderr,
            "ERROR: Could not destroy the shaders: %s \n",
            gluErrorString(ErrorCheckValue)
        ) ;
        exit(-1) ;
    }
}

void InitStuff(void)
{
    // Initialise GLFW extension
    if( !glfwInit() )
    {   // If the extension failed to initialize, then error out and leave.
        fprintf( stderr, "Failed to initialize GLFW\n" ) ;
        return ;
    }
    // Establish OpenGL version 3.3
    glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
    glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);    // This compensates for the bug in GLEW
    // Open a window and create its OpenGL context
    if( !glfwOpenWindow( 512, 512, 0,0,0,0, 0,0, GLFW_WINDOW ) )
    {
        fprintf( stderr, "Failed to open GLFW window.\n" ) ;
        glfwTerminate() ;
        exit(-1) ;
    }
    // Initialize GLEW extension
    if (glewInit() != GLEW_OK)
    {
        fprintf(stderr, "Failed to initialize GLEW\n") ;
        exit(-1) ;
    }
    // Set the title on the upper left of the window
    glfwSetWindowTitle("Test Window") ;
    // Ensure we can capture the escape key being pressed below
    glfwEnable( GLFW_STICKY_KEYS ) ;
    // Clear Screen And Depth Buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;

    // Establish the matrixes
    int width, height ;
    glfwGetWindowSize( &width, &height );       // assess window size
    height = height > 0 ? height : 1;           // avoid div/0 error
    // calculate the projection matrix values
    GLfloat     fov_y          = 45.0 ;
    GLfloat     aspect_ratio   = (GLfloat) width / (GLfloat) height ;
    GLfloat     near_plane     = 1.0 ;
    GLfloat     far_plane      = 100.0 ;
    GLfloat     y_scale        = 1.0 / tan( radians(fov_y / 2.0) ) ;
    GLfloat     x_scale        = y_scale / aspect_ratio ;
    GLfloat     frustum_length = far_plane - near_plane ;
    ProjMatrix = ZeroMatrix() ;
    ProjMatrix.m[0]  =   x_scale ;
    ProjMatrix.m[5]  =   y_scale ;
    ProjMatrix.m[10] =  -((far_plane + near_plane) / frustum_length) ;
    ProjMatrix.m[11] =  -1.0 ;
    ProjMatrix.m[14] =  -((2.0 * near_plane * far_plane) / frustum_length) ;

    // Establish the model and view matrixes as identity matrixes
    ModelMatrix = IdentityMatrix() ;
    ViewMatrix = IdentityMatrix() ;

    Translate(ViewMatrix, 0.0, 0.0, -2.0) ;
    // Creat the error enumeration
    GLenum ErrorCheckValue = glGetError();

    // Create the vertex and fragment shaders
    CreateShaders() ;
    // Bind the vertex shader attributes to their IDs
    attribute_in_Position =  glGetAttribLocation(GLSLProgID, "in_Position") ;
    attribute_in_Color    =  glGetAttribLocation(GLSLProgID, "in_Color"   ) ;
    uniform_ModelMatrix   = glGetUniformLocation(GLSLProgID, "ModelMatrix") ;
    uniform_ViewMatrix    = glGetUniformLocation(GLSLProgID, "ViewMatrix" ) ;
    uniform_ProjMatrix    = glGetUniformLocation(GLSLProgID, "ProjMatrix" ) ;

    // upload the projection matrix data to the GPU
    glUniformMatrix4fv(uniform_ModelMatrix, 1, GL_TRUE, ModelMatrix.m);
    glUniformMatrix4fv(uniform_ViewMatrix , 1, GL_TRUE, ViewMatrix.m );
    glUniformMatrix4fv(uniform_ProjMatrix , 1, GL_TRUE, ProjMatrix.m );

    // Build the Cube Geometry
    BuildCube() ;

    // Initialize the Vertex Buffer Object in OpenGL
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(GL_ARRAY_BUFFER, vertexSize * vertexDataLength, NULL, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, vertexSize * vertexDataLength, vertexData);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    // Initialize the Index Buffer Object in OpenGL
    glGenBuffers(1, &iboID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indexDataLength, NULL, GL_STATIC_DRAW);
    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLuint) * indexDataLength, indexData);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);



    // Check for errors, then done
    ErrorCheckValue = glGetError() ;
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
            stderr,
            "ERROR: Could not complete initialization: %s \n",
            gluErrorString(ErrorCheckValue)
        ) ;
        exit(-1) ;
    }
    // end InitStuff()
}

bool DrawStuff(int frame)
{
    int width, height ;
    glfwGetWindowSize( &width, &height ) ;   // First, re-assess the window size
    height = height > 0 ? height : 1 ;       // avoid div/0 error
    // for now, vary the background color so we know it's updating
    GLfloat phase_offset = 125.0f ;
    GLfloat speed_mult   = 0.01f ;
    GLfloat bgColorR = 0.5f * ( sin( (frame-0)*speed_mult ) + 1.0f );
    GLfloat bgColorG = 0.5f * ( sin( (frame-phase_offset)*speed_mult ) + 1.0f );
    GLfloat bgColorB = 0.5f * ( sin( (frame-2*phase_offset)*speed_mult ) + 1.0f );
    glClearColor( bgColorR, bgColorG, bgColorB, 0.0f );
    // Handle projection stuff
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glViewport( 0, 0, width, height );
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //
    GLenum ErrorCheckValue = glGetError();

    glEnable(GL_DEPTH_TEST) ;
    glDepthFunc(GL_LESS) ;
    glEnable(GL_CULL_FACE) ;
    glCullFace(GL_BACK) ;
    glFrontFace(GL_CCW) ;

    ErrorCheckValue = glGetError();
    if (ErrorCheckValue != GL_NO_ERROR)
    {
        fprintf(
            stderr,
            "ERROR: Could not set OpenGL culling options: %s \n",
            gluErrorString(ErrorCheckValue)
        ) ;
        exit(-1) ;
    }


    // switch to model view mode
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    // Assume I have multiple VBOs, but streamline for only one for now
    glPushMatrix() ;

        // re-bind the VBO data into the buffer
        glBindBuffer(GL_ARRAY_BUFFER, vboID) ;
        glVertexAttribPointer(attribute_in_Color, 4, GL_FLOAT, GL_FALSE, vertexSize, BUFFER_OFFSET(colorOffset)) ;
        glEnableVertexAttribArray(attribute_in_Color) ;

        glBindBuffer(GL_ARRAY_BUFFER, vboID) ;
        glVertexAttribPointer(attribute_in_Position, 3, GL_FLOAT, GL_FALSE, vertexSize, BUFFER_OFFSET(positionOffset)) ;
        glEnableVertexAttribArray(attribute_in_Position) ;

        // bind the indexes of the vertices to the buffer
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID) ;

        Rotate(ModelMatrix, 0.0, 0.0, 0.5) ;
        glUniformMatrix4fv(uniform_ModelMatrix, 1, GL_TRUE, ModelMatrix.m);

        ErrorCheckValue = glGetError();
        if (ErrorCheckValue != GL_NO_ERROR)
        {
            fprintf(
                stderr,
                "ERROR: Could not prep for draw: %s \n",
                gluErrorString(ErrorCheckValue)
            ) ;
            exit(-1) ;
        }

        glDrawArrays(GL_TRIANGLES, 0, indexDataLength);

        glDisableVertexAttribArray(attribute_in_Color) ;
        glDisableVertexAttribArray(attribute_in_Position) ;
        glBindBuffer(GL_ARRAY_BUFFER, 0) ;
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ;

    glPopMatrix();
    // lastly, swap buffers, then return the status
    glfwSwapBuffers();
    return !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
}

void KillStuff(void)
{
    DestroyShaders();
    glfwTerminate();
}

int main()
{
    int     frame = 0 ;
    bool    running = true;
    InitStuff() ;
    while(running)
    {
        frame++;
        running = DrawStuff(frame) ;
    }
    KillStuff() ;
    return 0;
}
//尝试在OpenGL 3.3中使用GLEW和GLFW创建多维数据集
#包括
#包括
#包括
#包括
#包括
//包括GLEW(openGL扩展牧马人)
#定义GLEW_静态
#包括
//包括GLFW(openGL框架)
#包括
//定义此辅助宏以获取数组位置
#定义缓冲区偏移量(i)((字符*)NULL+(i))
静态常数双PI=3.14159265358979323846;
浮动弧度(浮动分度)
{
返回指数*PI/180.0;
}
结构顶点
{//64字节=每个浮点16 x 4字节
glx,y,z;//位置坐标
GLfloat nx,ny,nz;//法线坐标
GLfloat r,g,b,a;//顶点着色的颜色坐标
GLfloat s0,t0;//s和t是标准纹理坐标
GLfloat s1,t1;//(仅用作填充)
GLfloat s2,t2;//(仅用作填充)
} ;
//顶点属性
GLuint vertexSize=sizeof(顶点);
胶合位置偏移=0;
胶合色偏移=24;
//顶点数据-动态存储,请参见BuildCube()
顶点*顶点数据;
GLuint*索引数据;
GLuint vertexDataLength=8;
GLuint indexDataLength=36;
结构矩阵
{//64字节=每个浮点16 x 4字节
glm[16];
} ;
//矩阵数据
矩阵模型矩阵;
矩阵视图矩阵;
矩阵ProjMatrix;
矩阵标识矩阵(void)
{//将对角线值设置为1
矩阵输出;
out.m[0]=1.0;out.m[1]=0.0;out.m[2]=0.0;out.m[3]=0.0;
out.m[4]=0.0;out.m[5]=1.0;out.m[6]=0.0;out.m[7]=0.0;
out.m[8]=0.0;out.m[9]=0.0;out.m[10]=1.0;out.m[11]=0.0;
out.m[12]=0.0;out.m[13]=0.0;out.m[14]=0.0;out.m[15]=1.0;
返回;
}
矩阵零矩阵(void)
{//将所有值设置为零
矩阵输出;
out.m[0]=0.0;out.m[1]=0.0;out.m[2]=0.0;out.m[3]=0.0;
out.m[4]=0.0;out.m[5]=0.0;out.m[6]=0.0;out.m[7]=0.0;
out.m[8]=0.0;out.m[9]=0.0;out.m[10]=0.0;out.m[11]=0.0;
out.m[12]=0.0;out.m[13]=0.0;out.m[14]=0.0;out.m[15]=0.0;
返回;
}
矩阵乘法(常数矩阵*m1,常数矩阵*m2)
{
//因为这些都是注释矩阵,所以我们可以通过一条捷径逃之夭夭
矩阵out=IdentityMatrix();
GLuint行、列、行偏移量;
用于(行=0;行<4;行++)
{
行_偏移=行*4;
用于(列=0;列<4;列++)
{
out.m[行偏移+列]=
(m1->m[行偏移+0]*m2->m[列+0])+
(m1->m[行偏移量+1]*m2->m[列+4])+
(m1->m[行偏移量+2]*m2->m[列+8])+
(m1->m[行偏移+3]*m2->m[列+12]);
}
}
返回;
}
无效转换(矩阵和inMat、GLfloat dx、GLfloat dy、GLfloat dz)
{
矩阵TM=识别矩阵();
TM.m[3]=dx;
TM.m[7]=dy;
TM.m[11]=dz;
inMat=乘法(&inMat,&TM);
}
空心旋转(矩阵和因马特、格洛特-桑、格洛特-杨、格洛特-藏)
{//以指定角度围绕原点旋转向量或点
//以弧度创建半角值
GLfloat rad_x_ang=弧度(xAng)/2.0*-1.0;//乘以-1使旋转右手
GLfloat rad_y_ang=弧度(阳)/2.0;
GLfloat rad_z_ang=弧度(zAng)/2.0;//乘以-1使旋转右手
//计算sin和cos值,这样它们就不会重复太多
GLfloat cosX=cos(rad_x_ang);
GLfloat sinX=sin(rad_x_ang);
GLfloat cosY=cos(rad_y__ang);
GLfloat sinY=sin(rad_y_ang);
GLfloat cosZ=cos(rad_z_ang);
GLfloat sinZ=sin(rad_z_ang);
//创建四元数向量:Q
GLfloat q0=cosZ*cosY*cosX+sinZ*sinY*sinX;
GLfloat q1=sinZ*cosY*cosX-cosZ*sinY*sinX;
GLfloat q2=cosZ*sinY*cosX+sinZ*cosY*sinX;
GLfloat q3=cosZ*cosY*sinX-sinZ*sinY*cosX;
//创建旋转矩阵
矩阵RM;
RM.m[0]=q0*q0+q1*q1+q2*q2+q3*q3;RM.m[1]=0.0;RM.m[2]=0.0;RM.m[3]=0.0;
RM.m[4]=0.0;RM.m[5]=q0*q0-q1*q1-q2*q2+q3*q3;RM.m[6]=2*q2*q3-2*q0*q1;RM.m[7]=2*q1*q3+2*q0*q2;
RM.m[8]=0.0;RM.m[9]=2*q2*q3+2*q0*q1;RM.m[10]=q0*q0-q1*q1+q2*q2-q3*q3;RM.m[11]=2*q1*q2-2*q0*q3;
RM.m[12]=0.0;RM.m[13]=2*q1*q3-2*q0*q2;RM.m[14]=2*q1*q2+2*q0*q3;RM.m[15]=q0*q0+q1*q1-q2*q2-q3*q3;
//将新旋转矩阵与当前传入矩阵相乘
inMat=乘法(&inMat,&RM);
}
常量GLchar*顶点着色器=
{
“#版本330\n”\
“属性vec3处于\u位置;\n”\
“颜色为\u的属性vec4;\n”\
“统一mat4模型矩阵;\n”\
“统一mat4视图矩阵;\n”\
“统一mat4 ProjMatrix;\n”\
“out vec4 ex_颜色;\n”\
“作废主(作废)\n”\
“{\n”\
gl_位置=(项目矩阵*视图矩阵*模型矩阵)*vec4(在_位置,1.0);\n\
“ex_Color=in_Color;\n”\
“}\n”
};
常量GLchar*碎片着色器=
{
“#版本330\n”\
“在vec4 ex_颜色中;\n”\
“输出向量4输出颜色;\n”\
“作废主(作废)\n”\
“{\n”\
“out\u Color=ex\u Color;\n”\
“}\n”
};
//着色器属性ID
胶合属性_在_位置;
颜色中的胶合属性;
GLuint一致矩阵;
GLuint均匀矩阵;
GLuint均匀矩阵;
//OpenGL对象ID
灰斑潜蝇;
白蜡虫;
GLuint-GLSLProgID;
胶膜;
胶合vboID;
粘液囊;
//远期申报