为什么不从矢量中绘制对象?哪里是我的错?
它显示m_x,m_y这个对象的位置会存在但是这个对象不在我的屏幕上。
main.cpp中:
#include <iostream>
#include <vector>
#define NDEBUG
#include "Figura.h"
#include "Balon.h"
#include <GL/freeglut.h>
using namespace std;
vector<CBalon> wektor_kol;
/* GLUT callback Handlers */
void resize(int width, int height)
{
const float ar = (float)width / (float)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void idle()
{
glutPostRedisplay();
}
void DrawRectangle(double width, double height)
{
glPushMatrix();
// TODO
// test functions below (glTranslated, glRotated, glColor3d) - what happen when you change their arguments?
// does their order change the result?
glTranslated(0.0, 0.0, 0.0);
glRotated(0, 1.0, 0.0, 0.0);
glRotated(0, 0.0, 1.0, 0.0);
glRotated(0, 0.0, 0.0, 1.0);
glColor3d(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
{
glVertex3d(-width / 2, height / 2, 0);
glVertex3d(width / 2, height / 2, 0);
glVertex3d(width / 2, -height / 2, 0);
glVertex3d(-width / 2, -height / 2, 0);
}
glEnd();
glPopMatrix();
}
void display()
{
// clear the scene
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
for (auto itr = wektor_kol.begin(); itr != wektor_kol.end(); itr++)
{
(*itr).Rysuj();
}
glPopMatrix();
glutSwapBuffers();
}
void InitGLUTScene(char* window_name)
{
glutInitWindowSize(800, 600);
glutInitWindowPosition(40, 40);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow(window_name);
// set white as the clear colour
glClearColor(1, 1, 1, 1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
}
void SetCallbackFunctions()
{
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutIdleFunc(idle);
}
void SetObjectsPositions()
{
CBalon balonik(0.6, 0.1, 0.5, 0.5);
balonik.positionSetter(0, 0);
wektor_kol.push_back(balonik);
}
int main(int argc, char *argv[])
{
// it's still possible to use console to print messages
printf("Hello openGL world!");
// the same can be done with cout / cin
glutInit(&argc, argv);
InitGLUTScene("freeglut template");
SetCallbackFunctions();
SetObjectsPositions();
// start GLUT event loop. It ends when user close the window.
glutMainLoop();
return 0;
}
Balon.cpp
#include "Balon.h"
#include "Figura.h"
#define NDEBUG
#define M_PI 3.14
#include <math.h>
#include <GL/freeglut.h>
CBalon::CBalon()
{
}
CBalon::CBalon(double radius, double red, double green, double blue)
{
m_r = red;
m_g = green;
m_b = blue;
m_radius = radius;
}
void CBalon::Rysuj()
{
glPushMatrix();
{
glTranslated(m_x, m_y, 0.0);
glRotated(m_z, 0.0, 0.0, 1.0);
glColor3d(m_r, m_g, m_b);
glBegin(GL_TRIANGLE_FAN);
{
for (int i = 0; i <= 360; i++)
{
// 180 - pi
// i - degInRad
float degInRad = i*M_PI / 180;
glVertex2f(cos(degInRad)*m_radius, sin(degInRad)*m_radius);
}
}
glEnd();
}
glPopMatrix();
}
double CBalon::redGetter()
{
return m_r;
}
CBalon::~CBalon()
{
}
Balon.h
#pragma once
#include "Figura.h"
class CBalon : public Figura
{
private:
double m_radius;
public:
CBalon();
CBalon(double radius, double red, double green, double blue);
double redGetter();
void Rysuj();
~CBalon();
};
Figura.cpp
#include "Figura.h"
Figura::Figura()
{
}
Figura::Figura(double _x, double _y, double _z, double _r, double _g, double _b, bool _u, double _rotation) : m_x(_x), m_y(_y), m_z(_z), m_r(_r), m_g(_g), m_b(_b), m_ukryj(_u), m_rotation(_rotation)
{
}
Figura::Figura(double red, double green, double blue)
: m_r(red), m_g(green), m_b(blue)
{
m_x = 0.0;
m_y = 0.0;
m_z = 0.0;
m_ukryj = true;
}
void Figura::Przesun(double dx, double dy)
{
m_x += dx;
m_y += dy;
}
bool Figura::GetterUkryj()
{
return m_ukryj;
}
void Figura::ZmienKolor(double _r, double _g, double _b)
{
m_r = _r;
m_g = _g;
m_b = _b;
}
double Figura::xGetter()
{
return m_x;
}
double Figura::yGetter()
{
return m_y;
}
void Figura::positionSetter(double x, double y)
{
m_x = x;
m_y = y;
}
Figura::~Figura()
{
}
Figura.h
#pragma once
class Figura
{
protected:
double m_r, m_g, m_b;
bool m_ukryj;
double m_x, m_y, m_z;
double m_alpha, m_beta, m_gamma;
double m_rotation;
public:
Figura();
Figura(double _x, double _y, double _z, double _r, double _g, double _b, bool _u, double _rotation);
Figura(double red, double green, double blue);
~Figura();
virtual void Rysuj() = 0;
void Ukryj();
void Pokaz();
void Przesun(double dx, double dy);
void Obroc(double dalpha, double dbeta, double dgamma);
void ZmienKolor(double _r, double _g, double _b);
double xGetter();
double yGetter();
void positionSetter(double x, double y);
bool GetterUkryj();
};
答案 0 :(得分:1)
错误是:
CBalon::CBalon(double radius, double red, double green, double blue): Figura(),m_radius(radius)
{
m_r = red;
m_g = green;
m_b = blue;
}
更改为:
CBalon::CBalon(double radius, double red, double green, double blue): Figura(red, green, blue),m_radius(radius)
{
}
解决了我的问题。