Sign in to follow this  
Enerjak

OpenGL Problem dynamically creating a vertex Buffer.

Recommended Posts

Ok, i'm not what to do on this one...basically i have this:

[code]

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]namespace[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]IEngine

[/color][/size][/color][/size][size="2"]{

[/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]()

{

[/size][size="2"][color="#010001"][size="2"][color="#010001"]primitiveCount[/color][/size][/color][/size][size="2"] = 0;

}

[/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]::~[/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]()

{

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]bool[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]createRenderable[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]std[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]string[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]name[/color][/size][/color][/size][size="2"])

{



[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]return[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]true[/color][/size][/color][/size][size="2"];

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]beginRenderable[/color][/size][/color][/size][size="2"]()

{

[/size][size="2"][color="#010001"][size="2"][color="#010001"]DWORD[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]size[/color][/size][/color][/size][size="2"] = ([/size][size="2"][color="#010001"][size="2"][color="#010001"]DWORD[/color][/size][/color][/size][size="2"])[/size][size="2"][color="#010001"][size="2"][color="#010001"]verts[/color][/size][/color][/size][size="2"].[/size][size="2"][color="#010001"][size="2"][color="#010001"]size[/color][/size][/color][/size][size="2"]();

[/size][size="2"][color="#010001"][size="2"][color="#010001"]HR[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]ID3D9Renderer[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]getDev9[/color][/size][/color][/size][size="2"]()->[/size][size="2"][color="#010001"][size="2"][color="#010001"]CreateVertexBuffer[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]primitiveCount[/color][/size][/color][/size][size="2"]*[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]sizeof[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]VertexPos[/color][/size][/color][/size][size="2"]),0,[/size][size="2"][color="#010001"][size="2"][color="#010001"]D3DFVF_XYZ[/color][/size][/color][/size][size="2"],

[/size][size="2"][color="#010001"][size="2"][color="#010001"]D3DPOOL_DEFAULT[/color][/size][/color][/size][size="2"],

&[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]this[/color][/size][/color][/size][size="2"]->[/size][size="2"][color="#010001"][size="2"][color="#010001"]pVertexBuffer[/color][/size][/color][/size][size="2"],

[/size][size="2"][color="#010001"][size="2"][color="#010001"]NULL[/color][/size][/color][/size][size="2"]))





[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"]* [/size][size="2"][color="#010001"][size="2"][color="#010001"]pDat[/color][/size][/color][/size][size="2"];

[/size][size="2"][color="#010001"][size="2"][color="#010001"]HR[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]pVertexBuffer[/color][/size][/color][/size][size="2"]->[/size][size="2"][color="#010001"][size="2"][color="#010001"]Lock[/color][/size][/color][/size][size="2"](0,0,([/size][size="2"][color="#010001"][size="2"][color="#010001"]LPVOID[/color][/size][/color][/size][size="2"]*)&[/size][size="2"][color="#010001"][size="2"][color="#010001"]pDat[/color][/size][/color][/size][size="2"],0));



[/size][size="2"][color="#010001"][size="2"][color="#010001"]memcpy[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]pDat[/color][/size][/color][/size][size="2"],&[/size][size="2"][color="#010001"][size="2"][color="#010001"]size[/color][/size][/color][/size][size="2"],[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]sizeof[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]verts[/color][/size][/color][/size][size="2"]));





}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]setPosition[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]float[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]X[/color][/size][/color][/size][size="2"], [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]float[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]Y[/color][/size][/color][/size][size="2"], [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]float[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]Z[/color][/size][/color][/size][size="2"])

{

[/size][size="2"][color="#010001"][size="2"][color="#010001"]verts[/color][/size][/color][/size][size="2"].[/size][size="2"][color="#010001"][size="2"][color="#010001"]push_back[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]new[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]VertexPos[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]X[/color][/size][/color][/size][size="2"],[/size][size="2"][color="#010001"][size="2"][color="#010001"]Y[/color][/size][/color][/size][size="2"],[/size][size="2"][color="#010001"][size="2"][color="#010001"]Z[/color][/size][/color][/size][size="2"]));

[/size][size="2"][color="#010001"][size="2"][color="#010001"]primitiveCount[/color][/size][/color][/size][size="2"]++;

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]setColour[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]DWORD[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]color[/color][/size][/color][/size][size="2"])

{

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]endRenderable[/color][/size][/color][/size][size="2"]()

{

[/size][size="2"][color="#010001"][size="2"][color="#010001"]HR[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]pVertexBuffer[/color][/size][/color][/size][size="2"]->[/size][size="2"][color="#010001"][size="2"][color="#010001"]Unlock[/color][/size][/color][/size][size="2"]());

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]drawRenderable[/color][/size][/color][/size][size="2"]()

{

[/size][size="2"][color="#010001"][size="2"][color="#010001"]ID3D9Renderer[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]getDev9[/color][/size][/color][/size][size="2"]()->[/size][size="2"][color="#010001"][size="2"][color="#010001"]SetRenderState[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]D3DRS_CULLMODE[/color][/size][/color][/size][size="2"],[/size][size="2"][color="#010001"][size="2"][color="#010001"]D3DCULL_CCW[/color][/size][/color][/size][size="2"]);

[/size][size="2"][color="#010001"][size="2"][color="#010001"]ID3D9Renderer[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]getDev9[/color][/size][/color][/size][size="2"]()->[/size][size="2"][color="#010001"][size="2"][color="#010001"]SetStreamSource[/color][/size][/color][/size][size="2"](0,[/size][size="2"][color="#010001"][size="2"][color="#010001"]pVertexBuffer[/color][/size][/color][/size][size="2"],0,[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]sizeof[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]verts[/color][/size][/color][/size][size="2"].[/size][size="2"][color="#010001"][size="2"][color="#010001"]size[/color][/size][/color][/size][size="2"]()));

[/size][size="2"][color="#010001"][size="2"][color="#010001"]ID3D9Renderer[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]getDev9[/color][/size][/color][/size][size="2"]()->[/size][size="2"][color="#010001"][size="2"][color="#010001"]SetFVF[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]D3DFVF_XYZ[/color][/size][/color][/size][size="2"]);



[/size][size="2"][color="#010001"][size="2"][color="#010001"]ID3D9Renderer[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]getDev9[/color][/size][/color][/size][size="2"]()->[/size][size="2"][color="#010001"][size="2"][color="#010001"]DrawPrimitive[/color][/size][/color][/size][size="2"]([/size][size="2"][color="#010001"][size="2"][color="#010001"]D3DPT_TRIANGLELIST[/color][/size][/color][/size][size="2"],0,[/size][size="2"][color="#010001"][size="2"][color="#010001"]primitiveCount[/color][/size][/color][/size][size="2"]);



}



}

[/size]
[/code]

basically i wanted people to be able to specify the position of each vertex and let D3D figure out the points then render the primitive count accordingly, but as you can see, that's not happening...So could someone tell me what to do to get it to work? (i'm making a D3D9/10/11 engine (also gonna throw in OpenGL), anyways, any thoughts?

Share this post


Link to post
Share on other sites
[quote name='Starnick' timestamp='1303598723' post='4802088']
Whoa nelly, your code snippet's formatting needs some fixing ;)
[/quote]

tell me about it......that's how it came out when i used the insert code snippet

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1303605662' post='4802115']
You can't use rich text/HTML inside a code block. You can try copying your code into notepad first to strip it of all formatting, and then pasting it into here.
[/quote]

[code]namespace IEngine
{
IPolygon::IPolygon()
{
primitiveCount = 0;
}

IPolygon::~IPolygon()
{

}

bool IPolygon::createRenderable(std::string name)
{

return true;
}

void IPolygon::beginRenderable()
{
DWORD size = (DWORD)verts.size();
HR(ID3D9Renderer::getDev9()->CreateVertexBuffer(primitiveCount*sizeof(VertexPos),0,D3DFVF_XYZ,
D3DPOOL_DEFAULT,
&this->pVertexBuffer,
NULL))



void* pDat;
HR(pVertexBuffer->Lock(0,0,(LPVOID*)&pDat,0));

memcpy(pDat,&size,sizeof(verts));


}

void IPolygon::setPosition(float X, float Y, float Z)
{
verts.push_back(new VertexPos(X,Y,Z));
primitiveCount++;
}

void IPolygon::setColour(DWORD color)
{

}

void IPolygon::endRenderable()
{
HR(pVertexBuffer->Unlock());
}

void IPolygon::drawRenderable(PRIMITIVE_TYPE primType)
{
D3DPRIMITIVETYPE d3dpType;

switch(primType)
{
case PT_LINE_LIST:
{
d3dpType = D3DPT_LINELIST;
}
break;
case PT_LINE_STRIP:
{
d3dpType = D3DPT_LINESTRIP;
}
break;
case PT_TRIANGLE_LIST:
{
d3dpType = D3DPT_TRIANGLELIST;
}
break;
case PT_TRIANGLE_STRIP:
{
d3dpType = D3DPT_TRIANGLESTRIP;
}
break;
case PT_POINT_LIST:
{
d3dpType = D3DPT_POINTLIST;
}
break;
case PRIMITIVE_TYPE::PT_TRIANGLE_FAN:
{
d3dpType = D3DPT_TRIANGLEFAN;
}
break;
}
ID3D9Renderer::getDev9()->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);

ID3D9Renderer::getDev9()->SetStreamSource(0,pVertexBuffer,0,sizeof(verts.size()));
ID3D9Renderer::getDev9()->SetFVF(D3DFVF_XYZ);

ID3D9Renderer::getDev9()->DrawPrimitive(d3dpType,0,primitiveCount);

}


}[/code]

hopefully this is better



Share this post


Link to post
Share on other sites
This is an array of pointers. You can't upload an array of pointers to DirectX -- it wants an array of values. Also, by using new here, you're leaking memory.[code]std::vector<VertexPos*> verts;
verts.push_back(new VertexPos(X,Y,Z));[/code] You want:[code]std::vector<VertexPos> verts;
verts.push_back(VertexPos(X,Y,Z));[/code]

Here, "[font="Lucida Console"]&size[/font]" is the address of a local variable -- you're copying random local variable data from the stack into your vertex buffer!
Also, "[font="Lucida Console"]sizeof(verts)[/font]" tells you the size of a std::vector, which is always going to return ~12 IIRC, no matter how many items are in the vector!
What is this memcpy supposed to be doing -- you've only just created the buffer, and don't have any data to copy into it yet?[code]DWORD size = (DWORD)verts.size();
memcpy(pDat,&size,sizeof(verts));[/code]

Again, here "sizeof(verts.size())" is telling you the size of an unsigned integer variable, which will always return 4, no matter how many items are in the vector... You did the correct calculation up in CreateVertexBuffer with "[font="Lucida Console"]primitiveCount*sizeof(VertexPos)[/font]"
[code]ID3D9Renderer::getDev9()->SetStreamSource(0,pVertexBuffer,0,sizeof(verts.size()));[/code]
[hr]The logic of your class doesn't make much sense to me either.
You initialize primitiveCount to 0, then you use that number (0) to create the vertex buffer (with 0 size), then you copy some random data into the vertex buffer. Then with each call to setPosition, you copy data into a vector (which is never copied into the vertex buffer).

I would guess that the logic should look like this - the size of the vertex buffer isn't known until after all the setPosition calls, which means you've got to create the vertex buffer in the call to endRenderable.[code] void IPolygon::beginRenderable()
{
assert( pVertexBuffer == NULL );
}

void IPolygon::setPosition(float X, float Y, float Z)
{
verts.push_back(VertexPos(X,Y,Z)); // don't need primitiveCount - verts.size() is the same thing
}


void IPolygon::endRenderable()
{
assert( !verts.empty() );
DWORD sizeInBytes = verts.size() *sizeof(VertexPos);//how many items * size of each item
HR(ID3D9Renderer::getDev9()->CreateVertexBuffer(sizeInBytes,0,D3DFVF_XYZ,
D3DPOOL_DEFAULT,
&pVertexBuffer,
NULL));

void* pDat;
HR(pVertexBuffer->Lock(0,0,&pDat,0));
memcpy(pDat,&verts[0],sizeInBytes);//copy from the vector into the buffer
HR(pVertexBuffer->Unlock());
}[/code]

Share this post


Link to post
Share on other sites
Ok, thanks, i got it to work thanks to you, now to do the same with generating indices and texture coordnates based on how many triangles in the vector (also make it shader based rendering so that way D3D9/10/11 are in sync)

Share this post


Link to post
Share on other sites
Ok, i'm having a little problem.... when i try to add just one triangle it adds like 10 or what ever, I'm trying to use the class to create a screen quad
and i get this output:

[img]http://img.photobucket.com/albums/v238/adarksoul/Untitled-2.png?t=1303696597[/img]




[code] ScreenQuad::ScreenQuad()
{
screen_Object = new DynamicObject9();
}

ScreenQuad::~ScreenQuad()
{

}

bool ScreenQuad::createScreenQuad()
{
screen_Object->beginRenderable();
screen_Object->setPosition(-1,-1,0);
screen_Object->setPosition(1,0,0);
screen_Object->setPosition(0,1,0);
screen_Object->endRenderable();

screen_Object->beginRenderable();
screen_Object->setPosition(0,1,1);
screen_Object->setPosition(-1,0,0);
screen_Object->setPosition(-1,-1,0);
screen_Object->endRenderable();
return true;
}

void ScreenQuad::drawQuad()
{
screen_Object->drawRenderable(PT_TRIANGLE_LIST);
}
[/code]

and here is the new version of the dynamic shape

[code]
DynamicObject9::DynamicObject9()
{

}

DynamicObject9::~DynamicObject9()
{

}

bool DynamicObject9::createRenderable(std::string name)
{
return true;
}

void DynamicObject9::beginRenderable()
{
//assert(pVertexBuffer == NULL);
}

void DynamicObject9::setPosition(float X, float Y, float Z)
{
VertexPosColor pos;
pos = VertexPosColor(X,Y,Z,0xFFFFFFFF);
verts.push_back(pos);
}

void DynamicObject9::endRenderable()
{
assert( !verts.empty());

DWORD sizeInBytes = verts.size() * sizeof(VertexData);

ID3D9Renderer::getDev9()->CreateVertexBuffer(sizeInBytes,0,D3DFVF_XYZ,D3DPOOL_DEFAULT,&pVertexBuffer,NULL);

void* pData;
pVertexBuffer->Lock(0,0,(void**)&pData,0);
memcpy(pData,&verts[0],sizeInBytes);
pVertexBuffer->Unlock();
}

void DynamicObject9::drawRenderable(PRIMITIVE_TYPE primType )
{
D3DPRIMITIVETYPE primType3D;

switch(primType)
{
case PT_LINE_LIST:
{
primType3D = D3DPT_LINELIST;
}
break;
case PT_LINE_STRIP:
{
primType3D = D3DPT_LINESTRIP;
}
break;
case PT_TRIANGLE_FAN:
{
primType3D = D3DPT_TRIANGLEFAN;
}
break;
case PT_TRIANGLE_LIST:
{
primType3D = D3DPT_TRIANGLELIST;
}
break;
case PT_TRIANGLE_STRIP:
{
primType3D = D3DPT_TRIANGLESTRIP;
}
break;
case PT_POINT_LIST:
{
primType3D = D3DPT_POINTLIST;
}
break;
}
ID3D9Renderer::getDev9()->SetFVF(D3DFVF_XYZ);
ID3D9Renderer::getDev9()->SetStreamSource(0,pVertexBuffer,0,sizeof(verts.size()));

ID3D9Renderer::getDev9()->DrawPrimitive(primType3D,0,verts.size());

}

[/code]

not sure why it's drawing so many polys when all i want is 2 for the screen quad

Share this post


Link to post
Share on other sites
What is the type of [font="CourierNew, monospace"][size="2"]verts[/size][/font]? You're putting [color="#1C2837"][font="CourierNew, monospace"][size="2"][color="#660066"]VertexPosColor[/color][/size][/font][/color]s into it, but are computing it's size in bytes as if it contains [color="#660066"][font="CourierNew, monospace"][size="2"]VertexData[/size][/font][/color]s.

Again, [color="#1C2837"][font="CourierNew, monospace"][size="2"][color="#000088"]sizeof[/color][color="#666600"]([/color][color="#000000"]verts[/color][color="#666600"].[/color][color="#000000"]size[/color][color="#666600"]())[/color][/size][/font][/color] will always return 4 (the size of the of an [font="Courier New"]unsigned int[/font] -- the type that's returned by [font="Courier New"]size()[/font]).

Also, [color="#660066"][font="CourierNew, monospace"][size="2"]DrawPrimitive[/size][/font][/color] wants you to pass in the number of primitives, but you're passing in the number of vertices. Inside the switch statement, you need to add logic for each primitive type that calculates the number of primitives based on the number of vertices ([i]e.g. for a tri-list, it's [font="Courier New"]numVerts/3[/font], for a tri-strip it's [font="Courier New"]numVerts-2[/font], etc...[/i])

Share this post


Link to post
Share on other sites
this is still not getting me anywhere, I'll just have to create a new vertex buffer for the screen quad and just fill it that way with the 6 vertices to make the quad..."sigh* i'll have to study this problem more carefully to better understand it, as it stands it only draws one triangle half way the screen thanks for your help

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
      627737
    • Total Posts
      2978873
  • 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