odrega
odrega

Reputation: 15

Opengl Rendering - Anything outside of the z range -1 to 1 doesn't appear?

Anything I render outside of the range 1 to -1 (in the z-range) just doesn't appear on the screen. I've been trying everything, including using different matrices to try to transform vertices outside of 1 to -1 into this range but nothing seems to work.

I'll put my code in below. It consists of a model class where data is stored, a shader program(which I won't include - its pretty simple) and a main class.

Vertex Shader

#version 330 core

in vec4 in_Position;
in vec4 in_Color;

out vec4 pass_Color;

void main(void) {

gl_Position = in_Position;
pass_Color = in_Color;

}

Fragment Shader

#version 330 core

in vec4 pass_Color;

out vec4 out_Color;

void main(void) {
out_Color = pass_Color;
}

Model Class

package util;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL20.*;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;


public class Model {

// Vertex Array ID
int vaoID;
// VBO ID's
int vboVertexID, vboColorID, vboIndexID;
// Vertex Count
int numVertices, numIndices;

public Model(FloatBuffer vertexData, int vertexCount, FloatBuffer colorData, int colorCount, ByteBuffer indexData, int indexCount) {
    // Create the vertex array
    vaoID = glGenVertexArrays();
    // Select the vertex array
    bind();
    // Attach vertex data
    attachVertexData(vertexData, vertexCount);
    // Attach Color data
    attachColorData(colorData, colorCount);
    // Deselect the vertex array
    unbind();
    // Indice attachment
    attachIndexArray(indexData);

    // Set the vertex count
    numVertices = vertexCount;
    numIndices = indexCount;
}

/**
 * Attach some vertex data
 */
public void attachVertexData(FloatBuffer vertexData, int vertexCount) {
    // Create the buffer
    vboVertexID = glGenBuffers();
    // Bind the new buffer
    glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
    // Give the data to the GPU
    glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
    // Set the location of the data within the vertex array
    glVertexAttribPointer(0, vertexCount, GL_FLOAT, false, 0, 0);
    // Deselect this buffer
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

/**
 * Attach some color data
 */
public void attachColorData(FloatBuffer colorData, int colorCount) {
    // Create the buffer
    vboColorID = glGenBuffers();
    // Bind the new buffer
    glBindBuffer(GL_ARRAY_BUFFER, vboColorID);
    // Give the data to the GPU
    glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
    // Set the location of the data within the vertex array
    glVertexAttribPointer(1, colorCount, GL_FLOAT, false, 0, 0);
    // Deselect this buffer
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

/**
 * Attach the index data
 */
public void attachIndexArray(ByteBuffer indexData) {
    // Create the buffer
    vboIndexID = glGenBuffers();
    // Bind it
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndexID);
    // Put the data in
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData, GL_STATIC_DRAW);
    // Unbind the buffer
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndexID);
}

/**
 * Enable the buffers
 */
public void enableAttribArrays() {
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
}

/**
 * Disable buffers
 */
public void disableAttribArrays() {
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
}

/**
 * Bind the Model
 */
public void bind() {
    glBindVertexArray(vaoID);
}

/**
 * Unbind the Model
 */
public void unbind() {
    glBindVertexArray(0);
}

/**
 * Bind the indices
 */
public void bindIndexBuffer() {
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndexID);
}

/**
 * Unbind the indices buffer
 */
public void unbindIndexBuffer() {
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

/**
 * Draw the vertex array
 */
public void drawArray() {
    glDrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_BYTE, 0);
}
}

Main Class

package d3;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.PixelFormat;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL20.*;
import util.Game;
import util.Model;
import util.ShaderProgram;

public class Pyramidion {

ShaderProgram shader;
Model model;

public Pyramidion() {

    try {
        Display.setDisplayMode(new DisplayMode(800, 600));
        Display.create(new PixelFormat(), new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true));

        Display.setVSyncEnabled(true);
    } catch (LWJGLException e) {
        e.printStackTrace();
    }

    init();

    while(!Display.isCloseRequested()) {

        render();

        Display.update();
    }

    Display.destroy();

}

public void init() {

    Display.setTitle("Pyramid");
    glViewport(0, 0, Display.getWidth(), Display.getHeight());

    // Shader Initialization
    setupShader();

    // Model Init
    setupModel();

}

private void setupModel() {

    int verticesCount = 4;
    float[] verticesData = {
        -0.5f, +0.5f, -10f, 1f, // Top Left
        -0.5f, -0.5f, 0f, 1f, // Bottom Left
        +0.5f, -0.5f, 0f, 1f, // Bottom Right
        +0.5f, +0.5f, 0f, 1f // Top Right
    };
    FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(verticesData.length);
    verticesBuffer.put(verticesData); verticesBuffer.flip();

    int colorCount = 4;
    float[] colorData = {
        0f, 1f, 0f, 1f, // Green
        1f, 0f, 0f, 1f, // Red
        0f, 0f, 1f, 1f, // Blue
        1f, 1f, 1f, 1f // White
    };
    FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(colorData.length);
    colorBuffer.put(colorData); colorBuffer.flip();

    int indicesCount = 6;
    byte[] indicesData = {
            0, 1, 2,
            2, 3, 0
    };
    ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesData.length);
    indicesBuffer.put(indicesData); indicesBuffer.flip();

    // Create Model
    model = new Model(verticesBuffer, verticesCount, colorBuffer, colorCount, indicesBuffer, indicesCount);

}

private void setupShader() {

    shader = new ShaderProgram();
    shader.attachVertexShader("src/d3/vertex.vert");
    shader.attachFragmentShader("src/d3/fragment.frag");
    shader.link();
    shader.bindAtrribLocation(0, "in_Position");
    shader.bindAtrribLocation(1, "in_Color");

}

public void render() {


    glClear(GL_COLOR_BUFFER_BIT);

    shader.bind();

    model.bind();
    model.enableAttribArrays();
    model.bindIndexBuffer();

    model.drawArray();

    model.unbindIndexBuffer();
    model.disableAttribArrays();
    model.unbind();

    ShaderProgram.unbind();

}

public static void main(String[] args) {
    new Pyramidion();
}

}

EDIT: Added the setup for matrices I had

Vertex Shader

#version 330 core

uniform mat4 model_Matrix;
uniform mat4 view_Matrix;
uniform mat4 projection_Matrix;

in vec4 in_Position;
in vec4 in_Color;

out vec4 pass_Color;

void main(void) {

gl_Position = projection_Matrix * view_Matrix * model_Matrix * in_Position;
//gl_Position = in_Position;
pass_Color = in_Color;

}

Code that sets up the Matrices

private void setupMatrices() {

    // Model - Identity Matrix
    model_Matrix = new Mat4(1.0f);
    shader.setUniformMat4("model_Matrix", model_Matrix);

    // View - translate it forward
    view_Matrix = new Mat4(1f);
    shader.setUniformMat4("view_Matrix", view_Matrix);

    // Projection - simple perspective
    projection_Matrix = Matrices.perspective(60, Display.getWidth() / Display.getHeight(), 0.1f, 100f);
    projection_Matrix = Matrices.ortho(-1, 1, 1, -1, 0.1f, 100f);
    projection_Matrix = new Mat4(1);
    shader.setUniformMat4("projection_Matrix", projection_Matrix);

}

Upvotes: 0

Views: 310

Answers (2)

odrega
odrega

Reputation: 15

It turns out I am an idiot. I was forgetting that when you upload a uniform to a shader you have to bind it first. That was the problem.

Upvotes: 0

datenwolf
datenwolf

Reputation: 162164

Have a close look to your vertex shader with matrix multiplications applied. Notably those two lines:

gl_Position = projection_Matrix * view_Matrix * model_Matrix * in_Position;
gl_Position = in_Position;

So you see it? Okay, look at the second line? What does it? It overwrites the gl_Position variable with the untransformed in_Position. Get rid of that and you should see matrices do their work.

Update

Next problem here

// Projection - simple perspective
projection_Matrix = Matrices.perspective(60, Display.getWidth() / Display.getHeight(), 0.1f, 100f);
projection_Matrix = Matrices.ortho(-1, 1, 1, -1, 0.1f, 100f);
projection_Matrix = new Mat4(1);

You should really settle for one matrix and stay with that. Right now you're just overwriting projecting_Matrix with a identity matrix.

In the calculation of the perspective matrix the division of display width and height will likely be an integer division with round-down. You have to cast the results of getWidth and getHeight to float first.

projection_Matrix =
    Matrices.perspective(
        60,
        (float)Display.getWidth() / (float)Display.getHeight(),
        0.1f, 100.f );

However I doubt you really want the display size there. More likely you want the viewport size use for the aspect ration calculation. Also 0.1 for near and 100 for far are not optimal values. You should choose near as large as possible.

Upvotes: 1

Related Questions