为 3D Cup 创建圆柱体基础(现代 OpenGL、GLM)
Posted
技术标签:
【中文标题】为 3D Cup 创建圆柱体基础(现代 OpenGL、GLM)【英文标题】:Creating Cylinder base for a 3D Cup (Modern OpenGL, GLM) 【发布时间】:2019-12-01 23:02:27 【问题描述】:嘿,我很难弄清楚如何为需要绘制的 3D 杯子创建 360 度底座。 我正在使用 GLEW、GLFW 和 GLM 数学
到目前为止,我可以创建 3D 立方体和 3D 金字塔。到目前为止,我的想法是我需要以 360 度的方式创建 6 个或更多的三角形副本。我该怎么做? Modern OpenGL 除了创建三角形、立方体、金字塔外,资源并不多。
代码:
#include <GLEW/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
// GLM Mathematics
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace std;
int width, height;
const double PI = 3.14159;
const float toRadians = PI / 180.0f;
// Draw Primitive(s)
void draw()
GLenum mode = GL_TRIANGLES;
GLsizei indices = 6;
glDrawElements(mode, indices, GL_UNSIGNED_BYTE, nullptr);
// Input Function Prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos);
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
// Declare View Matrix
glm::mat4 viewMatrix;
// Initialize FOV
GLfloat fov = 45.0f;
// Define Camera Attributes
glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 target = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraDirection = glm::normalize(cameraPosition - target);
glm::vec3 worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraRight = glm::normalize(glm::cross(worldUp, cameraDirection));
glm::vec3 cameraUp = glm::normalize(glm::cross(cameraDirection, cameraRight));
glm::vec3 cameraFront = glm::normalize(glm::vec3(0.0f, 0.0f, -1.0f));
// Declare target prototype
glm::vec3 getTarget();
// Camera transformation prototype
void TransformCamera();
// Boolean array for keys and mouse buttons
bool keys[1024], mouseButtons[3];
// Boolean to check camera transformation
bool isPanning = false, isOrbiting = false, isZooming = false;
// Radius, Pitch, and Yaw
GLfloat radius = 3.0f, rawYaw = 0.0f, rawPitch = 0.0f, degYaw, degPitch;
GLfloat deltaTime = 0.0f, lastFrame = 0.0f;
GLfloat lastX = 320, lastY = 240, xChange, yChange;
// Check for any initial mouse movement
bool firstMouseMove = true;
void initCamera();
// Create and Compile Shaders
static GLuint CompileShader(const string& source, GLuint shaderType)
// Create Shader object
GLuint shaderID = glCreateShader(shaderType);
const char* src = source.c_str();
// Attach source code to Shader object
glShaderSource(shaderID, 1, &src, nullptr);
// Compile Shader
glCompileShader(shaderID);
// Return ID of Compiled shader
return shaderID;
// Create Program Object
static GLuint CreateShaderProgram(const string& vertexShader, const string& fragmentShader)
// Compile vertex shader
GLuint vertexShaderComp = CompileShader(vertexShader, GL_VERTEX_SHADER);
// Compile fragment shader
GLuint fragmentShaderComp = CompileShader(fragmentShader, GL_FRAGMENT_SHADER);
// Create program object
GLuint shaderProgram = glCreateProgram();
// Attach vertex and fragment shaders to program object
glAttachShader(shaderProgram, vertexShaderComp);
glAttachShader(shaderProgram, fragmentShaderComp);
// Link shaders to create executable
glLinkProgram(shaderProgram);
// Delete compiled vertex and fragment shaders
glDeleteShader(vertexShaderComp);
glDeleteShader(fragmentShaderComp);
// Return Shader Program
return shaderProgram;
int main(void)
width = 640; height = 480;
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(width, height, "Main Window", NULL, NULL);
if (!window)
glfwTerminate();
return -1;
// Set input callback functions
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetScrollCallback(window, scroll_callback);
/* Make the window's context current */
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK)
cout << "Error!" << endl;
GLfloat vertices[] =
// Triangle 1
-0.5, -0.5, 0.0, // index 0
1.0, 0.0, 0.0, // red
-0.5, 0.5, 0.0, // index 1
0.0, 1.0, 0.0, // green
0.5, -0.5, 0.0, // index 2
0.0, 0.0, 1.0, // blue
// Triangle 2
0.5, 0.5, 0.0, // index 3
1.0, 0.0, 1.0 // purple
;
// Define element indices
GLubyte indices[] =
0, 1, 2,
1, 2, 3
;
// Plane Transforms
glm::vec3 planePositions[] =
glm::vec3(0.0f, 0.0f, 0.5f),
glm::vec3(0.5f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, -0.5f),
glm::vec3(-0.5f, 0.0f, 0.0f)
;
glm::float32 planeRotations[] =
0.0f, 90.0f, 0.0f, 90.0f
;
// Setup some OpenGL options
glEnable(GL_DEPTH_TEST);
// Wireframe mode
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
GLuint VBO, EBO, VAO;
glGenBuffers(1, &VBO); // Create VBO
glGenBuffers(1, &EBO); // Create EBO
glGenVertexArrays(1, &VAO); // Create VOA
glBindVertexArray(VAO);
// VBO and EBO Placed in User-Defined VAO
glBindBuffer(GL_ARRAY_BUFFER, VBO); // Select VBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); // Select EBO
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // Load vertex attributes
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // Load indices
// Specify attribute location and layout to GPU
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0); // Unbind VOA or close off (Must call VOA explicitly in loop)
// Vertex shader source code
string vertexShaderSource =
"#version 330 core\n"
"layout(location = 0) in vec4 vPosition;"
"layout(location = 1) in vec4 aColor;"
"out vec4 oColor;"
"uniform mat4 model;"
"uniform mat4 view;"
"uniform mat4 projection;"
"void main()\n"
"\n"
"gl_Position = projection * view * model * vPosition;"
"oColor = aColor;"
"\n";
// Fragment shader source code
string fragmentShaderSource =
"#version 330 core\n"
"in vec4 oColor;"
"out vec4 fragColor;"
"void main()\n"
"\n"
"fragColor = oColor;"
"\n";
// Creating Shader Program
GLuint shaderProgram = CreateShaderProgram(vertexShaderSource, fragmentShaderSource);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
// Set Delta time
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Resize window and graphics simultaneously
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
/* Render here */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use Shader Program exe and select VAO before drawing
glUseProgram(shaderProgram); // Call Shader per-frame when updating attributes
// Declare transformations (can be initialized outside loop)
glm::mat4 projectionMatrix;
viewMatrix = glm::lookAt(cameraPosition, getTarget(), worldUp);
projectionMatrix = glm::perspective(fov, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
// Get matrix's uniform location and set matrix
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
//glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glBindVertexArray(VAO); // User-defined VAO must be called before draw.
for (GLuint i = 0; i < 4; i++)
glm::mat4 modelMatrix;
modelMatrix = glm::translate(modelMatrix, planePositions[i]);
modelMatrix = glm::rotate(modelMatrix, planeRotations[i] * toRadians, glm::vec3(0.0f, 1.0f, 0.0f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(modelMatrix));
// Draw primitive(s)
draw();
// Unbind Shader exe and VOA after drawing per frame
glBindVertexArray(0); //Incase different VAO wii be used after
glUseProgram(0); // Incase different shader will be used after
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
// Poll camera transformations
TransformCamera();
//Clear GPU resources
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glfwTerminate();
return 0;
// Define Input Callback Functions
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
// Display ASCII Keycode
// ALT = ASCII 342
//cout << "ASCII: " << key << endl;
if (action == GLFW_PRESS)
keys[key] = true;
else if (action == GLFW_RELEASE)
keys[key] = false;
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
/*
// Display scroll offset
if (yoffset > 0)
cout << "Scroll Up: ";
if (yoffset < 0)
cout << "Scroll down: ";
cout << yoffset << endl;
*/
/*
if (isZooming)
// Clamp FOV
if (fov >= 1.0f && fov <= 55.0f)
fov -= yoffset * 0.01f;
// Default FOV
if (fov < 1.0f)
fov = 1.0f;
if (fov > 45.0f)
fov = 45.0f;
*/
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
// cout << "Mouse X: " << xpos << endl;
// cout << "Mouse Y: " << ypos << endl;
if (firstMouseMove)
lastX = xpos;
lastY = ypos;
firstMouseMove = false;
// Calculate cursor offset
xChange = xpos - lastX;
yChange = lastY - ypos;
lastX = xpos;
lastY = ypos;
// Zoom Camera
if (isZooming)
if (fov < 1.0f)
fov = 1.0f;
if (fov > 45.0f)
fov = 45.0f;
if (xpos > 0)
fov -= lastX * 0.01f;
if (ypos > 0)
fov -= lastY * 0.01f;
// Pan camera
if (isPanning)
if (cameraPosition.z > 0.f)
cameraFront.z = 1.0f;
else
cameraFront.z = -1.0f;
GLfloat cameraSpeed = xChange * deltaTime;
cameraPosition += cameraSpeed * cameraRight;
cameraSpeed = yChange * deltaTime;
cameraPosition += cameraSpeed * cameraUp;
// Orbit camera
if (isOrbiting)
rawYaw += xChange;
rawPitch += yChange;
// Convert Yaw and Pitch to degrees
degYaw = glm::radians(rawYaw);
// degPitch = glm::radians(rawPitch);
degPitch = glm::clamp(glm::radians(rawPitch), -glm::pi<float>() / 2.0f + .1f, glm::pi<float>() / 2.0f - .1f);
// Azimuth Altitude Formula
cameraPosition.x = target.x + radius * cosf(degPitch) * sinf(degYaw);
cameraPosition.y = target.y + radius * sinf(degPitch);
cameraPosition.z = target.z + radius * cosf(degPitch) * cosf(degYaw);
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
/*
// Detect Mouse Button Clicks
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
cout << "LMB Clicked" << endl;
if (button == GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW_PRESS)
cout << "MMB Clicked" << endl;
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
cout << "RMB Clicked" << endl;
*/
if (action == GLFW_PRESS)
mouseButtons[button] = true;
else if (action == GLFW_RELEASE)
mouseButtons[button] = false;
// Define getTarget function
glm::vec3 getTarget()
if (isPanning)
target = cameraPosition + cameraFront;
return target;
// Define TransformCamera function
void TransformCamera()
// Pan camera if left alt key and middle mouse button pressed same time
if (keys[GLFW_KEY_LEFT_ALT] && mouseButtons[GLFW_MOUSE_BUTTON_MIDDLE])
isPanning = true;
else
isPanning = false;
// Orbit camera if left alt key and left mouse button pressed same time
if (keys[GLFW_KEY_LEFT_ALT] && mouseButtons[GLFW_MOUSE_BUTTON_LEFT])
isOrbiting = true;
else
isOrbiting = false;
// Zoom camera if left alt key and right mouse button pressed same time
if (keys[GLFW_KEY_LEFT_ALT] && mouseButtons[GLFW_MOUSE_BUTTON_RIGHT])
isZooming = true;
else
isZooming = false;
// Reset Camera
if (keys[GLFW_KEY_F])
initCamera();
void initCamera()
cameraPosition = glm::vec3(0.0f, 0.0f, 3.0f);
target = glm::vec3(0.0f, 0.0f, 0.0f);
cameraDirection = glm::normalize(cameraPosition - target);
worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
cameraRight = glm::normalize(glm::cross(worldUp, cameraDirection));
cameraUp = glm::normalize(glm::cross(cameraDirection, cameraRight));
cameraFront = glm::normalize(glm::vec3(0.0f, 0.0f, -1.0f));
【问题讨论】:
【参考方案1】:AFAIK,在 OpenGL 中绘制圆的最佳方法是使用如下三角形:
你基本上是在做披萨。切片越多,看起来就越像一个圆圈。您可以使用 GL_TRIANGLE_FAN 来减少绘制圆所需的顶点数。
【讨论】:
我明白了。我开始手动绘制每个三角形。所以现在我有 6 个,但正如你所说,我想我需要更多才能使它更像一个圆圈。手动编码每一个并找出超过 6 个三角形的顶点 x、y 很烦人 更好的方法是执行以下操作: 1. 计算出你想要在圆的圆周上多少个点 2. 将 360 除以点数 - 这个值是一个“步长”量 3. 使用 for 循环,从角度 0 开始并按步长递增,直到达到 360 4. 在 for 循环的每次迭代中,计算当前角度的 sin 和 cos;角度的 cos 乘以所需的圆半径将是 X 位置,与 Y 位置类似,但带有 sin。看这张海报的代码:***.com/questions/22444450/drawing-circle-with-opengl以上是关于为 3D Cup 创建圆柱体基础(现代 OpenGL、GLM)的主要内容,如果未能解决你的问题,请参考以下文章