简单的现代OpenGL Hello World

时间:2018-08-24 19:33:46

标签: c++ opengl sdl

我想制作一个简单的现代OpenGL hello world程序。我已经对现代OpenGL有一定的经验。我什至甚至编译了自己的着色器以及所有这些东西。现在,我想使用一些更现代的功能,例如glVertexAttribPointer。代码可以编译,但是我得到的只是一个黑屏。没有显示三角形。我的显卡有问题吗?还是我做错了什么?

我正在关注本教程:https://www.youtube.com/watch?v=Dyue3MzJDss&index=5&list=PLRwVmtr-pp06qT6ckboaOhnm9FxmzHpbY

当我查看评论时,很多人说他们得到的只是黑屏。

#include <iostream>
#define GL3_PROTOTYPES 1
#include <GL/glew.h>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <vector>
#define BUFFER_OFFSET(i) ((char *)NULL + (i))

GLuint vbo;
GLuint w=0;
float f[]={0.0, 1.0, -1.0, -1.0, 1.0, -1.0};    //vertex array

void init()
{
  glClearColor(0,0,0,1);
  glEnable(GL_DEPTH_TEST);
  glGenBuffers(1,&vbo); //generate an index for the vertexbuffer
  glBindBuffer(GL_ARRAY_BUFFER,vbo);    //use vbo as ARRAY_BUFFER
  glBufferData(GL_ARRAY_BUFFER,sizeof(f),f,GL_STATIC_DRAW);//fill up the array with vertex and color-data
  glEnableVertexAttribArray(w);
  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
}


void display()
{
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glDrawArrays(GL_TRIANGLES,0,3);       //draw it
}

int main()
{
  SDL_Init(SDL_INIT_EVERYTHING);
  SDL_SetVideoMode(640,480,32,SDL_OPENGL);
  glewInit();
  Uint32 start;
  SDL_Event event;
  bool running=true;
  init(); 
  bool b=false;
  while(running)
  {
    start=SDL_GetTicks();
    while(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_QUIT:
                running=false;
                break;
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                    case SDLK_ESCAPE:
                        running=false;
                        break;
                }
                break;

        }
    }
    display();
    SDL_GL_SwapBuffers();
    if(1000.0/30>SDL_GetTicks()-start)
        SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  }
  glDeleteBuffers(1,&vbo);
  SDL_Quit();
}

0 个答案:

没有答案