我在创建多维数据集的地方有一些代码,然后将多边形模式更改为line
。然后我意识到它看起来不像立方体,而看起来像正方形,所以我尝试旋转它。它似乎在旋转,但是停止显示正方形的一部分,并且它似乎不是立方体。我不确定我是在正确旋转还是在正确绘制立方体,甚至两者都绘制。
完整的C++代码:
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm.hpp>
#include <GL/freeglut.h>
void DrawCube(GLfloat centerPosX, GLfloat centerPosY, GLfloat centerPosZ, GLfloat edgeLength);
int main(void) {
GLFWwindow* window;
//Init library
if (!glfwInit())
return -1;
//create a window
glEnable(GL_DEPTH_TEST);
GLfloat screenWidth = 640;
GLfloat screenHeight = 480;
window = glfwCreateWindow(screenWidth, screenHeight, "electroCaft", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glViewport(0.0f, 0.0f, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, screenWidth, 0, screenHeight, 0, 500); // essentially setting coodinates
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
GLfloat halfScreenWidth = screenWidth / 2;
GLfloat halfScreenHeight = screenHeight / 2;
//loop until user closes window
while (!glfwWindowShouldClose(window)) {
//render graphics
//glClear(GL_COLOR_BUFFER_BIT);
glClearColor(62.0f / 255.0f, 85.9f / 255.0f, 255.0 / 255.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//drawing here
DrawCube(halfScreenWidth, halfScreenHeight, -500, 250); //x,y,w,h z is calculated in cube func
//DrawCube(halfScreenWidth, halfScreenHeight - 100, -500, 250);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void DrawCube(GLfloat centerPosX, GLfloat centerPosY, GLfloat centerPosZ, GLfloat edgeLength) {
GLfloat halfSideLength = edgeLength * 0.5;
GLfloat vertices[] = {
// front face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, //top left
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, //top right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, //bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left
// back face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, //top left
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, //top right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, //bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom left
// left face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, //top left
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, //top right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, //bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left
// right face
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, //top left
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, //top right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, //bottom right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left
// top face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, //top left
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, //top right
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, //bottom right
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // bottom left
// bottom face
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, //top left
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, //top right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, //bottom right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength // bottom left
};
//glRotated(edgeLength, 0, 0, 1);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer( 3, GL_FLOAT, 0, vertices);
glDrawArrays(GL_QUADS, 0, 24);
glDisableClientState(GL_VERTEX_ARRAY);
}
最佳答案
不要计算立方体的平移顶点坐标。在(0,0,0)周围绘制立方体。使用 glTranslate
将多维数据集移至其在世界上的位置。在立方体之前,可以通过 glRotate
旋转。由于glTranslate
和glRotate
创建矩阵并将当前矩阵乘以新矩阵,因此glRotate
指令必须在glTranslate
指令之后执行。
如果立方体到眼睛位置的距离是500,而到远方平面的距离是500,则立方体的后半部分将被剪切。通过Orthographic projection设置 glOrtho
时,您必须更改远平面。例如。:
glViewport(0.0f, 0.0f, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, screenWidth, 0, screenHeight, 0, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
GLfloat angle = 1.0;
while (!glfwWindowShouldClose(window)) {
glClearColor(62.0f / 255.0f, 85.9f / 255.0f, 255.0 / 255.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef((GLfloat)screenWidth/2.0f, (GLfloat)screenHeight/2.0f, -500.0f );
glRotatef(angle, 0.5f, 1.0f, 0.0f);
angle += 1.0f;
DrawCube(0.0, 0.0, 0.0, 250);
glPopMatrix();
glfwSwapBuffers(window);
glfwPollEvents();
}
无论如何,对于“真实的” 3D外观,我建议通过Perspective projection设置
gluPerspective
。例如。:glViewport(0.0f, 0.0f, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (float)screenWidth/screenHeight, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
GLfloat angle = 1.0;
while (!glfwWindowShouldClose(window)) {
glClearColor(62.0f / 255.0f, 85.9f / 255.0f, 255.0 / 255.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0, 0.0, -500 );
glRotatef(angle, 0.5f, 1.0f, 0.0f);
angle += 1.0f;
DrawCube(0.0, 0.0, 0.0, 250);
glPopMatrix();
glfwSwapBuffers(window);
glfwPollEvents();
}
关于c++ - 多维数据集未显示为多维数据集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58142822/