gpt4 book ai didi

c++ - 为 3D 杯子创建圆柱体底座(现代 OpenGL、GLM)

转载 作者:行者123 更新时间:2023-11-28 04:03:33 25 4
gpt4 key购买 nike

嘿,我很难弄清楚如何为我需要绘制的 3D 杯子创建 360 度底座。我正在使用 GLEW、GLFW 和 GLM 数学

到目前为止,我已经可以创建 3D 立方体和 3D 金字塔了。到目前为止,我的想法是我需要以 360 度的方式创建 6 个或更多的三角形拷贝。我该怎么做?除了创建三角形、立方体、金字塔之外,现代 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));
}

最佳答案

据我所知,在 OpenGL 中绘制圆的最佳方法是像这样使用三角形:

enter image description here

您基本上是在制作披萨。切片越多,它看起来越像一个圆圈。您可以使用 GL_TRIANGLE_FAN 来减少绘制圆所需的顶点数。

关于c++ - 为 3D 杯子创建圆柱体底座(现代 OpenGL、GLM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59130804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com