简体   繁体   English

OpenGL ES 2.0立方体贴图未显示纹理

[英]OpenGL ES 2.0 cubemap is not showing a texture

I wanted to implement a skybox, but somehow there is nothing rendering. 我想实现一个天空盒,但是不知何故没有渲染。 In the Initialization I simply build load the 3 Positioncoords of a cube from (-1,-1,-1) to (1, 1, 1). 在初始化中,我只是简单地构建加载一个立方体的3个Positioncoords从(-1,-1,-1)到(1,1,1)。 Textures are also loading, probably and adding them to the cube map. 纹理也正在加载,可能会将其添加到立方体贴图。 By the rendering i only passing the ModelViewProj but the are no Translation, scaling or anything applied to it, only the Projection. 通过渲染,我只通过了ModelViewProj,但没有平移,缩放或对其应用任何操作,仅是投影。

Does someone see why it doesn't work? 有人知道为什么它不起作用吗? Did I forget something or understand something wrong? 我忘记了什么或理解错了吗?

Init VBO: 初始化VBO:

_VertexData = skyboxVertices;
_ByteSize = sizeof(GLfloat) * 3;
_NumTriangles = 36;

glEnable(GL_DEPTH_TEST);

glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);

glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), skyboxVertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0));
glBindVertexArrayOES(0);

[self loadTextureFromImage:path Type:typ];

Loading Textures: 加载纹理:

glGenTextures(1, &(_TextureID));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, _TextureID);

for (int face = 0; face < 6; ++face) {
    [self getImage:[NSString stringWithFormat:@"%i",face]];
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
}

glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)width, (int)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); // glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,(int)width,(int)height,0,GL_RGBA,GL_UNSIGNED_BYTE,imageData);

glBindTexture(GL_TEXTURE_CUBE_MAP, 0);


free(imageData);

drawing Skymap: 绘制Skymap:

glDepthMask(GL_FALSE);
glBindVertexArrayOES(_vertexArray);

glUseProgram(prog);

glEnable(GL_TEXTURE_CUBE_MAP);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, _TextureID);


GLuint projMatrix = glGetUniformLocation(prog, "modelViewProjectionMatrix");
GLuint normalMatrix = glGetUniformLocation(prog, "normalMatrix");
GLuint samplerCube =glGetUniformLocation(prog, "cubeMap");

glUniformMatrix4fv(projMatrix, 1, 0, _modelViewProj.m);
glUniform1i(samplerCube, 0);

glDrawArrays(GL_TRIANGLES, 0, _NumTriangles);
glDepthMask(GL_TRUE);

Vertex Shader: 顶点着色器:

attribute vec4 position;
attribute vec3 texCoord;
attribute vec3 normal;

varying lowp vec3 vTexCoord;

uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

void main()
{
    vTexCoord = texCoord;
    gl_Position = modelViewProjectionMatrix * position;
}

Fragment Shader: 片段着色器:

uniform samplerCube cubeMap;

varying lowp vec3 vTexCoord;

void main()
{
    lowp vec4 texCol = textureCube(cubeMap, vTexCoord);

    lowp vec4 color = vec4(1.0,0.0,0.0,1.0);

    gl_FragColor = vec4(texCol.rgba);
}

This is the hole Information 这是孔信息

GLfloat skyboxVertices[108] = {
    // Positions
    -1.0f,  1.0f, -1.0f,
    -1.0f, -1.0f, -1.0f,
    1.0f, -1.0f, -1.0f,
    1.0f, -1.0f, -1.0f,
    1.0f,  1.0f, -1.0f,
    -1.0f,  1.0f, -1.0f,

    -1.0f, -1.0f,  1.0f,
    -1.0f, -1.0f, -1.0f,
    -1.0f,  1.0f, -1.0f,
    -1.0f,  1.0f, -1.0f,
    -1.0f,  1.0f,  1.0f,
    -1.0f, -1.0f,  1.0f,

    1.0f, -1.0f, -1.0f,
    1.0f, -1.0f,  1.0f,
    1.0f,  1.0f,  1.0f,
    1.0f,  1.0f,  1.0f,
    1.0f,  1.0f, -1.0f,
    1.0f, -1.0f, -1.0f,

    -1.0f, -1.0f,  1.0f,
    -1.0f,  1.0f,  1.0f,
    1.0f,  1.0f,  1.0f,
    1.0f,  1.0f,  1.0f,
    1.0f, -1.0f,  1.0f,
    -1.0f, -1.0f,  1.0f,

    -1.0f,  1.0f, -1.0f,
    1.0f,  1.0f, -1.0f,
    1.0f,  1.0f,  1.0f,
    1.0f,  1.0f,  1.0f,
    -1.0f,  1.0f,  1.0f,
    -1.0f,  1.0f, -1.0f,

    -1.0f, -1.0f, -1.0f,
    -1.0f, -1.0f,  1.0f,
    1.0f, -1.0f, -1.0f,
    1.0f, -1.0f, -1.0f,
    -1.0f, -1.0f,  1.0f,
    1.0f, -1.0f,  1.0f
};

Your cubemap generating code has an error: 您的多维数据集生成代码有错误:

for (int face = 1; face <= 6; ++face) {

You should run this from 0 to 5, or else you wont upload anything for GL_TEXTURE_CUBE_MAP_POSITIVE_X, and you'll upload something for GL_TEXTURE_CUBE_MAP_POSITIVE_X+6, which is invalid. 您应该将其运行在0到5之间,否则您将不会上传GL_TEXTURE_CUBE_MAP_POSITIVE_X的任何内容,而会上传GL_TEXTURE_CUBE_MAP_POSITIVE_X + 6的内容,这是无效的。

You should show your shaders, as there may be an error there too. 您应该显示着色器,因为那里也可能有错误。

EDIT 编辑

Buffer creation seems to be incorrect. 缓冲区创建似乎不正确。 As you have a position, texCoord and a normal attributes, you should have 3 vertex attrib pointers, one for each: 当您拥有一个位置,texCoord和一个普通属性时,您应该具有3个顶点attrib指针,每个对应一个:

glEnableVertexAttribArray(0); //position
glEnableVertexAttribArray(1); //texcoord
glEnableVertexAttribArray(2); //normal

glVertexAttribPointer(0, 4, GL_FLOAT, false, vertexByteSize, 0); //position
glVertexAttribPointer(1, 3, GL_FLOAT, false, vertexByteSize, (void*)(4 * 4)); //texCoord
glVertexAttribPointer(2, 3, GL_FLOAT, false, vertexByteSize, (void*)(7 * 4)); //normal

NOTE i am using 0 1 2 for attribute locations, in your implementation you should fetch them with glGetAttribLocation(); 注意我将0 1 2用于属性位置,在实现中应使用glGetAttribLocation()获取它们;

Also, your variable _NumTriangles should be named _NumVertices, as it holds the number of vertices (a cube has 12 triangles --> 12*3 = 36 vertices are uploaded). 另外,您的变量_NumTriangles应该命名为_NumVertices,因为它拥有顶点数量(一个立方体有12个三角形-> 12 * 3 =上传了36个顶点)。

Also there may be some error with the buffer containing the data could you copy that too? 此外,包含数据的缓冲区可能存在一些错误,您也可以复制吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM