如何在GLFW 3中创建OpenGL 3.3上下文 [英] How do I create an OpenGL 3.3 context in GLFW 3

查看:249
本文介绍了如何在GLFW 3中创建OpenGL 3.3上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的OpenGL 3.3程序,该程序基于

I've written a simple OpenGL 3.3 program which is supposed to render a triangle, based off of this tutorial, except I'm using GLFW to create the window and context, instead of doing it from scratch. Also, I'm using Ubuntu.

虽然三角形没有渲染,但我只得到了黑屏.像glClearColor()glClear()之类的函数似乎可以正常工作,但是呈现三角形的代码却不起作用.这是它的相关位:

The triangle doesn't render though, I just get a black screen. Functions like glClearColor() and glClear() seem to be working exactly as they should, but the code to render the triangle doesn't. Here are the relevant bits of it:

#define GLFW_INCLUDE_GL_3
#include <GL/glew.h>
#include <GLFW/glfw3.h>

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, "GLFW test", NULL, NULL);

  glfwMakeContextCurrent(window);

  glewExperimental = GL_TRUE;
  glewInit();

  float vertices[] = {-0.5f, -0.5f, 0.0f, 0.5f, 0.5f, -0.5f};

  GLuint VBOid[1];

  glClear(GL_COLOR_BUFFER_BIT);

  glGenBuffers(1, VBOid);
  glBindBuffer(GL_ARRAY_BUFFER, VBOid[0]);
  glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), vertices, GL_STATIC_DRAW);

  glEnableVertexAttribArray(0);

  glBindBuffer(GL_ARRAY_BUFFER, VBOid[0]);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  glDrawArrays(GL_TRIANGLES, 0, 3);

  ...
}

我想念什么?

推荐答案

在核心配置文件OpenGL 3.3中,需要着色器才能进行渲染.因此,您需要编译并链接一个包含顶点和片段着色器的程序.

In core profile OpenGL 3.3, you need a shader in order to render. So you need to compile and link a program that contains a vertex and fragment shader.

这篇关于如何在GLFW 3中创建OpenGL 3.3上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