Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created July 10, 2013 14:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roxlu/5966828 to your computer and use it in GitHub Desktop.
Save roxlu/5966828 to your computer and use it in GitHub Desktop.
Attribute-less rendering with openGL - you're not allowed to draw with the default VertexArray so you need to create one.
#include <assert.h>
#include <roxlu/core/Log.h>
#include "Oils.h"
Oils::Oils()
:win_w(0)
,win_h(0)
,br_prog(0)
,br_vert(0)
,br_frag(0)
,br_vao(0)
{
}
Oils::~Oils() {
}
bool Oils::setup(int winW, int winH) {
assert(winW);
assert(winH);
assert(br_prog == 0);
win_w = winW;
win_h = winH;
br_vert = glCreateShader(GL_VERTEX_SHADER); glShaderSource(br_vert, 1, &OS_VS,NULL); glCompileShader(br_vert); eglGetShaderInfoLog(br_vert);
br_frag = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(br_frag, 1, &OS_FS,NULL); glCompileShader(br_frag); eglGetShaderInfoLog(br_frag);
br_prog = glCreateProgram(); glAttachShader(br_prog,br_vert); glAttachShader(br_prog,br_frag); glLinkProgram(br_prog); eglGetShaderLinkLog(br_prog);
glUseProgram(br_prog);
float pm[16] = { 0 };
rx_ortho_top_left(win_w, win_h, -1.0f, 1.0f, pm);
GLint u_pm = glGetUniformLocation(br_prog, "u_pm");
glUniformMatrix4fv(u_pm, 1, GL_FALSE, pm);
glGenVertexArrays(1, &br_vao);
return true;
}
void Oils::draw() {
glUseProgram(br_prog);
glBindVertexArray(br_vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
#ifndef ROXLU_OILS_H
#define ROXLU_OILS_H
#include <glr/GL.h>
#include <roxlu/math/Random.h>
#include <roxlu/math/Vec2.h>
#include <roxlu/opengl/GL.h>
#include <roxlu/opengl/Error.h>
using namespace roxlu;
using namespace gl;
static const char* OS_VS = ""
"#version 150\n"
"uniform mat4 u_pm;"
"const float w = 40.0;"
"const float h = 100.0;"
"const vec2 data[4] = vec2[] ("
" vec2(-w, h), "
" vec2(-w, -h), "
" vec2( w, h), "
" vec2( w, -h) "
");"
"void main() {"
" gl_Position = u_pm * vec4(data[gl_VertexID], 0.0, 1.0);"
"}"
"";
static const char* OS_FS = ""
"#version 150\n"
"out vec4 fragcolor;"
"void main() {"
" fragcolor = vec4(1.0); "
"}"
"";
class Oils {
public:
Oils();
~Oils();
bool setup(int winW, int winH);
void draw();
public:
int win_w;
int win_h;
GLuint br_prog;
GLuint br_vert;
GLuint br_frag;
GLuint br_vao;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment