Results 1 to 2 of 2

Thread: Using textures with box2D

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    5

    Using textures with box2D

    Ok I know this relates to Box2D, but I asked there, and they said it has to do more with textures, and GLUT, so here I am .

    Anyways, it really doesn't have much to do with Box2D, just how it's set up. My fist plan was to just try and get a texture on the screen. I got this to work. Here are my glut callback functions:

    Code :
    void display(void) {
     
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    	glMatrixMode (GL_MODELVIEW);
    	glLoadIdentity ();
     
    	glTranslatef (0, 0, -20);
    	glEnable(GL_TEXTURE_2D);
    	glBindTexture(GL_TEXTURE_2D, 1);
     
    	glBegin(GL_QUADS);
     
    	glTexCoord2f(0.0f, 0.0f);
    	glVertex3f(-10.0f, -10.0f, 0.0f);
    	glTexCoord2f(1.0f, 0.0f);
    	glVertex3f(10.0f, -10.0f, 0.0f);
    	glTexCoord2f(1.0f, 1.0f);
    	glVertex3f(10.0f, 10.0f, 0.0f);
    	glTexCoord2f(0.0f, 1.0f);
    	glVertex3f(-10.0f, 10.0f, 0.0f);
     
    	glEnd();
     
    	glDisable(GL_TEXTURE_2D);
    	glutSwapBuffers();
    }
     
    void reshape(int w, int h) {
     
    	if(h == 0)
    		h = 1;
     
    	float ratio = 1.0* w / h;
     
        glViewport (0, 0, w, h);
     
    	glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
     
    	gluPerspective (90, ratio, 1, 1000);
     
    	glutPostRedisplay ();
    }

    Then in my main function:

    Code :
    loadTGA("texture.tga", 1);

    This all works fine (the loadTGA function is elsewhere, but don't worry, it works fine).

    Now in Box2D, in order to display the bodies correcty, my callback functions are:

    Code :
    void display(void) {
     
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    	float32 timeStep = 1.0f / 30.0f;
    	int32 vIterations = 10;
    	int32 pIterations = 8;
     
    	world->Step(timeStep, vIterations, pIterations);
     
    	glutSwapBuffers();
    }
     
    void reshape(int w, int h) {
     
    	GLUI_Master.get_viewport_area(&tx, &ty, &tw, &th);
    	glViewport(0, 0, w, h);
     
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	float32 ratio = float32(tw) / float32(th);
     
    	b2Vec2 extents(ratio * 25.0f, 25.0f);
    	extents *= viewZoom;
     
    	b2Vec2 lower = viewCenter - extents;
    	b2Vec2 upper = viewCenter + extents;
     
    	// L/R/B/T
    	gluOrtho2D(lower.x, upper.x, lower.y, upper.y);
    }

    Some info is:
    viewZoom = 1.0.
    a b2Vec2 is a simple vector (x, y).

    Anyway this works fine. So in order to combine the two, first I add the loadTGA command to my main function.
    Then I change the display function to:

    Code :
    void display(void) {
     
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    	float32 timeStep = 1.0f / 30.0f;
    	int32 vIterations = 10;
    	int32 pIterations = 8;
     
    	world->Step(timeStep, vIterations, pIterations);
     
    	glTranslatef (0, 0, -20);
     
    	glEnable (GL_TEXTURE_2D); 
        glBindTexture (GL_TEXTURE_2D, 13); 
     
        /*glBegin (GL_QUADS);
            glTexCoord2f (0.0f,0.0f);
            glVertex3f (-10.0f, -10.0f, 0.0f);
            glTexCoord2f (1.0f, 0.0f); 
            glVertex3f (10.0f, -10.0f, 0.0f);
            glTexCoord2f (1.0f, 1.0f); 
            glVertex3f (10.0f, 10.0f, 0.0f);
            glTexCoord2f (0.0f, 1.0f);
            glVertex3f (-10.0f, 10.0f, 0.0f);
        glEnd (); 
     
        glDisable (GL_TEXTURE_2D);
     
    	glutSwapBuffers();
    }

    Now for my reshape function, I have absolutely no idea what to do . So...what do I have to change in order for texture to appear on the screen?


    This is important so I can then track the bodies with the texture, thus adding the texture to the body!

    Thanks in advance,
    Kcaz

  2. #2
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    5

    Re: Using textures with box2D

    So, can anyone help?

Similar Threads

  1. model textures being redirected to font textures
    By Tcll5850 in forum OpenGL: Advanced Coding
    Replies: 7
    Last Post: 09-07-2014, 10:43 PM
  2. FBO Attached Textures Reused as Shader Source Textures
    By Rennie Johnson in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-22-2012, 09:43 PM
  3. Convert mouse point to box2d point in opengl
    By Apoptyzm in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-16-2009, 01:25 PM
  4. Lots of small textures or fewer large textures?
    By jlamanna in forum OpenGL: Advanced Coding
    Replies: 30
    Last Post: 01-12-2004, 03:53 AM
  5. are GL_COLOR_INDEX textures converted to RGBA-Textures?
    By TB-Rex in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 12-18-2000, 06:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean