java - Opengl Rendering - Anything outside of the z range -1 to 1 doesn't appear? -
anything render outside of range 1 -1 (in z-range) doesn't appear on screen. i've been trying everything, including using different matrices try transform vertices outside of 1 -1 range nothing seems work.
i'll put code in below. consists of model class data stored, shader program(which won't include - pretty simple) , 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 vertex array vaoid = glgenvertexarrays(); // select vertex array bind(); // attach vertex data attachvertexdata(vertexdata, vertexcount); // attach color data attachcolordata(colordata, colorcount); // deselect vertex array unbind(); // indice attachment attachindexarray(indexdata); // set vertex count numvertices = vertexcount; numindices = indexcount; } /** * attach vertex data */ public void attachvertexdata(floatbuffer vertexdata, int vertexcount) { // create buffer vbovertexid = glgenbuffers(); // bind new buffer glbindbuffer(gl_array_buffer, vbovertexid); // give data gpu glbufferdata(gl_array_buffer, vertexdata, gl_static_draw); // set location of data within vertex array glvertexattribpointer(0, vertexcount, gl_float, false, 0, 0); // deselect buffer glbindbuffer(gl_array_buffer, 0); } /** * attach color data */ public void attachcolordata(floatbuffer colordata, int colorcount) { // create buffer vbocolorid = glgenbuffers(); // bind new buffer glbindbuffer(gl_array_buffer, vbocolorid); // give data gpu glbufferdata(gl_array_buffer, colordata, gl_static_draw); // set location of data within vertex array glvertexattribpointer(1, colorcount, gl_float, false, 0, 0); // deselect buffer glbindbuffer(gl_array_buffer, 0); } /** * attach index data */ public void attachindexarray(bytebuffer indexdata) { // create buffer vboindexid = glgenbuffers(); // bind glbindbuffer(gl_element_array_buffer, vboindexid); // put data in glbufferdata(gl_element_array_buffer, indexdata, gl_static_draw); // unbind buffer glbindbuffer(gl_element_array_buffer, vboindexid); } /** * enable buffers */ public void enableattribarrays() { glenablevertexattribarray(0); glenablevertexattribarray(1); } /** * disable buffers */ public void disableattribarrays() { gldisablevertexattribarray(0); gldisablevertexattribarray(1); } /** * bind model */ public void bind() { glbindvertexarray(vaoid); } /** * unbind model */ public void unbind() { glbindvertexarray(0); } /** * bind indices */ public void bindindexbuffer() { glbindbuffer(gl_element_array_buffer, vboindexid); } /** * unbind indices buffer */ public void unbindindexbuffer() { glbindbuffer(gl_element_array_buffer, 0); } /** * draw 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 setup matrices 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 sets matrices
private void setupmatrices() { // model - identity matrix model_matrix = new mat4(1.0f); shader.setuniformmat4("model_matrix", model_matrix); // view - translate 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); }
have close vertex shader matrix multiplications applied. notably 2 lines:
gl_position = projection_matrix * view_matrix * model_matrix * in_position; gl_position = in_position;
so see it? okay, @ second line? it? overwrites gl_position variable untransformed in_position. rid of , should see matrices 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 settle 1 matrix , stay that. right you're overwriting projecting_matrix identity matrix.
in calculation of perspective matrix division of display width , height integer division round-down. have cast results of getwidth , getheight float first.
projection_matrix = matrices.perspective( 60, (float)display.getwidth() / (float)display.getheight(), 0.1f, 100.f );
however doubt want display size there. more want viewport size use aspect ration calculation. 0.1 near , 100 far not optimal values. should choose near large possible.
Comments
Post a Comment