# OpenGL Converting D3D vertex buffers to OpenGL vertex arrays...

## Recommended Posts

Hi, Firstly, apologies if this is in the wrong forum - moderators, feel free to move it if it is, but given that it mentions Direct3D and OpenGL, I didn't know which forum to put it in, and I didn't want to cross-post. Secondly, apologies if this seems like a really obvious question. It kinda does, to me, but I couldn't find any threads which answered when I searched the forums. So, I've got a bit of code I found online which I'm playing around with, which renders stuff using DirectX 8, and I'm trying to abstract the rendering calls to a common interface so I can have the same code run on an OpenGL renderer. I'm pretty comfortable with OpenGL, not so comfortable with DirectX, although I do know a little bit. The interface has methods, which do roughly the following. This isn't the exact or complete code because I've just tried to show the interesting stuff - I can post the full source if it'll help.
void CreateVertexBuffer(UINT nLength, UINT nVertexSize, DWORD FVF, D3DPOOL Pool)
{
// m_pD3DDevice being an IDirect3DDevice8
m_pD3DDevice->CreateVertexBuffer(nLength*nVertexSize,
D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY, FVF,
Pool, &m_pVertexBuffer);

m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, nVertexSize);
}

void LockVertexBuffer(UINT nVertices, BYTE **ppVertices)
{
}

void UnlockVertexBuffer()
{
m_pVertexBuffer->Unlock();
}

// ... Similar functions for the index buffers - you get the picture ...

void DrawVertexBuffer(int numVerts, int numIndeces)
{
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, numVerts, 0, numIndeces/3);
}

The program calls CreateVertexBuffer once on initialisation, and then several times a frame it calls LockVertexBuffer, fills the resulting buffer with triangle data, calls UnLockVertexBuffer then DrawVertexBuffer, then locks the buffers again to generate more vertices, and so on. So, how to port this to OpenGL? I've got CreateVertexBuffer just setting up an array of vertices, LockVertexBuffer trying to point *ppVertices at that array and calling glLockArraysEXT(0, nVertices). UnlockVertexBuffer does nothing but call glUnlockArraysEXT(), and DrawVertexBuffer sets up the client states, does a glDrawElements, and disables the client states again. This doesn't seem to work, it crashes in annoying ways which don't give me a callstack I understand or the source code for the point where the crash occurs. I suspect this is because I'm misunderstanding the differences between D3D vertex buffers and OpenGL vertex arrays. If I understand correctly, locking a vertex buffer in D3D with the D3DLOCK_DISCARD allocates memory for a new buffer to write to every time you call it (giving you a pointer to that memory), and then automagically cleaning that memory up somehow at a later date when it's not being used (or is it done in the m_pVertexBuffer->Unlock?). Whereas, my OpenGL implementation sets up one vertex array to rule them all, and just lets the program write to it, render it, write to it again, render it... I'm not sure what needs to change in my OpenGL implementation to get the same behaviour as the D3D version. What do Direct3D vertex buffers do that OpenGL vertex arrays don't? And how would I go about adding that functionality to my OpenGL implementation?

##### Share on other sites
Hi ElectroDruid,

I don't know much about DX so i'm shooting in the dark here. Here is how i think it should be done :

1) CreateVertexBuffer should create a VBO or allocate a chunk of system memory depending on the value of the pool variable. Also, usage (which is hard-coded in your case) can be given as a hint to the driver when creating the VBO.

2) LockVertexBuffer should map the VBO using glMapBuffer. glLockArrays() is from the GL_EXT_compiled_vertex_arrays extension which is deprecated iirc. In D3D when you lock a VB, you are requesting a pointer in order to write to it, as you described. The equivalent functionality in GL is given by glMapBuffer.

3) UnlockVertexBuffer should unmap the VBO in order to be able to use it for rendering.

4) DrawVertexBuffer should set all the glXXXPointer variables and enable all the required client state. I'm not really sure, but i suspect that in the D3D case you posted, this happens in the CreateVertexBuffer function. Maybe because the vertex format is needed to be known when locking the buffer? I'm not sure.
Finally you can safely call glDrawElements.

Hope that helps. As i said i don't know much about DX, so if anyone find any mistakes in the above descriptions, please correct me.

HellRaiZer

EDIT : I think, until now, there is no flag in GL equivalent to D3DLOCK_DISCARD, and other similar flags. I think this is because you can't specify the size of the memory you are going to modify when locking/mapping the VB. IIRC this will be added in Long Peaks. Check the latest OGL Pipeline article for more info.

##### Share on other sites
Quote:
 Original post by HellRaiZerEDIT : I think, until now, there is no flag in GL equivalent to D3DLOCK_DISCARD, and other similar flags.
You can perform a discard lock in OGL by calling BufferData with a NULL pointer.

##### Share on other sites
hey

Quote:
 The program calls CreateVertexBuffer once on initialisation, and then several times a frame it calls LockVertexBuffer, fills the resulting buffer with triangle data, calls UnLockVertexBuffer then DrawVertexBuffer, then locks the buffers again to generate more vertices, and so on.

I too know very little about Direct3D. But I wonder, are you rendering static objects? and if so then why must you keep filling the Vertex Buffer several times each frame? Do you use a single Vertex Buffer to render multiple objects?

Anyhows... with Vertex Buffer Objects in OpenGL, for static objects you only need to generate the Vertex Buffer Object, and load in the Vertex Data once, which is stored in Video Memory. You then can render them very much alike regular Vertex Arrays in OpenGL.

Note, you must use a separate, and different target/type of Vertex Buffer Object for Vertex Indices (ELEMENT_ARRAY_BUFFER, rather than ARRAY_BUFFER).

You allocate/load in the Vertex Data with glBufferData(), and at this time you specify the data usage, whether it be STATIC/DYNAMIC/STREAM along with READ/COPY/DRAW flags. You can then update the data via glBufferSubData().

You can also gain direct access to data via glMapBuffer()/glUnmapBuffer(), which returns a pointer to the Vertex Data.

BTW, it's recommended that you use glDrawRangeElements(), rather than glDrawElements() when drawing Vertex Buffer Objects, which you can read about in the article I link to below.

Finally, rather than try and learn/understand from my brief explainations, I recommend you checkout THIS.

edit
To conclude, I made some pesudo code...

glGenBuffers...
glBindBuffer...
glBufferData...

I think the official OpenGL API is HERE.

// init vertex buffer object...// note, this would be done when you load your geometry// first you generate your vertex buffer object via...void glGenBuffers(GLsizei n, GLuint * buffers)// then you bind this buffer via... void glBindBuffer(GLenum target, GLuint buffer)void glBindBuffer(GLenum target, GLuint buffer)// then you allocate/load data into this buffer via...// note, "GLenum target" set to GL_ARRAY_BUFFER for vertex data, and GL_ELEMENT_ARRAY_BUFFER for vertex indicesvoid glBufferData(GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage)// and remember to unbind your vertex buffer object via passing 0 for "uint buffer"...void glBindBuffer(GLenum target, GLuint buffer)// and later on when it comes to drawing...// you draw alike regular opengl vertex arrays, except you specify offset relative to each vertex buffer object// note, this would obviously be done in a separate function// enable vertex arraysglEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(GL_NORMAL_ARRAY);glEnableClientState(GL_TEXTURE_COORD_ARRAY);// bind your vertex data VBOvoid glBindBuffer(GL_ARRAY_BUFFER, GLuint buffer);// set your vertex array pointers// note, instead of pointer you use offset relative to origin of VBO dataglVertexPointer(3, GL_FLOAT, 0, (const GLvoid*)0);glNormalPointer(GL_FLOAT, 0, (const GLvoid*)normalOffset);glTexCoordPointer(2, GL_FLOAT, 0, (const GLvoid*)texCoordOffset);// remember to unbind your VBOglBindBuffer(GL_ARRAY_BUFFER_ARB, 0);// now bind your vertex indices VBOvoid glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, GLuint buffer)// draw vertex array// note, again instead of pointer you use offset relative to origin of VBO data// note, use glDrawRangeElements() for best performancevoid glDrawRangeElements (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, (const GLvoid*)indexOffset);// again, remember to unbind your VBOglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);// disable your vertex arraysglDisableClientState(GL_VERTEX_ARRAY);glDisableClientState(GL_NORMAL_ARRAY);glDisableClientState(GL_TEXTURE_COORD_ARRAY);

