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 8080937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:34:01+00:00 2026-06-05T16:34:01+00:00

For some reason I can’t detect I can’t set a z index for any

  • 0

For some reason I can’t detect I can’t set a z index for any triangle. The triangle will display, at the same depth, from 1 to -1 (inclusive), but after that it dissapers. I am not applying any MVP matrix (trying to figure out this first) and I would expect to be able to see my triangle get smaller as I decrease the z value. This is what I have so far:

//My main function
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_ALPHA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("Window");

if (init_resources()) {
    glutDisplayFunc(onDisplay);
    glutIdleFunc(idle);
    glEnable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}

//And in my display function
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

And with my vertext shader:

gl_Position = vec4(coord2d, 0.0, 1.0);

glGetIntegerv(GL_DEPTH_BITS, &depth) also returns 24.

edit: full code

Sprite *sprite1;
Sprite *sprite2;

GLuint program;
GLuint cameraAttribute;

int init_resources()
{   

    //Link shaders, create program and attach shaders
    GLint link_ok = GL_FALSE;
    GLuint vs, fs;
    if ((vs = create_shader("vertShader.sh", GL_VERTEX_SHADER))   == 0) return 0;
    if ((fs = create_shader("fragShader.sh", GL_FRAGMENT_SHADER)) == 0) return 0;

    program = glCreateProgram();
    glAttachShader(program, vs);
    glAttachShader(program, fs);


    glLinkProgram(program);
    glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
    if (!link_ok) {
        fprintf(stderr, "glLinkProgram:");
        print_log(program);
        return 0;
    }

    //Create Attributes
    cameraAttribute = glGetUniformLocation(program, "camera");
    if(cameraAttribute == -1){
        cout << "Error!";
    }


    //Init sprites
    sprite1 = new Sprite();
    sprite2 = new Sprite();


    return 1;
}

void onDisplay()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
    glUseProgram(program);


    sprite1->render();
    sprite2->render();

    glutSwapBuffers();
}

void idle()
{

    //Create Projection Matrix to change clip space.
    mat4 projectionMatrix = perspective(60.0f, (float)glutGet(GLUT_WINDOW_WIDTH) / (float)glutGet(GLUT_WINDOW_HEIGHT), 0.1f, 100.f);
    glUniformMatrix4fv(cameraAttribute, 1, GL_FALSE, &projectionMatrix[0][0]);

}

void free_resources()
{
    glDeleteProgram(program);
}


int main(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA|GLUT_ALPHA|GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    glutCreateWindow("My Second Triangle");
    glEnable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);

    if (init_resources()) {
        glutDisplayFunc(onDisplay);
        glutIdleFunc(idle);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        //uses glGetError to check for how many errors there are.
        cout << CheckGLErrors() << "\n";

        glutMainLoop();
    }


    free_resources();
    return 0;
}

And my sprite file:

#include "Sprite.h"

Sprite::Sprite(){
    init();
}


void Sprite::init(){

    GLfloat triangle_vertices[] = {
        0.0,  0.8f,
        -0.8f, -0.8f,
        0.8f, -0.8f,
    };

    //Set attributes
    locationAttribute = glGetAttribLocation(program, "coord2d");
    if (locationAttribute == -1) {
        fprintf(stderr, "Could not bind attribute location");
    }

    translationUniform = glGetUniformLocation(program, "translationUniform");
    if(translationUniform == -1){
        fprintf(stderr, "Could not bind attribute transformation");
    }


    //Create buffer
    glGenBuffers(1, &mainBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, mainBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);

    glVertexAttribPointer(
                          locationAttribute, // attribute
                          2,                 // number of elements per vertex, here (x,y)
                          GL_FLOAT,          // the type of each element
                          GL_FALSE,          // take our values as-is
                          0,                 // no extra data between each position
                          0                  // offset of first element
    );

    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void Sprite::render(){
    glBindBuffer(GL_ARRAY_BUFFER, mainBuffer);
    glDrawArrays(GL_TRIANGLES, 0, 3);

}

And Vertex shader: (note, versions are added by the parser)

attribute vec2 coord2d;

uniform mat4 translationUniform;
uniform mat4 camera;


void main(void) {

    gl_Position = camera * vec4(coord2d, -1.1, 1.0);
}
  • 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-05T16:34:03+00:00Added an answer on June 5, 2026 at 4:34 pm

    glEnable(GL_BLEND | GL_DEPTH_TEST);

    You can’t combine these like this, you have to issue two glEnable commands. This is probably generating an error, or enabling something completely different.

    You’re confusing this with glClear, which uses bitwise masks.

    Start using glGetError in your code, it would have caught this problem for you.

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

Sidebar

Related Questions

For some reason I can't get gMap to display multiple maps on the same
For some reason I can't edit XHTML source. Can we add any attributes to
For some reason I can't find any documentation in disabling the resize functionality.
For some reason i can't get my image to display in canvas.. i get
For some reason I can't update the ApplicationBar icons when I make any changes
Very odd situation here. For some reason I can't call 'Where', or any other
For some reason I can't hide WPF Toolkit's DataGridColumn. I am trying to do
For some reason I can't get this script to run when I add the
For some reason i can't get the variable 'total' to define at all... I
I know PHP 5 already supports SQLite but for some reason I can't get

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.