Skip to main content
Fixing the code display
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

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

#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
#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);
}
    wall_1x1 wall[2] = {
    wall_1x1(0, -2, 0),
    wall_1x1(1, -2, 0)
};
    ....
    
    wall[0].up();
    wall[1].up();

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

#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
#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);
}
    wall_1x1 wall[2] = {
    wall_1x1(0, -2, 0),
    wall_1x1(1, -2, 0)
};
    ....
    
    wall[0].up();
    wall[1].up();

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

#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
#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);
}
wall_1x1 wall[2] = {
    wall_1x1(0, -2, 0),
    wall_1x1(1, -2, 0)
};
....

wall[0].up();
wall[1].up();
Source Link
user68817
user68817

sfml/opengl, two objects of same class are sharing variables

whenever I use the none-default constructer, 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();