Please correct me if I have made any mistakes. I hope this helps :).

cya

[Edited by - yosh64 on July 11, 2007 10:08:29 AM]

##### Share on other sites
Wow... Okay, I have to hold my hands up and admit that I'm a bit out of my depth here. I've not used any of the advanced (read: not immediate mode) stuff in OpenGL since version 1.2 (hence the use of the deprecated extensions in my code). I don't know what's new since then and I'm kinda lost.

VBOs, glMapBuffer, glBufferData, glBufferSubData, glMapBuffer, glUnmapBuffer... All of these things are new to me. I've been working as a gameplay coder on consoles for nearly 5 years now, and haven't done any graphics programming in that time, so I'm clearly pretty out of touch with how the APIs work nowadays. This is a pet project on the PC, so I need to play catch-up.

I looked on NeHe but didn't see anything which might be of use - can someone point me to any kind of resource/tutorial/example code/whatever which demonstrates this new (to me at least) OpenGL functionality in action, so I can see what makes it tick?

Oh, and:

Quote:
 But I wonder, are you rendering static objects? and if so then why must you keep filling the Vertex Buffer several times each frame? Do you use a single Vertex Buffer to render multiple objects?

You hit the nail on the head, kinda. I'm not rendering static objects, I'm rendering very dynamic ones (metaballs, specifically), so I'm needing to construct new meshes every frame. I mean meshes as a plural in the sense that I want to make sure that (for example) if two metaballs seperate, and form seperate unconnected meshes, I want to render both of them rather than have one of them suddenly vanish. I'm trying to use a single vertex buffer for everything, even though I'm aware that it's probably wrong, because that's the only way I know about how to do this kind of thing. It strikes me that the most efficient thing would be to do the maths to calculate a bunch of triangles (connected or not), and then just pipe all the triangles to get rendered at the same time. I'm aware that this might not work, and that I might need to render each mesh seperately, but right now I'm just trying to get *something* onscreen without the program crashing.

I guess what I really need (and I will thank my lucky stars if such a thing exists, although I suspect it doesn't) is something like a piece of source code (a "Rosetta Stone", if you will) which draws something very simple (a sphere, or a cube) using Direct3D vertex buffers, and can do the same thing with the same results in OpenGL using vertex arrays, or VBOs, or whatever the best equivalent is. Does such a thing exist?

##### Share on other sites

If you allocate the vertex buffer in D3DPOOL_DEFAULT, Direct3D tries to put it in video memory or AGP memory (unless you're using software vertex processing, then it has to be in system memory). Either way, the memory is not directly accessible by you.

When you call Lock() on the buffer, Direct3D maps the vertex buffer to a special part of memory; when you perform a write, it doesn't go to system memory but is routed through an I/O bus and to the actual location of the buffer. When you call Unlock(), it unmaps the memory so that you no longer have direct access to it. I think that if you're getting a crash, it might be because you're keeping around this pointer.

With D3DLOCK_DISCARD, Direct3D discards the entire contents of the vertex buffer. The old vertex buffer could be floating around somewhere; your video card could be rendering from it right now. You would never know, because it's not in your control anymore. Instead, you get mapped a new vertex buffer that has nothing to do with your old buffer. This means all the data you uploaded before is gone, too.

##### Share on other sites
Quote:
 Original post by ElectroDruidI looked on NeHe but didn't see anything which might be of use - can someone point me to any kind of resource/tutorial/example code/whatever which demonstrates this new (to me at least) OpenGL functionality in action, so I can see what makes it tick?

Can't point you to any direct turotials, but whenever i'm interested in seeing whats new or getting to grips with an opengl extension i usually check out Delphi3D first and its extension list each with full spec. Although i guess its no different to opengl.org list. They canbe a bit hard to get into, but after a while they begin to make sense.

I guess you could also search on thos sites for more specific information such as tutorials.

##### Share on other sites
hey

Quote:
 I'm trying to use a single vertex buffer for everything, even though I'm aware that it's probably wrong, because that's the only way I know about how to do this kind of thing.

I don't see anything wrong with doing this, and are sure it's quite possible. When loading the scene, I would just allocate one Vertex Buffer Object, large enough to hold the maximum amount of vertex data you will need.

Ohh, if you would prefer to update your vertex data in system memory, then when it comes to rendering you can just bind your VBO, and use glBufferSubData() to copy and update your Vertex Buffer Object.

Remember you will also need to update your vertex indices, or maybe just change the number of elements to draw, if I make any sense?

Finally, remember for getting started you may prefer not even to use Vertex Buffer Objects, and/or even Vertex Arrays, and go back to the basics of glBegin()/glEnd(), hehe.

Here are a couple of tutorials I goog'd on Vertex Buffer Objects, THIS, and NeHe lesson 45. But I think you may need to refer back to that original link on Vertex Buffer Objects I posted, or the OpenGL API or something.

edit
You can find an example of both Vertex Arrays, and Vertex Buffer Objects (refered to as AGP Memory) on page 5 of the OpenGL section of ultimategameprogramming.com. You can probably also find a simular example for DirectX at ultimategameprogramming.com also.

cyas

[Edited by - yosh64 on July 12, 2007 1:10:25 AM]

##### Share on other sites
Hi,

Thanks for all the replies and links, I think I'm starting to get a handle on how VBOs work now - although I'm clearly not quite there yet :) I've got code which compiles and runs without crashing, but it's not drawing anything. I've tried the usual testing stuff (setting a non-black glClearColour, disabling culling, lighting, texturing and alpha blending, triple-checking my camera position, orientation, clip planes and FOV to make sure it matches what the Direct3D renderer is doing, and drawing an immediate mode test quad to prove that I can see it), and all of that seems to work fine. The rest of the program hasn't changed, and it seems to be filling the vertex and index arrays with sensible values, and telling the renderer to draw about the number of polygons I'd expect. I just don't see any polys, and I'm at a bit of a loss as to what could be going wrong.

I've ended up trying to use glDrawArrays for the rendering, because glDrawElements was crashing, and I can't get my compiler to accept glDrawRangedElements for some reason. There's also potentially some weirdness with the way I'm using glBindBufferARB in my Lock/Unlock functions, but I'm still not 100% sure how glBindBufferARB works. If someone could take a quick look at what my code is doing, and give me an idea about what might be going wrong, that would be great.

GLuint vboVerts = 0;GLuint vboIndeces = 0;SVertex*				mpVertexBuffer;UINT*					mpIndexBuffer;struct SVertex{	Vector3f v;	// Vertex	Vector3f n;	// Normal	Vector2f t; // Texture coordinates (currently not used)};void Initialize(){	// ... Set up the window, the openGL extensions, and set some default GL states	// (lighting, texturing and backface culling turned off for testing, etc) ...		CreateVertexBuffer(MAX_VERTICES, sizeof(SVertex));	CreateIndexBuffer(MAX_INDICES);}void Uninitialize(){	delete[] mpVertexBuffer;	delete[] mpIndexBuffer;	 glDeleteBuffersARB(1, &vboVerts);	 glDeleteBuffersARB(1, &vboIndeces);}void CreateVertexBuffer(UINT nLength, UINT nVertexSize){	mpVertexBuffer = new SVertex[nLength];	glGenBuffersARB(1, &vboVerts);	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVerts);	glBufferDataARB(GL_ARRAY_BUFFER_ARB, nLength * nVertexSize, mpVertexBuffer, GL_DYNAMIC_DRAW_ARB);}void LockVertexBuffer(UINT nVertices, BYTE **ppVertices){	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVerts);	// Not sure if this should be here	(BYTE*)(*ppVertices) = (BYTE*)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB);	mpVertexBuffer = (SVertex*)(*ppVertices);}void UnlockVertexBuffer(){	glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);	glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);	// Not sure if this should be here}void CreateIndexBuffer(UINT nLength){	mpIndexBuffer = new UINT[nLength];	glGenBuffersARB(1, &vboIndeces);	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboIndeces);	glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, nLength * sizeof(UINT), mpIndexBuffer, GL_DYNAMIC_DRAW_ARB);}void LockIndexBuffer(UINT nIndices, BYTE **ppIndices){	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboIndeces);	// Not sure if this should be here	(BYTE*)(*ppIndices) = (BYTE*)glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB);	mpIndexBuffer = (UINT*)(*ppIndices);}void UnlockIndexBuffer(){	glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB);	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);	// Not sure if this should be here}void DrawVertexBuffer(int numVerts, int numIndeces){	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVerts);	glNormalPointer(GL_FLOAT, sizeof(SVertex), &mpVertexBuffer[0].n);	glVertexPointer(3, GL_FLOAT, sizeof(SVertex), &mpVertexBuffer[0].v);	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboIndeces);	glIndexPointer(GL_UNSIGNED_SHORT, 0, 0);		glEnableClientState(GL_VERTEX_ARRAY);	glEnableClientState(GL_NORMAL_ARRAY);	//	glDrawElements(GL_TRIANGLES, numIndeces, GL_UNSIGNED_INT, mpIndexBuffer); // This crashes the program//	glDrawRangeElements(GL_TRIANGLES, 0, numIndeces, numIndeces, GL_UNSIGNED_INT, mpIndexBuffer); // I couldn't get this to compile	glDrawArrays( GL_TRIANGLES, 0, numIndeces);	glDisableClientState(GL_VERTEX_ARRAY);	glDisableClientState(GL_NORMAL_ARRAY);}

Usage: The rest of the code is doing stuff in the following order, about half a dozen times per frame...

LockVertexBuffer();
LockIndexBuffer();
... Generate a bunch of vertex/index data ...
UnlockVertexBuffer();
UnlockIndexBuffer();
DrawVertexBuffer();

Any ideas?

##### Share on other sites
I think there are a couple of problems with the code you posted.

1) You don't have to allocate memory for passing it to the glBufferData() function. You can pass NULL in there. This way, when you Lock the VB/IB you won't overwrite the pointer value in 'mpVertexBuffer' and 'mpIndexBuffer'.

2) About your comments/questions in the Lock functions. Yes you have to bind the buffer before mapping it. OTOH there is no need to bind a zero buffer when unmapping, but that's ok if you re-bind it later for using it.

3) What's the value of 'mpVertexBuffer' when specifying glXXXPointer in DrawVertexBuffer? If it holds the last value from the Lock function, then this is wrong. If it's NULL then it should work. The point is that when using VBOs you don't specify a true pointer in memory, but an offset in the VB.

4) glIndexPointer() isn't responsible for setting the triangle indices pointer. It is related to per-vertex indexed color, which you don't need. In order for the glDrawElements to work you have to bind the index buffer (as you do) and then pass an offset in the IBO as the last parameter in the glDrawElements() call.

The DrawVertexBuffer() function should look something like this :
void DrawVertexBuffer(int numVerts, int numIndeces){	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVerts);	glNormalPointer(GL_FLOAT, sizeof(SVertex), (void*)(0 + sizeof(Vector3f)));	glVertexPointer(3, GL_FLOAT, sizeof(SVertex), (void*)(0));	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboIndeces);	glEnableClientState(GL_VERTEX_ARRAY);	glEnableClientState(GL_NORMAL_ARRAY);		glDrawElements(GL_TRIANGLES, numIndeces, GL_UNSIGNED_INT, 0);	glDisableClientState(GL_VERTEX_ARRAY);	glDisableClientState(GL_NORMAL_ARRAY);}

I hope i haven't done any mistakes, and the above makes sense.

HellRaiZer

##### Share on other sites
hey

Firstly, I'm not sure as to how you are using your functions... but, you must be sure to unbind each type of your bound VBO's after use. For the way I see your code, I think you just need to unbind each type of your VBO's in your CreateVertexBuffer, CreateVertexIndexBuffer, and your DrawVertexBuffer functions. That means in your DrawVertexBuffer function, you should unbind for each of your GL_ARRAY_BUFFER_ARB, and GL_ELEMENT_ARRAY_BUFFER_ARB VBO's.

