9 Replies - 575 Views - Last Post: 22 January 2011 - 10:30 AM Rate Topic: -----

#1 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

#define question

Posted 20 January 2011 - 05:12 PM

Hi, I'm needing some advice on how #defines work, i'm currently experimenting with openGL VBO's and have my code working fine with just vertex arrays but issues when trying to convert it to VBO usage ( back to the problem... ) I'm wanting to use a #define to tell the compiler where or not to use VBO's so say I have within my code in the areas that use VBO a #ifndef __USE_VBO and in my main.cpp i use #define __USE_VBO it will use the function within the ifndef else it wont, I'm wanding if that is the correct way to do this or does this #define need to be somewhere else. I'll put a example below to explain abit better :-

// main.cpp
#define __USE_VBO_

// in geo.cpp
#ifndef __USE_VBO_
	//Bind the vertex array and set the vertex pointer to point at it
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
    //glVertexPointer(3, GL_FLOAT, 0, 0);
    glVertexAttribPointer((GLint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    
    //Bind the color array
    glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
    //glColorPointer(3, GL_FLOAT, 0, 0);
    glVertexAttribPointer((GLint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffer);
    glVertexAttribPointer((GLint)2, 2, GL_FLOAT, GL_FALSE, 0, 0);

    //Bind the index array
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);

    //Draw the triangles
    glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, 0);
#else
//Bind the vertex array and set the vertex pointer to point at it
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
    //glVertexPointer(3, GL_FLOAT, 0, 0);
    glVertexAttribPointer((GLint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    
    //Bind the color array
    glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
    //glColorPointer(3, GL_FLOAT, 0, 0);
    glVertexAttribPointer((GLint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffer);
    glVertexAttribPointer((GLint)2, 2, GL_FLOAT, GL_FALSE, 0, 0);

    //Bind the index array
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);

    //Draw the triangles
    glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, 0);

#endif



I've tried doing this but it comes with messy results far from what i expected, please can someone advice me on this.
thanks
Dave

Is This A Good Question/Topic? 0
  • +

Replies To: #define question

#2 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: #define question

Posted 20 January 2011 - 05:47 PM

Sorry for posting this topic in the wrong forum, I'm just going to leave this topic and do the neccessary changes by hand. If there was a way to delete this topic I would but unforunatly there isn't an option.
Was This Post Helpful? 0
  • +
  • -

#3 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: #define question

Posted 20 January 2011 - 06:48 PM

What problems are you having specifically?
Was This Post Helpful? 0
  • +
  • -

#4 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: #define question

Posted 20 January 2011 - 06:52 PM

The problems I'm having are with using the VBO as posted just after this one. OpenGL VBO Problem I wanted to use the #defines to easily switch between using and not using the VBO to help with trying to debug the problem.
Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: #define question

Posted 20 January 2011 - 07:19 PM

Not seeing all of you code, I would guess that it might be best for you to create two projects: one using VBOs and the other not using them. :)
Was This Post Helpful? 1
  • +
  • -

#6 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: #define question

Posted 21 January 2011 - 10:11 AM

Thanks, that will do for now, but was also thinking along the line of if one computer supports VBOs and another not that I'd be able to use a test to determine if VBO support is available on the current computer and if not then to revert back to just using vertex array? is #defines a good approach to this of another method?
Was This Post Helpful? 0
  • +
  • -

#7 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: #define question

Posted 21 January 2011 - 12:43 PM

View PostKain6622, on 21 January 2011 - 01:11 PM, said:

Thanks, that will do for now, but was also thinking along the line of if one computer supports VBOs and another not that I'd be able to use a test to determine if VBO support is available on the current computer and if not then to revert back to just using vertex array? is #defines a good approach to this of another method?

#defines (aka preprocessor macros) are only good for compile-time stuff. If you want to check for VBO support at runtime, you should have a flag somewhere like

int vbo_supported;
...
// Check for VBO support in an init function
// This line will differ if you aren't using GLUT
vbo_supported = glutExtensionSupported("GL_ARB_vertex_buffer_object");
...
if (vbo_supported)
{
    // VBO code
}

else
{
    // Vertex arrays
}



Here is a link to my mesh rendering class. I have compile-time and runtime checks for VBOs. Specifically you should look at dgMeshRender towards the bottom.

http://dagger3d.svn....src/mesh/mesh.c
Was This Post Helpful? 1
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: #define question

Posted 21 January 2011 - 12:51 PM

No, I wouldn't use #defines for testing support - you should only use them when you know what is supported on what platform to fashion the correct translation unit for that platform. When checking for support you should use methods that provide a reason for that lack of support, and preprocessor directives don't directly do that. :)

This link shows you how to check if VBOs are supported with this code snippet:

	// Check For VBOs Supported
#ifndef NO_VBOS
	g_fVBOSupported = IsExtensionSupported( "GL_ARB_vertex_buffer_object" );
	if( g_fVBOSupported )
	{
           // ...
        }



EDIT - Beaten to it! :D

This post has been edited by ButchDean: 21 January 2011 - 12:52 PM

Was This Post Helpful? 1
  • +
  • -

#9 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: #define question

Posted 22 January 2011 - 10:26 AM

Thanks for the help Tom and ButchDean. :D
Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: #define question

Posted 22 January 2011 - 10:30 AM

No problem, Kain6622. Glad you sorted the problem. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1