我有三个CustomPixel
个对象 - 当我直接在绘制函数draw
中绘制它时 - 它正常工作。
但是当我将它们放入queue
并用while
循环一个接一个地绘制它们时 - 屏幕上没有任何矩形。
这是我的代码
#include <gl\glew.h>
#include <gl\freeglut.h>
#include <iostream>
#include "OpenGL.h"
#include "Constants.h"
#include "Geometrics.h"
#include <queue>
std::queue<CustomPixel*> queue;
CustomPixel *pixel1 = new CustomPixel(0.0, 250.0, 100.0);
CustomPixel *pixel2 = new CustomPixel(101.0, 250.0, 100.0);
CustomPixel *pixel3 = new CustomPixel(202.0, 250.0, 100.0);
void draw() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
pixel1->drawRectangle(1.0, 0.0, 0.0); //it works!
if (!queue.empty()) {
while(!queue.empty()) {
CustomPixel *item = queue.front();
//doesn't work -> but
//in debugger it enters this method with correct CustomPixel
//attributes(x1, x2...etc)
item->drawRectangle(1.0, 0.0, 0.0);
queue.pop();
}
}
glEnd();
glFlush();
};
int main(int iArgc, char** cppArgv) {
//console for debug
OpenGL *opengl = new OpenGL(iArgc, cppArgv);
opengl->setSize(WIDTH, HEIGHT);
opengl->setWindowPosition(200, 200);
opengl->create();
//after OpenGL object created -> fill the queue with objects and the draw it
queue.push(pixel1);
queue.push(pixel2);
queue.push(pixel3);
opengl->run(draw);//<- can be any draw function
//clean all garbage
delete pixel1;
delete pixel2;
delete pixel3;
delete opengl;
return 0;
}
P.S使用c ++已经很久了。也许我忘记了什么?
这里是drawRectangle方法
void CustomPixel::drawRectangle(float r, float g, float b) {
glColor3f(r, g, b);
CustomLine::drawLine(this->x1, this->y1, this->x1, this->y2);
CustomLine::drawLine(this->x1, this->y1, this->x2, this->y1);
CustomLine::drawLine(this->x2, this->y1, this->x2, this->y2);
CustomLine::drawLine(this->x2, this->y2, this->x1, this->y2);
};
和drawLine
void CustomLine::drawLine(float x1, float y1, float x2, float y2) {
float dx = (x2 - x1) / STEP;
float dy = (y2 - y1) / STEP;
float vX = x1 / WIDTH;
float vY = y1 / HEIGHT;
for(int i = 0; i < STEP; i++) {
glVertex2f(vX + dx * i / WIDTH, vY + dy * i / HEIGHT);
}
};