我无法使用以下程序渲染圆圈,无法检测代码中的问题。此代码编译良好并运行,但不显示所需的输出
这是输出:
在此代码中,首先使用 glfw 进行初始化,然后定义着色器并相应地设置统一投影矩阵,然后渲染循环开始,其中midPointCircleAlgo
调用执行渲染的函数,进一步的绘图函数在屏幕上显示输出。我哪里错了。
这是顶点着色器: -
#version 330 core
layout (location = 0) in vec2 apos;
uniform mat4 projection;
void main()
{
gl_Position = projection*vec4(apos,0.0,1.0);
}
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "Header.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void plot(int x, int y);
void midPointCircleAlgo(int r);
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3 );
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(800,600,"Circle",NULL,NULL);
if(!window){
std::cout << "Failed to open window";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0,0,800,600);
glfwSetFramebufferSizeCallback(window,framebuffer_size_callback);
Shader shader("shader.vs","shader.fs");//I use a header file containing shader abstraction
glm::mat4 projection;
projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f);
int pmatrix = glGetUniformLocation(shader.ID, "projection");
shader.use();
glUniformMatrix4fv(pmatrix,1,GL_FALSE,glm::value_ptr(projection));
int r = 10;
while(!glfwWindowShouldClose(window))
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//render Circle
shader.use();
midPointCircleAlgo(10);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow *window,int width,int height){
glViewport(0,0,width,height);
}
void midPointCircleAlgo(int r)
{
int x = 0;
int y = r;
float decision = 5 / 4 - r;
plot(x, y);
while (y > x)
{
if (decision < 0)
{
x++;
decision += 2 * x + 1;
}
else
{
y--;
x++;
decision += 2 * (x - y) + 1;
}
plot(x, y);
plot(x, -y);
plot(-x, y);
plot(-x, -y);
plot(y, x);
plot(-y, x);
plot(y, -x);
plot(-y, -x);
}
}
void plot(int x,int y)
{
int vertices[] = { x,y};
unsigned int VBO,VAO;
glGenBuffers(1,&VBO);
glGenVertexArrays(1,&VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 2, GL_INT, GL_TRUE, 0, (void *)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawArrays(GL_POINTS, 0, 2);
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
}