Sign in to follow this  
LizardCPP

OpenGL VBO and interleaved data problem with JOGL

Recommended Posts

It's been a while since I have done any OpenGL coding and now I'm trying to get back into the game using JOGL, OpenGL bindings for Java. I got a simple program up and running that draws two triangles as a quad using the old fixed-function pipeline (i.e. glBegin(GL_TRIANGLES) / glEnd()). Now I'm trying to get it to work with VBO:s, indexed interleaved vertex data as i used to do it 5 years ago. Then after that I'm thinking of working my way up to use the 3.2 profile.

Update: I created a simpler test program that displays my problem in one file, see post 6 in this thread.

Update 2: This has now been solved, see post 7.

The sample program requests a OpenGL 2.0 profile and then tries to render two triangles as a quad with one color per vertex. The vertex data is interleaved with first 3 floats for position and then 4 floats for color. 4 vertex is set in a VBO and then 6 indices are set in a index buffer. Result is a black window, nothing is rendered,glGetError returns 0 if called after glDrawRangeElements().

Geometry class:

import java.nio.Buffer;

public class Geometry
{
/**
*
*/

private VertexFormat vertexFormat;

/**
*
*/

private Buffer vertexBuffer;

/**
*
*/

private Buffer indexBuffer;


/**
* @param vertexFormat
* @param vertexBuffer
* @param indexBuffer
*/

public Geometry(VertexFormat vertexFormat, Buffer vertexBuffer, Buffer indexBuffer)
{
this.vertexFormat = vertexFormat;
this.vertexBuffer = vertexBuffer;
this.indexBuffer = indexBuffer;
}


/**
* @return
*/

public int getStride()
{
return vertexFormat.getStride();
}


public Buffer getVertexData()
{
return vertexBuffer;
}

public Buffer getIndexData()
{
return indexBuffer;
}
}











JOGLBufferObject class:

import javax.media.opengl.GL2;
import javax.media.opengl.GLContext;
import java.nio.Buffer;

class JOGLBufferObject implements BufferObject
{
/**
* The OpenGL api object
*/

private GL2 gl;

/**
* This will either be set to GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER
*/

private int type;

/**
* Size of the buffer in bytes
*/

private int size;

/**
* The buffers id
*/

private int[] bufferId;


/**
*
* @param type the type of the buffer, will either be GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER
* @param size size of the buffer in bytes
*/

private JOGLBufferObject(int type, int size)
{
this.gl = GLContext.getCurrentGL().getGL2();

this.type = type;
this.bufferId = new int[1];
this.size = size;

gl.glGenBuffers(1, bufferId, 0);
gl.glBindBuffer(type, bufferId[0]);
gl.glBufferData(type, size, null, GL2.GL_DYNAMIC_DRAW);
}


/**
*
* @param size
* @return
*/

public static JOGLBufferObject createVertexBuffer(int size)
{
return new JOGLBufferObject(GL2.GL_ARRAY_BUFFER, size);
}


/**
*
* @param size
* @return
*/

public static JOGLBufferObject createIndexBuffer(int size)
{
return new JOGLBufferObject(GL2.GL_ELEMENT_ARRAY_BUFFER, size);
}


/**
* Retrieve the size of the buffer
*
* @return size of the buffer
*/

public int getSize()
{
return size;
}


/**
*
*/

public void destroy()
{
gl.glDeleteBuffers(1, bufferId, 0);
}


/**
*
*/

public void bind()
{
gl.glBindBuffer(type, bufferId[0]);
}


/**
*
*/

public void unbind()
{
gl.glBindBuffer(type, 0);
}


/**
*
* @param offset
* @param size
* @param buffer
*/

public void update(int offset, int size, Buffer buffer)
{
gl.glBufferSubData(type, offset, size, buffer);
}
}











Setup of geometry:

