Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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 带纹理的扩展旋转立方体示例_C_Opengl - Fatal编程技术网

C 带纹理的扩展旋转立方体示例

C 带纹理的扩展旋转立方体示例,c,opengl,C,Opengl,我目前正在开发一个旋转立方体示例的扩展。 但由于有很多变化,因为索引缓冲区对于纹理不是一个好主意,所以我 现在有一个bug。我不明白为什么我的立方体现在只由一些三角形组成。 所以我希望其他人能发现这个问题 为了更好地理解,我把整个代码放在这一页上 GLfloat vertex\u buffer\u data[]={/*8个立方体顶点*/ -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.

我目前正在开发一个旋转立方体示例的扩展。 但由于有很多变化,因为索引缓冲区对于纹理不是一个好主意,所以我 现在有一个bug。我不明白为什么我的立方体现在只由一些三角形组成。 所以我希望其他人能发现这个问题

为了更好地理解,我把整个代码放在这一页上

GLfloat vertex\u buffer\u data[]={/*8个立方体顶点*/
-1.0, -1.0,  1.0,
1.0, -1.0,  1.0,
1.0,  1.0,  1.0,
-1.0,  1.0,  1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0,  1.0, -1.0,
-1.0,  1.0, -1.0,
};
GLushort索引_缓冲_数据[]={//6*2三角形的索引
0, 1, 2,
2, 3, 0,
1, 5, 6,
6, 2, 1,
7, 6, 5,
5, 4, 7,
4, 0, 3,
3, 7, 4,
4, 5, 1,
1, 0, 4,
3, 2, 6,
6, 7, 3,
};
GLfloat UV[]={
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0
};
类型定义结构{
GLfloat位置[3];
gluv[2];
}顶点数据;
顶点数据顶点[索引大小];
/*----------------------------------------------------------------*/
/******************************************************************
*
*展示
*
*当需要修改窗口内容时,调用此函数
*绘制/重画。它已通过“glutDisplayFunc()”指定;
*启用顶点属性,在C程序和
*着色器中的属性名称
*
*******************************************************************/
无效显示()
{
/*清除窗口;在“初始化()中指定颜色”*/
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
GlenableVertexAttributeArray(vPosition);
glBindBuffer(GL_数组_BUFFER,VBO);
glvertexattributepointer(vPosition,3,GL_FLOAT,GL_FALSE,0,0);
GlenableVertexAttributeArray(vUV);
glBindBuffer(GL_数组_BUFFER,TextureID);
glvertexattributepointer(vUV,2,GL_FLOAT,GL_FALSE,0,0);
/*GlenableVertexAttributeArray(vColor);
glBindBuffer(GL_数组_BUFFER,CBO);
glvertexattributepointer(vColor,3,GL_FLOAT,GL_FALSE,0,0);
*/
//glBindBuffer(GL_元素数组缓冲区,IBO);
//闪烁大小;
//glGetBufferParameteriv(GL_元素\数组\缓冲区、GL_缓冲区\大小和大小);
/*激活第一个(也是唯一一个)纹理单元*/
玻璃纹理(GL_纹理0);
/*绑定当前纹理*/
glBindTexture(GL_TEXTURE_2D,TextureID);
/*从片段着色器获取纹理统一句柄*/
TextureUniform=glGetUniformLocation(着色器程序,“myTextureSampler”);
/*设置统一取样器变量的位置*/
glUniform1i(结构均匀,0);
/*启用位置和UV属性*/
GlenableVertexAttributeArray(vPosition);
GlenableVertexAttributeArray(vUV);
glvertexattributepointer(vPosition,3,GL_FLOAT,GL_FALSE,sizeof(vertex_buffer_data),0);
glvertexattributepointer(vUV,2,GL_FLOAT,GL_FALSE,sizeof(VertexData),(const GLvoid*)sizeof(顶点[0]。位置));
/*将程序与着色器矩阵关联*/
GLint projectionUniform=glGetUniformLocation(着色器程序,“ProjectionMatrix”);
if(projectionUniform==-1)
{
fprintf(stderr,“无法绑定统一的ProjectionMatrix\n”);
出口(-1);
}
glUniformMatrix4fv(投影均匀,1,GL_真,投影矩阵);
GLint ViewUniform=glGetUniformLocation(着色器程序,“ViewMatrix”);
如果(ViewUniform==-1)
{
fprintf(stderr,“无法绑定统一ViewMatrix\n”);
出口(-1);
}
glUniformMatrix4fv(ViewUniform,1,GL_TRUE,ViewMatrix);
闪烁旋转均匀=glGetUniformLocation(着色器程序,“模型矩阵”);
如果(旋转均匀==-1)
{
fprintf(stderr,“无法绑定统一ModelMatrix\n”);
出口(-1);
}
glUniformMatrix4fv(旋转均匀,1,GL_真,模型矩阵);
GLDRAWARRAY(GLU三角形,0,numvities);
/*禁用属性*/
GLDisableVertexAttributeArray(vPosition);
glDisableVertexAttribArray(vUV);
/*前后缓冲区之间的交换*/
glutSwapBuffers();
}
void SetupDataBuffers()
{
glGenBuffers(1,&VBO);
glBindBuffer(GL_数组_BUFFER,VBO);
glBufferData(GLU数组缓冲区、大小(顶点)、顶点、GLU静态图);
}
void getNewVertices(){
int i;
对于(i=0;i
首先,您的顶点指针是错误的

您没有在任何地方使用(名为的VBO)
TextureID
,因此请删除所有这些代码: 替换为: 最后,由于您将
position
uv
打包到一个结构数组中,这就是您想要的:
这将修复我立即发现的代码错误,可能还有其他问题我没有解决。

“顶点指针错误”==索引缓冲区数据中的顺序。?(我想把它们都写在纸上,但这有点太多了。)不,我的意思是它们的设置是错误的。最初,每个顶点位置的跨步设置为
GLfloat vertex\u buffer\u data[]
的大小,此时它应等于
VertexData
的一个实例的大小。
 GLfloat vertex_buffer_data[] = { /* 8 cube vertices */
    -1.0, -1.0,  1.0,
     1.0, -1.0,  1.0,
     1.0,  1.0,  1.0,
    -1.0,  1.0,  1.0,
    -1.0, -1.0, -1.0,
     1.0, -1.0, -1.0,
     1.0,  1.0, -1.0,
    -1.0,  1.0, -1.0,
};

GLushort index_buffer_data[] = { // Indices of 6*2 triangles
    0, 1, 2,
    2, 3, 0,
    1, 5, 6,
    6, 2, 1,
    7, 6, 5,
    5, 4, 7,
    4, 0, 3,
    3, 7, 4,
    4, 5, 1,
    1, 0, 4,
    3, 2, 6,
    6, 7, 3,
};

GLfloat UV[] = {
    0.0, 0.0,
    1.0, 1.0,
    0.0, 1.0,

    0.0, 0.0,
    1.0, 0.0,
    1.0, 1.0,

    0.0, 0.0,
    1.0, 1.0,
    0.0, 1.0,

    0.0, 0.0,
    1.0, 0.0,
    1.0, 1.0,

    0.0, 0.0,
    1.0, 1.0,
    0.0, 1.0,

    0.0, 0.0,
    1.0, 0.0,
    1.0, 1.0,

    0.0, 0.0,
    1.0, 1.0,
    0.0, 1.0,

    0.0, 0.0,
    1.0, 0.0,
    1.0, 1.0,

    0.0, 0.0,
    1.0, 1.0,
    0.0, 1.0,

    0.0, 0.0,
    1.0, 0.0,
    1.0, 1.0,

    0.0, 0.0,
    1.0, 1.0,
    0.0, 1.0,

    0.0, 0.0,
    1.0, 0.0,
    1.0, 1.0
};

typedef struct {
    GLfloat Position[3];
    GLfloat UV[2];
} VertexData;

VertexData Vertices[index_size];



/*----------------------------------------------------------------*/


/******************************************************************
*
* Display
*
* This function is called when the content of the window needs to be
* drawn/redrawn. It has been specified through 'glutDisplayFunc()';
* Enable vertex attributes, create binding between C program and 
* attribute name in shader
*
*******************************************************************/

void Display()
{
    /* Clear window; color specified in 'Initialize()' */
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnableVertexAttribArray(vPosition);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glEnableVertexAttribArray(vUV);
    glBindBuffer(GL_ARRAY_BUFFER, TextureID);
    glVertexAttribPointer(vUV, 2, GL_FLOAT, GL_FALSE, 0, 0);

    /*glEnableVertexAttribArray(vColor);
    glBindBuffer(GL_ARRAY_BUFFER, CBO);
    glVertexAttribPointer(vColor, 3, GL_FLOAT,GL_FALSE, 0, 0);   
    */

    //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
    //GLint size;
    //glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);


    /* Activate first (and only) texture unit */
    glActiveTexture(GL_TEXTURE0);

    /* Bind current texture  */
    glBindTexture(GL_TEXTURE_2D, TextureID);

    /* Get texture uniform handle from fragment shader */ 
    TextureUniform  = glGetUniformLocation(ShaderProgram, "myTextureSampler");

    /* Set location of uniform sampler variable */ 
    glUniform1i(TextureUniform, 0);

    /* Enable position and UV attribute */
    glEnableVertexAttribArray(vPosition);
    glEnableVertexAttribArray(vUV);


    glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_buffer_data), 0);
    glVertexAttribPointer(vUV, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const GLvoid*) sizeof(Vertices[0].Position));

    /* Associate program with shader matrices */
    GLint projectionUniform = glGetUniformLocation(ShaderProgram, "ProjectionMatrix");
    if (projectionUniform == -1) 
    {
        fprintf(stderr, "Could not bind uniform ProjectionMatrix\n");
    exit(-1);
    }
    glUniformMatrix4fv(projectionUniform, 1, GL_TRUE, ProjectionMatrix);

    GLint ViewUniform = glGetUniformLocation(ShaderProgram, "ViewMatrix");
    if (ViewUniform == -1) 
    {
        fprintf(stderr, "Could not bind uniform ViewMatrix\n");
        exit(-1);
    }
    glUniformMatrix4fv(ViewUniform, 1, GL_TRUE, ViewMatrix);

    GLint RotationUniform = glGetUniformLocation(ShaderProgram, "ModelMatrix");
    if (RotationUniform == -1) 
    {
        fprintf(stderr, "Could not bind uniform ModelMatrix\n");
        exit(-1);
    }
    glUniformMatrix4fv(RotationUniform, 1, GL_TRUE, ModelMatrix);  

    glDrawArrays(GL_TRIANGLES, 0, numVertices);

    /* Disable attributes */
    glDisableVertexAttribArray(vPosition);
    glDisableVertexAttribArray(vUV);   

    /* Swap between front and back buffer */ 
    glutSwapBuffers();
}

void SetupDataBuffers()
{
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}

void getNewVertices() {
    int i;
    for(i=0; i < index_size; i++){
        Vertices[i].Position[0] = vertex_buffer_data[3*index_buffer_data[i]+0];
        Vertices[i].Position[1] = vertex_buffer_data[3*index_buffer_data[i]+1];
        Vertices[i].Position[2] = vertex_buffer_data[3*index_buffer_data[i]+2];
        Vertices[i].UV[0] = UV[2*i];
        Vertices[i].UV[1] = UV[2*i+1];
    }
}
glEnableVertexAttribArray(vPosition);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);

glEnableVertexAttribArray(vUV);
glBindBuffer(GL_ARRAY_BUFFER, TextureID);
glVertexAttribPointer(vUV, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer (GL_ARRAY_BUFFER, VBO);
glEnableVertexAttribArray (vPosition);
glEnableVertexAttribArray (vUV);
glVertexAttribPointer (vPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), 0);
glVertexAttribPointer (vUV,       2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const GLubyte*)0 + offsetof(struct VertexData,UV));