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

  • SEARCH
  • Home
  • 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 8447303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:09:05+00:00 2026-06-10T10:09:05+00:00

I cannot get my cube to show after trying to do a manual perspective

  • 0

I cannot get my cube to show after trying to do a manual perspective transformation My code is below. I have a suspicion it maybe my near and far plane numbers.

-(void)drawRect:(NSRect)dirtyRect
{
    // get the dimensions of the window
    NSSize dim = [self frame].size;

    // clear the background with color
    glClearColor(0.0, 0.0, 0.0, 0.4);
    glViewport(0, 0, dim.width, dim.height);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // variables
    GLfloat cubeHeight = 90.0;
    GLfloat cubeWidth = 90.0;
    GLfloat cubeLength = 200.0;
    //GLfloat alpha = 0.0;
    //GLfloat beta = 0.0;
    //GLfloat gamma = 0.0;

    // cube position data
    GLfloat cubePositionData[] = {0.0, 0.0, 0.0,
        0.0, 0.0, cubeLength,
        cubeWidth, 0.0, cubeLength,
        cubeWidth, 0.0, 0.0,
        0.0, cubeHeight, 0.0,
        0.0, cubeHeight, cubeLength,
        cubeWidth, cubeHeight, cubeLength,
        cubeWidth, cubeHeight, 0.0};

    // cube indices data
    GLubyte cubeIndices[] = {0, 1, 2, 0, 2, 3,
        4, 5, 6, 4, 6, 7,
        1, 2, 6, 1, 6, 5,
        2, 3, 7, 2, 7, 6,
        3, 0, 4, 3, 4, 7,
        0, 1, 5, 0, 5, 4};

    // cube color data
    GLfloat cubeColorData[] = {0.0, 0.3, 0.8, 1.0,
        0.0, 1.0, 0.0, 1.0,
        0.0, 1.0, 0.0, 1.0,
        0.0, 0.3, 0.8, 1.0,
        0.0, 0.3, 0.8, 1.0,
        0.0, 1.0, 0.0, 1.0,
        0.0, 1.0, 0.0, 1.0,
        0.0, 0.3, 0.8, 1.0};

    // array to hold buffer IDs
    GLuint vertexBuffers[2];

    // bind each array of data to separate buffers
    // bind cube position data to the first buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cubePositionData), cubePositionData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    // bind the cube color data to the second buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cubeColorData), cubeColorData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // enable the shader program
    GLuint programID = [self loadShaders];
    glUseProgram(programID);

    // enable vertex attributes
    // enable cube position attributes
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[0]);
    glEnableVertexAttribArray(VERTEX_POS_INDEX);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    // enable cube color attributes
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[1]);
    glEnableVertexAttribArray(VERTEX_COLOR_INDEX);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // point to the enabled attribute data
    glVertexAttribPointer(VERTEX_POS_INDEX, VERTEX_POS_SIZE, GL_FLOAT, GL_FALSE, 0, cubePositionData);
    glVertexAttribPointer(VERTEX_COLOR_INDEX, VERTEX_COLOR_SIZE, GL_FLOAT, GL_FALSE, 0, cubeColorData);

    GLfloat offset[] = {0.5, 0.5};
    GLint offsetUnif = glGetUniformLocation(programID, "offset");
    GLint zNearUnif = glGetUniformLocation(programID, "zNear");
    GLint zFarUnif = glGetUniformLocation(programID, "zFar");
    GLint frustumScaleUnif = glGetUniformLocation(programID, "frustumScale");

    glUniform2fv(offsetUnif, 1, offset);
    glUniform1f(frustumScaleUnif, 1.0f);
    glUniform1f(zNearUnif, 1.0);
    glUniform1f(zFarUnif, 3.0);

    // draw the elements
    glDrawElements(GL_TRIANGLES, sizeof(cubeIndices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, cubeIndices);

    glDisableVertexAttribArray(VERTEX_POS_INDEX);
    glDisableVertexAttribArray(VERTEX_COLOR_INDEX);
    glUseProgram(0);

    // flush buffer
    glFlush();
    [[self openGLContext] flushBuffer];

}

What might the problem be? Oh, and the shaders too:

#version 120

attribute vec3 position;
attribute vec4 inColor;

uniform vec2 offset;
uniform float zNear;
uniform float zFar;
uniform float frustumScale;

varying vec4 outColor;

void main()
{
    vec4 cameraPos = vec4(position, 1.0) + vec4(offset.x, offset.y, 0.0, 0.0);
    vec4 clipPos;

    clipPos.xy = cameraPos.xy * frustumScale;

    clipPos.z = cameraPos.z * (zNear + zFar) / (zNear - zFar);
    clipPos.z += 2 * zNear * zFar / (zNear - zFar);

    clipPos.w = -cameraPos.z;

    gl_Position = clipPos;
    outColor = inColor;
}

#version 120

varying vec4 outColor;

void main()
{
    gl_FragColor = outColor;
}

OK, changed a section of my code to look like this:

GLfloat offset[] = {-2.0, -2.0};
GLint offsetUnif = glGetUniformLocation(programID, "offset");
GLint zNearUnif = glGetUniformLocation(programID, "zNear");
GLint zFarUnif = glGetUniformLocation(programID, "zFar");
GLint frustumScaleUnif = glGetUniformLocation(programID, "frustumScale");

glUniform2fv(offsetUnif, 1, offset);
glUniform1f(frustumScaleUnif, 1.0);
glUniform1f(zNearUnif, 1.0);
glUniform1f(zFarUnif, 25.0);

I get this:

Program result

Shouldn’t the back end (green) look smaller in the distance and not bigger, like a trapezoid base?

  • 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-10T10:09:06+00:00Added an answer on June 10, 2026 at 10:09 am

    OK, I think I fixed it. Changed my vertex shader to look like this:

    #version 120
    
    attribute vec3 position;
    attribute vec4 inColor;
    
    uniform vec2 offset;
    uniform float zNear;
    uniform float zFar;
    uniform float frustumScale;
    
    varying vec4 outColor;
    
    void main()
    {
        vec4 cameraPos = vec4(position.x, position.y, -position.z, 1.0) + vec4(offset.x, offset.y, -10.0, 0.0);
        vec4 clipPos;
    
        clipPos.xy = cameraPos.xy * frustumScale;
    
        clipPos.z = cameraPos.z * (zNear + zFar) / (zNear - zFar);
        clipPos.z += 2 * zNear * zFar / (zNear - zFar);
    
        clipPos.w = -cameraPos.z;
    
        gl_Position = clipPos;
        outColor = inColor;
    }
    

    And I get this:

    program result

    Maybe maybe?

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

Sidebar

Related Questions

I cannot get my code to work! I am trying to implement the quick
I cannot get the code below to insert data. <?php $xml_gsm = null; try
I cannot get Bootstrap Carousel to start, um, carousel-ing. Below is all the code
I cannot get my Spring web app to find my scripts. I have the
I cannot get my SID working ... <?php session_start(); // Or maybe pass along
I cannot get Javascript to run after a ASP.NET postback from a client script
I cannot get GLEW to link correctly with my program. I have the path
I cannot get this to work. I have opened a SQL Server Express table
I cannot get this working correctly, actually at all. What I have so far
I cannot get this to work. I have a MemoryStream object. This class has

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.