Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Creating an OpenGL Cube Class  (Read 12541 times)

0 Members and 1 Guest are viewing this topic.

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« on: December 19, 2010, 08:30:07 pm »
Ok Well I am trying to create an OpenGL cube class but if I try and translate the cube and then have a gl shape folowing it, it affects that shape. Is there anyway that I can have a matrix or something for each cube shape and have every tranlation or rotation done no matter where it's done only effects my shape. So it kinda like each shape has it's own little space in memory and when I call a function it affects that variable or matrix which is in each class.

So pretty much how would I make a function only affect a shape of my choice. would I have to put the shape in a seperate matrix and then translate that martix which is a variable for the cube class?

If so how would I do this?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #1 on: December 19, 2010, 10:19:49 pm »
Do you use glPushMatrix / glPopMatrix ?
SFML / OS X developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #2 on: December 20, 2010, 05:55:13 am »
Quote from: "Hiura"
Do you use glPushMatrix / glPopMatrix ?


Ya but wouldn't I be pushing the same matrix for all of the shapes since there all on the modelview matrix. And if I push the martix, how does it know which shapes matrix I am pushing?

How would I use I use glLoadMatrixf(const GLfloat *     m) to load each classes matrix? Like would I load a matrix and then call my shape's verts? And then load it whenever I want to make translations or rotations. But would the matrix be big enough? And how would I do this?

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Creating an OpenGL Cube Class
« Reply #3 on: December 20, 2010, 09:20:02 am »
You need to look more at how OpenGL works. Have a read of this:
http://www.opengl.org/documentation/red_book/

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #4 on: December 20, 2010, 03:47:36 pm »
Ok well This is waht I am trying to do. I have an array of floats called size.
See, I am trying to put the shape in the array called sides. Then I load "Sides" and do a rotation or translation, but it doesnt work...

void   Block::SetPosition(float x,float y, float z)
{
   //glMatrixMode(GL_MODELVIEW);
   glLoadMatrixf(Sides);
   glLoadIdentity();
   glTranslatef(x,y,z);
}

void   Block::Rotate(float angle, float x, float y, float z)
{
   //glMatrixMode(GL_MODELVIEW);
   glLoadMatrixf(Sides);
   glLoadIdentity();
   glRotatef(angle,x,y,z);
}

void   Block::Draw()
{
   glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);
   
   //glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(Sides);
    glPushMatrix();
    glBindTexture(GL_TEXTURE_2D, SideTexture);
   
   glColor4f(1.f, 1.f, 1.f, 1.f);
        // Draw a cube
        float size = 20.f;
        glBegin(GL_QUADS);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f( size,  size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, -size);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, size);
            glTexCoord2f(1, 1); glVertex3f( size,  size, size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, size);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f(-size,  size,  size);
            glTexCoord2f(1, 0); glVertex3f(-size, -size,  size);

            glTexCoord2f(0, 0); glVertex3f(size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f(size,  size,  size);
            glTexCoord2f(1, 0); glVertex3f(size, -size,  size);

      glEnd();

      glBindTexture(GL_TEXTURE_2D, BottomTexture);

      glBegin(GL_QUADS);
         
         glColor4f(1.f, 1.f, 1.f, 1.f);

            glTexCoord2f(0, 1); glVertex3f(-size, -size,  size);
            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
            glTexCoord2f(1, 1); glVertex3f( size, -size,  size);

      glEnd();

      glBindTexture(GL_TEXTURE_2D, TopTexture);
         glColor4f(1.f, 1.f, 1.f, 1.f);

      glBegin(GL_QUADS);

            glTexCoord2f(0, 1); glVertex3f(-size, size,  size);
            glTexCoord2f(0, 0); glVertex3f(-size, size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, size, -size);
            glTexCoord2f(1, 1); glVertex3f( size, size,  size);

        glEnd();

      glPopMatrix();
}

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #5 on: December 20, 2010, 06:06:48 pm »
I didn't understand what you mean but I see glLoadMatrixf(Sides); in both set functions and draw function.
BTW, please use [ code ][/ code ]
SFML / OS X developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #6 on: December 20, 2010, 06:11:55 pm »
Quote from: "Hiura"
I didn't understand what you mean but I see glLoadMatrixf(Sides); in both set functions and draw function.
BTW, please use [ code ][/ code ]


Well that is what I need help with. I am wondering how I can store data for each shape not on the stack but on a martix that each class has, and then being able to manipulate everything in that matrix with translation and rotation

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #7 on: December 20, 2010, 06:13:58 pm »
Maybe you can see things another way : you can for example set the pos and rotation _right_ before drawing the stuff. Do you see what I mean ?
SFML / OS X developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #8 on: December 20, 2010, 06:16:12 pm »
Quote from: "Hiura"
Maybe you can see things another way : you can for example set the pos and rotation _right_ before drawing the stuff. Do you see what I mean ?


Ya but what if I want to have an input that changes the rotation and I can't draw it right then... So that is why I want everything more object oriented..

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Creating an OpenGL Cube Class
« Reply #9 on: December 20, 2010, 06:18:52 pm »
You need a matrix class that you can store, manipulate and send to glLoadMatrix whenever you want (ie. before rendering the corresponding shape). I think you can find pretty good maths libraries that are designed for OpenGL.
Laurent Gomila - SFML developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #10 on: December 20, 2010, 06:19:02 pm »
You still can have set/get functions with this technique.
SFML / OS X developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #11 on: December 20, 2010, 06:23:23 pm »
Quote from: "Hiura"
You still can have set/get functions with this technique. In fact it has nothing to do with OO concept.


Why? I mean I want to set the position just for that class. How is that not OO

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #12 on: December 20, 2010, 06:26:23 pm »
Quote from: "Laurent"
You need a matrix class that you can store, manipulate and send to glLoadMatrix whenever you want (ie. before rendering the corresponding shape). I think you can find pretty good maths libraries that are designed for OpenGL.


So I would need to find a math lib and then setup like a 3D matrix. And then load it and push it to manpulate and pop it when done. Also how would I call the shape?

I kinda want a class like rectangle in sfml except 3D...

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #13 on: December 20, 2010, 06:31:14 pm »
Quote from: "Fierce_Dutch"
Why? I mean I want to set the position just for that class. How is that not OO
Forget the last sentence. -I need some sleep- :oops:
SFML / OS X developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #14 on: December 20, 2010, 06:34:56 pm »
Quote from: "Hiura"
Quote from: "Fierce_Dutch"
Why? I mean I want to set the position just for that class. How is that not OO
Forget the last sentence. -I need some sleep- :oops:


ok lol, but like I said in the previous message. I want a class like sf::Rect but for it to be 3D... And always have a set size.. So how would I do this? With a Matrix?