多维数据集未显示为多维数据集
Cube not being displayed as a cube
我有一些创建立方体的代码,然后将多边形模式更改为 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
to move the cube at its position in the world. Befor the the cube can be rotated by glRotate
。由于 glTranslate
和 glRotate
创建一个矩阵并将当前矩阵乘以新矩阵,因此必须在 glTranslate
指令之后执行 glRotate
指令。
如果立方体到眼睛位置的距离为 500,到远平面的距离为 500,则立方体的后半部分将被剪裁。设置 Orthographic projection by 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();
}
无论如何,对于 "real" 三维外观,我建议设置 Perspective projection by 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();
}
我有一些创建立方体的代码,然后将多边形模式更改为 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
to move the cube at its position in the world. Befor the the cube can be rotated by glRotate
。由于 glTranslate
和 glRotate
创建一个矩阵并将当前矩阵乘以新矩阵,因此必须在 glTranslate
指令之后执行 glRotate
指令。
如果立方体到眼睛位置的距离为 500,到远平面的距离为 500,则立方体的后半部分将被剪裁。设置 Orthographic projection by 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();
}
无论如何,对于 "real" 三维外观,我建议设置 Perspective projection by 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();
}