Maxjen

Members
  • Content count

    27
  • Joined

  • Last visited

Community Reputation

199 Neutral

About Maxjen

  • Rank
    Member
  1. Crazy GLSL bug

    Thanks again!   Well, with the fix it is effectively clamped now. It can't get greater than 1 because both vectors for the dot product are normalized and if it is smaller than 0 the if-block where the specularColor is calculated wont be entered and specularColor stays at (0, 0, 0).
  2. Crazy GLSL bug

    Okay, I figured it out, thanks! Like you suggested I tried different values increasing by 0.1 and found out that both lines give identical results except if the value is exactly 1. My guess is that if you write pow(specularFactor, 1.0) openGL removes that line because it expects the result to be specularFactor. However in my case specularFactor was negative for some fragments and then pow yields undefined results (apparently even if the second parameter is 1.0). The solution was a simple if statement: if (specularFactor > 0.0) { specularFactor = pow(specularFactor, matSpecularPower); ... Now the artefacts are gone and everything works perfectly.
  3. Hi,   I am experiencing a really weird GLSL bug. I have implemented point lights following this tutorial, but I get some strange artefacts and I narrowed them down to the following line in my fragment shader: specularFactor = pow(specularFactor, matSpecularPower); specularFactor is of type "highp float" and matSpecularPower is "uniform highp float" and set to 1.0 for debugging purposes. If I change the line to the following everything looks different and I don't have those black artefacts: specularFactor = pow(specularFactor, 1.0); Here are some screenshots: with first line with second line   How is this possible if matSpecular is also set to 1.0? And I am sure it is because I also get the first result if I change it to this: if (matSpecularPower == 1.0) { specularFactor = pow(specularFactor, matSpecularPower); } And the second result if I change it to this: if (matSpecularPower == 1.0) { specularFactor = pow(specularFactor, 1.0); } I am on Linux and have an Intel graphics card if that is important. Help, please! Even ideas of things I could try would be appreciated.
  4. C++ c

    Ah, I just overlooked it. I expected it in the removeElement method, but it was in the clear method.   Your correct. A warning when I accidentally do something wrong is all I need, I just didn't see that it's already there.   Yes, that's better, thanks!
  5. C++ c

    Okay, now I know that you can write unsigned instead of unsigned int. Also thanks for showing how to implement the rule of three. I think there might be a little bug if removeElement is called for an element that isn't used. The value of nextFreeIndex of that element would get lost and be overridden by freeIndex. Maybe an assertion that isUsed() is -1 like you suggested earlier would help here?   I agree about the name, though it's hard to think of something new. Maybe just call it Container?
  6. C++ c

    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!
  7. C++ c

    Ah, I didn't know about operator new(). That should make everything a bit cleaner. Learned something again, thanks!
  8. C++ c

    I never actually used this class for vertex buffers but initially I started managing memory like this because I needed to upload stuff into the vertex buffer. I later thought it was neat and made it into a class without realizing that it wouldn't work correctly if used with non-trivial objects. To fix that I started this post but it's probably more trouble than it's worth.   I did that at first, but I adjusted my code according to fastcall22's second response using placement new.   I think I am currently not relocating objects with C memory functions. I use placement new to copy construct the old objects. But I just noticed that I forgot to call the destructors of the released objects when relocating.   It turns out that I don't need this class. If anything I will make a version only for trivial objects. But I learned plenty about placement new, smart pointers, initialization lists and the rule of three/five, so thanks! I appreciate all the responses.
  9. C++ c

    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.   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))); } };
  10. C++ c

    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?
  11. C++ c

    Hi,   I made a very simple container class because I need a vector where I can remove elements in the middle without changing the position of the other elements but I am worried that it doesn't free up everything when I delete it. Here is the code: 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 public: Vector(int initialCapacity) { elementCount = 0; elementCapacity = initialCapacity; elements = (T*)malloc(elementCapacity * sizeof(T)); freeElement = 0; freeElementList = (int*)malloc(elementCapacity * sizeof(int)); for (int i = 0; i < elementCapacity; ++i) freeElementList[i] = i + 1; } ~Vector() { free(freeElementList); free(elements); } int addElement(T& element) { if (elementCount == elementCapacity) { elementCapacity *= 2; int* oldFreeElementList = freeElementList; freeElementList = (int*)malloc(elementCapacity * sizeof(int)); memcpy(freeElementList, oldFreeElementList, elementCount * sizeof(int)); free(oldFreeElementList); for (int i = elementCount; i < elementCapacity; ++i) freeElementList[i] = i + 1; T* oldElements = elements; elements = (T*)malloc(elementCapacity * sizeof(T)); memcpy(elements, oldElements, elementCount * sizeof(T)); free(oldElements); } elements[freeElement] = element; int result = freeElement; freeElement = freeElementList[freeElement]; elementCount++; return result; } void removeElement(int index) { freeElementList[index] = freeElement; freeElement = index; elementCount--; } T getElement(int index) { return elements[index]; } }; For example if I use it to store STL vectors will the destructor actually delete all their elements?   EDIT: sorry, I forgot to set the right title. It was supposed to be something like "C++ custom container"
  12. glm and perspective matrices

    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.
  13. glm and perspective matrices

    Thanks! It still doesn't work with z = -1, but the projection matrix on that page is the same as the one in glm so now I should be able to figure out what the problem is.
  14. Hi,   I made a simple scene with only one triangle and am now trying to use a projection matrix created by glm. However when I multiply each vertex with it in my shader the triangle disappears.   This is how I create the projection matrix: projectionMatrix = glm::perspective(45.0f, 800.0f/600.0f, 0.1f, 100.0f);   My triangle vertices have these coordinates: v1(-1, -1, 1) v2(1, -1, 1) v3(0, 1, 1)   I multiply the vertices with the matrix like this: gl_Position = projectionMatrix * vec4(in_Position.x, in_Position.y, in_Position.z, 1.0);   I am also a bit confused how the projection matrix works. After reading this tutorial I compared the matrix shown there with the one in glm and they differ slightly. For example glm uses the w component of the vector to store -z instead of +z. Doesn't this flip the signs of the x and y components after perspective divide? I also manually multiplied vectors with zNear(0.1f) and zFar(100.0f) as their z component. I think the multiplication should give values that become -1 and 1 after perspective divide but it doesn't.   glm::vec4 v1(-1.0f, -1.0f, 0.1f, 1.0f); v1 = projectionMatrix * v1;        // v1 = ( -1.810660, -2.414213, -0.300400, -0.100000)   glm::vec4 v2(1.0f, -1.0f, 100.0f, 1.0f); v2 = projectionMatrix * v2;       // v2 = ( 1.810660, -2.414213, -100.400406, -100.000000)   Hopefully someone can help me.
  15. 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.