Jump to content
  • Advertisement

Ahl

Member
  • Content count

    144
  • Joined

  • Last visited

Everything posted by Ahl

  1. I've been searching around, looking for a way to generate, write too and flip between console buffers.  The best I've found consists of simply building a giant string and Console.Write()ing it all in one go or generating an array of class objects that contain the character, X and Y position and a single Color attribute and pushing it to a console buffer handle.  Asside from the fact that that single color attribute only covers text color and not background color, it seems like there should be an easier way to do this.   Thoughts?
  2. Hey all,   I'm writing a Mediator class in C++ and am having trouble with a part of it.  I've done this before in C# (which was really easy) and trying to do the same thing in C++ is kicking my butt.  I have a std::map that contains a std::string for the key and a std::vector<> for the value.  The idea being any number of functions/methods can be registered under any number of keys and all said functions/methods can be invoked with one call.   I'm having two primary problems:  1, I can't figure out how to get both functions AND class methods into the std::map.  Currently I'm using: typedef std::function<void(void* Object)> FunctionDef; I have a Register method in my Mediator class that takes a std::string for the key and a FunctionDef for the value.  So far this works fine for functions but I can't get it to work with class methods.   And 2;  I have an Unregister method in my Mediator class that takes the same inputs and removes the appropriate function from the appropriate key.  Or, at least, it's supposed too.  template<class T> static void Unregister(std::string Key, T Function) { if (MediatorMap.count(Key) != 0) { FunctionDef Func = Function; for (std::vector<FunctionDef>::iterator It = MediatorMap[Key].begin(); It != MediatorMap[Key].end(); ++It) { if ((*It).target_type().name() == Func.target_type().name()) { std::cout << "1" << std::endl; std::cout << (*It).target<T>() << " == " << Func.target<T>() << " ?? \n"; if ((*It).target<T>() == Func.target<T>()) { std::cout << "2" << std::endl; } } } if (MediatorMap[Key].size() == 0) { MediatorMap.erase(MediatorMap.find(Key)); } } } This is basicaly just testing code ATM.  The problem seems to be, and mind you I can only test this with functions, that whatever info is provided by .target<T>() (I'm guessing the pointer) never matches, even when sending the same function.   I admit to not really being sure of what I'm doing.  This is new territory for me and my C++ is a little rusty.  Any help would be appreciated.
  3. Hey all,   The question isn't so much how to do it, I've figured that out, it's how you unbind a VBO and why having a bound VBO seems to block the usage of Vertex Arrays?   I have the following code in my rendering function:   if (VBO_Support) { glVertexPointer( 3, GL_FLOAT, sizeof(MyVertex), (GLvoid*) (*RenderListIT)->RenderMesh->VBO_Offset ); glNormalPointer( GL_FLOAT, sizeof(MyVertex), (GLvoid*)((*RenderListIT)->RenderMesh->VBO_Offset+sizeof(float)*3) ); glTexCoordPointer( 2, GL_FLOAT, sizeof(MyVertex), (GLvoid*)((*RenderListIT)->RenderMesh->VBO_Offset+sizeof(float)*6) ); glColorPointer( 3, GL_FLOAT, sizeof(MyVertex), (GLvoid*)((*RenderListIT)->RenderMesh->VBO_Offset+sizeof(float)*8) ); } else { glVertexPointer( 3, GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->x ); glNormalPointer( GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->nx ); glTexCoordPointer( 2, GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->u ); glColorPointer( 3, GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->a ); } And I have a toggle that changes VBO_Support back and forth.  When I have a VBO bound (glBindBufferARB()) and VBO_Support = true everything runs fine but none of my objects display.  Why is that?   Thanks in advance.
  4. Ive seen examples using gluLookAt(), glOrtho(), gluOrtho2d(), etc. One example I saw said all you need is to just use glTranslate(). So what is presently the best, easiest method to achieve this effect? Thanks in advance.
  5. Alright, then I need to figure out what I'm doing wrong. I have the following: void RenderScene() { std::sort (RenderList.begin(), RenderList.end(), SortRenderList); glMatrixMode(GL_MODELVIEW); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0f, 1.0f, 1.0f); glLoadIdentity(); glTranslatef(MyCamera->GetPosX(), MyCamera->GetPosY(), MyCamera->GetPosZ()); uintptr_t LastKey = (uintptr_t)-1; uintptr_t LastMeshKey = (uintptr_t)-1; uintptr_t LastTexKey = (uintptr_t)-1; uintptr_t LastMatKey = (uintptr_t)-1; for (RenderListIT = RenderList.begin(); RenderListIT != RenderList.end(); RenderListIT++) { glPushMatrix(); glLoadIdentity(); glTranslatef((*RenderListIT)->Position->x, (*RenderListIT)->Position->y, (*RenderListIT)->Position->z); glScalef ((*RenderListIT)->Scale->x, (*RenderListIT)->Scale->y, (*RenderListIT)->Scale->z); glRotatef ((*RenderListIT)->Rotation->x, 1.0f, 0.0f, 0.0f); glRotatef ((*RenderListIT)->Rotation->y, 0.0f, 1.0f, 0.0f); glRotatef ((*RenderListIT)->Rotation->z, 0.0f, 0.0f, 1.0f); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glEnable(GL_LIGHTING); if ((*RenderListIT)->Key != LastKey) { LastKey = (*RenderListIT)->Key; DrawCalls++; if ((*RenderListIT)->RenderMesh->Materials->Key != LastMatKey) { LastMatKey = (*RenderListIT)->RenderMesh->Materials->Key; if ((*RenderListIT)->RenderMesh->HasMaterials == true) { glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (*RenderListIT)->RenderMesh->Materials->Diffuse ); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (*RenderListIT)->RenderMesh->Materials->Specular ); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (*RenderListIT)->RenderMesh->Materials->Ambient ); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (*RenderListIT)->RenderMesh->Materials->Emission ); } } if ((*RenderListIT)->RenderMesh->Key != LastMeshKey) { LastMeshKey = (*RenderListIT)->RenderMesh->Key; if (VBO_Support) { glVertexPointer( 3, GL_FLOAT, sizeof(MyVertex), (GLvoid*) (*RenderListIT)->RenderMesh->VBO_Offset ); glNormalPointer( GL_FLOAT, sizeof(MyVertex), (GLvoid*)((*RenderListIT)->RenderMesh->VBO_Offset+sizeof(float)*3) ); glTexCoordPointer( 2, GL_FLOAT, sizeof(MyVertex), (GLvoid*)((*RenderListIT)->RenderMesh->VBO_Offset+sizeof(float)*6) ); glColorPointer( 3, GL_FLOAT, sizeof(MyVertex), (GLvoid*)((*RenderListIT)->RenderMesh->VBO_Offset+sizeof(float)*8) ); } else { glVertexPointer( 3, GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->x ); glNormalPointer( GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->nx ); glTexCoordPointer( 2, GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->u ); glColorPointer( 3, GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->a ); } } if ((*RenderListIT)->RenderMesh->Active->Key != LastTexKey) { LastTexKey = (*RenderListIT)->RenderMesh->Active->Key; if ((*RenderListIT)->RenderMesh->HasTexture == true) { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, (*RenderListIT)->RenderMesh->Active->TextureHandle); } else { glDisable(GL_TEXTURE_2D); } } } glDrawArrays(GL_TRIANGLES, 0, (*RenderListIT)->RenderMesh->NumVerticies); glPopMatrix(); } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisable(GL_LIGHTING); } And it's not moving the screen. HELP!
  6. I understand the camera class. My question is what OpenGL commands to use.
  7. I haven't been able to find one. I tried writing one myself which proved to be an exercise in futility. Assimp isn't incredibly difficult for simply loading models and once you get the wrapper done you can use any model type Assimp supports.
  8. Having trouble getting my VBOs to work. No compiler errors. No run time errors. Just nothing showing up on the screen. VA's work though. I'm wondering if my implementation is the issue. I'll try to explain as best I can... All the model information gets stored in the following: struct MyVertex { float x, y, z; // Vertex float nx, ny, nz; // Normal float u, v; // Texcoords float a, r, g, b; // Color float padding[4]; }; This is how I load the VBO: for (Itter = Mesh_Multimap.begin(); Itter != Mesh_Multimap.end(); ++Itter) { VBO_Size += (*Itter).second->NumVerticies*sizeof(MyVertex); } glGenBuffersARB( 1, &VBO_ID ); glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBO_ID ); glBufferDataARB( GL_ARRAY_BUFFER_ARB, VBO_Size, NULL, GL_STATIC_DRAW_ARB ); for (VBO_Vector_IT = VBO_Vector.begin(); VBO_Vector_IT != VBO_Vector.end(); ++VBO_Vector_IT) { glBufferSubDataARB( GL_ARRAY_BUFFER_ARB, VBO_Offset, (*VBO_Vector_IT)->NumVerticies*sizeof(MyVertex), (*VBO_Vector_IT)->VertexData ); (*VBO_Vector_IT)->VBO_Offset = VBO_Offset; VBO_Offset += (*VBO_Vector_IT)->NumVerticies*sizeof(MyVertex); } Rendering: glVertexPointer( 3, GL_FLOAT, 0, &(*RenderListIT)->RenderMesh->VBO_Offset ); glNormalPointer( GL_FLOAT, 0, &(*RenderListIT)->RenderMesh->VBO_Offset ); glTexCoordPointer( 2, GL_FLOAT, 0, &(*RenderListIT)->RenderMesh->VBO_Offset ); That's the point that's giving me pause. When just rendering with VAs I need to do the following: glVertexPointer( 3, GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->x ); glNormalPointer( GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->nx ); glTexCoordPointer( 2, GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->u ); glColorPointer( 3, GL_FLOAT, sizeof(MyVertex), &(*RenderListIT)->RenderMesh->VertexData->a ); I had to specify what part of the struct to pull the appropriate data from. Am I supposed to be doing this with the VBOs somehow? What am I missing?
  9. Ahl

    VBO help...

    No. If vertex data starts at offset 0 (the most common case in that scenario), then normal data starts at offset 0 + sizeof(float) * 3 and so on. Otherwise your normals are always your positions of the same vertex, your texture coordinates are just your (x, y) coordinates of the same vertex and your colors are the (x, y, z, nx) coordinates of the same vertex. Setting the stride to sizeof(MyVertex) was correct but only half the problem. [/quote] Boom! That did it. Thank you very much!
  10. Ahl

    VBO help...

    struct MyVertex { float x, y, z; // Vertex float nx, ny, nz; // Normal float u, v; // Texcoords float a, r, g, b; // Color float padding[4]; }; Keep in mind that this is my data structure. An array of this is what is being loaded into the buffer, so the offset will be the same for all of the vertexs/normals/texcoords/etc per model. I figured out another problem. I had the stride set to 0. I set it to sizeof(MyVertex) and now my model is coming up properly. The textures are still wonky though. It's doing now what it was doing before when we figured out I had to do the whole &(*RenderListIT)->RenderMesh->VertexData->x bit.
  11. Ahl

    VBO help...

    Yeah, still getting garbled models, even when just loading one. I have this sneaky suspician that it has to do with not being able (or not know how) to specift what part of my struct each pointer needs to be using...
  12. Ahl

    VBO help...

    Ooh, good catch. Didn't even see that. I converted it over from where I needed it for the VAs and forgot about that. Thank you. Stuff is showing up now but it's all scrambled...
  13. I'm trying to generate a unique key from the pointers of various objects for the sake of sorting by catagory, sub catagory, etc. They key itself will be 32bit. For the sake of this question let's say I have a Car object made up of various Make, Model, Color, Package objects. That Car object is put into a Vector and sorted so that all the given catagories are lumped together appropriately. If I only have a total of 8 Makes, and the Make is our most important catagory to sort by, then I use the 4 most significant bits of our key. If I have 100 models per make thats the next 7 bits. 20 colors per model is the next 5 bits. So on. Where my question comes in is this. If I'm using a 32 pointer for my Make, Model, Color, Package objects, I need to somehow reduce that size down to the size of associated bits in the key. So for my Make bits I need to reduce the size of my Make pointer down to 4 bits and still maintain some level of integrity in regards to it's uniqueness. For obvious reseaons I can't have my Ford and Chevy come out the same at 4 bits. The method I've been playing with so far has looked something like this: I take bottom 16 bits of my pointer and AND them together with my top 16 bits. So: TempID = MakeID & 0x000fff TempID <<= 16 MakeId &= TempID At this point it seems to me (and please correct me if I'm wrong) that there is only one other pointer that would give me the same result and the statistical probability of getting that pointer would be incredible minute. But I need it to be 4 bits and not 16. So I can do that two more times. What I'm worried about is the ability to maintain the unique-ness and how important that unique-ness is in terms of the statistical possibility of getting identicle results from another pointer. I hope this all makes sense. It's giving me a headache just thinking about it. lol.
  14. But would it be fast? Sorting by a single number is really fast.
  15. So hashing is probably not the right word to use. I'll try to explain this in regards to my needs: I'm writing the graphics handling code for my project. I have a vector of RenderObjects. Each RenderObject has pointer to a Model, Texture, Material and Shader object which are all stored in another container. It also has a key that the vector is sorted by. The Key is made up of some form of unique identifier from each of those 4 objects with the Model identifier in the most significant bits followed by Material, Texture then Shader. The example I was following (which I cannot find now, should have book marked it) used various bit manipulations of the pointer to generate this key. The result, when the vector is sorted using this key, has all like models/textures/materials/shaders lumped together to minimize OpenGL calls. No need to load a model or bind a texture that's already loaded/bound, right?
  16. Using glRasterPosxf() to position my text on my screen. I'm looking for a simple, reliable way to maintain text position despite screen resolution... Help? Thanks in advance.
  17. Hey all, I'm loading a model with AssImp and using DevIL for the textures. I'm using models from Turbosquid for testing and I'm having some troubles. First I'll start with asking what exactly I'm supposed to do with the following? The model is made up of a single Mesh, so what do I do with this texture? Thanks in advance.
  18. That did it. It's working now. Awesome, thank you.
  19. struct MyVertex { float x, y, z; // Vertex float nx, ny, nz; // Normal float u, v; // Texcoords float a, r, g, b; // Color float padding[4]; }; That's my VertexData
  20. Loading the texture: GLuint loadImage(const char* theFileName) { ILuint imageID; // Create an image ID as a ULuint GLuint textureID = 0; // Create a texture ID as a GLuint ILboolean success; // Create a flag to keep track of success/failure ILenum error; // Create a flag to keep track of the IL error state ilGenImages(1, &imageID); // Generate the image ID ilBindImage(imageID); // Bind the image success = ilLoadImage((ILstring)theFileName); // Load the image file // If we managed to load the image, then we can start to do things with it... if (success) { // If the image is flipped (i.e. upside-down and mirrored, flip it the right way up!) ILinfo ImageInfo; iluGetImageInfo(&ImageInfo); if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT) { iluFlipImage(); } // Convert the image into a suitable format to work with // NOTE: If your image contains alpha channel you can replace IL_RGB with IL_RGBA success = ilConvertImage(ilGetInteger(IL_IMAGE_FORMAT), IL_UNSIGNED_BYTE); // Quit out if we failed the conversion if (!success) { error = ilGetError(); std::cout << "Image conversion failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl; //exit(-1); } // Generate a new texture glGenTextures(1, &textureID); // Bind the texture to a name glBindTexture(GL_TEXTURE_2D, textureID); // Set texture clamping method glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // Set texture interpolation method to use linear interpolation (no MIPMAPS) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Specify the texture specification glTexImage2D(GL_TEXTURE_2D, // Type of texture 0, // Pyramid level (for mip-mapping) - 0 is the top level ilGetInteger(IL_IMAGE_BPP), // Image colour depth ilGetInteger(IL_IMAGE_WIDTH), // Image width ilGetInteger(IL_IMAGE_HEIGHT), // Image height 0, // Border width in pixels (can either be 1 or 0) ilGetInteger(IL_IMAGE_FORMAT), // Image format (i.e. RGB, RGBA, BGR etc.) IL_UNSIGNED_BYTE, // Image data type ilGetData()); // The actual image data itself glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); } else // If we failed to open the image file in the first place... { error = ilGetError(); std::cout << "Image load failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl; std::cout << theFileName << std::endl; exit(-1); } ilDeleteImages(1, &imageID); // Because we have already copied image data into texture data we can release memory used by image. //std::cout << "Texture creation successful." << std::endl; return textureID; // Return the GLuint to the texture so you can use it! } Draw the scene: void RenderScene() { std::sort (RenderList.begin(), RenderList.end(), SortRenderList); glMatrixMode(GL_MODELVIEW); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0f, 1.0f, 1.0f); Material *TempMat = NULL; MyVertex *TempVerts = NULL; glDisable(GL_TEXTURE_2D); for (RenderListIT = RenderList.begin(); RenderListIT != RenderList.end(); RenderListIT++) { glLoadIdentity(); glTranslatef((*RenderListIT)->Position->x, (*RenderListIT)->Position->y, (*RenderListIT)->Position->z); glScalef ((*RenderListIT)->Scale->x, (*RenderListIT)->Scale->y, (*RenderListIT)->Scale->z); glRotatef ((*RenderListIT)->Rotation->x, 1.0f, 0.0f, 0.0f); glRotatef ((*RenderListIT)->Rotation->y, 0.0f, 1.0f, 0.0f); glRotatef ((*RenderListIT)->Rotation->z, 0.0f, 0.0f, 1.0f); if (TempMat == NULL || TempMat != (*RenderListIT)->RenderMesh->Materials) { TempMat = (*RenderListIT)->RenderMesh->Materials; glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (*RenderListIT)->RenderMesh->Materials->Diffuse ); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (*RenderListIT)->RenderMesh->Materials->Specular ); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (*RenderListIT)->RenderMesh->Materials->Ambient ); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (*RenderListIT)->RenderMesh->Materials->Emission ); } if (TempVerts == NULL || TempVerts != (*RenderListIT)->RenderMesh->VertexData) { TempVerts = (*RenderListIT)->RenderMesh->VertexData; if ((*RenderListIT)->RenderMesh->HasVertexs == true) { glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer( 3, GL_FLOAT, sizeof(MyVertex), (*RenderListIT)->RenderMesh->VertexData ); } else { glDisableClientState(GL_VERTEX_ARRAY); } if ((*RenderListIT)->RenderMesh->HasNormals == true) { glEnable(GL_LIGHTING); glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer( GL_FLOAT, sizeof(MyVertex), (*RenderListIT)->RenderMesh->VertexData ); } else { glDisable(GL_LIGHTING); glDisableClientState(GL_NORMAL_ARRAY); } if ((*RenderListIT)->RenderMesh->HasTexcoords == true) { if ((*RenderListIT)->RenderMesh->HasTexture == true) { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, (*RenderListIT)->RenderMesh->Active->TextureHandle); } glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer( 2, GL_FLOAT, sizeof(MyVertex), (*RenderListIT)->RenderMesh->VertexData ); } else { glDisable(GL_TEXTURE_2D); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } if ((*RenderListIT)->RenderMesh->HasColors) { glEnableClientState(GL_COLOR_ARRAY); glColorPointer( 3, GL_FLOAT, sizeof(MyVertex), (*RenderListIT)->RenderMesh->VertexData ); } else { glDisableClientState(GL_COLOR_ARRAY); } } glDrawArrays(GL_TRIANGLES, 0, (*RenderListIT)->RenderMesh->NumVerticies); } //RenderList.clear(); glutSwapBuffers(); } the model http://www.turbosquid.com/FullPreview/Index.cfm/ID/526437
  21. Right, they should have correct texture coordinates but when I apply the texture I get the following: This would be the second part of my question. The texture seems to be shrunk considerably and then the black edges get clamped all the way around instead of being stretched appropriately.
  22. I'm going to comment based solely on the name of the post, without having actually read any of the content there-in: Couldn't you take a snap-shot of a section of the information in a given model or image and hard code your project to look for it? An example: I have a bitmap called Bitmap or a model called Model. (Very original I know.) I take the first 100 bits of information as a key hard coded into my game so that when I load my assets I check them against my keys. If they don't match I know they've been altered. ?
  23. I'm following http://blog.andreask...sh-in-code.html and have translated it into C/C++ (the example code is in C#.) What I get, depending on the recusion level obviously, is this: Clearly it's generating a sphere and, just as clearly, it's not an Icosphere. The faces are not triangular. From the inside looking out: I've been going over my code, over and over, but I can't find where my mistake is. Here is the code: #include <iostream> #include <vector> #include <map> #ifndef PROCEDURAL_ICOSPHERE #define PROCEDURAL_ICOSPHERE struct Point3D { float x, y, z; Point3D(float x = 0.0, float y = 0.0, float z = 0.0): x(x), y(y), z(z) {}; }; struct MeshGeometry3D { std::vector<Point3D> Vertexs; std::vector<Point3D>::iterator VertexIT; std::vector<int> TriangleIndices; std::vector<int>::iterator TriangleIndiceIT; Point3D* VertexArray; int VertexArraySize; void GenerateVertexArray() { VertexArraySize = 0; VertexArray = new Point3D[TriangleIndices.size()-1]; for (TriangleIndiceIT = TriangleIndices.begin(); TriangleIndiceIT != TriangleIndices.end(); ++TriangleIndiceIT) { VertexArray[VertexArraySize] = Vertexs[(*TriangleIndiceIT)]; VertexArraySize++; } } }; class IcoSphereCreator { private: struct TriangleIndices { int v1, v2, v3; TriangleIndices(int v1, int v2, int v3): v1(v1), v2(v2), v3(v3){}; }; MeshGeometry3D Geometry; std::vector<TriangleIndices> Faces; std::vector<TriangleIndices>::iterator FaceIT; std::map<int, int> MiddlePointIndex; std::map<int, int>::iterator MiddlePointIndexIT; int Index; int AddVertex( Point3D p) { float length = sqrt( p.x*p.x + p.y*p.y + p.z*p.z); Geometry.Vertexs.push_back( Point3D( p.x/length, p.y/length, p.z/length) ); return Index++; } void AddFace( int vert1, int vert2, int vert3) { Faces.push_back( TriangleIndices(vert1, vert2, vert3) ); } int GetMiddlePoint(int p1, int p2) { // first check if we have it already bool FirstIsSmaller = p1 < p2; int SmallerIndex = FirstIsSmaller ? p1 : p2; int GreaterIndex = FirstIsSmaller ? p2 : p1; int Key = (SmallerIndex << 16) + GreaterIndex; if (MiddlePointIndex.count(Key) > 0) { //std::cout << "yes" << std::endl; return MiddlePointIndex.find(Key)->second; } // not in cache, calculate it Point3D point1 = Geometry.Vertexs[p1]; Point3D point2 = Geometry.Vertexs[p2]; Point3D middle = Point3D((point1.x + point2.x) / 2, (point1.y + point2.y) / 2, (point1.z + point2.z) / 2); // add vertex makes sure point is on unit sphere int i = AddVertex(middle); // store it, return index MiddlePointIndex.insert(std::pair<int, int>(Key, i)); return i; } public: IcoSphereCreator() { Index = 0; } MeshGeometry3D Create(int RecursionLevel) { float t = (float)((1.0 + sqrt(5.0)) / 2.0); AddVertex( Point3D(-1.0, t, 0.0)); AddVertex( Point3D( 1.0, t, 0.0)); AddVertex( Point3D(-1.0, -1, 0.0)); AddVertex( Point3D( 1.0, -t, 0.0)); AddVertex( Point3D(0.0, -1, t)); AddVertex( Point3D(0.0, 1, t)); AddVertex( Point3D(0.0, -1, -t)); AddVertex( Point3D(0.0, 1, -t)); AddVertex( Point3D(t, 0.0, -1)); AddVertex( Point3D(t, 0.0, 1)); AddVertex( Point3D(-t, 0.0, -1)); AddVertex( Point3D(-t, 0.0, 1)); // 5 faces around point 0 AddFace( 0, 11, 5 ); AddFace( 0, 5, 1 ); AddFace( 0, 1, 7 ); AddFace( 0, 7, 10); AddFace( 0, 10, 11); // 5 adjacent faces AddFace( 1, 5, 9 ); AddFace( 5, 11, 4 ); AddFace( 11, 10, 2 ); AddFace( 10, 7, 6 ); AddFace( 7, 1, 8 ); // 5 faces around point 3 AddFace( 3, 9, 4 ); AddFace( 3, 4, 2 ); AddFace( 3, 2, 6 ); AddFace( 3, 6, 8 ); AddFace( 3, 8, 9 ); // 5 adjacent faces AddFace( 4, 9, 5 ); AddFace( 2, 4, 11); AddFace( 6, 2, 10); AddFace( 8, 6, 7 ); AddFace( 9, 8, 1 ); for (int i = 0; i < RecursionLevel; i++) { std::vector<TriangleIndices> Faces2; std::vector<TriangleIndices>::iterator Face2IT; for (FaceIT = Faces.begin(); FaceIT != Faces.end(); ++FaceIT) { int a = GetMiddlePoint((*FaceIT).v1, (*FaceIT).v2); int b = GetMiddlePoint((*FaceIT).v2, (*FaceIT).v3); int c = GetMiddlePoint((*FaceIT).v3, (*FaceIT).v1); Faces2.push_back( TriangleIndices((*FaceIT).v1, a, c)); Faces2.push_back( TriangleIndices((*FaceIT).v2, b, a)); Faces2.push_back( TriangleIndices((*FaceIT).v3, c, b)); Faces2.push_back( TriangleIndices(a, b, c)); } Faces = Faces2; } for (FaceIT = Faces.begin(); FaceIT != Faces.end(); ++FaceIT) { Geometry.TriangleIndices.push_back((*FaceIT).v1); Geometry.TriangleIndices.push_back((*FaceIT).v2); Geometry.TriangleIndices.push_back((*FaceIT).v3); } return Geometry; } }; #endif Thanks in advance.
  24. This is a look from the inside of my sphere outward. The dots are my surface normals. In some parts of the sphere they appear to be correct, in others some faces have 2, some none. using http://devmaster.net/forums/topic/1065-calculating-normals-of-a-mesh/ as a reference I hammered out the following: struct MeshGeometry3D { std::vector<Vec3> Vertexs; std::vector<Vec3>::iterator VertexIT; std::vector<int> TriangleIndices; std::vector<int>::iterator TriangleIndiceIT; Vec3* VertexArray; Vec3* VertexNormalArray; int VertexArraySize; int VertexNormalArraySize; void FinalizeMesh() { VertexArraySize = 0; VertexNormalArraySize = 0; VertexArray = new Vec3[TriangleIndices.size()-1]; VertexNormalArray = new Vec3[TriangleIndices.size()]; for (TriangleIndiceIT = TriangleIndices.begin(); TriangleIndiceIT != TriangleIndices.end(); ++TriangleIndiceIT) { VertexArray[VertexArraySize] = Vertexs[(*TriangleIndiceIT)]; VertexArraySize++; } for (TriangleIndiceIT = TriangleIndices.begin(); TriangleIndiceIT != TriangleIndices.end(); ) //++TriangleIndiceIT { Vec3 p1 = Vertexs[(*TriangleIndiceIT++)]; Vec3 p2 = Vertexs[(*TriangleIndiceIT++)]; Vec3 p3 = Vertexs[(*TriangleIndiceIT++)]; Vec3 u = p2 - p1; Vec3 v = p3 - p1; Vec3 Normal = u.CrossProduct(v); Normal.Normalize(); VertexNormalArray[VertexNormalArraySize++] = Normal; VertexNormalArray[VertexNormalArraySize++] = Normal; VertexNormalArray[VertexNormalArraySize++] = Normal; } for( int i = 0; i < VertexArraySize; ++i ) { for( int j = 0; j < VertexNormalArraySize; ++j ) VertexNormalArray += VertexNormalArray[j]; VertexNormalArray /= VertexNormalArraySize; VertexNormalArray.Normalize(); } } };
  25. Hmmm... But the following also works: for (FaceIT = Faces.begin(); FaceIT != Faces.end(); ++FaceIT) { Geometry.TriangleIndices.push_back((*FaceIT).v1); Geometry.TriangleIndices.push_back((*FaceIT).v3); Geometry.TriangleIndices.push_back((*FaceIT).v2); } return Geometry; I think I need to check my windings... On a possibly related/unrelated note my surface (working up to Vertex) normals are coming out all wonky... Thoughts anyone?
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!