OpenGL和GLFW:不绘制多边形
OpenGL and GLFW: Not drawing polygon
我能够创建一个窗口并将其清除为所需的颜色。 但无法在左下角画一个正方形。
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const GLint WIDTH = 720;
const GLint HEIGHT = 480;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Attempts to set to opengl 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Learn OpenGL", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if (nullptr == window)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit())
{
std::cout << "Failed to initialise GLEW" << std::endl;
return EXIT_FAILURE;
}
glViewport(0, 0, screenWidth, screenHeight);
while (!glfwWindowShouldClose(window))
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 1.0f);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.5, 0.0, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
glFlush();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return EXIT_SUCCESS;
}
已弃用的固定函数功能(glBegin()
et al & the matrix stacks)在核心上下文中不起作用(GLFW_OPENGL_CORE_PROFILE
)。
切换到兼容性上下文:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const GLint WIDTH = 720;
const GLint HEIGHT = 480;
int main()
{
glfwInit();
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Learn OpenGL", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if (nullptr == window)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit())
{
std::cout << "Failed to initialise GLEW" << std::endl;
return EXIT_FAILURE;
}
glViewport(0, 0, screenWidth, screenHeight);
while (!glfwWindowShouldClose(window))
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.5, 0.0, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return EXIT_SUCCESS;
}
或者提供一些着色器并使用VAO和VBO上传您的几何体。
相关文章:
- 在 QT C++中绘制/操作多边形
- 如何绘制具有随机顶点数量的多边形并旋转它们
- 为什么 OpenGL 不在此代码中绘制多边形?
- 如何在QGraphicsScene上绘制填充多边形
- OpenGL和GLFW:不绘制多边形
- 即使 OpenGL、SDL 和 GLSL 都没有抛出错误,也没有绘制多边形
- 如何在SDL中绘制多边形
- 通过鼠标单击绘制多边形
- 绘制多边形网格时出现 Opengl 性能问题
- 如何在现代openGL中绘制带有三维点的多边形
- 在C++中使用多线程绘制多边形
- OpenGL 不会为不在命名空间中的对象绘制多边形
- c++opengGL:绘制多边形+图像处理内部像素
- 使用 GL_POLYGON 在 OpenGL 中绘制多边形时获得意外输出
- 使用OpenGL在Maya中绘制多边形导线
- 在OpenCV中绘制多边形
- QGraphicsPolygonItem绘制一个打开(非关闭)的多边形
- 在OpenGL中使用多边形绘制2D粗圆弧
- glDrawElements绘制多边形
- 绘制数以千计的多边形与OpenGL VBO