float[] vertexData = new float[]
{
0.75f, 0.25f, -2.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.75f, 0.75f, -2.0f, 0.0f, 1.0f, 0.0f, 1.0f,
0.25f, 0.25f, -2.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.25f, 0.75f, -2.0f, 0.5f, 0.5f, 0.5f, 1.0f
};


ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(7 * 4 * 4);
FloatBuffer vertexBuffer = vertexByteBuffer.asFloatBuffer();
vertexBuffer.put(vertexData);

int[] indexData = new int[]
{
0, 1, 2, 2, 1, 3
};

ByteBuffer indexByteBuffer = ByteBuffer.allocateDirect(6 * 4);
IntBuffer indexBuffer = indexByteBuffer.asIntBuffer();
indexBuffer.put(indexData);

VertexFormat vertexFormat = new VertexFormat(7 * 4);
geometry = new Geometry(vertexFormat, vertexByteBuffer, indexByteBuffer);










JOGLRenderer, it's the render(Geometry geometry) function that draws nothing:

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLContext;
import javax.media.opengl.glu.GLU;
import java.nio.Buffer;

public class JOGLRenderer implements Renderer
{
/**
*
*/

private GL2 gl;

/**
*
*/

private GLU glu;

/**
*
*/

private BufferObject vertexBuffer;

/**
*
*/

private BufferObject indexBuffer;

// TEMP
private boolean setupDone = false;


/**
*/

public JOGLRenderer()
{
this.gl = GLContext.getCurrentGL().getGL2();
this.glu = new GLU();

this.vertexBuffer = JOGLBufferObject.createVertexBuffer(1024);
this.indexBuffer = JOGLBufferObject.createIndexBuffer(1024);

gl.glShadeModel(GL2.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
}


@Override
public void reshape(int x, int y, int width, int height)
{
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();

gl.glViewport(0, 0, width, height);

gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);

gl.glMatrixMode(GL2.GL_MODELVIEW);
}

@Override
public void prepare()
{
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}

@Override
public void render(OLDGeometry oldGeometry)
{
gl.glBegin(GL.GL_TRIANGLES);

gl.glColor3f(oldGeometry.color.getX(), oldGeometry.color.getY(), oldGeometry.color.getZ());

gl.glVertex3f(oldGeometry.topLeft.getX(), oldGeometry.topLeft.getY(), oldGeometry.topLeft.getZ());
gl.glVertex3f(oldGeometry.topRight.getX(), oldGeometry.topRight.getY(), oldGeometry.topRight.getZ());
gl.glVertex3f(oldGeometry.bottomLeft.getX(), oldGeometry.bottomLeft.getY(), oldGeometry.bottomLeft.getZ());

gl.glVertex3f(oldGeometry.bottomLeft.getX(), oldGeometry.bottomLeft.getY(), oldGeometry.bottomLeft.getZ());
gl.glVertex3f(oldGeometry.topRight.getX(), oldGeometry.topRight.getY(), oldGeometry.topRight.getZ());
gl.glVertex3f(oldGeometry.bottomRight.getX(), oldGeometry.bottomRight.getY(), oldGeometry.bottomRight.getZ());

gl.glEnd();
}

@Override
public void render(Geometry geometry)
{
Buffer vertexBufferData = geometry.getVertexData();
Buffer indexBufferData = geometry.getIndexData();
int stride = geometry.getStride(); // the stride is here 28 i.e. 7*4


gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);


if (!setupDone)
{
vertexBuffer.bind();
indexBuffer.bind();

vertexBuffer.update(0, vertexBufferData.capacity(), vertexBufferData); // capacity returns 112 i.e. 7*4*4
indexBuffer.update(0, indexBufferData.capacity(), indexBufferData); // capacity returns 24 i.e. 6*4

setupDone = true;
}


gl.glVertexPointer(3, GL2.GL_FLOAT, stride, 0);
gl.glColorPointer(4, GL2.GL_FLOAT, stride, 3 * 4);


gl.glDrawRangeElements(GL2.GL_TRIANGLES, 0, 5, 2, GL2.GL_UNSIGNED_INT, 0);

gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
}

