Skip to content

Commit

Permalink
generated methods with pointer params, almost functional
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpvar committed Jun 1, 2010
1 parent 748cc79 commit b0f6b5d
Show file tree
Hide file tree
Showing 7 changed files with 1,824 additions and 825 deletions.
96 changes: 82 additions & 14 deletions example/nehe/lesson6.js
Expand Up @@ -2,45 +2,57 @@ var gl = require(__dirname + "/../../lib/node-ogl"),
sys = require("sys"),
fs = require("fs"),
Buffer = require('buffer').Buffer,
width = 1280,
height = 1024,
width = 640,
height = 480,
xrot = 0,
yrot = 0,
zrot = 0,
file = __dirname + "/lesson6/NeHe.bmp",
textureBuffer,
image = {};
image = { width: 256, height: 256, size: 256*265*3, texture: -1};

gl.OpenWindow(width,height,0,0,0,0,0,0,0);

textureBuffer = fs.readFileSync(file);
textureBuffer = fs.readFileSync(file, "binary");
image.width = (textureBuffer.charCodeAt(19)*256) + textureBuffer.charCodeAt(20);
image.height = (textureBuffer.charCodeAt(21)*256) + textureBuffer.charCodeAt(22);
image.buffer = new Buffer(image.size);
image.buffer.write(textureBuffer.slice(24,textureBuffer.length));

image = {buffer: textureBuffer, width: 0, height: 0, texture: [0]};
for (var i=0;i<image.size;i+=3) { // reverse all of the colors. (bgr -> rgb)
var temp = image.buffer[i];
image.buffer[i] = image.buffer[i+2];
image.buffer[i+2] = temp;
}

// Create Texture
gl.glGenTextures(1, image.texture[0]);
gl.glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size)
var asdf;
sys.puts("ret'd: " + gl.glGenTextures(1, asdf));// Create Texture
sys.puts("ret'd: " + gl.glGenTextures(1, image.texture));

sys.puts(sys.inspect(image.texture));

gl.glBindTexture(gl.GL_TEXTURE_2D, image.texture[0]); // 2d texture (x and y size)

// scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_MAG_FILTER,gl.GL_LINEAR);
// scale linearly when image smalled than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_MIN_FILTER,gl.GL_LINEAR);

gl.glTexImage2D(gl.GL_TEXTURE_2D, // 2d texture
0, // level of detail 0 (normal)
3, // 3 components (red green blue)
width, // x size from image
height, // y size from image
image.width, // x size from image
image.height, // y size from image
0, // border 0 (normal)
gl.GL_RGB, // rgb color data
gl.GL_UNSIGNED_BYTE, // unsigned byte data
buffer); // the actual data
image.buffer); // the actual data




gl.glShadeModel(gl.GL_SMOOTH);
gl.glClearColor(1.0, 1.0, 1.0, 1.0);
gl.glClearColor(0.0, 0.0, 0.0, 0.0);
gl.glClearDepth(1.0); // Depth Buffer Setup
gl.glEnable(gl.GL_TEXTURE_2D); // Enable Texture Mapping
gl.glEnable(gl.GL_DEPTH_TEST); // Enables Depth Testing
Expand All @@ -54,4 +66,60 @@ gl.glMatrixMode(gl.GL_MODELVIEW);
//gl.glFlush();


//gl.glFlush();
setInterval(function() {
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
gl.glLoadIdentity(); // Reset The View

gl.glTranslatef(0.0,0.0,-5.0); // move 5 units into the screen.

gl.glRotatef(xrot,1.0,0.0,0.0); // Rotate On The X Axis
gl.glRotatef(yrot,0.0,1.0,0.0); // Rotate On The Y Axis
gl.glRotatef(zrot,0.0,0.0,1.0); // Rotate On The Z Axis

gl.glBindTexture(gl.GL_TEXTURE_2D, image.texture[0]); // choose the texture to use.

gl.glBegin(gl.GL_QUADS); // begin drawing a cube

// Front Face (note that the texture's corners have to match the quad's corners)
gl.glTexCoord2f(0.0, 0.0); gl.glVertex3f(-1.0, -1.0, 1.0); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0, 0.0); gl.glVertex3f( 1.0, -1.0, 1.0); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0, 1.0); gl.glVertex3f( 1.0, 1.0, 1.0); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0, 1.0); gl.glVertex3f(-1.0, 1.0, 1.0); // Top Left Of The Texture and Quad

// Back Face
gl.glTexCoord2f(1.0, 0.0); gl.glVertex3f(-1.0, -1.0, -1.0); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0, 1.0); gl.glVertex3f(-1.0, 1.0, -1.0); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0, 1.0); gl.glVertex3f( 1.0, 1.0, -1.0); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0, 0.0); gl.glVertex3f( 1.0, -1.0, -1.0); // Bottom Left Of The Texture and Quad

// Top Face
gl.glTexCoord2f(0.0, 1.0); gl.glVertex3f(-1.0, 1.0, -1.0); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0, 0.0); gl.glVertex3f(-1.0, 1.0, 1.0); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0, 0.0); gl.glVertex3f( 1.0, 1.0, 1.0); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0, 1.0); gl.glVertex3f( 1.0, 1.0, -1.0); // Top Right Of The Texture and Quad

// Bottom Face
gl.glTexCoord2f(1.0, 1.0); gl.glVertex3f(-1.0, -1.0, -1.0); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0, 1.0); gl.glVertex3f( 1.0, -1.0, -1.0); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0, 0.0); gl.glVertex3f( 1.0, -1.0, 1.0); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0, 0.0); gl.glVertex3f(-1.0, -1.0, 1.0); // Bottom Right Of The Texture and Quad

// Right face
gl.glTexCoord2f(1.0, 0.0); gl.glVertex3f( 1.0, -1.0, -1.0); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0, 1.0); gl.glVertex3f( 1.0, 1.0, -1.0); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0, 1.0); gl.glVertex3f( 1.0, 1.0, 1.0); // Top Left Of The Texture and Quad
gl.glTexCoord2f(0.0, 0.0); gl.glVertex3f( 1.0, -1.0, 1.0); // Bottom Left Of The Texture and Quad

// Left Face
gl.glTexCoord2f(0.0, 0.0); gl.glVertex3f(-1.0, -1.0, -1.0); // Bottom Left Of The Texture and Quad
gl.glTexCoord2f(1.0, 0.0); gl.glVertex3f(-1.0, -1.0, 1.0); // Bottom Right Of The Texture and Quad
gl.glTexCoord2f(1.0, 1.0); gl.glVertex3f(-1.0, 1.0, 1.0); // Top Right Of The Texture and Quad
gl.glTexCoord2f(0.0, 1.0); gl.glVertex3f(-1.0, 1.0, -1.0); // Top Left Of The Texture and Quad

gl.glEnd(); // done with the polygon.
gl.SwapBuffers();
xrot+=15.0; // X Axis Rotation
yrot+=15.0; // Y Axis Rotation
zrot+=15.0; // Z Axis Rotation
}, 30);

0 comments on commit b0f6b5d

Please sign in to comment.