• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

C++ Help?

Status
Not open for further replies.

Haly

One day I realized that sadness is just another word for not enough coffee.
Thanks for the replies, although I think perhaps I asked the wrong question. I'll try again after thinking about it some more, and when I'm not on my phone.
 

usea

Member
Get out of the habit of thinking of patterns as something you can have opinions on. Patterns are a description of existing practices, not a prescription or a technique. Understanding the way programs are structured is important, but trying to use 'design patterns' like building blocks, especially where you start talking about one somehow being 'better' than another, is just a recipe for bad code.

And, incidentally, static methods are an implementation of the 'singleton design pattern.' They are not what's usually called a singleton in C++, but they are singletons in the broader pattern sense.
Why can't more people be like you? I have been trying to get this message across to one of my coworkers for 9 months. He has trouble seeing that means to an end are just that. We are constantly getting in fights because he has this set of behavior in his mind that we should be sticking to, but has no concept of actions->results.

His favorite thing to say is "this is stupid. we shouldn't have to do things like this." When I push him to elaborate, asking him WHY or what action would better accomplish our goals, he doesn't want to hear it. He only knows his principles and that our actions violate them. He's incapable of seeing actions in terms of their actual effects and the surrounding circumstances, Also he doesn't know object oriented programming at all and we work in C#, so that's another layer of frustration. His ideas of what we should/shouldn't be doing are almost always rooted in some misconception or lack of information.

Sorry to rant. I know there's another thread for that. I want to work in a better environment.
 

maharg

idspispopd
Isn't a singleton more of an anti-pattern?

No. If you have a singular resource of any kind it is a singleton. This could include something like an event loop, a main window, a random number generator (if you used a newly instantiated RNG every time you'd get terrible randomization) or the operating system's api itself. Singletons exist everywhere, and they are a pattern.

Whether or not a C++ singleton, in the form of a global variable or singular smart pointer, is an anti-pattern probably depends on who you ask.

Frankly, I find the concept of 'anti-pattern' kind of bizarre. They take the worst aspects of how people make patterns into concrete prescriptive recipes and then turn them on their head to something negative. Most of the time I see people talking about anti-patterns, they're really just talking about interface leakage.
 

Kalnos

Banned
Fair enough. I guess the real problem is forcing patterns instead of realizing that "Hey, this situation could be described by x pattern" while writing the code which is what you seemed to be implying earlier. I have seen a few discussions on singletons before on gamedev.net and they always turn ugly and it seems to be because people are using them for the wrong reasons, which is probably a result of being forced.
 

wolfmat

Confirmed Asshole
Why can't more people be like you? I have been trying to get this message across to one of my coworkers for 9 months.

It's the end of the fucking first chapter of "Design Patterns", for Christ's sake:
"No discussion of how to use design patterns would be complete without a few words on how not to use them. Design patterns should not be applied indiscriminately. Othen they achieve flexibility and variability by introducing additional levels of indirection, and that can complicate a desing and/or cost you some performance. A design pattern should only be applied when the flexibility it affords is actually needed."

Tell your buddy to not just nod along, but to also read his books.
 

maharg

idspispopd
I think a lot of people who talk a lot about design patterns haven't actually read the GoF book. Often they've read something much more gungho about the idea of compositable software components than that book was. There's a lot of these in the Java world.
 

wolfmat

Confirmed Asshole
I have no principal issue with guerrilla coding like that, as long as there are merits to it. Like, if you've jumped on the bandwagon of holding your code against design patterns, then that is a useful practice. Whether you've read the book or not.

But if you're getting into a principal discussion about design patterns per se, then you've got to either figure out by yourself that they're not a catch-all, or you've got to do your research, or reflect on your experience in a meaningful fashion, and then form an argument.

I mean, as soon as you've gotten into the oft-mentioned boilerplate hell that just goes naturally along with it, you've gotta have had that moment of meta thinking where you go: "Wait, does all this actually merit the effort I put into it? Do I actually gain momentum from this construct?"

This is something I'd probably fire people over if I were in the position. Or at least have a very serious discussion about the relationship between time, size of codebase, difficulty of maintaining, and money.

Sometimes, even something as seemingly basic as MVC can be drilled away because there's nothing in it that furthers the cause. A common example for this would be if your view and your controller do the same thing because there's just no logic beyond "switch to value, display value". In that case, you might want to melt things together instead of spreading very elementary code across a modular boilerplate desert.

And I've done my fair share of projects in the Java / RoR world.

Patterns are the standards you have to apply when you're getting into projects that naturally demand them. That's my view of things. The trick is to recognize this situation. Recognize the need for scalability, modularity, blah. Recognize the need in the design phase, as early as possible. If there is no need, then use your design skills to adapt the design so that you're not hindered by it. That's actually really important, and really hard to do when things blow up.

I get when coders go like, "if everything's a pattern, work becomes much easier". That is true when you're making trivial things that directly map onto patterns. Like, if you're a GUI factory. It's important to not forget that you're not always an architect though. Sometimes, you're a guy that transforms data like a madman.

For example, I have to transform binary data desribing 3D meshes to objects that are displayed on a frame-to-frame basis. I'm not gonna go ahead and build a city of classes around the concept. That's completely wasteful in terms of cycles. It is also hard to optimize. Instead, I'm gonna sequence the data in a manner that makes it trivial for the graphics backend to process. There's nothing beautiful about that, but it wouldn't make much sense otherwise.

On the other hand, I have dynamic properties that convey state in a world setting, and I'm constantly comparing entities against each other. Of course I'm going to build OO layers to make it easier to maintain and expand.

I hate it when professionals go "nail, meet hammer" to then proceed to copypaste the shit out of their code. Take your job seriously.
 

bluemax

Banned

So I'm in the process of porting a game from DirectX to OpenGL and I'm struggling a bit with the renderer. Its a 2D game so it seems like it should be straightforward but I don't have much experience with graphics.

I'm stuck trying to get multiple things to draw on screen. What I have so far is:

1. Compile and link shader.
2. Generate texture, bind it with shader.
3. Genereate a Vertex buffer. My vertices are stored in an array of GLFloats, I bind the array to the vertex buffer object.
4. Bind the indices to another buffer object.
5. Fetch the vertex attributes from the shader and bind the vertex attrib pointers.
6. Call glDrawTriangles

Right now I have two different vertex buffer objects, one ui elements and one for non ui elements, but it seems like even though I'm packing more than one set of vertices into the buffers at a time I only have one quad for each vertex buffer.
 

wolfmat

Confirmed Asshole
Right now I have two different vertex buffer objects, one ui elements and one for non ui elements, but it seems like even though I'm packing more than one set of vertices into the buffers at a time I only have one quad for each vertex buffer.

Are you using glDrawElements() or glDrawArrays()? glDrawTriangles() doesn't exist in the OGL API.

In case you're using glDrawArrays():

My guess is that either your vertex attrib pointer is off, or your glDrawArrays call is wrong.
Here's an annotated example for glVertexPointer setup:
Code:
// Args: Mode, data type, stride, offset pointer
// 8*sizeof(GL_FLOAT) for stride because structure is FACE1:nor:f,f,f;tri:f,f,f;tex:f,f;FACE2...
glVertexPointer(GL_TRIANGLES,	GL_FLOAT, 8*sizeof(GLfloat), &[gameData polydata][3]);

Here's an annotated example for the draw call:
Code:
// Args: Mode, offset, data length
glDrawArrays(GL_TRIANGLES,[reference offset], [[reference faces]count]*3);

Don't forget that you have to multiply face count by 3 if you want to draw triangles in glDrawArrays().

Edit: Don't let the ObjC stuff trip you up; [something bla] is reading the property bla from the object something. Like dot notation (which I could've used anyway).
 

bluemax

Banned
Derp, I meant glDrawElements.

I think I'm doing something like:


glVertexAttribPointer(getPositionAttrib(), sizeof(vertexStruct), GL_UNSIGNED, false, sizeof(vertexStruct), 0);
glVertexAttribPointer(getDiffuseAttrib(), sizeof(vertexStruct), GL_UNSIGNED, false, sizeof(vertexStruct), sizeof(float) *2);
glVertexAttribPointer(getDiffuseAttrib(), sizeof(vertexStruct), GL_UNSIGNED, false, sizeof(vertexStruct), sizeof(float) *5);
glVertexAttribPointer(getTexCoordsAttrib(), sizeofVertexStruct), , GL_UNSIGNED, false, sizeof(vertexStruct), sizeof(float) *8);
//glUniformStuff goes here
glDrawElements(GL_TRIANGLESTRIP, i forget what I have for the rest...)

my vertexStructure is:

struct{
float[2] Position,
float[3] Diffuse,
float[3] Tint,
float[2] Texture,
}

This is all inexact as I don't have the code in front of me.
 

wolfmat

Confirmed Asshole
I'd have to look at the code, I think.

It seems to me that you're using glVertexAttribPointer() wrong. The second argument should be the number of elements for the data type you're describing. So, for example, if you're describing Position, the second argument should be 2. Not sizeof(vertexStruct).

You should wrap this section with glGetError() to see what's up. My guess is it'll respond with GL_INVALID_VALUE because size isn't 1, 2, 3 or 4.
(Just add a call to glGetError() and write its return value into a field, then look up the error code)



Also, for debugging purposes, it would be wise to concentrate on one thing at a time. Just try to paint triangles right first.
 

bluemax

Banned
I'd have to look at the code, I think.

It seems to me that you're using glVertexAttribPointer() wrong. The second argument should be the number of elements for the data type you're describing. So, for example, if you're describing Position, the second argument should be 2. Not sizeof(vertexStruct).

You should wrap this section with glGetError() to see what's up. My guess is it'll respond with GL_INVALID_VALUE because size isn't 1, 2, 3 or 4.
(Just add a call to glGetError() and write its return value into a field, then look up the error code)



Also, for debugging purposes, it would be wise to concentrate on one thing at a time. Just try to paint triangles right first.

I can properly draw quads that are textured correctly I just can't get see to get more than 1 per vbo.

I'll post more of the code when I get to work tomorrow.
 

blu

Wants the largest console games publisher to avoid Nintendo's platforms.
Derp, I meant glDrawElements.

I think I'm doing something like:


glVertexAttribPointer(getPositionAttrib(), sizeof(vertexStruct), GL_UNSIGNED, false, sizeof(vertexStruct), 0);
glVertexAttribPointer(getDiffuseAttrib(), sizeof(vertexStruct), GL_UNSIGNED, false, sizeof(vertexStruct), sizeof(float) *2);
glVertexAttribPointer(getDiffuseAttrib(), sizeof(vertexStruct), GL_UNSIGNED, false, sizeof(vertexStruct), sizeof(float) *5);
glVertexAttribPointer(getTexCoordsAttrib(), sizeofVertexStruct), , GL_UNSIGNED, false, sizeof(vertexStruct), sizeof(float) *8);
//glUniformStuff goes here
glDrawElements(GL_TRIANGLESTRIP, i forget what I have for the rest...)

my vertexStructure is:

struct{
float[2] Position,
float[3] Diffuse,
float[3] Tint,
float[2] Texture,
}

This is all inexact as I don't have the code in front of me.
I think you're mis-remembering that glVertexAttribPointer code. Aside from what wolfmat mentioned, your 3rd argument is entirely wrong - none of your attribute scalars is of type unsigned int (they're all floats). So checking the actual code is a good idea ; )
 

KorrZ

Member
Quick question about something I'm not quite fully understanding, sorry if it's a dumb question but if someone could please explain.

When dealing with a dynamically allocated array of structures it says since you have an address, you can't use the . operator so you access members in two ways

ps->price or (*ps).price

But if you're dealing with an array of structures, and using a for loop to get values into members these two don't work:

ps->price or (*ps + i).price

Instead it seems to work if you just use the regular old . as in

ps.price

Could someone please explain to me why it works this way? It just doesn't seem to make sense to me why the rules would change because it seems to only apply when I'm using an index.
 

wolfmat

Confirmed Asshole
I can properly draw quads that are textured correctly I just can't get see to get more than 1 per vbo.

I'll post more of the code when I get to work tomorrow.

The thing with an immediate mode library is that it'll silently fail a lot of the time. Often enough, this results in partial drawing and the like. The draw call succeeded after all up until this-and-that error occured. That's why you should always check if everything's free of errors with glGetError() at least everytime you add or alter OGL statements.

Edit: Don't forget to remove the glGetError() calls as soon as you don't need them, they kill performance.
 

dabig2

Member
Quick question about something I'm not quite fully understanding, sorry if it's a dumb question but if someone could please explain.

When dealing with a dynamically allocated array of structures it says since you have an address, you can't use the . operator so you access members in two ways

ps->price or (*ps).price

But if you're dealing with an array of structures, and using a for loop to get values into members these two don't work:

ps->price or (*ps + i).price

Instead it seems to work if you just use the regular old . as in

ps.price

Could someone please explain to me why it works this way? It just doesn't seem to make sense to me why the rules would change because it seems to only apply when I'm using an index.


Remember that an array index is just a dereference pointer. So you can treat ps as a "regular boring variable", which is when you use the dot operator. You only use -> when you're operating directly on a pointer.

Now, if you dynamically created a pointer that points to an array of structures like so (let's name the structure "Gaf"
Code:
typedef Gaf* psPtr; //declare pointer to structure
psPtr psList = new Gaf[10]; //dynamically allocate array of structure instances

psList[0] ->price = whatever;

Then that will be the way you can use the arrow operator on an array of structures. See how it works out? The array index dereferences the psList pointer. But that pointer is not yet a "regular boring variable" as it's pointing to an array. Now, since you're dealing with an array (which is just a pointer in and of itself), you can use the arrow operator to dereference that.

I hope that made even a little bit of sense. It's 4:30 a.m. here so forgive any rambling or mistakes :)
 

Slavik81

Member
KorrZ, all of those could potentially be valid C++, depending on what you're trying to do and what context they're in. It would help if you showed how you declared the array you were talking about. Particularly, I think, in making clearer examples.
 

KorrZ

Member
KorrZ, all of those could potentially be valid C++, depending on what you're trying to do and what context they're in. It would help if you showed how you declared the array you were talking about. Particularly, I think, in making clearer examples.

Sorry, It's not that I was having trouble getting it working just curiosity of why it works differently. I was doing something like:

Code:
struct bob 
{
    std::string name;
    double amount;
};

bob * ps = new bob[size];

for (int i = 0; i < size; i++)
{
    std::cout << "Enter name: ";
    getline(std::cin, ps[i].name);
    std::cout << "Enter amount: ";
    std::cin >> ps[i].amount;
}

and was just curious why you used ps.amount/name instead of ps->name because so far as I've been learning if you were to do it with just one structure without indexing you use the arrow operator.

Code:
bob * ps = new bob;
std::cin >> ps->amount;

What I'm getting from dabig2 though is that the index acts as a dereference on it's own, so you no longer need to worry about the -> in that case? Just seeking some clarity :)
 

Slavik81

Member
That was very eloquent. And, yes, dabig2 describes it accurately.

I prefer just to remember what it is that I'm actually trying to store. When I say 'new bob[size]' I am getting a pointer to an array of 'bob' objects. When I say 'new bob' I am getting a pointer to a 'bob' object. To me, it's thus obvious that the array requires '.' and the single object requires use '->'.

This understanding depends on the fact that operator[] behaves identically for both pointers to arrays and for arrays themselves. (Which can be further explained, if need be, but the gist of it is that an array object is only barely distinguished from a pointer itself.)

PS. We miss you, cpp_is_king.
 

bluemax

Banned
The thing with an immediate mode library is that it'll silently fail a lot of the time. Often enough, this results in partial drawing and the like. The draw call succeeded after all up until this-and-that error occured. That's why you should always check if everything's free of errors with glGetError() at least everytime you add or alter OGL statements.

Edit: Don't forget to remove the glGetError() calls as soon as you don't need them, they kill performance.

All right, so here's a more complete version of what I'm doing:

1. Compile and link shaders (I'll skip the code for this)

2. Bind Shader Uniforms and Attributes:

Code:
void Effect::BindSimpleVertexAttributes()
{
    attributes[ATTRIB_POSITION] = glGetAttribLocation(ShaderProgramObj, "Position");
    attributes[ATTRIB_DIFFUSE] = glGetAttribLocation(ShaderProgramObj, "DiffuseSource");
    attributes[ATTRIB_TINT] = glGetAttribLocation(ShaderProgramObj, "TintSource");
    //attributes[ATTRIB_BRIGHTNESS] = glGetAttribLocation(ShaderProgramObj, "BrightnessSource");
    glEnableVertexAttribArray(GetAttributeLocation(ATTRIB_POSITION));
    glEnableVertexAttribArray(GetAttributeLocation(ATTRIB_DIFFUSE));
    glEnableVertexAttribArray(GetAttributeLocation(ATTRIB_TINT));
    //glEnableVertexAttribArray(GetAttributeLocation(ATTRIB_BRIGHTNESS));
    
    uniforms[UNIFORM_VIEWPROJECTION_MATRIX] = glGetUniformLocation(ShaderProgramObj, "ViewProjection");
    
    attributes[ATTRIB_TEXCOORD0] = glGetAttribLocation(ShaderProgramObj, "TexCoordIn");
    glEnableVertexAttribArray(GetAttributeLocation(ATTRIB_TEXCOORD0));
    uniforms[UNIFORM_TEXTURE_SAMPLER] = glGetUniformLocation(ShaderProgramObj, "Texture");
}
(I also have a similar function for my UI shader and its attributes)

3. Bind the vertex buffer object
Code:
GLuint m_simpleVertexBuffer;
    glGenBuffers(1, &m_simpleVertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, m_simpleVertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(m_simpleVertices), &m_simpleVertices, GL_STATIC_DRAW);

(where m_simpleVertices right now is just an array of vertex structures with position coordinates, texture coordinates and floats)

(This is what a vertex struct element looks like:)
typedef struct {
    float Position[3];
    float Diffuse[4];
    float Tint[4];
    float Texture[2];
} SimpleVertex;

4. Set Vertex Attributes and Uniforms
Code:
void GLESDevice::SetSimpleVertexAttribs()
{
    const GLKMatrix4 *viewProjection = m_CurrentEffect->GetMatrix("ViewProjection");
    
    glUniformMatrix4fv(m_CurrentEffect->GetUniformLocation(UNIFORM_VIEWPROJECTION_MATRIX), 1, 0, viewProjection->m);
    
    glViewport(0, 0, Height, Width);
    
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_POSITION), 3, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), 0);
    
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_DIFFUSE), 4, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), (GLvoid*) (sizeof(float) * 3));
    
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_TINT), 4, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), (GLvoid*) (sizeof(float) * 7));
    
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_TEXCOORD0), 2, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), (GLvoid*) (sizeof(float) * 11));
    
    glUniform1i(m_CurrentEffect->GetUniformLocation(UNIFORM_TEXTURE_SAMPLER), 0);
}

5. And finally draw
Code:
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glBindVertexArrayOES(m_vertexArray);
    glDrawElements(GL_TRIANGLES, sizeof(m_simpleIndices)/sizeof(m_simpleIndices[0]), GL_UNSIGNED_BYTE, 0);
 

bluemax

Banned
And I just noticed that: glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_POSITION), 3, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), 0);

Is causing glGetError() to return GL_INVALID_OPERATION. Weird.
 

KorrZ

Member
That was very eloquent. And, yes, dabig2 describes it accurately.

I prefer just to remember what it is that I'm actually trying to store. When I say 'new bob[size]' I am getting a pointer to an array of 'bob' objects. When I say 'new bob' I am getting a pointer to a 'bob' object. To me, it's thus obvious that the array requires '.' and the single object requires use '->'.

This understanding depends on the fact that operator[] behaves identically for both pointers to arrays and for arrays themselves. (Which can be further explained, if need be, but the gist of it is that an array object is only barely distinguished from a pointer itself.)

PS. We miss you, cpp_is_king.

Thanks for the help, I just wasn't putting two and two together in my head properly for some reason. It makes a lot more sense to me now, I wasn't thinking of the fact that arrays are basically pointers.
 

wolfmat

Confirmed Asshole
All right, so here's a more complete version of what I'm doing:

3. Bind the vertex buffer object
Code:
GLuint m_simpleVertexBuffer;
    glGenBuffers(1, &m_simpleVertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, m_simpleVertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(m_simpleVertices), &m_simpleVertices, GL_STATIC_DRAW);

(where m_simpleVertices right now is just an array of vertex structures with position coordinates, texture coordinates and floats)
Well, this is just one buffer. I thought you wanted two.
5. And finally draw
Code:
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glBindVertexArrayOES(m_vertexArray);
    glDrawElements(GL_TRIANGLES, sizeof(m_simpleIndices)/sizeof(m_simpleIndices[0]), GL_UNSIGNED_BYTE, 0);
You need to bind the buffer you want to draw with glBindBuffer(). If you bind when you create the buffer, and never again, the last created buffer will stay bound, and that's it. That might lead to your problem. It actually looks like that's what you're doing.

And I just noticed that: glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_POSITION), 3, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), 0);

Is causing glGetError() to return GL_INVALID_OPERATION. Weird.

Is the call between glBegin and glEnd? Because that is not allowed. Apart from that, glVertexAttribPointer() can only generate GL_INVALID_VALUE and GL_INVALID_ENUM. Are you sure it's this call?
 

bluemax

Banned
So to clear this up, do I want a buffer for every object on screen, or just every object associated with a particular type of shader?

To be more clear I have the following right now:

Code:
    if (m_CurrentEffect->GetShaderName() == "Effects/SimpleVertex")
    {
        m_simpleVertexList = data->GetSimpleVertex();
        
        for (int i=0;i<m_simpleVertexList.size();i++)
        {
            m_simpleVertices[i] = m_simpleVertexList[i];
        }
        
        glGenVertexArraysOES(1, &m_simpleVertexArray);
        glBindVertexArrayOES(m_simpleVertexArray);
        
        glGenBuffers(1, &m_simpleVertexBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, m_simpleVertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(m_simpleVertices), &m_simpleVertices, GL_STATIC_DRAW);
    }

    else if (m_CurrentEffect->GetShaderName() == "Effects/UIVertex")
    {
        m_uiVertexList = data->GetUIVertex();
        
        for (int i=0;i<m_uiVertexList.size();i++)
        {
            m_uiVertices[i] = m_uiVertexList[i];
        }

        glGenVertexArraysOES(1, &m_uiVertexArray);
        glBindVertexArrayOES(m_uiVertexArray);
        
        glGenBuffers(1, &m_uiVertexBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, m_uiVertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(m_uiVertices), &m_uiVertices, GL_STATIC_DRAW);
     }
    
    if (!m_CurrentEffect)
    {
        cout<<"Bad shader!"<<endl;
        return false;
    }
    
    if (m_CurrentEffect->GetShaderName() == "Effects/SimpleVertex")
    {
        SetSimpleVertexAttribs();
        glDrawArrays(GL_TRIANGLES, 0, m_simpleVertexList.size());
        glBindVertexArrayOES(0);
    }

    else if (m_CurrentEffect->GetShaderName() == "Effects/UIVertex")
    {
        
        SetUIVertexAttribs();
        glDrawArrays(GL_TRIANGLES, 0, m_uiVertexList.size());
        glBindVertexArrayOES(0);
    }

void GLESDevice::SetSimpleVertexAttribs()
{
    const GLKMatrix4 *viewProjection = m_CurrentEffect->GetMatrix("ViewProjection");
    
    glUniformMatrix4fv(m_CurrentEffect->GetUniformLocation(UNIFORM_VIEWPROJECTION_MATRIX), 1, 0, viewProjection->m);
    
    glViewport(0, 0, Height, Width);
    
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_POSITION), 3, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), 0);
    
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_DIFFUSE), 4, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), (GLvoid*) (sizeof(float) * 3));
    
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_TINT), 4, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), (GLvoid*) (sizeof(float) * 7));
    
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_TEXCOORD0), 2, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), (GLvoid*) (sizeof(float) * 11));
    
    glUniform1i(m_CurrentEffect->GetUniformLocation(UNIFORM_TEXTURE_SAMPLER), 0);
}

void GLESDevice::SetUIVertexAttribs()
{
    const GLKMatrix4 *viewProjection = m_CurrentEffect->GetMatrix("ViewProjection");
    
    glUniformMatrix4fv(m_CurrentEffect->GetUniformLocation(UNIFORM_VIEWPROJECTION_MATRIX), 1, 0, viewProjection->m);
    
    glViewport(0, 0, Height, Width);
    
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_POSITION), 3, GL_FLOAT, GL_FALSE, 0, 0);
    m_lastError = glGetError();
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_DIFFUSE), 4, GL_FLOAT, GL_FALSE, sizeof(UIVertex), (GLvoid*) (sizeof(float) * 3));
    m_lastError = glGetError();
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_TINT), 4, GL_FLOAT, GL_FALSE, sizeof(UIVertex), (GLvoid*) (sizeof(float) * 7));
    m_lastError = glGetError();
    glVertexAttribPointer(m_CurrentEffect->GetAttributeLocation(ATTRIB_TEXCOORD0), 2, GL_FLOAT, GL_FALSE, sizeof(UIVertex), (GLvoid*) (sizeof(float) * 11));
    m_lastError = glGetError();
    glUniform1i(m_CurrentEffect->GetUniformLocation(UNIFORM_TEXTURE_SAMPLER), 0);
    m_lastError = glGetError();
}
 

wolfmat

Confirmed Asshole
So to clear this up, do I want a buffer for every object on screen, or just every object associated with a particular type of shader?
Not necessarily; I was just going by what you were saying in the original posting — you said you wanted multiple VBOs (and you can do that, and it makes sense, as long as you're not going crazy with it). Of course you can have one VBO with all objects in it. It completely depends on what you're aiming for.

You would separate VBOs to make a distinction between separate entity classes. After all, you might want to alter the data in a generic manner or something. Like, VBO 1 has 3D elements with shading, VBO 2 has 2D elements without shading.

Once you make a distinction, you might also want to use different shaders for the distinct entity classes, although that's also not a necessity. It's a design thing.
To be more clear I have the following right now:

Code:
    glGenBuffers(1, &m_simpleVertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, m_simpleVertexBuffer);
    glGenBuffers(1, &m_uiVertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, m_uiVertexBuffer);

    if (m_CurrentEffect->GetShaderName() == "Effects/SimpleVertex")
    {
        m_simpleVertexList = data->GetSimpleVertex();
        
        for (int i=0;i<m_simpleVertexList.size();i++)
        {
            m_simpleVertices[i] = m_simpleVertexList[i];
        }
        
        glGenBuffers(1, &m_simpleVertexBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, m_simpleVertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(m_simpleVertices), &m_simpleVertices, GL_STATIC_DRAW);/
    }
Well, now that you have multiple buffers: Every time you want to draw a buffer, you need to bind it beforehand. Then draw it according to the index buffer.

Here's the drawcode layout, reduced to the basic proceedings:
Code:
glBindBuffer(GL_ARRAY_BUFFER, vboCoords1); // vertex coords
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndices1); // indices

// vertex array stuff
glEnableClientState(GL_VERTEX_ARRAY); // vertex coords are in an array
glVertexPointer(3, GL_FLOAT, 0, 0); // trivial triangle setup without stride

// draw 6 quads with an offset following out of index array -- this will pull data from vbo 1
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, 0);

// Switch to another VBO
glBindBuffer(GL_ARRAY_BUFFER, vboCoords2); // vertex coords
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndices2); // indices

// draw 6 quads with an offset following out of index array -- this will pull data from vbo 2
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, 0);

glDisableClientState(GL_VERTEX_ARRAY); // done with the vertex array

// bind with 0 -- no more VBOs this frame, the end
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
 

bluemax

Banned
Well the engine is a 2d one and there are only 2 shaders, one for UI elements and one for not UI elements, so to me it seems like I should have one VBO/VBA for UI elements and one pair for not UI elements. I just wanted to make sure this was logically consistent.

Lets say I have one VBO/VBA pair for my UI elements, how do I make it draw all of them at once? I assume I put all the vertices into one data structure, bind it to the VBO and then draw?
 

wolfmat

Confirmed Asshole
Lets say I have one VBO/VBA pair for my UI elements, how do I make it draw all of them at once? I assume I put all the vertices into one data structure, bind it to the VBO and then draw?

Pretty much. Got to obviously adhere to the structure (triangle strips, count etc), but that's what you do. If you're doing glDrawElements(), you need to use an index array, also. And that has to be in a VBO as well (I think, maybe you can mix there). And then you're done!
 

bluemax

Banned
Pretty much. Got to obviously adhere to the structure (triangle strips, count etc), but that's what you do. If you're doing glDrawElements(), you need to use an index array, also. And that has to be in a VBO as well (I think, maybe you can mix there). And then you're done!

I switched away from glDrawElements earlier today, so right now I'm drawing using glDrawArrays. My non vertex stuff seems to drawing a bit better after some reorganization thanks to your tips but the UI stuff is still borked (although I also need to fix the UI shader as I'm still trying to port that from HLSL to GLSL). Then again I'm also getting a weird crash when copying lists that is new too.
 

Unicorn

Member
I'm sorry if this is the wrong place to ask, but I don't see there's a coding OT anywhere. I have a fairly simple question.

I want to start learning Java, mostly for Android Development and some dabbling with Papyrus for Skyrim CK. Recently I've been following a string of youtube videos by a guy outlining Android development, but he often glosses over the java stuff and why he's doing what he's doing in the code. I feel my understanding of the dev cycle would greatly improve if I could get a better handle on Java and coding (or is it scripting?) in general.

Do any of you guys have an in-depth resource, like either a line-up of tutorial videos that go over the basics in a fairly quick pace, or perhaps online literature that breaks things down for the beginner? I found that the youtube video series I was following was helping me learn a lot better than I would from a book.
 

wolfmat

Confirmed Asshole
I switched away from glDrawElements earlier today, so right now I'm drawing using glDrawArrays. My non vertex stuff seems to drawing a bit better after some reorganization thanks to your tips but the UI stuff is still borked (although I also need to fix the UI shader as I'm still trying to port that from HLSL to GLSL). Then again I'm also getting a weird crash when copying lists that is new too.

Well, you've got your work cut out for you alright. If you've got more questions, don't be afraid to ask.
 

MJLord

Member
Arrg can somebody please help me ! I tried to use the DevIL library for importing textures into my application but I changed my mind because I had a LINK error saying it couldnt open "ilut.h" even though i was using the library functions -.-

anyway long story short, i took it out and deleted the paths to the directory and im still stuck with the same error message even though im not referencing the entire library in my code and I've got rid of the directory paths.
 

mooooose

Member
For my algorithms class I need to do lab write ups about sort algorithms, usually doing best, worst, and average cases and recording their times. For merge sort, I shouldn't need to do that because the time complexity is the same for each of them, right? If I do, how would I do that?
 

tokkun

Member
For my algorithms class I need to do lab write ups about sort algorithms, usually doing best, worst, and average cases and recording their times. For merge sort, I shouldn't need to do that because the time complexity is the same for each of them, right? If I do, how would I do that?

For best case, it usually occurs with a pre-sorted array for most algorithms. For worst case I would recommend just doing a bunch of random permutations and looking at the worst one. You might think that you can game the algorithm by maximizing the number of swaps, but I'm willing to bet that the worst-case runtime will actually come by maximizing the number of branch mispredictions.
 

mooooose

Member
So I have another question... I'm doing a project in Assembly/SPIM and I have no real idea where to start.

I have to take a string of an opcode. Like "addi $t4, $t3", and return that in hex. So I need to parse the string. How do I compare what I part with what's in my d-sect? Like "addi" vs the string "addi" stored in my d-sect, but also a string for "sub", "add", etc.? I only need to support 4 instructions and 8 registers.

Then when I know which it is, I know the op code in binary, but then I need to add the registers to the binary string and return it all in hex. I have no idea where to start. Does anyone want to offer any guidance?
 
D

Deleted member 30609

Unconfirmed Member
not to be a complete dick, but why have you left your second year CS homework to the last minute for us to do? Don't you have some tutors or lecturers that are intimately familiar with both your current coursework and your already existing knowledge, and therefore be better equipped to help you than us? Send them an email. It's what they're there for!

It's the same thing with your recurrence relation problem from yesterday. Asking on this forum because you don't understand it and its due in tomorrow won't save you in the exam, or later in the workplace, or even in a job interview. You need to immerse yourself in this material from right now.

As far as your MIPS problem goes, you're going to need to describe it a little more clearly. If you want to return a value, put it in $v0 by convention. Also, addi doesn't have that sort of opcode, it's "addi - destination register - register 1 - constant".

For merge sort, you can describe its recuurance relation like this:

where c is a fluid constant value

T(N) = 2t(N/2) + N*c
T(1) = c

now, assume that N = 2^k. This is something relating to the smoothness theorem. Not something I'll get into here, but it's an assumption you'll often make when working with these sort of relations.

T(N)/N = T(N/2) / (N/2) + c
T(N/2) / (N/2) = T(N/4) / (N/4) + c
T(N/4) / (N/4) = T(N/8) / (N/8) + c
...
T(2)/2 = T(1) / 1 + c
T(2)/2 = T(1) + c

now you can see that this telescopes down (substitute T(N/2) in the first equation with the complete second equation, and so forth)

T(N)/N = T(1) + c * k
(as N = 2^k, we would have had to telescope down k times)

now, since

N = 2^k

then

k = log(n) [base 2]

so

T(N)/N = T(1) + c * logN

T(1) = a constant, so

T(N)/N = c + c*logN
T(N) = cN + c*N*logN

When we're looking at complexity in terms of Big O, we're only interested in the upper bound on the rate of growth, or in other words, the bit that grows larger than the other as N gets bigger. Therefore merge sort has a complexity of O(NlogN).

These aren't concepts I got overnight. I may not have communicated them clearly here. You need to really spend the time piecing it together, bit by bit. To give you some perspective, I'm not a graduate. In fact, I'm currently in week eight of a semester. I learnt this in week two. It doesn't take a super genius to be able to get to this, you just need to dedicate the time to doing it.

If you need anymore help or this isn't clear, feel free to PM me.
 

ayeorkean

Member
Don't know whether this is c++ specific or visual studio, but I'm currently working in c++.

I'm trying to debug and have command line arguments, I'm trying to pass in a .txt file. However I don't know how to pass it in. I've already declared the command line arguments in the properties. But I need to pass in a specific file. Any clue?

$(pathfile)?

http://msdn.microsoft.com/en-us/library/ekbzk5f8.aspx
 

Haly

One day I realized that sadness is just another word for not enough coffee.
EDIT: Nevermind, misread. No idea what to do.
 

Slavik81

Member
Don't know whether this is c++ specific or visual studio, but I'm currently working in c++.

I'm trying to debug and have command line arguments, I'm trying to pass in a .txt file. However I don't know how to pass it in. I've already declared the command line arguments in the properties. But I need to pass in a specific file. Any clue?

$(pathfile)?

http://msdn.microsoft.com/en-us/library/ekbzk5f8.aspx

That's Visual Studio-specific. But you should be able to just pass in an absolute or relative path.

How would you do it from the command line (outside of VS)?
 
Hey, guys. Pretty much, trying to make a program with a box with info in it for a gift certificate for her bday in about an hour and a half.

Issue is, the EXE gives a 'blahblah.dll not found' error on other computers.

Does anyone know how to fix this without them installing stuff?

code looks like this:

Code:
#include <iostream>

using namespace std;


int main ( ) 
{
    int stall;
    cout << "--------------------------------------------------------------------------------\n";
    cout << "|                                                                              |\n";
    cout << "|                                                                              |\n";
    cout << "|                                                                              |\n";
    cout << "|                                                                              |\n";
    cout << "|                                                                              |\n";
    cout << "|                                                                              |\n";
    cout << "|                                                                              |\n";
    cout << "|                                                                              |\n";
    cout << "|                                                                              |\n";
    cout << "--------------------------------------------------------------------------------\n";
    cin >> stall;
return 0;
}

Thanks in advance!
 

fenners

Member
Hey, guys. Pretty much, trying to make a program with a box with info in it for a gift certificate for her bday in about an hour and a half.

Issue is, the EXE gives a 'blahblah.dll not found' error on other computers.

Does anyone know how to fix this without them installing stuff?

code looks like this:

Thanks in advance!

What did you creat the project with? Was it a wizard in Visual Studio?

What's the "blahblah.dll" exactly? It makes a difference. You might be linking in some debug dll that an "ordinary" machine won't have...
 
Hey, guys. Pretty much, trying to make a program with a box with info in it for a gift certificate for her bday in about an hour and a half.

Issue is, the EXE gives a 'blahblah.dll not found' error on other computers.


Thanks in advance!
Educated guess: whatever compiler you're using has that dll packaged with it. Copy it over with with exe to the computer you want to run it on.

The exe and dll (on the target computer) should be put in the same directory.
 
What did you creat the project with? Was it a wizard in Visual Studio?

What's the "blahblah.dll" exactly? It makes a difference. You might be linking in some debug dll that an "ordinary" machine won't have...

Yes. Made it in visual studio. Made an empty project, Added a .cpp to the source files, set it to release, compiled it, etc.

It was msvcp100.dll.
 
Ah, visual studio. You'll need to update the c runtime (vcredist*.exe) on the target machine. Ostensibly, the one you compiled on was newer.
 
Status
Not open for further replies.
Top Bottom