Created
May 24, 2012 07:36
-
-
Save roxlu/2780048 to your computer and use it in GitHub Desktop.
Basic bare bones openGL with openFrameworks (VBO, VAO, SHADER and texture(s))
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(){ | |
ofEnableNormalizedTexCoords(); | |
ofDisableArbTex(); | |
ofSetFrameRate(60); | |
ofBackground(22,33,44); | |
// Shader + VAO/VBO | |
// ----------------- | |
// create shader program | |
shader.setupShaderFromSource(GL_VERTEX_SHADER, VS); | |
shader.setupShaderFromSource(GL_FRAGMENT_SHADER, FS); | |
shader.linkProgram(); | |
GLint pos_attrib = glGetAttribLocation(shader.getProgram(), "pos"); | |
GLint tex_attrib = glGetAttribLocation(shader.getProgram(), "tex"); | |
printf("TEX: %d\n", tex_attrib); | |
// 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, i/float(N), 1.0 - j/float(N)); | |
} | |
} | |
// 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 for the pos. | |
glEnableVertexAttribArray(tex_attrib); // enable vertex attribs for the texcoord. | |
glVertexAttribPointer(pos_attrib, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, pos)); // where to find the pos atribute | |
glVertexAttribPointer(tex_attrib, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, tex)); // where to find the pos atribute | |
// Texture | |
// ---------- | |
ofImage img; | |
if(!img.loadImage("http://www.roxlu.com/assets/downloads/freakday.png")) { | |
printf("cannot load.\n"); | |
} | |
glEnable(GL_TEXTURE_2D); | |
glGenTextures(1, &tex); | |
glBindTexture(GL_TEXTURE_2D, tex); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.getWidth(), img.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.getPixels()); | |
} | |
//-------------------------------------------------------------- | |
void testApp::draw(){ | |
glEnable(GL_BLEND); | |
// translate to center. | |
view_matrix.setTranslation(-HALF_WIDTH, -HALF_HEIGHT, -10.0f); | |
// draw with our shader. | |
shader.begin(); | |
// we assign our texture to the texture unit 2 (only showing that we're not using the default (0) unit | |
glEnable(GL_TEXTURE_2D); | |
glActiveTexture(GL_TEXTURE2); | |
shader.setUniform1i("texture", 2); | |
glBindTexture(GL_TEXTURE_2D, tex); | |
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 100 | |
#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; \ | |
attribute vec2 tex; \ | |
uniform mat4 view_matrix; \ | |
uniform mat4 projection_matrix; \ | |
varying vec2 vtex; \ | |
\ | |
void main() { \ | |
gl_Position = projection_matrix * view_matrix * pos;\ | |
vtex = tex; \ | |
} "; | |
static const string FS = " \ | |
uniform sampler2D texture; \ | |
varying vec2 vtex; \ | |
void main() { \ | |
gl_FragColor = texture2D(texture, vtex); \ | |
} "; | |
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; | |
GLuint tex; | |
}; |
Author
roxlu
commented
May 24, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment