简体   繁体   English

OpenGL拒绝绘制三角形

[英]OpenGL refuses to draw a triangle

I'm attempting to draw a single large triangle in a window in OpenGL. 我试图在OpenGL的窗口中绘制一个大的三角形。 My program compiles and runs, but I get just a black screen in my window. 我的程序编译并运行,但我的窗口只有一个黑屏。

I've checked and double-checked multiple tutorials and it seems like my steps are correct... Am I missing something obvious? 我已经检查并仔细检查了多个教程,看起来我的步骤是正确的......我错过了一些明显的东西吗?

Here is the program in its entirety: 以下是该计划的全部内容:

#include <stdlib.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GLUT/glut.h>

GLuint VBO;

struct vector {
  float _x;
  float _y;
  float _z;
  vector() { }
  vector(float x, float y, float z) { _x = x; _y = y; _z = z; }
};

void render()
{
  glClear(GL_COLOR_BUFFER_BIT);

  glEnableVertexAttribArray(0);
  glBindBuffer(GL_ARRAY_BUFFER, VBO);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

  glDrawArrays(GL_TRIANGLES, 0, 3);

  glDisableVertexAttribArray(0);

  glutSwapBuffers();
}

void create_vbo()
{
  vector verts[3];
  verts[0] = vector(-1.0f, -1.0f, 0.0f);
  verts[1] = vector(1.0f, -1.0f, 0.0f);
  verts[2] = vector(0.0f, 1.0f, 0.0f);

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    glutInitWindowSize(1024, 768);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Triangle Test");
    glutDisplayFunc(render);

    glewInit();

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    create_vbo();
    glutMainLoop();

    return 0;
}

Update: It turns out that drawing this way without a "program" (that is, compiled shader files) produces undefined behavior (the newer your graphics card, the more likely it is to work, however). 更新:事实证明,没有“程序”(即编译的着色器文件)以这种方式绘制会产生未定义的行为(您的图形卡越新,它就越有可能工作)。

Because my card is right on the edge and only supports OpenGL 2.1, it was a little difficult to find an appropriate shader example that would work -- seems like there are many different tutorials out there written at different stages in the evolution of OpenGL. 因为我的卡是正确的,并且只支持OpenGL 2.1,所以找到一个合适的着色器示例有点困难 - 似乎在OpenGL的演变过程中不同阶段有很多不同的教程。

My vertex shader (entire file): 我的顶点着色器(整个文件):

void main()
{
  gl_Position = ftransform();
}

My fragment shader (entire file): 我的片段着色器(整个文件):

void main()
{
    gl_FragColor = vec4(0.4,0.4,0.8,1.0);
}

I used the example LoadShaders function from this OpenGL Tutorial Site to create the program, and now, I, too, can see the triangle! 我使用OpenGL Tutorial Site中的示例LoadShaders函数来创建程序,现在,我也可以看到三角形!

(Thanks to @chbaker0 for pointing me in the right direction.) (感谢@ chbaker0指出我正确的方向。)

I do not know if this will help you or not but in your create_vbo() function where you have: 我不知道这是否会对你有所帮助,但在你的create_vbo()函数中你有:

  glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

try this instead: 试试这个:

  glBufferData( GL_ARRAY_BUFFER, sizeof( verts[0] * 3 ), &verts[0], GL_STATIC_DRAW );

And after this function call add in this function call to the end of your create_vbo() function 在此函数调用之后,将此函数调用添加到create_vbo()函数的末尾

// This MUST BE LAST! Used to Stop The Buffer!
glBindBuffer( GL_ARRAY_BUFFER, 0 ); 

It is hard for me to see your error. 我很难看到你的错误。 In my projects I do have some vbos, but I am also using vaos as well. 在我的项目中,我确实有一些vbos,但我也使用vaos。 My code is able to working in OpenGL 2.0 - 4.5 but for the older versions there is a split in logic because of the deprecated functions within the API. 我的代码能够在OpenGL 2.0 - 4.5中工作,但是对于旧版本,由于API中已弃用的功能,因此逻辑上存在分歧。 I also do not use glut. 我也不使用过剩。 I hope this helps. 我希望这有帮助。

The other thing I noticed too is did you pay attention to your vertex winding order? 我注意到的另一件事是你注意你的顶点缠绕顺序吗? Meaning are they being used by OpenGL in a CCW order or CW order? 这意味着OpenGL在CCW订单或CW订单中使用它们? Is back face culling turned on or off? 背面剔除是打开还是关闭? There are a lot of elements to consider when setting up and configuring an OpenGL context. 在设置和配置OpenGL上下文时,需要考虑很多元素。 It has been a while since I worked with older versions of OpenGL but I do know that once you start working with a specific version or newer you will have to supply your own model view projection matrix, just something to consider. 自从我使用旧版本的OpenGL以来已经有一段时间了,但我知道一旦你开始使用特定版本或更新版本,你将不得不提供自己的模型视图投影矩阵,这是需要考虑的事情。

The issue I ran into was using pipeline features without defining a shader program. 我遇到的问题是使用管道功能而没有定义着色器程序。 The spec says this should work, but on my graphics card it did not did. 该规范说这应该可行,但在我的显卡上却没有。 (See my update in the question for more specifics). (有关更多细节,请参阅我在问题中的更新)。

Thanks to all the commenters for nudging me in the right direction. 感谢所有评论者推动我朝着正确的方向前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM