0

我有三个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;
}

PS自从我使用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);
};

和画线

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);
}
};
4

0 回答 0