سؤال

I'm new to Openframeworks and C++, but not programming in general (PHP and Objective-C). I've created a class that draws a cube where the height, width and depth are variables. I'm attempting to create 3 versions of said class, each with different dimensions.

However, the first object's dimensions are being used for each instance. I attempted to switch the order of drawing, and no matter what, whatever is drawn first is used for all. Also, I cout the dimensions to the console and the correct dimensions are being sent, yet only the first version is drawn.

cube.h

#ifndef _CUBE
#define _CUBE

#include "ofMain.h"

class cube
{
public:

void update();
void draw();

cube(float _w, float _h, float _d);

float w;
float h;
float d;

ofMesh mesh;
ofVbo myVbo;


private:
};

#endif

cube.cpp

#include "cube.h"

cube::cube(float _w, float _h, float _d)
{
w = _w;
h = _h;
d = _d;

static GLfloat vdata[8][3] = {
    {-w, -h, -d}, {-w, h, -d},
    {w, h, -d}, {w, -h, -d},
    {-w, -h, d}, {w, -h, d},
    {-w, h, d}, {w, h, d}
};

static GLint indices[6][4] = {
    {3, 2, 1, 0},
    {3, 5, 4, 0},
    {3, 5, 7, 2},
    {0, 4, 6, 1},
    {1, 2, 7, 6},
    {5, 4, 6, 7}
};

ofColor color(ofRandom(255),ofRandom(255),ofRandom(255));
float hue = 254.f;

for (int i=0; i<8; ++i)
{
    mesh.addVertex(ofVec3f( vdata[i][0], vdata[i][1], vdata[i][2] ));
    mesh.addColor(color);
    color.setHue(hue);
    hue -= 20.f;

    for (int i=0; i<6; ++i)
    {
        mesh.addIndex(indices[i][0]);
        mesh.addIndex(indices[i][1]);
        mesh.addIndex(indices[i][2]);
        mesh.addIndex(indices[i][3]);
    }
}

myVbo.setMesh(mesh, GL_STATIC_DRAW);
}

void cube::draw()
{
    myVbo.drawElements(GL_QUADS, 24);
}

void cube::update()
{

}

testApp.h

#pragma once

#include "ofMain.h"
#include "cube.h"

class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();

ofEasyCam camera;
cube *cube1;
cube *cube2;
cube *cube3;

void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
};

testApp.cpp

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup()
{
glEnable(GL_DEPTH_TEST);
ofBackground(33, 33, 33);

cube1 = new cube(50, 100, 25);
cube2 = new cube(300, 50, 80);
cube3 = new cube(250, 160, 30);
}

//--------------------------------------------------------------
void testApp::update()
{

}

//--------------------------------------------------------------
void testApp::draw()
{
camera.begin();

cube1->draw();

ofPushMatrix();
ofTranslate(40, 60);
cube2->draw();
ofPopMatrix();

ofPushMatrix();
ofTranslate(80, 100);
cube3->draw();
ofPopMatrix();
}

/*other code*/
هل كانت مفيدة؟

المحلول

After combing the code and testing out different things, I found the fix. I simply dropped the static from GFloat and GLint arrays. I still don't understand why completely, but it worked.

نصائح أخرى

Sorry, I cannot post comment as I do not have enough reputation. I was not able to find error in code by reading, so I quickly gave it a run in OF and as you can see...enter image description here

no problem in here... I am running OF 0.8 on VS2012Express - Win7 i7 nVidia Quatro 4000.

EDIT: maybe it is compiler issue? I had such a problem with addons that could not even compile. But that is just a wild guess...

EDIT: I just noticed that antony answered his own question, when I finally figured out why it was happening. I cannot still cannot add comments, so... Thing is that values of static variables are all identical for all instances of class. There are more stuff to it, but generally, if you change static variable (let's say static int width) in one object, change will occur in all of them.

And here is how it should look like :)

enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top