Created
May 24, 2012 07:15
-
-
Save roxlu/2779991 to your computer and use it in GitHub Desktop.
Basic bare bones openGL (shader,vbo,vao) with openFrameworks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "testApp.h" | |
//-------------------------------------------------------------- | |
void testApp::setup(){ | |
ofSetFrameRate(60); | |
ofBackground(22,33,44); | |
// create shader program | |
shader.setupShaderFromSource(GL_VERTEX_SHADER, VS); | |
shader.setupShaderFromSource(GL_FRAGMENT_SHADER, FS); | |
shader.linkProgram(); | |
GLint pos_attrib = glGetAttribLocation(shader.getProgram(), "pos"); | |
// setup matrices to replace GL_MODELVIEW and GL_PROJECTION | |
view_matrix.translate(0.0f,0.0f,-10.0f); | |
projection_matrix.makePerspectiveMatrix(65.0f, ofGetWidth()/ofGetHeight(), 0.1f, 100.0f); | |
// create the mesh (of N by N points) | |
float di = float(WIDTH) / N; | |
float dj = float(HEIGHT) / N; | |
mesh.assign(NUM); | |
for(int i = 0; i < N; ++i) { | |
for(int j = 0; j < N; ++j) { | |
mesh[DX(i,j)].set(i * di, j * dj, 0, di/float(WIDTH), dj/float(HEIGHT)); | |
} | |
} | |
// create VBO & VAO | |
glGenVertexArraysAPPLE(1, &vao); | |
glBindVertexArrayAPPLE(vao); | |
glGenBuffers(1, &vbo); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, mesh.getNumBytes(), mesh.getPtr(), GL_STATIC_DRAW); // pass data to vbo | |
glEnableVertexAttribArray(pos_attrib); // enable vertex attribs so we can use them in the shader | |
glVertexAttribPointer(pos_attrib, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, pos)); // where to find the pos atribute | |
} | |
//-------------------------------------------------------------- | |
void testApp::draw(){ | |
// translate to center. | |
view_matrix.setTranslation(-HALF_WIDTH, -HALF_HEIGHT, -10.0f); | |
// draw with our shader. | |
shader.begin(); | |
shader.setUniformMatrix4f("view_matrix", view_matrix); | |
shader.setUniformMatrix4f("projection_matrix", projection_matrix); | |
glBindVertexArrayAPPLE(vao); | |
glDrawArrays(GL_POINTS, 0, mesh.size()); | |
shader.end(); | |
} | |
void testApp::debugDraw() { | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45.0f, ofGetWidth()/ofGetHeight(), 0.1, 100.0f); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glColor3f(1.0f, 1.0f, 1.0f); | |
glTranslatef(-HALF_WIDTH,-HALF_HEIGHT,-10.0f); | |
glPointSize(1); | |
glBegin(GL_POINTS); | |
for(int i = 0; i < N; ++i) { | |
for(int j = 0; j < N; ++j) { | |
glVertex3fv(mesh[DX(i,j)].pos.getPtr()); | |
} | |
} | |
glEnd(); | |
} | |
//-------------------------------------------------------------- | |
void testApp::update(){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::keyPressed(int key){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::keyReleased(int key){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseMoved(int x, int y){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseDragged(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mousePressed(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseReleased(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::windowResized(int w, int h){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::gotMessage(ofMessage msg){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::dragEvent(ofDragInfo dragInfo){ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#define N 20 | |
#define WIDTH 3 | |
#define HEIGHT 5 | |
#define HALF_WIDTH (WIDTH*0.5) | |
#define HALF_HEIGHT (HEIGHT*0.5) | |
#define NUM (N*N) | |
#define DX(i,j) (((j)*(N)) + (i)) | |
static const string VS = " \ | |
attribute vec4 pos; \ | |
uniform mat4 view_matrix; \ | |
uniform mat4 projection_matrix; \ | |
\ | |
void main() { \ | |
gl_Position = projection_matrix * view_matrix * pos;\ | |
} "; | |
static const string FS = " \ | |
void main() { \ | |
gl_FragColor = vec4(1.0, 0.0, 0.2, 1.0); \ | |
} "; | |
struct Vertex { | |
ofVec3f pos; | |
ofVec2f tex; | |
Vertex() { | |
} | |
Vertex(const float& x, const float& y, const float& z, const float& u, const float& v) | |
:pos(x,y,z) | |
,tex(u,v) | |
{ | |
} | |
void set(const float& x, const float& y, const float& z, const float& u, const float& v) { | |
pos.set(x,y,z); | |
tex.set(u,v); | |
} | |
}; | |
struct SimpleMesh { | |
int add(const Vertex& v) { | |
vertices.push_back(v); | |
return vertices.size() - 1; | |
} | |
float* getPtr() { | |
return &vertices[0].pos.x; | |
} | |
size_t size() { | |
return vertices.size(); | |
} | |
size_t getNumBytes() { | |
return vertices.size() * sizeof(Vertex); | |
} | |
void assign(size_t num) { | |
vertices.assign(num, Vertex()); | |
} | |
Vertex& operator[](const unsigned int& dx) { | |
return vertices.at(dx); | |
} | |
vector<Vertex> vertices; | |
}; | |
class testApp : public ofBaseApp{ | |
public: | |
void setup(); | |
void update(); | |
void draw(); | |
void debugDraw(); | |
void keyPressed(int key); | |
void keyReleased(int key); | |
void mouseMoved(int x, int y); | |
void mouseDragged(int x, int y, int button); | |
void mousePressed(int x, int y, int button); | |
void mouseReleased(int x, int y, int button); | |
void windowResized(int w, int h); | |
void dragEvent(ofDragInfo dragInfo); | |
void gotMessage(ofMessage msg); | |
ofShader shader; | |
ofMatrix4x4 view_matrix; | |
ofMatrix4x4 projection_matrix; | |
SimpleMesh mesh; | |
GLuint vbo; | |
GLuint vao; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: