Jump to content

  • Log In with Google      Sign In   
  • Create Account

Brother Bob

Member Since 26 Nov 2001
Offline Last Active Today, 01:43 PM

#5231506 Extremely weird, illogical bug. Part of false if clause executed.

Posted by Brother Bob on 28 May 2015 - 11:01 AM

Description is a WCHAR[128]. You then take the address of that. Then you cast that address, which is a WCHAR **, to a WCHAR *.

The type of &adapterDesc.Description is WCHAR (*)[128], not WCHAR **. The types on either side of the assignment are ultimately different and incompatible, but the pointer is likely going to end up pointing to the first element of the Description array anyway in practice, setting aside all the theoretical problems.

#5230885 OpenGL vs DirectX

Posted by Brother Bob on 25 May 2015 - 01:42 PM

What fastcall said.

#5228949 How to add vertices in a VBO

Posted by Brother Bob on 14 May 2015 - 07:49 AM

You can change the content of a buffer in various ways, for example by calling glBufferSubData or by mapping the content to memory with glMapBuffer. If your buffer content can change in size, then start with a large buffer and draw only the necessary part of it so you can expand it later when needed without having to relocate the entire buffer.

#5224922 C++ Ternary operator ?: surprising conversion rules.

Posted by Brother Bob on 22 April 2015 - 03:42 PM

Is the condition supposed to be false?


I assume it's supposed to be false, yes. The interesting part of that code happens when the false-expression is evaluated.

#5223336 Template or Macro

Posted by Brother Bob on 14 April 2015 - 11:47 PM

There are a few templates in the standard library for safe delete already: here, hereand here, for example. So the standard apparently thought it was better to use templates in this case at least.

#5222784 How does boost::shared_ptr/make_shared equality work?

Posted by Brother Bob on 12 April 2015 - 12:48 PM

For the purpose of explaining it in simple terms: yes, your heap allocated object can be managed by the shared pointer, but the stack allocated object cannot.

#5222772 How does boost::shared_ptr/make_shared equality work?

Posted by Brother Bob on 12 April 2015 - 11:28 AM

Partly correct, given the actual wording you use. The different smart pointers are about managing ownership of the instance in various ways, but automatic objects are already owned and managed by the surrounding scope. The automatic ownership is not compatible with the shared ownership provided by the shared pointer.


You can, however, pass ownership to an existing object, but it has to be something that you can pass the ownership of. Dynamically allocated objects can have their ownership transferred, but automatic objects like you asked about cannot.

// allocat an object
Object *o = new Object;


// pass the object to the shared pointer and give it full ownership
shared_ptr<Object> s(o);

#5222767 How does boost::shared_ptr/make_shared equality work?

Posted by Brother Bob on 12 April 2015 - 10:21 AM

Oh yes, that makes sense.
BUT on the other hand, would it also be reasonable to instead just use make_shared, because that would then be created its own dynamically allocated copy of o? If I'm getting this.

That is that make_shared does: it allocates a new object, that you can then used to shared among other shared pointers.


Also, after doing some more experimenting... is the following the correct/best/quickest way to compare the address of a shared_ptr's object and that of another object?:

boost::shared_ptr< Object > sP;
Object o;

if( sP.get() == &o )
Seems that way from what I can see.

Unless you screw up ownership like in your last post, an object managed by a shared pointer and an object with automatic storage can never have the same address.

#5222764 How does boost::shared_ptr/make_shared equality work?

Posted by Brother Bob on 12 April 2015 - 10:03 AM

No, that won't work. Your object o has automatic storage and is managed automatically by the surrounding scope and will be destroyed when leaving the scope. You now also have a shared pointer that has assumed ownership of it and will also try to destroy it. Your code thus has conflicting ownership over the object o.


You have to allocate the object dynamically so that you can pass the full ownership of the resource to the shared pointer.

#5222753 How does boost::shared_ptr/make_shared equality work?

Posted by Brother Bob on 12 April 2015 - 09:32 AM

The shared_ptr class behaves like a pointer, so testing for equality compares the pointer values. You create two shared pointer instances and they hold different instances of your Object class. It seems like this is the kind of comparison you want, but you just misunderstand that make_shared allocates a new copy and shares it.

#5222273 Helps! Errors In glGetAttribLocationARB

Posted by Brother Bob on 09 April 2015 - 12:11 PM

The normal attribute is not used and is removed from the shader by the shader compiler. This is not an error.

#5218604 Array of structure to VAO how to?

Posted by Brother Bob on 23 March 2015 - 03:02 PM

You have to sort that out with the index; pos_index, for example, is the index of your shader attribute you want to feed the pos data into.You can ask for the attribute location with glGetAttribLocation or explicitly specify its location using the layout specifier.

layout(location = 2) in vec3 position;

That defines your position attribute as a 3-component vector at location 2, which is the index you then pass to glVertexAttribPointer.

#5218579 Array of structure to VAO how to?

Posted by Brother Bob on 23 March 2015 - 02:02 PM

For each of the attributes, call glVertexAttribPointer with the appropriate set of parameters for attribute index, size, type and normalization. The key in using array-of-structure types with vertex arrays is in the stride and offset parameters.


The stride is the distance between two instances of an attributes. In your case, you pack all your vertex structure data between each attribute, so the stride is simply sizeof(V_INDEX). The pointer parameter is a pointer to the attribute within your vertex structure, or equivalently the offset of the attribute in the structure, so the pointer is simply offsetof(V_INDEX, pos) for example. You probably have to cast the integer result from offsetof to a void pointer type.

glVertexAttribPointer(pos_index, 3, GL_DOUBLE, FALSE, sizeof(V_INDEX), (void *)offsetof(V_INDEX, pos));
glVertexAttribPointer(uv_index, 2, GL_FLOAT, FALSE, sizeof(V_INDEX), (void *)offsetof(V_INDEX, uv));

#5218434 String replacing

Posted by Brother Bob on 23 March 2015 - 05:37 AM

Sigh... Do you have a solution without regex? The upgrade to C++11 turned out to be troublesome (some libs I use won't compile with C++11).

Jan2go gave another solution above. Use the find and replace repeatedly until the whole string has been searched and replaced.

#5218054 String replacing

Posted by Brother Bob on 21 March 2015 - 08:57 AM

A regular expression can do the job for you; it's in the <regex> header.

auto string = std::string("string\\nwith\\nembedded\\nnew-lines");
auto pattern = std::regex("\\\\n");
auto result = std::regex_replace(string, pattern, "\n");

std::cout << string << std::endl;
std::cout << result << std::endl;

The sub-string you want to search for is "\n", but the slash has to be escaped in the regular expression since the slash is a special character, so the actual regular expression pattern becomes "\\n". The two slashes has to be escaped again in the source code since they are special characters to the C++ compiler as well. Hence, the four back-slashes.