Advertisement

Please help me simplify this code

Started by December 28, 2001 09:10 AM
27 comments, last by Keermalec 23 years, 1 month ago
Thanks Frank, I'm not sure I understand the utility of storing my display list geometry using a pointer, but maybe I am missing something here.

Here is how I have currently solved my display list problem, in case anyone is in a similar fix. I wont even post what I had before as it was over a hundred lines long!

    // Each display list will be named models[x]int models[5];// Vertices for drawing modelsstatic float vertices[12][3] ={	{ 1, 0, 0 },		// Vertex 0	{ 1, 0, -1 },		// Vertex 1	{ 0, 0, -1 },		// Vertex 2	{ 5, 0, 0 },		// Vertex 3	{ 5, 0, -1 },		// Vertex 4	{ 1, 0, -1 },		// Vertex 5	{ 0, 0, -5 },		// Vertex 6	{ 1, 0, -5 },		// Vertex 7	{ 1, 0, -1 },		// Vertex 8	{ 5, 0, -1 },		// Vertex 9	{ 5, 0, -5 },		// Vertex 10	{ 1, 0, -5 }		// Vertex 11};// Indices refering to the above list of vertices// Note the first entry indicates the number of useful entry pairs following// Zeros have been added at the end to make them all 17 entries longint indices[5][17] ={	{3,0,7,7,10,10,3,0,0,0,0,0,0,0,0,0,0},		// Display List 1	{2,0,8,9,3,0,0,0,0,0,0,0,0,0,0,0,0},		// Display List 2	{4,0,7,7,11,3,4,4,5,0,0,0,0,0,0,0,0},		// Display List 3	{5,6,11,0,1,1,2,3,4,4,5,0,0,0,0,0,0},		// Display List 4	{8,0,1,1,2,3,4,4,5,6,7,7,8,9,10,10,11}		// Display List 5};void makeDisplayList ( int &list, int array[] ){	// Create display list	list = glGenLists(1);	glNewList(list, GL_COMPILE);		// Draw stuff		for ( int i = 0; i < array[0]; i++ )		{			glBegin(GL_LINES);				glVertex3fv( vertices[array[i*2+1]] );				glVertex3fv( vertices[array[i*2+2]] );			glEnd();		}	glEndList();}// At initialisation, make display listsfor ( int i = 0; i < 5; i++ ) makeDisplayList ( models[i], indices[i] );// At rendering time, call themfor ( int i = 0; i < 5; i++ ) glCallList ( models[i] );            


I know I ould have made it a bit simpler using glDrawElements but I want ît compatible with opengl 1.1, as stupid M$ is still shipping OpenGL 1.1 with Windows and I want it to work on anyone's system.

This works beautifuly for me. Thanks to all those who helped!

Keer

Edited by - Keermalec on December 31, 2001 1:07:31 PM
look up sprintf
something like this

char szbuffer[1024];
sprintf(szBuffer,"list%d",i);

should do the trick, the string will be stored in szBuffer.



Edited by - kwizatz on December 31, 2001 12:28:46 PM
Advertisement
Kwisatz, the question is: how do I print out that buffer to the compiler and use it as a line of code?
You cant since C/C++ is a compiled language. With clever use of the preprocessor you can do something like this, however it wonnt be dynamic. The best way to do this is with an array.


list = make x lists

for i from 0 to x
glCallList(list)

Look into linked lists (nothing to do with openGZL lists) as well. They you could get somewhere with them.

If you really want to do what you suggest, make a hash table where eacch entry stores the opengl list pointer

If youre going to use C/C++ you gotta get used to the fact that its a compiled language. sounds like you are used to programming in an interpreted language (java-script comes to mind, perl?) where what you are suggesting is possible.
The reason you can''t do it the original way you wanted is not because C++ is compiled (VB is compiled and this can be done in it... although I would never use it because it would be really slow)

It is possible that you could write a function which would call another function given the functions name as a string and the arguments as a string, but for the amount of work arounds and macros you would have to write it wouldn''t be worth it. VB does it by storing most identifiers in the exe, but when C++ is compiled, those identifiers are lost.

Trying is the first step towards failure.
Trying is the first step towards failure.
I think it is weird that such a question resulted in two pages of replies. And still growing...
Advertisement
@Keermalec

like i said,in NeHe''s & Giuseppe D''Agata tutorial no. 17
they do this:

glCallLists(strlen(string),GL_BYTE,string);
where string is the string to be printed to the screen.

of course you could also use a pre-defined array,
i was using a pointer structure because it is more flexible.

if you want to call a sub-section you might do:

glCallLists(NUMBER_TO_CALL, DATA_SIZE, list+START_INDEX);

IMPORTANT: make sure that NUMBER_TO_CALL does not go over the boudaries of your list.

this should do what you want and is more flexible.
Hello again, its true you cant run C code as a script because, C is not an scripting language, however, you could embed TCL in C++, which in turn uses TK and OpenGL, this way you can achieve what you want, check http://www.scriptics.com/ and http://www.activestate.com/ if you are interested

You could do it this way... though why u would want to is beyond me . It should be ok with any compiler that supports templates properly. I tried templatised functions instead of a class but my compiler didn''t like it.

  #include <iostream>void g(int i){	std::cout << i << " ";}template<int num>class list{ public:	void func(){		g(num);		l.func();			} private:	list<num - 1> l;};template<>class list<0>{ public:	void func(){		g(0);	}};void main(){	list<6> l;	l.func();}  

This topic is closed to new replies.

Advertisement