@Override
public void present()
{
// No need to swap buffers, jogl does that for me
}
}











Render loop:

renderer.prepare();
renderer.render(geometry);
renderer.present();












Can anyone see what I'm doing wrong?

[Edited by - LizardCPP on November 13, 2010 8:00:52 AM]

Share this post


Link to post
Share on other sites

gl.glDrawRangeElements(GL2.GL_TRIANGLES, 0, 5, 2, GL2.GL_UNSIGNED_INT, 0);



I think you've got the wrong value for 'count' here. It should be the number of indices, not the number of primitives. I believe this code only draws two vertices and quits, so you're not completing the first triangle.

Share this post


Link to post
Share on other sites
Quote:
Original post by karwosts
*** Source Snippet Removed ***

I think you've got the wrong value for 'count' here. It should be the number of indices, not the number of primitives. I believe this code only draws two vertices and quits, so you're not completing the first triangle.



Thanks for your replay, but I have now tried a count between 2-6 nothing is rendered in any case.


Share this post


Link to post
Share on other sites
I realize this is a bit of work for you, but can you redo your code into a nice little linear flow without so many function calls? I found myself rolling up and down the screen about 50 times trying to follow the flow of your program and it's difficult to gather in my head in what order things are happening.

maybe just make a new quickie render hack function that is like the following:


glGenBuffers()
glBindBuffer()
glBufferData()
glBindBuffer()
glBufferData()
glVertexPointer()
glColorPointer()
glDrawElements()



It doesn't have to be clean and you can break on the second frame so you don't have to worry about initializing outside of your render loop, but that's probably the best way to get the basics nailed down, and once you have it working, you can OO'ize your code into classes. But just for me trying to read it's very difficult to follow.

You can certainly choose not to do this, but it's much easier to debug that way.

Share this post


Link to post
Share on other sites
Quote:
Original post by karwosts
I realize this is a bit of work for you, but can you redo your code into a nice little linear flow without so many function calls? I found myself rolling up and down the screen about 50 times trying to follow the flow of your program and it's difficult to gather in my head in what order things are happening.

maybe just make a new quickie render hack function that is like the following:

*** Source Snippet Removed ***

It doesn't have to be clean and you can break on the second frame so you don't have to worry about initializing outside of your render loop, but that's probably the best way to get the basics nailed down, and once you have it working, you can OO'ize your code into classes. But just for me trying to read it's very difficult to follow.

You can certainly choose not to do this, but it's much easier to debug that way.


Yeah, I'll think I have to do that. Create a single class that does this without the OO. I'll get back to this post tomorrow if I can't get that to work.

Thanks for trying to help me.

Share this post


Link to post
Share on other sites
Ok I have created a single file that displays my problem, it can be compiled and run if one has a java SDK and JOGL.

The problem is still that nothing is rendered. glGetError returns 0 after glDrawRangelElements.


import com.jogamp.opengl.util.AnimatorBase;
import com.jogamp.opengl.util.FPSAnimator;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

/**
* @version 2010-nov-13
*/

