Archived

This topic is now archived and is closed to further replies.

nitzan

VAR help

Recommended Posts

So I am trying to do draw the Lesson 07 cube with the VAR extension. I changed the code to draw a triangle-strip cube instead of the normal cube like this:
  
GLfloat box_vertex[24];
GLfloat box_normal[24];
GLfloat box_color[24];
GLfloat box_tex[16];
GLubyte box_indices[18];
  
Set up the arrays with the right data.
  
	box_vertex[0] = -1.0f; // x 

	box_vertex[1] = -1.0f; // y

	box_vertex[2] =  1.0f; // z


	box_vertex[3] = -1.0f;
	box_vertex[4] =  1.0f;
	box_vertex[5] =  1.0f;

	box_vertex[6] =  1.0f;
	box_vertex[7] =  1.0f;
	box_vertex[8] =  1.0f;

	box_vertex[9] =   1.0f;
	box_vertex[10] = -1.0f;
	box_vertex[11] =  1.0f;

	box_vertex[12] =  1.0f;
	box_vertex[13] = -1.0f;
	box_vertex[14] = -1.0f;

	box_vertex[15] =  1.0f;
	box_vertex[16] =  1.0f;
	box_vertex[17] = -1.0f;

	box_vertex[18] = -1.0f;
	box_vertex[19] =  1.0f;
	box_vertex[20] = -1.0f;

	box_vertex[21] = -1.0f;
	box_vertex[22] = -1.0f;
	box_vertex[23] = -1.0f;



	box_normal[0] = -0.71f;
	box_normal[1] =     0;
	box_normal[2] =  0.71f;

	box_normal[3] = -0.71f;
	box_normal[4] =     0;
	box_normal[5] =  0.71f;

	box_normal[6] =  0.71f;
	box_normal[7] =     0;
	box_normal[8] =  0.71f;

	box_normal[9] =  0.71f;
	box_normal[10] =    0;
	box_normal[11] = 0.71f;

	box_normal[12] =  0.71f;
	box_normal[13] =     0;
	box_normal[14] = -0.71f;

	box_normal[15] =  0.71f;
	box_normal[16] =     0;
	box_normal[17] = -0.71f;

	box_normal[18] = -0.71f;
	box_normal[19] =     0;
	box_normal[20] = -0.71f;

	box_normal[21] = -0.71f;
	box_normal[22] =     0;
	box_normal[23] = -0.71f;

	box_color[0] = 0.478f;
	box_color[1] = 0.341f;
	box_color[2] = 0.129f;

	box_color[3] = 0.478f;
	box_color[4] = 0.341f;
	box_color[5] = 0.129f;

	box_color[6] = 0.478f;
	box_color[7] = 0.341f;
	box_color[8] = 0.129f;

	box_color[9] = 0.478f;
	box_color[10] = 0.341f;
	box_color[11] = 0.129f;

	box_color[12] = 0.478f;
	box_color[13] = 0.341f;
	box_color[14] = 0.129f;

	box_color[15] = 0.478f;
	box_color[16] = 0.341f;
	box_color[17] = 0.129f;

	box_color[18] = 0.478f;
	box_color[19] = 0.341f;
	box_color[20] = 0.129f;

	box_color[21] = 0.478f;
	box_color[22] = 0.341f;
	box_color[23] = 0.129f;

	box_tex[0] = 0.0f;
	box_tex[1] = 1.0f;

	box_tex[2] = 0.0f;
	box_tex[3] = 0.0f;

	box_tex[4] = 1.0f;
	box_tex[5] = 0.0f;

	box_tex[6] = 1.0f;
	box_tex[7] = 1.0f;

	box_tex[8] = 0.0f;
	box_tex[9] = 1.0f;

	box_tex[10] = 0.0f;
	box_tex[11] = 0.0f;

	box_tex[12] = 1.0f;
	box_tex[13] = 0.0f;

	box_tex[14] = 1.0f;
	box_tex[15] = 1.0f;

	box_indices[0] = 4;
	box_indices[1] = 7;
	box_indices[2] = 3;
	box_indices[3] = 0;
	box_indices[4] = 0;
	box_indices[5] = 1;
	box_indices[6] = 7;
	box_indices[7] = 6;
	box_indices[8] = 4;
	box_indices[9] = 5;
	box_indices[10] = 3;
	box_indices[11] = 2;
	box_indices[12] = 0;
	box_indices[13] = 1;
	box_indices[14] = 1;
	box_indices[15] = 6;
	box_indices[16] = 2;
	box_indices[17] = 5;
  
Draw vertex arrays
  
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);

	glBindTexture(GL_TEXTURE_2D, texture[filter]);

	glVertexPointer(3, GL_FLOAT, 0, box_vertex);
	glColorPointer(3, GL_FLOAT, 0, box_color);
	glTexCoordPointer(2, GL_FLOAT, 0, box_tex);
	glNormalPointer(GL_FLOAT, 0, box_normal);

	glDrawElements(GL_TRIANGLE_STRIP, 18, GL_UNSIGNED_BYTE, box_indices);

	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);
  
Now this all works fine. Now I wanted to change the code to use VAR extension. This is the code I added modified:
  
PFNWGLALLOCATEMEMORYNVPROC wglAllocateMemoryNV = NULL;
PFNGLVERTEXARRAYRANGENVPROC glVertexArrayRangeNV = NULL;
  
I changed this:
  
//GLubyte box_indices[18];

GLubyte *box_indices;
  
and then I *think* i initialized the VAR with this:
  
	glVertexArrayRangeNV = (PFNGLVERTEXARRAYRANGENVPROC)wglGetProcAddress("glVertexArrayRangeNV");
	wglAllocateMemoryNV = (PFNWGLALLOCATEMEMORYNVPROC) wglGetProcAddress("wglAllocateMemoryNV");

	box_indices = (GLubyte*)wglAllocateMemoryNV(18, 0.2f, 0.2f, 0.5f);
    glVertexArrayRangeNV(18*sizeof(GLfloat), box_indices);
  
The code compiles with no errors or warnings, but crashes when I try to run it. What am I missing ? Nitzan ------------------------- www.geocities.com/nitzanw www.scorchedearth3d.net -------------------------

Share this post


Link to post
Share on other sites
Forgot to add that I also have this:


  
glEnableClientState(GL_VERTEX_ARRAY_RANGE_NV);


Nitzan

-------------------------
www.geocities.com/nitzanw
www.scorchedearth3d.net
-------------------------

Share this post


Link to post
Share on other sites
dont alloc the indicees, alloc the vertexdata...

and hoew about some errorchecking? are the functionpointers valid?

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
You missed the point of the VAR extension. You must allocate a chunk of VAR memory for the vertices and not for the indices. Your box_vertex, box_normal, box_color and box_tex arrays have to be copied into the VAR memory. box_indices stays in normal system RAM.

BTW: your code would probably even work (although *very* slow, since VAR would be deactivated and glDrawElements() will read it''s vertices from VAR memory), if you actually initialized the box_indices array... Currently, it contains random data

/ Yann

Share this post


Link to post
Share on other sites
Ok, so I allocated a big chunk of memory:

memory = (GLfloat*)wglAllocateMemoryNV(18, 0.2f, 0.2f, 0.5f);

Then I did a memcopy for the vertices, normals, colors, and texture coordinates:

memcpy(memory, (void*)box_vertex, 24*sizeof(float));
memcpy(memory+24*sizeof(float), (void*)box_normal, 24*sizeof(float));
memcpy(memory+48*sizeof(float), (void*)box_color, 24*sizeof(float));
memcpy(memory+72*sizeof(float), (void*)box_tex, 16*sizeof(float));

Now how do I call:
glVertexPointer(3, GL_FLOAT, 0, box_vertex);
glColorPointer(3, GL_FLOAT, 0, box_color);
glTexCoordPointer(2, GL_FLOAT, 0, box_tex);
glNormalPointer(GL_FLOAT, 0, box_normal);

glDrawElements(GL_TRIANGLE_STRIP, 18, GL_UNSIGNED_BYTE, box_indices);

And still reference my allocated memory instead of box_vertex, box_color, box_tex, and box_normal ?

Thanks,

Nitzan


-------------------------
www.geocities.com/nitzanw
www.scorchedearth3d.net
-------------------------

Share this post


Link to post
Share on other sites
Whops I had a mistake...

memory allocation is actually:

memory = (GLfloat*)wglAllocateMemoryNV(88*sizeof(GLfloat), 0.0f, 0.0f, 1.0f);

and I can see the cube if I call:

glVertexPointer(3, GL_FLOAT, 0, memory);
glColorPointer(3, GL_FLOAT, 0, memory);
glTexCoordPointer(2, GL_FLOAT, 0, memory);
glNormalPointer(GL_FLOAT, 0, memory);

But obviously its getting the wrong data from memory (since the cube textures look funky.

Do I need to interleave the arrays in memory and then set a stride ?

Nitzan

-------------------------
www.geocities.com/nitzanw
www.scorchedearth3d.net
-------------------------

Share this post


Link to post
Share on other sites
OK, first make sure you have an appropriate AGP driver installed, since you are allocating AGP memory.

quote:

memory = (GLfloat*)wglAllocateMemoryNV(18, 0.2f, 0.2f, 0.5f);


Problem: you are only allocating 18 byte of VAR memory for all the vertices, normals, colours and texcoords. This will crash. [Edit: OK, forget it]

Try this:

      

vertex_size = 8*3;
normal_size = 8*3;
colour_size = 8*3;
texcoord_size = 8*2;

global_size = vertex_size + normal_size + colour_size + texcoord_size;

memory = (GLfloat*)wglAllocateMemoryNV(global_size*sizeof(GLfloat), 0.2f, 0.2f, 0.5f);

if( memory==NULL ) {
printf("Couldn't allocate VAR memory !\n");
// insert further error handling code here, or just exit the program

}

GLfloat *vertex_mem = memory;
GLfloat *normal_mem = vertex_mem + vertex_size
GLfloat *colour_mem = normal_mem + normal_size;
GLfloat *texcoord_mem = colour_mem + colour_size;

memcpy(vertex_mem, (void*)box_vertex, vertex_size*sizeof(GLfloat));
memcpy(normal_mem, (void*)box_normal, normal_size*sizeof(GLfloat));
memcpy(colour_mem, (void*)box_color, colour_size*sizeof(GLfloat));
memcpy(texcoord_mem, (void*)box_tex, texcoord_size*sizeof(GLfloat));


glVertexPointer(3, GL_FLOAT, 0, vertex_mem);
glColorPointer(3, GL_FLOAT, 0, colour_mem);
glTexCoordPointer(2, GL_FLOAT, 0, texcoord_mem);
glNormalPointer(GL_FLOAT, 0, normal_mem);

glDrawElements(GL_TRIANGLE_STRIP, 18, GL_UNSIGNED_BYTE, box_indices);


That should work.

/ Yann

[edited by - Yann L on August 18, 2002 2:33:32 PM]

Share this post


Link to post
Share on other sites
Wow, thanks Yann!

It totally works!

Nitzan

-------------------------
www.geocities.com/nitzanw
www.scorchedearth3d.net
-------------------------

Share this post


Link to post
Share on other sites