使用openGL / SDL绘制点

时间:2016-10-16 17:49:19

标签: c++ opengl opengl-3

我正试图跟踪我的手然后每个x,我会在openGL窗口上画一个点。

这是我的代码:

#include "GraphicsEffects.h"
#include "Errors.h"

GraphicsEffects::GraphicsEffects():
 _vboID(0)
{}


void GraphicsEffects::init(){
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("2D Painting", SDL_WINDOWPOS_CENTERED,       SDL_WINDOWPOS_CENTERED , 600, 600, SDL_WINDOW_OPENGL);

if(_window == nullptr){
    fatalError("SDL window could not be created! "); }


SDL_GLContext glContext = SDL_GL_CreateContext(_window);
if(glContext == nullptr) {
    fatalError("SDL_GL context could not be created!");
}

GLenum error = glewInit();
if(error != GLEW_OK){
    fatalError("Could not initialize glew!");
}

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

if(_vboID == 0){
    glGenBuffers(1, &_vboID);
}

_colorProgram.compileShaders("Shaders/color.vert", "Shaders/color.frag");
_colorProgram.addAttribute("vertexPosition");
_colorProgram.linkShaders();

}


void GraphicsEffects::draw(int x, int y){
int pointData[2];
pointData[0] = x;
pointData[1] = y;

glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

_colorProgram.use();

glBindBuffer(GL_ARRAY_BUFFER, _vboID);

glBufferData(GL_ARRAY_BUFFER, sizeof(pointData), pointData, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0, 0);

glDrawArrays(GL_POINT, 0, 1);

glBindBuffer(GL_ARRAY_BUFFER, 0);

_colorProgram.unuse();

SDL_GL_SwapWindow(_window);


}

对于Shaders / color.vert:“顶点着色器文件”

#version 130

in vec2 vertexPosition;

void main() {
    gl_Position.xy = vertexPosition;
    gl_Position.z = 0;
    gl_Position.w = 1.0;
}

for Shader / color.frag:“fragment shader”

#version 130 

out vec4 color ;

void main() {
    color = vec4(1.0, 1.0, 0.0, 1.0);
}

我预计我会用我的(x,y)动作得分,但窗户上什么都没有出现!

1 个答案:

答案 0 :(得分:0)

这一行

glDrawArrays(GL_POINT, 0, 1);

将生成GL_INVALID_ENUM错误,不执行任何其他操作。正确的值为GL_POINTS,而不是GL_POINT

您应该添加错误检查,至少对于开发版本。如果可能,请查看debug output functionality较新的OpenGL版本。