public class JOGLTest
{
public static void main(String[] args)
{
JFrame frame = new JFrame("JOGL test");
frame.setSize(640, 480);

GLProfile glProfile = GLProfile.get(GLProfile.GL2);
GLCapabilities glCapabilities = new GLCapabilities(glProfile);

GLCanvas canvas = new GLCanvas(glCapabilities);
frame.add(canvas);

canvas.addGLEventListener(new JOGLRenderer());

AnimatorBase animator = new FPSAnimator(canvas, 60);
animator.add(canvas);
animator.start();

frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

frame.setVisible(true);
}

private static class JOGLRenderer implements GLEventListener
{
@Override
public void init(GLAutoDrawable drawable)
{
GL2 gl = drawable.getGL().getGL2();

gl.glShadeModel(GL2.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);


// Create vertex data
float[] vertexData = new float[]
{
0.75f, 0.25f, -2.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.75f, 0.75f, -2.0f, 0.0f, 1.0f, 0.0f, 1.0f,
0.25f, 0.25f, -2.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.25f, 0.75f, -2.0f, 0.5f, 0.5f, 0.5f, 1.0f
};


ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(7 * 4 * 4);
FloatBuffer vertexBuffer = vertexByteBuffer.asFloatBuffer();
vertexBuffer.put(vertexData);


// Create vertex buffer
int[] vertexBufferId = new int[1];
gl.glGenBuffers(1, vertexBufferId, 0);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vertexBufferId[0]);
gl.glBufferData(GL2.GL_ARRAY_BUFFER, 1024, null, GL2.GL_DYNAMIC_DRAW);

// Load vertex data into vertex buffer
gl.glBufferSubData(GL2.GL_ARRAY_BUFFER, 0, vertexByteBuffer.capacity(), vertexByteBuffer);


// Create index data
int[] indexData = new int[]
{
0, 1, 2, 2, 1, 3
};

ByteBuffer indexByteBuffer = ByteBuffer.allocateDirect(6 * 4);
IntBuffer indexBuffer = indexByteBuffer.asIntBuffer();
indexBuffer.put(indexData);


// Create index buffer
int[] indexBufferId = new int[1];
gl.glGenBuffers(1, indexBufferId, 0);
gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indexBufferId[0]);
gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, 1024, null, GL2.GL_DYNAMIC_DRAW);

// Load index data into index buffer
gl.glBufferSubData(GL2.GL_ELEMENT_ARRAY_BUFFER, 0, indexByteBuffer.capacity(), indexByteBuffer);
}

@Override
public void dispose(GLAutoDrawable drawable)
{
}

@Override
public void display(GLAutoDrawable drawable)
{
GL2 gl = drawable.getGL().getGL2();

gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();


/*

This rendered a purple quad

gl.glBegin(GL2.GL_TRIANGLES);
gl.glColor3f(0.5f ,0.0f, 0.5f);
gl.glVertex3f(0.75f, 0.25f, -2);
gl.glVertex3f(0.75f, 0.75f, -2);
gl.glVertex3f(0.25f, 0.25f, -2);

gl.glVertex3f(0.25f, 0.25f, -2);
gl.glVertex3f(0.75f, 0.75f, -2);
gl.glVertex3f(0.25f, 0.75f, -2);
gl.glEnd();
*/


// this below renders nothing

int stride = 7 * 4;

gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);

gl.glVertexPointer(3, GL2.GL_FLOAT, stride, 0);
gl.glColorPointer(4, GL2.GL_FLOAT, stride, 3 * 4);

gl.glDrawRangeElements(GL2.GL_TRIANGLES, 0, 5, 6, GL2.GL_UNSIGNED_INT, 0);
// gl.glDrawElements(GL2.GL_TRIANGLES, 6, GL2.GL_UNSIGNED_INT, 0);

// glGetError returns 0
int error = gl.glGetError();

gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
{
GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();

gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();

gl.glViewport(0, 0, width, height);

gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);

gl.glMatrixMode(GL2.GL_MODELVIEW);
}
}
}



Share this post


Link to post
Share on other sites
Ok I solved it myself and the problem lies in how one should work with java.nio.Buffer. I have to call the order() method on the ByteBuffer with ByteOrder.nativeOrder(). Then it works!

Full working source:

import com.jogamp.opengl.util.AnimatorBase;
import com.jogamp.opengl.util.FPSAnimator;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

/**
* @version 2010-nov-13
*/

