[英]Update OpenGL cube after translation and scale?
我有一堂課,應該畫一個立方體。
它在主體上很好地繪制了我的多維數據集,但是我使用默認的x,y,z值創建了它,以便默認情況下將其放置在屏幕中央。 我想通過調用build
來繪制並縮放多維數據集,但是我顯然缺少了一些東西,因為我的解決方案不適用於轉換和縮放。
目前,我的代碼如下:
build.h
class build
{
public:
enum shape { CUBE }; //this is to have different 3D objects drawn by this class
build(shape myShape);
~build();
void set_pos(vec2 new_pos);
vec3 get_pos();
void change_size(GLfloat new_side);
GLfloat get_size();
void move(vec3 &move_vec);
private:
void draw_cube();
shape myShape= CUBE;
vec3 pos = vec3(/* pos here */);
GLfloat side = 5.0f;
};
build.cpp
build::build(shape my_shape)
{
myShape = my_shape;
switch (myShape ) {
case CUBE:
draw_cube()
break;
default:
draw_cube()
break;
}
}
build::~build()
{
}
void build::draw_cube()
{
GLfloat verts[] =
{
pos.x, pos.y + side, pos.z
/* Rest of the vertices here */
};
GLfloat col[] =
{
//colours
};
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(col[0], col[2], col[2]);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, verts);
glDrawArrays(GL_QUADS, 0, 24);
glDisableClientState(GL_VERTEX_ARRAY);
}
void build::set_pos(vec2 new_pos)
{
glTranslatef(new_pos.x, new_pos.y, -250);
}
vec3 build::get_pos()
{
return vec3(pos);
}
void build::change_size(GLfloat new_side)
{
side = new_side;
}
GLfloat build::get_size()
{
return GLfloat(side);
}
void build::move(vec3 &move_vec)
{
pos += move_vec;
}
然后我調用main.cpp
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 500);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//This is to rotate the cube by pressing key, and it works
glPushMatrix();
glTranslatef(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, -250);
glRotatef(rotationX, 1, 0, 0);
glRotatef(rotationY, 0, 1, 0);
glTranslatef(-SCREEN_WIDTH / 2, -SCREEN_HEIGHT / 2, 250);
build *cube = new build(build::CUBE);
//My problem is here, since the cube is drawn at the origin but doesn't move if I call move() method
cube->move(vec3(100.0f, 0.0f, 0.0f));
glPopMatrix();
glfwSwapBuffers(window);
glfwPollEvents();
我想保留此代碼結構,因為我想通過調用build::OBJECTTYPE
繪制不同的3D對象,但是我必須能夠獲取並設置對象的位置和大小,因為稍后在代碼中將需要它。
代替使用glTranslatef(pos1,pos2,pos3)
cube->move(..)
,使用glTranslatef(pos1,pos2,pos3)
,也可以傳遞posX
參數,就像從main傳遞rotationX
, rotationY
,它將轉換多維數據集。 另外,如果您沒有得到想要的結果,則可以嘗試在glPushMatrix()
之后調用此函數。
您可以在類build
的構造函數中進行“繪制”調用( build::draw_cube
)。
線
build *cube = new build(build::CUBE);
總是創建一個新的立方體並繪制它。 之后,您移動多維數據集:
cube->move(vec3(100.0f, 0.0f, 0.0f));
但是您永遠不會繪制移動的多維數據集,因為在下一個循環中,您將在初始位置創建一個新的多維數據集。
首先,您應該在build
類中實現一個默認的構造函數:
build::build() {}
在繪制立方體之前,請設置初始位置:
build cube;
cube.move( vec3(100.0f, 0.0f, 0.0f) );
cube.draw_cube();
當然,您可以操縱GL_MODELVIEW
矩陣,而不用創建具有不同頂點的網格:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(100.0f, 0.0f, 0.0f);
glTranslatef(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, -250);
glRotatef(rotationX, 1, 0, 0);
glRotatef(rotationY, 0, 1, 0);
glTranslatef(-SCREEN_WIDTH / 2, -SCREEN_HEIGHT / 2, 250);
build cube;
cube.draw_cube();
glPopMatrix();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.