Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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 ES 2.0纹理显示为黑色_C_Graphics_Opengl Es_Opengl Es 2.0 - Fatal编程技术网

C OpenGL ES 2.0纹理显示为黑色

C OpenGL ES 2.0纹理显示为黑色,c,graphics,opengl-es,opengl-es-2.0,C,Graphics,Opengl Es,Opengl Es 2.0,我正在使用OpenGL ES 2.0为嵌入式设备开发一个应用程序 这是我的片段着色器: varying vec2 v_texCoord; uniform sampler2D s_texture; void main() { gl_FragColor = texture2D(s_texture, v_texCoord); } 我正确设置了纹理。出于某种原因,调用glTexImage2D不会产生我想要的结果。纹理是完全黑色的,而不是用我提供的数据填充 这是我创建纹理的方式: GLuint

我正在使用OpenGL ES 2.0为嵌入式设备开发一个应用程序

这是我的片段着色器:

varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main() {
  gl_FragColor = texture2D(s_texture, v_texCoord);
}
我正确设置了纹理。出于某种原因,调用
glTexImage2D
不会产生我想要的结果。纹理是完全黑色的,而不是用我提供的数据填充

这是我创建纹理的方式:

   GLuint textureId;

   // 2x2 Image, 3 bytes per pixel (R, G, B)
   GLubyte pixels[6 * 3] =
   {  
      255,   0,   0, // Red
        0, 255,   0, // Green
        0,   0, 255, // Blue
      255, 255,   0,  // Yellow
        0, 255, 255,
        255, 0, 255
   };

   // Use tightly packed data
   glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );

   // Generate a texture object
   glGenTextures ( 1, &textureId );

   // Bind the texture object
   glBindTexture ( GL_TEXTURE_2D, textureId );

   // Load the texture
   glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, 3, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels );


   // Set the filtering mode
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
之后,我像这样绑定纹理:

samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );
glActiveTexture ( GL_TEXTURE0 );
glBindTexture ( GL_TEXTURE_2D, textureId );

// Set the sampler texture unit to 0
glUniform1i ( samplerLoc, 0 );

我确认在调试时,顶点和纹理坐标已绑定并传递给着色器。因此,它必须是
s_纹理
glTexImage2D
函数本身的问题

也许我遗漏了一些明显的东西,但如果您仅存储2 x 2像素,每种颜色3字节(=4 x 3字节=12字节)作为纹理;那么6英寸的“GLubyte像素[6*3]”从何而来

6*3等于18和!=2的幂

//加载纹理glTexImage2D(GL_texture_2D,0,GL_RGB,3,2,0, GL_RGB,GL_无符号字节,像素)

根据规范: glTexImage2D(GLenum目标、闪烁级别、闪烁内部格式、GLsizei宽度、GLsizei高度、闪烁边界、GLenum格式、GLenum类型、常量GLvoid*数据)

粗体变量最好是^2

那么,试试这个:

//加载纹理glTexImage2D(GL_texture_2D,0,GL_RGB,2,2,0, GL_RGB,GL_无符号字节,像素)

从像素数组中删除最后2行,然后将所有值更改为“255”;如果你得到一个白色的纹理,这意味着它的工作

另一个例子:

GLubyte像素[4*3]={

255,0,0,//红色//绿色//蓝色//像素0

255,0,0,//红色//绿色//蓝色//像素1

255,0,0,//红色//绿色//蓝色//像素2

255,0,0//红色//绿色//蓝色//像素3

})

一切都显示为红色

如果这样做不行,也许我的代码会有所帮助:

void CMesh::renderMesh(GLuint program, glm::mat4 *mvp){

glUseProgram(program);

int mvpLocation = glGetUniformLocation(program, "matViewProjection");
int texLocation = glGetUniformLocation(program, "baseMap");
glUniformMatrix4fv( mvpLocation, 1, GL_FALSE, glm::value_ptr(*mvp));

int vertexAttribLocation = glGetAttribLocation(program, "vertex");
int uvAttribLocation = glGetAttribLocation(program, "texturecoordinate");

// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureIndex);
glUniform1i(texLocation, 0);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(
   vertexAttribLocation,            
   3,                               // size
   GL_FLOAT,                        // type
   GL_FALSE,                        // normalized?
   0,                               // stride
   (void*)0                         // array buffer offset
);

glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
glVertexAttribPointer(
   uvAttribLocation,                  
   2,                  // size
   GL_FLOAT,           // type
   GL_FALSE,           // normalized?
   0,                  // stride
   (void*)0            // array buffer offset
);

// Draw the triangles !
glDrawArrays(GL_TRIANGLES, 0, vertices.size() );

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
};

对于U和V尺寸,您需要将钳制模式设置为
climp_to_EDGE
,否则纹理不完整,将采样为黑色。

是否使用
GetError
检查错误?您也可能以错误的顺序调用
TexImage
TexParameter
TexParameter
通常在
TexImage
之前),尽管我不确定这是否会影响任何事情。也就是说,
TexImage
不太可能出现问题,而且几乎肯定是您的错误。是的,我犯了。没有记录任何错误。我知道它不可能是顶点着色器,因为当我调试时,我看到顶点坐标和纹理坐标传递正确。这几乎是教科书上的一个直截了当的例子。我不知道我错过了什么(也可能是因为你的纹理不是正方形,或者它的尺寸不是二的幂。我假设他的2x2是一个打字错误。尼克,对不起……你是对的。我错过了那一个。但即使那样,3 x 2纹理?天哪!!!它工作了!谢谢!!但我似乎无法解释为什么我需要设置钳制,因为我所有的纹理坐标都在范围内。但无论如何还是要感谢!!两种纹理的非能量需要夹紧到边缘才能工作(以及其他),这就是为什么。