public class JOGLTest
{
public static void main(String[] args)
{
JFrame frame = new JFrame("JOGL test");
frame.setSize(640, 480);

GLProfile glProfile = GLProfile.get(GLProfile.GL2);
GLCapabilities glCapabilities = new GLCapabilities(glProfile);

GLCanvas canvas = new GLCanvas(glCapabilities);
frame.add(canvas);

canvas.addGLEventListener(new JOGLRenderer());

AnimatorBase animator = new FPSAnimator(canvas, 60);
animator.add(canvas);
animator.start();

frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

frame.setVisible(true);
}

private static class JOGLRenderer implements GLEventListener
{
@Override
public void init(GLAutoDrawable drawable)
{
GL2 gl = drawable.getGL().getGL2();

gl.glShadeModel(GL2.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);


// Create vertex data
float[] vertexData = new float[]
{
0.75f, 0.25f, -2.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.75f, 0.75f, -2.0f, 0.0f, 1.0f, 0.0f, 1.0f,
0.25f, 0.25f, -2.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.25f, 0.75f, -2.0f, 0.5f, 0.5f, 0.5f, 1.0f
};


ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(7 * 4 * 4);
vertexByteBuffer.order(ByteOrder.nativeOrder()); // <-- this line was missing
FloatBuffer vertexBuffer = vertexByteBuffer.asFloatBuffer();
vertexBuffer.put(vertexData);


// Create vertex buffer
int[] vertexBufferId = new int[1];
gl.glGenBuffers(1, vertexBufferId, 0);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vertexBufferId[0]);
gl.glBufferData(GL2.GL_ARRAY_BUFFER, 1024, null, GL2.GL_DYNAMIC_DRAW);

// Load vertex data into vertex buffer
gl.glBufferSubData(GL2.GL_ARRAY_BUFFER, 0, vertexByteBuffer.capacity(), vertexByteBuffer);


// Create index data
int[] indexData = new int[]
{
0, 1, 2, 2, 1, 3
};

ByteBuffer indexByteBuffer = ByteBuffer.allocateDirect(6 * 4);
indexByteBuffer.order(ByteOrder.nativeOrder()); // <- this line was missing
IntBuffer indexBuffer = indexByteBuffer.asIntBuffer();
indexBuffer.put(indexData);


// Create index buffer
int[] indexBufferId = new int[1];
gl.glGenBuffers(1, indexBufferId, 0);
gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indexBufferId[0]);
gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, 1024, null, GL2.GL_DYNAMIC_DRAW);

// Load index data into index buffer
gl.glBufferSubData(GL2.GL_ELEMENT_ARRAY_BUFFER, 0, indexByteBuffer.capacity(), indexByteBuffer);
}

@Override
public void dispose(GLAutoDrawable drawable)
{
}

@Override
public void display(GLAutoDrawable drawable)
{
GL2 gl = drawable.getGL().getGL2();

gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();


/*

This rendered a purple quad

gl.glBegin(GL2.GL_TRIANGLES);
gl.glColor3f(0.5f ,0.0f, 0.5f);
gl.glVertex3f(0.75f, 0.25f, -2);
gl.glVertex3f(0.75f, 0.75f, -2);
gl.glVertex3f(0.25f, 0.25f, -2);

gl.glVertex3f(0.25f, 0.25f, -2);
gl.glVertex3f(0.75f, 0.75f, -2);
gl.glVertex3f(0.25f, 0.75f, -2);
gl.glEnd();
*/


int stride = 7 * 4;

gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);

gl.glVertexPointer(3, GL2.GL_FLOAT, stride, 0);
gl.glColorPointer(4, GL2.GL_FLOAT, stride, 3 * 4);

gl.glDrawRangeElements(GL2.GL_TRIANGLES, 0, 5, 6, GL2.GL_UNSIGNED_INT, 0);
// gl.glDrawElements(GL2.GL_TRIANGLES, 6, GL2.GL_UNSIGNED_INT, 0);

// glGetError returns 0
int error = gl.glGetError();

gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
{
GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();

gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();

gl.glViewport(0, 0, width, height);

gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);

gl.glMatrixMode(GL2.GL_MODELVIEW);
}
}
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627719
    • Total Posts
      2978796
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now