0
\$\begingroup\$

whenever I use the none-default constructor, it only adds to the coordinates of the previous class(in an array)

wall_1x1 wall[2] = {
    wall_1x1(0, -2, 0), //first argument is x, second z, and third ry
    wall_1x1(1, -2, 0)
};

the second object in the array will be behind the first object(by -2 z) and if I give the first object an ry of 90, all the other objects will have the same rotation.

#pragma once
#ifndef WALL_1X1_H

#include <GL\glew.h>
#include <SFML\Graphics.hpp>

class wall_1x1 {

public:
  wall_1x1();
  wall_1x1(float x, float z, float ry);
  ~wall_1x1();
  void up();

private:
  float m_x;
  float m_z;
  float m_ry;
  GLuint tex;
  sf::Image data;
};

#endif

and the .cpp class:

#include "wall_1x1.h"

GLfloat verts[] = {
  0, 0, 0,
  1, 0, 0,
  1, 1, 0,
  0, 1, 0
};

GLfloat coords[] = {
  0, 0,
  2, 0,
  2, 2,
  0, 2
};

wall_1x1::wall_1x1() {
  m_ry = 0;
  m_x = 0;
  m_z = 0;
}

wall_1x1::wall_1x1(float x, float z, float ry) {
  m_ry = 0;
  m_x = 0;
  m_z = 0;

  m_x = x;
  m_z = z;
  m_ry = ry;

  data.loadFromFile("wall1.png");

  glGenTextures(1, &tex);
  glBindTexture(GL_TEXTURE_2D, tex);
  glTexImage2D(
    GL_TEXTURE_2D, 
    0, 
    GL_RGBA, 
    data.getSize().x, 
    data.getSize().y, 
    0, 
    GL_RGBA, 
    GL_UNSIGNED_BYTE, 
    data.getPixelsPtr());
}


wall_1x1::~wall_1x1() {
}

void wall_1x1::up() {
  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glVertexPointer(3, GL_FLOAT, 0, verts);
  glTexCoordPointer(2, GL_FLOAT, 0, coords);
  glBindTexture(GL_TEXTURE_2D, tex);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTranslatef(m_x, 0, m_z);
  glRotatef(m_ry, 0, 1, 0);
  glDrawArrays(GL_QUADS, 0, 4);
  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

and the important parts in main:

wall_1x1 wall[2] = {
    wall_1x1(0, -2, 0),
    wall_1x1(1, -2, 0)
};
....

wall[0].up();
wall[1].up();
\$\endgroup\$
1
\$\begingroup\$

You can see the value of your member variables in your debugger, and it will show you the correct values.

Your problem lies in that the matrix transformation commands of legacy OpenGL are accumulating on top of each other.

If you want to start from a known state, you need to look into the functions that let you load an identity matrix, push and pop on the matrix stacks, and load/store matrices.

\$\endgroup\$
  • \$\begingroup\$ Now I feel dumb, like you said I forgot to add "glPushMatrix" and "glPopMatrix" but thanks! \$\endgroup\$ – user68817 Jul 19 '15 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy