Skip to content

Commit

Permalink
now able to clear the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpvar committed Aug 2, 2011
1 parent 9a31399 commit ba19672
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
67 changes: 38 additions & 29 deletions build/main.cc
Expand Up @@ -3,11 +3,6 @@
#include <assert.h>
#include <unistd.h>

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/graphics/IOGraphicsLib.h>
#include <Accelerate/Accelerate.h>
#include <ApplicationServices/ApplicationServices.h>

#include "../src/typedarray/ArrayBuffer.h"

using namespace std;
Expand Down Expand Up @@ -41,33 +36,25 @@ CGDirectDisplayID getDisplay(unsigned int index) {
return onlineDisplays[index];
}

void draw(WebGLRenderingContext *gl, WebGLProgram *program, Float32Array *array) {
gl->clearColor(1, 0, 1, 1);
cout << "clear color: " << gluErrorString(glGetError()) << endl;

gl->clear(WebGLRenderingContext::COLOR_BUFFER_BIT);
cout << "clear: " << gluErrorString(glGetError()) << endl;

void draw(WebGLRenderingContext *gl, WebGLProgram *program, Float32Array *array, DOMString *pos) {
gl->bindBuffer(WebGLRenderingContext::ARRAY_BUFFER, gl->createBuffer());
cout << "bind buffer: " << gluErrorString(glGetError()) << endl;

gl->bufferData(WebGLRenderingContext::ARRAY_BUFFER, array, WebGLRenderingContext::STATIC_DRAW);
cout << "buffer data: " << gluErrorString(glGetError()) << endl;

DOMString *pos = new DOMString();
pos->value = "pos";

GLint attr = gl->getAttribLocation(program, pos);
cout << "attrib location" << attr << " ERROR: " << gluErrorString(glGetError()) << endl;
gl->enableVertexAttribArray(attr);
cout << "enable vertex attribute array: " << gluErrorString(glGetError()) << endl;

gl->vertexAttribPointer(attr, 3, WebGLRenderingContext::FLOAT, false, 0, 0);
gl->vertexAttribPointer(attr, sizeof(GLfloat), WebGLRenderingContext::FLOAT, false, 0, 0);

cout << "vertex attribute pointer: " << gluErrorString(glGetError()) << endl;
gl->drawArrays(WebGLRenderingContext::TRIANGLE_STRIP, 0, 4);

gl->drawArrays(WebGLRenderingContext::TRIANGLES, 0, 3);
cout << "draw arrays: " << gluErrorString(glGetError()) << endl;

}

int main() {
Expand All @@ -79,9 +66,12 @@ int main() {
displayMask = CGDisplayIDToOpenGLDisplayMask(targetDisplay);

CGLPixelFormatAttribute attribs[] = {
kCGLPFANoRecovery,
kCGLPFADoubleBuffer,
kCGLPFAFullScreen,
kCGLPFAStencilSize, ( CGLPixelFormatAttribute ) 8,
kCGLPFADisplayMask,
(CGLPixelFormatAttribute)displayMask,
(CGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask( kCGDirectMainDisplay ),
(CGLPixelFormatAttribute)NULL
};

Expand All @@ -91,16 +81,21 @@ int main() {
CGLCreateContext(pixelFormatObj, NULL, &context);
CGLDestroyPixelFormat(pixelFormatObj);
CGLSetCurrentContext(context);
CGLRetainContext(context);
CGLSetFullScreenOnDisplay(context, displayMask);

WebGLRenderingContext *gl = new WebGLRenderingContext();
WebGLProgram *program = gl->createProgram();

DOMString *pos = new DOMString();
pos->value = "pos";

DOMString *vertex_shader = new DOMString();
vertex_shader->value = "attribute vec3 pos;\nvoid main() {\n gl_Position = vec4(pos, 2.0);\n}";

DOMString *fragment_shader = new DOMString();
fragment_shader->value = "void main() {\n gl_FragColor = vec4(0.5, 0.5, 1.0, 1.0);\n}";
fragment_shader->value = "void main() {\n gl_FragColor = vec4(1.0, 0, 1, 1.0);\n}";


addShader(gl, program, WebGLRenderingContext::VERTEX_SHADER, vertex_shader);
addShader(gl, program, WebGLRenderingContext::FRAGMENT_SHADER, fragment_shader);
Expand All @@ -110,24 +105,38 @@ int main() {

gl->useProgram(program);

Float32Array *array = new Float32Array(12);
GLfloat tmp[12] = {
-1, 0, 0,
Float32Array *array = new Float32Array(9);
GLfloat tmp[9] = {
0, 1, 0,
0, -1, 0,
1, 0, 0
-1, -1, 0,
1, -1, 0,
};

// I'm not proud of this.
memcpy(tmp, array->data, array->size);

gl->enable(WebGLRenderingContext::DEPTH_TEST);
// 5 frames for now
int a = 5;
int a = 500;
int interval = 1;
while(a--) {
draw(gl, program, array);
usleep(100000);
gl->viewport(0, 0, 1600, 1200);

gl->clearColor(0.5, 0.5, 0.5, 1);
cout << "clear color: " << gluErrorString(glGetError()) << endl;

gl->clear(WebGLRenderingContext::COLOR_BUFFER_BIT | WebGLRenderingContext::DEPTH_BUFFER_BIT);
cout << "clear: " << gluErrorString(glGetError()) << endl;

gluPerspective(45, 1600 / 1200, 0.1, 100.0);
glLoadIdentity();
glTranslatef(-1.5, 0.0, -7.0);
draw(gl, program, array, pos);
CGLSetParameter( context, kCGLCPSwapInterval, &interval );
CGLFlushDrawable( context );
}

CGLReleaseContext(context);
cout << "Done!" << endl;
return 0;
}
Binary file modified build/test
Binary file not shown.
2 changes: 1 addition & 1 deletion src/WebGLProgram.cc
Expand Up @@ -15,7 +15,7 @@ WebGLProgram::WebGLProgram() {
}

WebGLProgram::~WebGLProgram() {

glDeleteProgram(this->id);
}

void WebGLProgram::link() {
Expand Down
4 changes: 2 additions & 2 deletions src/WebGLRenderingContext.cc
Expand Up @@ -94,11 +94,11 @@ void WebGLRenderingContext::bufferData(GLenum target, GLsizeiptr size, GLenum us
}

void WebGLRenderingContext::bufferData(GLenum target, ArrayBufferView *data, GLenum usage) {
glBufferData(target, data->length, (void *)&data, usage);
glBufferData(target, data->length, (GLvoid *)&data, usage);
}

void WebGLRenderingContext::bufferData(GLenum target, ArrayBuffer *data, GLenum usage) {
glBufferData(target, sizeof(GLfloat) * 12, (void *)&data->data, usage);
glBufferData(target, data->size, data->data, usage);
}

void WebGLRenderingContext::bufferSubData(GLenum target, GLintptr offset, ArrayBufferView *data) {
Expand Down
2 changes: 2 additions & 0 deletions src/arch/wrapper.h
Expand Up @@ -8,6 +8,8 @@
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <CoreFoundation/CoreFoundation.h>
#include <ApplicationServices/ApplicationServices.h>
#else
#ifdef _WIN32
#include <windows.h>
Expand Down
2 changes: 1 addition & 1 deletion src/typedarray/ArrayBuffer.h
Expand Up @@ -18,7 +18,7 @@ class ArrayBuffer {
public:

GLuint length;
GLfloat *data;
void *data;
unsigned int size;

ArrayBuffer();
Expand Down

0 comments on commit ba19672

Please sign in to comment.