Hmm, I quickly looked through THIS, and I don't think there is much written about when to unbind your VBO's and such, so my advise is just based on my experience, as I also ran into such problems when first using VBO's.

And yes, you should both bind, and unbind your VBO's in your Lock/Unlock functions.

Anyhows I hoped that helps :).

cya

##### Share on other sites
@ HellRaiZer

I took your advice (passing NULL into the glBufferDataARB() calls in CreateVertexBuffer and CreateIndexBuffer, not directly allocating any memory for mpVertexBuffer or mpIndexBuffer, and I experimented with removing the glBindBufferDataARB() calls in my unlock functions although it doesn't seem to make much difference to anything), and rewriting the DrawVertexBuffer() function) as you suggested. I'm getting a few frame's worth of garbage, followed by a crash. With regards to your question:

Quote:
 What's the value of 'mpVertexBuffer' when specifying glXXXPointer in DrawVertexBuffer? If it holds the last value from the Lock function, then this is wrong. If it's NULL then it should work. The point is that when using VBOs you don't specify a true pointer in memory, but an offset in the VB.

mpVertexBuffer and mpIndexBuffer both get set the first time the respective Lock functions are called, and still have the same values when DrawVertexBuffer is called, since nowhere in my code resets those pointers to NULL. Given than none of my code explicity does anything through those pointers (they're not passed into glBufferDataARB when the buffers are created, and in your version of the draw code, they're not referenced in glNormalPointer or glVertexPointer either). So, do I even need to keep those pointers around inside my rendering code?

@yosh64:

I'm not quite sure I follow you. Firstly, I assume that by unbinding, you mean calling glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0) or glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0)? Just checking...

I didn't quite follow what you said I should bind or unbind at which point, but I've added an unbind to CreateVertexBuffer, so it looks like this:
glGenBuffersARB(1, &vboVerts);glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVerts);	// BindglBufferDataARB(GL_ARRAY_BUFFER_ARB, nLength * nVertexSize, NULL, GL_DYNAMIC_DRAW_ARB);glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);	// Unbind

And similarly added an unbind to the end of CreateIndexBuffer. I've left the Lock and Unlock functions the same (ie Lock binds the buffer, Unlock unbinds it). And I'm also unbinding both buffers at the end of the draw function.

Still no joy :( Still getting garbage and crashes. I'll go through all of those tutorials and the white paper again with a fine tooth comb to see if there's anything I missed, I guess. It's just annoying that I can't debug any of the crashes. It seems like VBOs are pretty all-or-nothing; either they completely work, or they crash with no way of telling you why they crashed.

EDIT: although glDrawElements still crashes (as does glDrawRangedElements, which I figured out how to get compiling), using glDrawArrays actually gives something that looks a bit like what I expect. The vertices I can see all seem to be getting generated in the right place, but either the indeces are wrong or I'm missing about half of the vertex data - there are a lot of holes in my mesh, and a lot of weird triangles in places I don't expect. I can post the current version of my code, and screenshots about how the GL version and the D3D version look different if it'll help - although I suspect that it's wrong to be using glDrawArrays in this context anyway, so I might just be barking up the wrong tree.

[Edited by - ElectroDruid on July 15, 2007 3:23:54 PM]

##### Share on other sites
Just to let everyone know that I sorted the problem. I am a happy bunny.

I went back to the red book (I have an old copy) to read up on glDrawElements and glDrawArrays, and remembered that glDrawArrays doesn't use index data, so if I was getting crashes, it must have been because of the index data. Turns out I was using GL_UNSIGNED_INT when the program was storing indeces as unsigned shorts. Doh! The problem is fixed now, and the program renders beautifully under both APIs.

Many thanks for your help, it was much appreciated!

ElectroDruid

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627737
• Total Posts
2978873
• ### Similar Content

• 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!

• 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

• 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?

• 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.

• 10
• 10
• 21
• 14
• 12