Skip to content

Instantly share code, notes, and snippets.

@armadillu
Created January 28, 2014 15:14
Show Gist options
  • Save armadillu/8669388 to your computer and use it in GitHub Desktop.
Save armadillu/8669388 to your computer and use it in GitHub Desktop.
Delayed Texture load - over several frame - OpenGL
#include "testApp.h"
void testApp::setup(){
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(22);
ofEnableAlphaBlending();
ofDisableArbTex();
img.loadImage("world4096.jpg");
img2.loadImage("crap4096.jpg"); //this will be loaded into gpu over frames
startLoading = false;
}
void testApp::update(){}
int loadedScanLinesSoFar = 0;
void testApp::draw(){
int texW = img.getWidth();
int texH = img.getHeight(); // == # scanLines
int numLinesPerFrame = 1; //we can increase that to reduce overhead
float maxTimeTakenPerFrame = 0.0003; //max seconds to spend loading tex data on a single frame
ofTextureData texData = img.getTextureReference().texData;
if(loadedScanLinesSoFar < texH && startLoading){
////////////////////////////////////////////////////
float startTime = ofGetElapsedTimef();
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texData.textureID );
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texData.textureID);
float currentTime = 0;
while (currentTime < maxTimeTakenPerFrame && loadedScanLinesSoFar < texH ) {
unsigned char * data = img2.getPixels() + 3 * texW * loadedScanLinesSoFar; //assuming RGB here TODO!
glTexSubImage2D(GL_TEXTURE_2D,
0, //level
0, //x offset
loadedScanLinesSoFar, //y offset
texW, //width
numLinesPerFrame, //height >> numLinesPerFrame line per iteration
GL_RGB, //format
GL_UNSIGNED_BYTE,
data
);
loadedScanLinesSoFar += numLinesPerFrame;
currentTime = ofGetElapsedTimef() - startTime;
}
glDisable(GL_TEXTURE_2D);
////////////////////////////////////////////////////
}
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texData.textureID );
//tex quad
glBegin(GL_QUADS);
glTexCoord2i(0,0);
glVertex2i(0,0);
glTexCoord2i(1,0);
glVertex2i(ofGetWidth(),0);
glTexCoord2i(1,1);
glVertex2i(ofGetWidth(),ofGetHeight());
glTexCoord2i(0,1);
glVertex2i(0,ofGetHeight());
glEnd();
glDisable(GL_TEXTURE_2D);
//clock
ofPushMatrix();
ofTranslate(50, 50);
ofSetColor(0);
ofCircle(0, 0, 50);
ofRotate(ofGetFrameNum(), 0, 0, 1);
ofSetColor(255);
ofRect(0, 0, 50, 4);
ofPopMatrix();
if(startLoading){
ofDrawBitmapStringHighlight("loaded " + ofToString(loadedScanLinesSoFar) + " lines so far (" + ofToString(texW) + ")", ofGetWidth() / 2, 20);
}else{
ofDrawBitmapStringHighlight("click anywhere to start delayed Texture Load", ofGetWidth() / 2, 20);
}
ofDrawBitmapStringHighlight( "fps: " + ofToString(ofGetFrameRate()), ofGetWidth() / 2, 40);
}
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){
startLoading = true;
}
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){
}
#pragma once
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
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);
ofImage img;
ofImage img2;
bool startLoading;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment