Jump to content

  • Log In with Google      Sign In   
  • Create Account

Maxjen

Member Since 07 Apr 2012
Offline Last Active Mar 25 2014 11:47 AM

#5141352 C++ c

Posted by Maxjen on 22 March 2014 - 09:18 PM

Wow, lots of interesting stuff. I didn't even know you could have internal structs. That alone will be very helpful in some other places. It was a bit overwhelming, but I think I understood most of it now (though I wouldn't be able to reproduce it) except the reinterpret cast. I will look that up.

 

I will also look at the code some more and try to adapt as much as possible to improve my coding skills. Thanks for taking the time! You guys are awesome!




#5141120 C++ c

Posted by Maxjen on 21 March 2014 - 03:52 PM

I thought about it a bit more and I think I can actually do what I want with a map or a list. Initially it was important that the position of the elements remained the same because I uploaded the whole thing into an opengl vertex buffer. I wanted to be able to delete vertices in the middle, but if I used the swap-back method the index buffer wouldn't be correct anymore.

 

However in my new render class I am using a huge container with all the vertex data of my world and in every frame I assemble a vertex buffer and an index buffer using only the elements of the container that I want to render.(which I will later do with some sort of spatial partitioning) So now only that assembled vertex buffer needs to be in contiguous space and for that I can use a normal STL vector.(since I don't have to delete anything)

 

I will be thinking about what the most optimal container for the vertex pool is.

 

How does getElement know that index is a valid item?  

How are you going to implement iterating through the entire vector?

In my use case I wouldn't have to iterate over the vector except to delete it. And the classes that would use the vector would keep track of all the elements they added and only use getElement for those elements.

 

Even though I probably wont use the class anymore, did I get it right now?

template <typename T>
class Vector
{
private:
	unsigned char* elements;
	int elementCount;
	int elementCapacity;
	int freeElement; // first free element
	int* freeElementList; // each array element is the index of the next free element
	bool* isUsed;
public:
	Vector(int initialCapacity) {
		elementCount = 0;
		elementCapacity = initialCapacity;
		elements = new unsigned char[elementCapacity * sizeof(T)];

		freeElement = 0;
		freeElementList = new int[elementCapacity];
		isUsed = new bool[elementCapacity];
		for (int i = 0; i < elementCapacity; ++i) {
			freeElementList[i] = i + 1;
			isUsed[i] = false;
		}
	}

	~Vector() {
		for (int i = 0; i < elementCapacity; ++i) {
			if (isUsed[i])
				((T*)(elements + i * sizeof(T)))->~T();
		}

		delete [] freeElementList;
		delete [] elements;
		delete [] isUsed;
	}

	int addElement(const T& element) {
		if (elementCount == elementCapacity) {
			elementCapacity *= 2;

			int* oldFreeElementList = freeElementList;
			freeElementList = new int[elementCapacity];
			memcpy(freeElementList, oldFreeElementList, elementCount * sizeof(int));
			delete [] oldFreeElementList;

			bool* oldIsUsed = isUsed;
			isUsed = new bool[elementCapacity];
			memcpy(isUsed, oldIsUsed, elementCount * sizeof(bool));
			delete [] oldIsUsed;

			for (int i = elementCount; i < elementCapacity; ++i) {
				freeElementList[i] = i + 1;
				isUsed[i] = false;
			}

			unsigned char* oldElements = elements;
			elements = new unsigned char[elementCapacity * sizeof(T)];
			for (int i = 0; i < elementCount; ++i) {
				if (isUsed[i])
					new (elements + i * sizeof(T)) T(*((T*)(oldElements + i * sizeof(T))));
			}
			delete [] oldElements;
		}
		new (elements + freeElement * sizeof(T)) T(element);

		isUsed[freeElement] = true;

		int result = freeElement;
		freeElement = freeElementList[freeElement];
		elementCount++;

		return result;
	}

	void removeElement(int index) {
		if (isUsed[index]) {
			((T*)(elements + index * sizeof(T)))->~T();
			freeElementList[index] = freeElement;
			freeElement = index;
			elementCount--;
		}
	}

	T getElement(int index) {
		return *((T*)(elements + index * sizeof(T)));
	}
};




#5141023 C++ c

Posted by Maxjen on 21 March 2014 - 12:05 PM

Ok, I changed the class to use new/delete instead of malloc/free and I am calling the destructors manually. However, I don't know if I still have to call the constructors manually. I think right now the line "elements[freeElement] = element;" uses the copy constructor to construct the elements, or not?

template <typename T>
class Vector
{
private:
	T* elements;
	int elementCount;
	int elementCapacity;
	int freeElement; // first free element
	int* freeElementList; // each array element is the index of the next free element
	bool* isUsed;
public:
	Vector(int initialCapacity) {
		elementCount = 0;
		elementCapacity = initialCapacity;
		elements = new T [elementCapacity];

		freeElement = 0;
		freeElementList = new int [elementCapacity];
		isUsed = new bool [elementCapacity];
		for (int i = 0; i < elementCapacity; ++i) {
			freeElementList[i] = i + 1;
			isUsed[i] = false;
		}
	}

	~Vector() {
		for (int i = 0; i < elementCapacity; ++i) {
			if (isUsed[i])
				elements[i].~T();
		}

		delete [] freeElementList;
		delete [] elements;
		delete [] isUsed;
	}

	int addElement(T& element) {
		if (elementCount == elementCapacity) {
			elementCapacity *= 2;

			int* oldFreeElementList = freeElementList;
			freeElementList = new int [elementCapacity];
			memcpy(freeElementList, oldFreeElementList, elementCount * sizeof(int));
			delete [] oldFreeElementList;

			bool* oldIsUsed = isUsed;
			isUsed = new bool [elementCapacity];
			memcpy(isUsed, oldIsUsed, elementCount * sizeof(bool));
			delete [] oldIsUsed;

			for (int i = elementCount; i < elementCapacity; ++i) {
				freeElementList[i] = i + 1;
				isUsed[i] = false;
			}

			T* oldElements = elements;
			elements = new T [elementCapacity];
			memcpy(elements, oldElements, elementCount * sizeof(T));
			delete [] oldElements;
		}
		elements[freeElement] = element;
		isUsed[freeElement] = true;

		int result = freeElement;
		freeElement = freeElementList[freeElement];
		elementCount++;

		return result;
	}

	void removeElement(int index) {
		elements[index].~T();
		freeElementList[index] = freeElement;
		freeElement = index;
		elementCount--;
	}

	T getElement(int index) {
		return elements[index];
	}

};

About the rule of three: If I don't plan to copy instances of the class do I still need it?




#5129445 glm and perspective matrices

Posted by Maxjen on 06 February 2014 - 06:02 PM

I finally found the problem. In my shader-class I called glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, stride, offset) with 2 instead of 3 as the size parameter. This was because I mainly copy pasted the class from an older 2d project where I only needed 2 components per vertex. Oh well, at least I learned quite a bit about projection matrices.




#5091028 glGetUniformLocation not working

Posted by Maxjen on 02 September 2013 - 07:03 AM

In your shader, using your own uniform is correct, don't use gl_*. Use #version 400 core and they should give compiler errors as they're removed from the language.

 

Anyway your problem seems to be this:

    glDetachShader(programId, vertexShaderId);
    glDetachShader(programId, fragmentShaderId);

Remove that code.

Ok, I figured it out. The problem was that I didn't use the variables so they were compiled out. Maybe that is what Geometrian meant. The detaching of the shader after linking should be okay: http://stackoverflow.com/questions/9113154/proper-way-to-delete-glsl-shader

But thanks for the #version 400 core tip. Now I only have to solve the texture problem.




PARTNERS