0

立方体を作成し、それにテクスチャを適用しようとすると、テクスチャは適用されませんが、opengl は黒い立方体を返し、テクスチャは黒色で表示されます。私はQTを使用しています。コードは次のとおりです。

(私はQTを使用しています)

テクスチャ関数の作成:

GLuint AOpenGlWidget::textureFromRsc(QString place){
// Read the bmp file
QImage img(place);
QImage tb;

QImage fixedImage( img.width(), img.height(), QImage::Format_ARGB32);
QPainter painter(&fixedImage);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.fillRect(fixedImage.rect(), Qt::transparent);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.drawImage( 0, 0, img);
painter.end();

tb = QGLWidget::convertToGLFormat(fixedImage);

GLuint textureId;
glGenTextures(1,&textureId);
glBindTexture(GL_TEXTURE_2D,textureId);

glTexImage2D(GL_TEXTURE_3D,0,4,tb.width(),tb.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,tb.bits());

if(!tb.bits()){
    QMessageBox::information(0,"Texture loading error.","There was an error while trying to load a texture.");
}

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

return textureId;
}



PaintGl 機能:

void AOpenGlWidget::paintGL(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears the screen and paint the bg with the color specified on glClearColor



// Another stuff (MVP Matrix... bla bla bla)

glUseProgram(programId);

GLuint MatrixId = glGetUniformLocation(programId,"MVP");
glUniformMatrix4fv(MatrixId,1,GL_FALSE,glm::value_ptr(MVP));
glActiveTexture(cursor_texture);
glBindTexture(GL_TEXTURE_2D,cursor_texture);

glUniform1i(glGetUniformLocation(programId,"textura"),cursor_texture);
// Draw or triangles

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);   

glBindBuffer(GL_ARRAY_BUFFER,trianglebuffer[0]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0);


glBindBuffer(GL_ARRAY_BUFFER,trianglebuffer[1]);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,(void*)0);


glDrawArrays(GL_TRIANGLES,0,12*3);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}

バッファ:
三角形バッファ[1] (UV 座標):

const GLfloat simple_triangle_buffer_txcoord[] = {
     0.0f,0.0f,
     0.0f,1.0f,
     1.0f,1.0f,
     1.0f,1.0f,
     1.0f,0.0f,
     0.0f,0.0f,
    0.0f,0.0f,
     0.0f,1.0f,
     1.0f,1.0f,
     1.0f,1.0f,
     1.0f,0.0f,
     0.0f,0.0f,
     0.0f,0.0f,
     0.0f,1.0f,
     1.0f,1.0f,
     1.0f,1.0f,
     1.0f,0.0f,
     0.0f,0.0f,
     0.0f,0.0f,
     0.0f,1.0f,
     1.0f,1.0f,
     1.0f,1.0f,
     1.0f,0.0f,
     0.0f,0.0f,
     0.0f,0.0f,
     0.0f,1.0f,
     1.0f,1.0f,
     1.0f,1.0f,
     1.0f,0.0f,
     0.0f,0.0f,
     0.0f,0.0f,
     0.0f,1.0f,
     1.0f,1.0f,
     1.0f,1.0f,
     1.0f,0.0f,
     0.0f,0.0f
};

バッファ:
三角形バッファ[0] (キューブ):

const GLfloat simple_triangle_buffer[] = {
    // Front
    -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,

    // Top
    -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,

    // Left
    -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,

    // Back
    -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,

    // Bottom
    -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,

    // Right
    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

};

そしてシェーダー (Frag & Vert):

// Fragment
#version 330 core
out vec3 color;
in vec2 thcoord;
in vec2 UV;
uniform sampler2D d_textura;
void main(){
color = texture(d_textura,thcoord).rgb;
}
// Vertex
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 tcoord;
out vec2 thcoord;
uniform mat4 MVP;
void main(){
vec4 v = vec4(vertexPosition_modelspace,1);
thcoord=tcoord;
gl_Position = MVP * v;
}

UV座標がちょっとわかりずらい…

4

1 に答える 1

3

あなたの glTexImage2D 呼び出しは間違っていますか?

glTexImage2D( GL_TEXTURE_3D ,0,4,tb.width(),tb.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,tb.bits()); ?

于 2012-12-04T15:29:49.910 に答える