Created
April 21, 2018 10:22
-
-
Save sortofsleepy/0559865c407e641c64034bcc8e08985e to your computer and use it in GitHub Desktop.
Fullscreen quad + mesh scene testing
This file contains hidden or 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 "ofApp.h" | |
| //! vertex data to render the camera image | |
| float kImagePlaneVertexData[8] = { | |
| -1.0, -1.0, | |
| 1.0, -1.0, | |
| -1.0, 1.0, | |
| 1.0, 1.0 | |
| }; | |
| float verts[16] = { | |
| -1.0, -1.0, 0.0, 1.0, | |
| 1.0, -1.0, 1.0, 1.0, | |
| -1.0, 1.0, 0.0, 0.0, | |
| 1.0, 1.0, 1.0, 0.0, | |
| }; | |
| float texCoords[8] = { | |
| 0.0, 1.0, | |
| 1.0,1.0, | |
| 0.0,0.0, | |
| 1.0,0.0 | |
| }; | |
| float aspect = 0.0f; | |
| float diffX = 0.0f; | |
| float diffY = 0.0f; | |
| ofVec2f aspectRatio; | |
| //-------------------------------------------------------------- | |
| void ofApp::setup() { | |
| ofDisableArbTex(); | |
| image.load("image.jpg"); | |
| cap = ofRectangle(0, 0, image.getWidth(),image.getHeight()); | |
| screen = ofRectangle(0, 0, ofGetWindowWidth(), ofGetWindowHeight()); | |
| aspect = 1; | |
| ofLog() << (float)ofGetWindowWidth() / (float)ofGetWindowHeight(); | |
| float ratio = (float)ofGetWindowWidth() / (float)ofGetWindowHeight(); | |
| aspectRatio.x = (float)aspect; | |
| aspectRatio.y = (float)aspect; | |
| // 2 should instead be the scale factor from [[UIScreen mainScreen] scale] | |
| // or maybe aspect ratio? | |
| cap.scaleFromCenter(ofPoint(ratio,ratio)); | |
| diffX = cap.getWidth() - ofGetWindowWidth(); | |
| diffY = cap.getHeight() - ofGetWindowHeight(); | |
| diffX /= 2.0; | |
| diffY /= 2.0; | |
| //vMesh.setVertexData(kImagePlaneVertexData, 4, 8, GL_DYNAMIC_DRAW); | |
| vMesh.setVertexData(verts, 4, 16, GL_DYNAMIC_DRAW); | |
| vMesh.enableTexCoords(); | |
| vMesh.setTexCoordData(texCoords, 8, GL_DYNAMIC_DRAW); | |
| fbo.allocate(ofGetWindowWidth(),ofGetWindowHeight(), GL_RGBA); | |
| drawFbo.allocate(cap.getWidth(),cap.getHeight(), GL_RGBA); | |
| shader.setupShaderFromSource(GL_VERTEX_SHADER, camera_convert_vertex); | |
| shader.setupShaderFromSource(GL_FRAGMENT_SHADER, camera_convert_fragment); | |
| shader.bindDefaults(); | |
| shader.linkProgram(); | |
| plane = ofMesh::plane(image.getWidth(),image.getHeight()); | |
| sphere = ofMesh::sphere(200); | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::update() { | |
| /* | |
| drawFbo.begin(); | |
| shader.begin(); | |
| shader.setUniformTexture("image", image.getTexture(), 0); | |
| shader.setUniform2f("aspectRatio", aspectRatio.x,aspectRatio.y); | |
| vMesh.draw(GL_TRIANGLE_STRIP, 0, 16); | |
| shader.end(); | |
| drawFbo.end(); | |
| fbo.begin(); | |
| drawFbo.draw(-diffX, -diffY); | |
| fbo.end(); | |
| */ | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::draw() { | |
| //ofEnableDepthTest(); | |
| //fbo.draw(0,0,ofGetWindowWidth(),ofGetWindowHeight()); | |
| shader.begin(); | |
| shader.setUniformTexture("image", image.getTexture(), 0); | |
| shader.setUniform2f("aspectRatio", aspectRatio.x, aspectRatio.y); | |
| vMesh.draw(GL_TRIANGLE_STRIP, 0, 16); | |
| shader.end(); | |
| ofPushMatrix(); | |
| ofTranslate(ofGetWindowWidth() / 2, ofGetWindowHeight() / 2); | |
| sphere.draw(); | |
| ofPopMatrix(); | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::keyPressed(int key) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::keyReleased(int key) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mouseMoved(int x, int y) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mouseDragged(int x, int y, int button) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mousePressed(int x, int y, int button) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mouseReleased(int x, int y, int button) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mouseEntered(int x, int y) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::mouseExited(int x, int y) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::windowResized(int w, int h) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::gotMessage(ofMessage msg) { | |
| } | |
| //-------------------------------------------------------------- | |
| void ofApp::dragEvent(ofDragInfo dragInfo) { | |
| } |
This file contains hidden or 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 STRINGIFY(A) #A | |
| class ofApp : 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 mouseEntered(int x, int y); | |
| void mouseExited(int x, int y); | |
| void windowResized(int w, int h); | |
| void dragEvent(ofDragInfo dragInfo); | |
| void gotMessage(ofMessage msg); | |
| int captureWidth = 1280; | |
| int captureHeight = 720; | |
| ofRectangle cap, screen; | |
| ofFbo fbo,drawFbo; | |
| ofShader shader; | |
| ofMesh plane; | |
| ofImage image; | |
| int scale = 2; | |
| //! mesh to render camera image | |
| ofVbo vMesh; | |
| ofMesh renderPlane; | |
| ofShader renderShader; | |
| ofMesh sphere; | |
| const std::string camera_convert_vertex = STRINGIFY( | |
| attribute vec2 position; | |
| attribute vec2 texcoord; | |
| varying vec2 vUv; | |
| varying vec2 uv; | |
| uniform mat4 projectionMatrix; | |
| uniform mat4 modelViewMatrix; | |
| uniform mat4 modelViewProjectionMatrix; | |
| const vec2 scale = vec2(0.5, 0.5); | |
| void main() { | |
| vUv = position.xy * scale + scale; | |
| uv = texcoord; | |
| gl_Position = vec4(position, 1.0, 1.0); | |
| } | |
| ); | |
| const std::string camera_convert_fragment = STRINGIFY( | |
| // this is the yyuv texture from ARKit | |
| uniform sampler2D image; | |
| uniform vec2 aspectRatio; | |
| varying vec2 vUv; | |
| varying vec2 uv; | |
| void main() { | |
| // flip uvs so image isn't inverted. | |
| //vec2 textureCoordinate = 1.0 - vec2(vUv.s, vUv.t); | |
| vec2 textureCoordinate = vUv; | |
| textureCoordinate.x *= aspectRatio.x; | |
| textureCoordinate.y *= aspectRatio.y; | |
| vec4 tex = texture2D(image, textureCoordinate); | |
| gl_FragColor = tex; | |
| } | |
| ); | |
| const std::string render_vertex = STRINGIFY( | |
| attribute vec2 position; | |
| varying vec2 vUv; | |
| uniform mat4 viewMatrix; | |
| uniform mat4 projectionMatrix; | |
| uniform float aspect; | |
| //const vec2 scale = vec2(0.5, 0.5); | |
| void main() { | |
| vec2 scale = vec2(aspect, 0.5); | |
| vUv = position.xy * scale + scale; | |
| gl_Position = projectionMatrix * viewMatrix * vec4(position, -5.0, 1.0); | |
| } | |
| ); | |
| const std::string render_fragment = STRINGIFY( | |
| // this is the yyuv texture from ARKit | |
| uniform sampler2D image; | |
| varying vec2 vUv; | |
| void main() { | |
| // flip uvs so image isn't inverted. | |
| vec2 textureCoordinate = 1.0 - vec2(1.0 - vUv.s, vUv.t); | |
| vec4 tex = texture2D(image, textureCoordinate); | |
| gl_FragColor = tex; | |
| } | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment