Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8928969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:34:15+00:00 2026-06-15T08:34:15+00:00

When I create my cube, and I try to apply a texture to it,

  • 0

When I create my cube, and I try to apply a texture to it, the texture is not applied but opengl returns a black cube, the texture is showing as a black color.
I’m using QT, here is the code:

(I’m using QT)

Create Texture Function:

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 Function:

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);
}

Buffer:
trianglebuffer[1] (UV Coord):

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
};

Buffer:
trianglebuffer[0] (Cube):

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

};

And the shaders (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;
}

It’s a little bit difficult for me to understand the UV Coordinates…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:34:16+00:00Added an answer on June 15, 2026 at 8:34 am

    Is your glTexImage2D call wrong?

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using numpy to create a cube array with sides of length 100, thus
hello i create a cube and want on one side an texture. glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,
I read the dimensions of a cube from a file (x,y,z) and create opengl
I want to create cube model of a 32x32 bitmap in opengl. For example,
I'm trying to create a cube using one series of vertices such as attempted
I'm trying to create a simple Skybox using Three.js but have run into an
I'm attempting to make a chromed cube in GLUT / OpenGL and I'm not
I've seen many CSS3 Cube using div's, wanted to create the same with unordered
I'm trying to create a cube using SQL Server 2008 R2. When I tried
To draw a cube which has 6 sides using libgdx need 6 meshes. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.