Advertisement

vector arrays - vertex arrays

Started by April 29, 2005 08:00 PM
10 comments, last by Halibut 19 years, 4 months ago
i have my vertices stored in vectors. i put them in vectors because its alot easier to load that way. now im looking for a way to take those vectors and put them into vertex arrays. so i have 2 questions: 1. do vertex arrays support type vector? 2. if not, whats the easiest way to copy that information into a vertex array? btw hopefully i will be able to put a demo up soon of my engine! it will be very basic but hey, its a conglomerate of 2 years worth of learning off and on.
1. I doubt it. the OGL interface is in C code.

2. My experience with STL Vector is limited, but I would expect the only way would be to copy element by element

Is there a particular reason for not being able to use an array?
Disclaimer: "I am in no way qualified to present advice on any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)"
Advertisement
1) Yes. Use something like &vertices[ 0 ].x for address. But use of normal arrays is sometimes perfered.
2) Create array of same size and either copy one by one or use std::copy
You should never let your fears become the boundaries of your dreams.
Quote: Original post by falkone
Is there a particular reason for not being able to use an array?


okay, this is what works, but is impracticle

GLfloat vertArray[9] = {/*store verts here*/};


i need to create a dynamic array but it never works

GLfloat *vertArray;vertArray = new GLfloat[9];   //resize accordinglyvertArray[0] = 0.0f; //and so on down the list, filling each array value...vertArray[8] = 20.0f;


[Edited by - adam17 on April 30, 2005 1:35:59 PM]
So you mean you have a vector of vertices, not a vector class for your vertex? Use vector.pointer. It's stored as an array internally anyway. Just don't hold on to that or it'll become invalid.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!
Quote: Original post by adam17
Quote: Original post by falkone
Is there a particular reason for not being able to use an array?


okay, this is what works, but is impracticle

GLfloat vertArray[9] = {/*store verts here*/};



Well, if you're only going to have 9 vertices (which is quite limited), there's nothing wrong with using a standard array. You're right, though, that you're going to want a dynamic array for anything useful.

Quote:
i need to create a dynamic array but it never works

GLfloat *vertArray;vertArray = new GLfloat[9];   //resize accordinglyvertArray[0] = 0.0f; //and so on down the list, filling each array value...vertArray[8] = 20.0f;


What about it doesn't work? That code looks fine to me. Is it not drawing properly? Are you sure you're making the call to gl*Pointer and the subsequent call to glDraw* properly?
Without order nothing can exist - without chaos nothing can evolve.
Advertisement
here is how i am defining the arrays:

	vertArray = new GLfloat[9];	vertArray[0] =  0.0; vertArray[1] =  0.0; vertArray[2] =  0.0;	vertArray[3] =-20.0; vertArray[4] = 40.0; vertArray[5] =  0.0;	vertArray[6] = 20.0; vertArray[7] = 40.0; vertArray[8] =  0.0;


here is how im drawing the vertex arrays:

//i can switch between commenting either the vertex arrays, //or drawing the triangles by hand and my program crashes.//if i comment both sections, the program runs fine. //defining the array does not crash the program.//any ideas?glEnableClientState(GL_VERTEX_ARRAY);	glVertexPointer(3, GL_FLOAT, 0, vertArray);	glDrawArrays(GL_TRIANGLES, 0, 3);	glDisableClientState(GL_VERTEX_ARRAY);glBegin(GL_TRIANGLES);	glVertex3f(vertArray[0], vertArray[1], vertArray[2]);	glVertex3f(vertArray[3], vertArray[4], vertArray[5]);	glVertex3f(vertArray[6], vertArray[7], vertArray[8]);	glEnd();
Your code looks fine to me. My Vertex Array code looks almost exactly like that.

Where exactly is the error happening?
the error is occuring when i uncomment

glEnableClientState(GL_VERTEX_ARRAY);glVertexPointer(3, GL_FLOAT, 0, vertArray);glDrawArrays(GL_TRIANGLES, 0, 3);glDisableClientState(GL_VERTEX_ARRAY);

or when i uncomment
glBegin(GL_TRIANGLES);glVertex3f(vertArray[0], vertArray[1], vertArray[2]);glVertex3f(vertArray[3], vertArray[4], vertArray[5]);glVertex3f(vertArray[6], vertArray[7], vertArray[8]);glEnd();
Have you tried it with an array of floats and not GLfloats?

I don't know why that would fix your problem, but that's the only part that differs from the code that I use.

I'm assuming the program compiles and then crashes when it gets to your uncommented code. Are you using any kind of debugger? I've been able to solve many a problem with either gdb or microsoft's debugger.

If all else fails, you can try it the old fashioned way with malloc.

GLfloat *vertArray;//initialize withvertArray=(GLfloat*)malloc(9*sizeof(GLfloat));//free the memory withfree(vertArray);


[Edited by - Halibut on May 16, 2005 3:49:19 AM]

This topic is closed to new replies.

Advertisement