使用 OpenGL 在 C++ 中绘制 8 x 15 矩形网格 - 崩溃
Draw 8 x 15 Rectangle-Grid in C++ with OpenGL - crash down
我想用 OpenGl 在 C++ 中创建 8 x 15 矩形矩阵。
我首先为一个矩形创建一个 Class。然后我为一行矩形创建了一个 class,它包含一个 8 个矩形的数组。然后我为 15 行(整个矩阵)创建了一个 class。
我想我有一个指针问题...
这是我为测试所做的:
创建了 1..2 个矩形并绘制它们 - 好的
创建 1..2 行并绘制它们 -- ok
创建 1 个字段并绘制它 -- ok
创建了 2 个矩阵-- windows 刚刚打开并崩溃了...它是白色的,带有错误消息 xD
这就是我的显示功能...例如,它适用于 3 行。
但是如果我输入 MatrixRectangles field1(0,0);
和 MatrixRectangles field2(0,0);
并在 display 函数中用 void drawAllRows();
打印出来,它就不会工作。
void display(void) {
// glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 0, 0.01, 0, 0, 0, 0, 1, 0);
glBegin(GL_LINE_LOOP);
glVertex3f(-1, -1, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, -1, 0);
glEnd();
row1.drawAllRectangles();
row2.drawAllRectangles();
row3.drawAllRectangles();
glFlush();
}
class RectangleGL {
public:
RectangleGL();
RectangleGL(float hoehe, float breite, float startx, float starty);
virtual ~RectangleGL();
float getWidth() const;
float getHight() const;
void setBreite(float breite);
void setHight(float hohe);
void drawRectangle();
float getStartx() const;
void setStartx(float startx);
void printRectangleData();
void setStarty(float starty);
private:
float hight;
float width;
float startx;
float starty;
};
#endif /* VIERECKGL_H_
/*
* Reihe_ViereckeGL.cpp
*
* Created on: 19.10.2019
* Author: N1
*/
#include "Reihe_ViereckeGL.h"
rowRectangle::rowRectangle() {
// TODO Auto-generated constructor stub
}
rowRectangle::rowRectangle(float startx,float starty) {
// TODO Auto-generated constructor stub
for (int i = 0; i < 8; i++) {
float m = i * 0.1;
this->rectangles[i] = new RectangleGL(0.1, 0.1, m+startx, starty); // always a new x position , everytime higher...
}
}
rowRectangle::~rowRectangle() {
// TODO Auto-generated destructor stub
}
float rowRectangle::getStarty() const {
return starty;
}
void rowRectangle::setStarty(float starty) {
this->starty = starty;
}
void rowRectangle::drawAllRectangles() {
for (int i = 0; i < 8; i++) {
this->rectangles[i]->drawRectangle();
}
}
/*
* Reihe_ViereckeGL.h
*
* Created on: 19.10.2019
* Author: N1
*/
#ifndef REIHE_VIERECKEGL_H_
#define REIHE_VIERECKEGL_H_
#include "ViereckGL.h"
class rowRectangle {
public:
rowRectangle();
rowRectangle(float startx,float starty);
virtual ~rowRectangle();
float getStarty() const;
void setStarty(float starty);
void drawAllRectangles();
RectangleGL* rectangles[8];
private:
int anzReihe;
int rowNr;
float startx; // offset x..
float starty; // offset... y
};
#endif /* REIHE_VIERECKEGL_H_ */
/*
* FeldViereckeGL.h
*
* Created on: 19.10.2019
* Author: N1
*/
#ifndef FELDVIERECKEGL_H_
#define FELDVIERECKEGL_H_
#include "Reihe_ViereckeGL.h"
class MatrixRectangles {
public:
MatrixRectangles(float startx, float starty);
virtual ~MatrixRectangles();
rowRectangle *rowRectangles[8];
void drawAllRows();
};
#endif /* FELDVIERECKEGL_H_ */
/*
* FeldViereckeGL.cpp
*
* Created on: 19.10.2019
* Author: N1
*/
#include "FeldViereckeGL.h"
MatrixRectangles::MatrixRectangles(float startx, float starty) {
// TODO Auto-generated constructor stub
for (int i = 0; i < 15; i++) {
float m = i * 0.1;
this->rowRectangles[i] = new rowRectangle(startx, m + starty); // everytime a higher position
}
}
MatrixRectangles::~MatrixRectangles() {
// TODO Auto-generated destructor stub
}
void MatrixRectangles::drawAllRows() {
// zeichne alle reihen
for (int i = 0; i < 15; i++) {
rowRectangles[i]->drawAllRectangles(); // draw all rectangles !
}
}
我希望得到一个漂亮的 8 x 15 矩形矩阵。但是打开的 window 崩溃了——它变白了——“, exe 不再工作了”
您的行数组只有 8 个元素:
rowRectangle *rowRectangles[8];
但是您正在迭代 15 个元素:
for (int i = 0; i < 15; i++) {
float m = i * 0.1;
this->rowRectangles[i] = new rowRectangle(startx, m + starty); // everytime a higher position
}
这就是它崩溃的原因。
一个好的做法是为行数定义一个常量变量。
static constexpr int numRows = 15;
rowRectangle *rowRectangles[numRows];
并且到处使用 numRows
而不是 15
。这将防止将来出现错误。而且,当您需要更改行数时,只需在一个地方进行。
我想用 OpenGl 在 C++ 中创建 8 x 15 矩形矩阵。 我首先为一个矩形创建一个 Class。然后我为一行矩形创建了一个 class,它包含一个 8 个矩形的数组。然后我为 15 行(整个矩阵)创建了一个 class。
我想我有一个指针问题... 这是我为测试所做的:
创建了 1..2 个矩形并绘制它们 - 好的
创建 1..2 行并绘制它们 -- ok
创建 1 个字段并绘制它 -- ok
创建了 2 个矩阵-- windows 刚刚打开并崩溃了...它是白色的,带有错误消息 xD
这就是我的显示功能...例如,它适用于 3 行。
但是如果我输入 MatrixRectangles field1(0,0);
和 MatrixRectangles field2(0,0);
并在 display 函数中用 void drawAllRows();
打印出来,它就不会工作。
void display(void) {
// glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 0, 0.01, 0, 0, 0, 0, 1, 0);
glBegin(GL_LINE_LOOP);
glVertex3f(-1, -1, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, -1, 0);
glEnd();
row1.drawAllRectangles();
row2.drawAllRectangles();
row3.drawAllRectangles();
glFlush();
}
class RectangleGL {
public:
RectangleGL();
RectangleGL(float hoehe, float breite, float startx, float starty);
virtual ~RectangleGL();
float getWidth() const;
float getHight() const;
void setBreite(float breite);
void setHight(float hohe);
void drawRectangle();
float getStartx() const;
void setStartx(float startx);
void printRectangleData();
void setStarty(float starty);
private:
float hight;
float width;
float startx;
float starty;
};
#endif /* VIERECKGL_H_
/*
* Reihe_ViereckeGL.cpp
*
* Created on: 19.10.2019
* Author: N1
*/
#include "Reihe_ViereckeGL.h"
rowRectangle::rowRectangle() {
// TODO Auto-generated constructor stub
}
rowRectangle::rowRectangle(float startx,float starty) {
// TODO Auto-generated constructor stub
for (int i = 0; i < 8; i++) {
float m = i * 0.1;
this->rectangles[i] = new RectangleGL(0.1, 0.1, m+startx, starty); // always a new x position , everytime higher...
}
}
rowRectangle::~rowRectangle() {
// TODO Auto-generated destructor stub
}
float rowRectangle::getStarty() const {
return starty;
}
void rowRectangle::setStarty(float starty) {
this->starty = starty;
}
void rowRectangle::drawAllRectangles() {
for (int i = 0; i < 8; i++) {
this->rectangles[i]->drawRectangle();
}
}
/*
* Reihe_ViereckeGL.h
*
* Created on: 19.10.2019
* Author: N1
*/
#ifndef REIHE_VIERECKEGL_H_
#define REIHE_VIERECKEGL_H_
#include "ViereckGL.h"
class rowRectangle {
public:
rowRectangle();
rowRectangle(float startx,float starty);
virtual ~rowRectangle();
float getStarty() const;
void setStarty(float starty);
void drawAllRectangles();
RectangleGL* rectangles[8];
private:
int anzReihe;
int rowNr;
float startx; // offset x..
float starty; // offset... y
};
#endif /* REIHE_VIERECKEGL_H_ */
/*
* FeldViereckeGL.h
*
* Created on: 19.10.2019
* Author: N1
*/
#ifndef FELDVIERECKEGL_H_
#define FELDVIERECKEGL_H_
#include "Reihe_ViereckeGL.h"
class MatrixRectangles {
public:
MatrixRectangles(float startx, float starty);
virtual ~MatrixRectangles();
rowRectangle *rowRectangles[8];
void drawAllRows();
};
#endif /* FELDVIERECKEGL_H_ */
/*
* FeldViereckeGL.cpp
*
* Created on: 19.10.2019
* Author: N1
*/
#include "FeldViereckeGL.h"
MatrixRectangles::MatrixRectangles(float startx, float starty) {
// TODO Auto-generated constructor stub
for (int i = 0; i < 15; i++) {
float m = i * 0.1;
this->rowRectangles[i] = new rowRectangle(startx, m + starty); // everytime a higher position
}
}
MatrixRectangles::~MatrixRectangles() {
// TODO Auto-generated destructor stub
}
void MatrixRectangles::drawAllRows() {
// zeichne alle reihen
for (int i = 0; i < 15; i++) {
rowRectangles[i]->drawAllRectangles(); // draw all rectangles !
}
}
我希望得到一个漂亮的 8 x 15 矩形矩阵。但是打开的 window 崩溃了——它变白了——“, exe 不再工作了”
您的行数组只有 8 个元素:
rowRectangle *rowRectangles[8];
但是您正在迭代 15 个元素:
for (int i = 0; i < 15; i++) {
float m = i * 0.1;
this->rowRectangles[i] = new rowRectangle(startx, m + starty); // everytime a higher position
}
这就是它崩溃的原因。
一个好的做法是为行数定义一个常量变量。
static constexpr int numRows = 15;
rowRectangle *rowRectangles[numRows];
并且到处使用 numRows
而不是 15
。这将防止将来出现错误。而且,当您需要更改行数时,只需在一个地方进行。