2003/09/05 04:28:35
[org.ibex.core.git] / src / org / xwt / plat / OpenGL.cc
diff --git a/src/org/xwt/plat/OpenGL.cc b/src/org/xwt/plat/OpenGL.cc
new file mode 100644 (file)
index 0000000..f23c179
--- /dev/null
@@ -0,0 +1,269 @@
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [LGPL]
+// Author: Brian Alliet
+
+// IMPROVMENT: use alpha testing? might be faster
+
+#include <org/xwt/plat/OpenGL.h>
+#include <org/xwt/plat/OpenGL$GLDoubleBuffer.h>
+#include <org/xwt/plat/OpenGL$GLPicture.h>
+#include <org/xwt/plat/OpenGL$RectGLPicture.h>
+#include <org/xwt/plat/OpenGL$SquareGLPicture.h>
+#include <org/xwt/plat/OpenGL$MosaicGLPicture.h>
+
+#include <java/lang/Error.h>
+
+#ifdef __APPLE__
+#include <OpenGL/gl.h>
+#include <OpenGL/glu.h>
+#include <OpenGL/glext.h>
+#else
+#warning I am not sure if these is correct
+#include <gl/gl.h>
+#include <gl/glu.h>
+#include <gl/glext.h>
+#endif
+
+#include <stdio.h>
+
+namespace org { namespace xwt { namespace plat {
+
+#define checkGLError() checkGLError2(__FILE__,__LINE__)
+static void checkGLError2(const char *file,int line) {
+    GLenum err = glGetError();
+    if(err == GL_NO_ERROR) return;
+
+    fprintf(stderr,"%s:%d: GL Error: %s",file,line,(char *) gluErrorString(err));
+    exit(EXIT_FAILURE);
+}
+
+void OpenGL::natInit() {
+    GLint i;
+    const GLubyte *s;
+    activateSharedContext();
+    s = glGetString(GL_EXTENSIONS);
+    rectangularTextures = (jboolean) gluCheckExtension((GLubyte*)"GL_EXT_texture_rectangle",s);
+    glGetIntegerv(GL_MAX_TEXTURE_SIZE,&i);
+    maxTexSize = (jint) i;
+    if(rectangularTextures) {
+        glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_EXT,&i);
+        maxRectTexSize = (jint) i;
+    }
+    s = glGetString(GL_VENDOR);
+    vendor = JvNewStringLatin1(s==0 ? "" : (char*)s);
+    s = glGetString(GL_RENDERER);
+    renderer = JvNewStringLatin1(s==0 ? "" : (char*)s);
+    s = glGetString(GL_VERSION);
+    version = JvNewStringLatin1(s==0 ? "" : (char*)s);
+}
+
+void OpenGL$GLDoubleBuffer::drawableInit(jint width, jint height) {
+    glClearColor (0.3f, 0.7f, 1.0f, 1.0f);
+    glClearDepth( 0.0f );
+    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
+    glShadeModel(GL_SMOOTH);
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+    glViewport(0,0,width,height);
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity(); 
+    glOrtho(0,width,height,0,-1,1);
+    // CHECKME: This causes textures to be blurry, but something like this
+    // (but not this) might be required to draw straight lines   
+    //glMatrixMode(GL_MODELVIEW);
+    //glLoadIdentity(); 
+    //glTranslatef(0.375, 0.375, 0.0);    
+    
+    checkGLError();
+}
+
+void OpenGL$GLDoubleBuffer::setColor(jint argb) {
+    float alpha = ((argb >> 24) & 0xff) / 255.0;
+    float red   = ((argb >> 16) & 0xff) / 255.0;
+    float green = ((argb >>  8) & 0xff) / 255.0;
+    float blue  = ((argb >>  0) & 0xff) / 255.0;
+    glColor4f(red,green,blue,alpha);
+}
+
+void OpenGL$GLDoubleBuffer::fillTrapezoid(jint x1, jint x2, jint y1, jint x3, jint x4, jint y2, jint color) {
+    activateContext();
+    setColor(color);
+    glBegin(GL_QUADS); 
+        glVertex3f(x1,y1,0.0f );   
+        glVertex3f(x3,y2,0.0f );  
+        glVertex3f(x4,y2,0.0f ); 
+        glVertex3f(x2,y1,0.0f ); 
+    glEnd();
+}
+
+void OpenGL$GLDoubleBuffer::fillRect(jint x1, jint y1, jint x2, jint y2,jint color) {
+    activateContext();
+    setColor(color);
+    glBegin(GL_QUADS); 
+        glVertex3f(x1,y2,0.0f );
+        glVertex3f(x2,y2,0.0f );
+        glVertex3f(x2,y1,0.0f );
+        glVertex3f(x1,y1,0.0f );
+    glEnd();
+}
+
+void OpenGL$GLDoubleBuffer::setClip(jint x1, jint y1, jint x2, jint y2) {
+    //fprintf(stderr,"setClip: %d %d %d %d\n",x1,y1,x2,y2);
+    if(x1==0 && y1==0 && x2==width && y2==height) {
+        if(glScissorEnabled) {
+            activateContext();
+            glDisable(GL_SCISSOR_TEST);
+            glScissorEnabled = false;
+        }
+        return;
+    }
+    activateContext();
+    if(!glScissorEnabled) {
+        glEnable(GL_SCISSOR_TEST);
+        glScissorEnabled = true;
+    }
+    if((x1 >= x2) || (y1 >= y2))
+        glScissor(0,0,0,0);
+    else
+        glScissor(x1,height-y2,x2-x1,y2-y1);
+    checkGLError();
+}
+
+void OpenGL$GLDoubleBuffer::resetClip() {
+    activateContext();
+    if(glScissorEnabled) {
+        glDisable(GL_SCISSOR_TEST);
+        glScissorEnabled = false;
+    }    
+}
+
+void OpenGL$RectGLPicture::natInit(JArray<jint> *data_) {
+    unsigned char *buf;
+    int i,j;
+    int size = width*height;
+    jint *data = elements(data_);
+    buf = new unsigned char[size*4];
+    GLuint tex;
+
+    for(i=0,j=0;i<size;i++,j+=4) {
+        jint pixel = data[i];
+        buf[j+0] = (pixel >> 16) & 0xff;
+        buf[j+1] = (pixel >>  8) & 0xff;
+        buf[j+2] = (pixel >>  0) & 0xff;
+        buf[j+3] = (pixel >> 24) & 0xff;
+    }
+    
+    gl->activateSharedContext();
+    glEnable(GL_TEXTURE_RECTANGLE_EXT);
+    glGenTextures(1,&tex);
+    glBindTexture(GL_TEXTURE_RECTANGLE_EXT,tex);
+    glPixelStorei(GL_PACK_ALIGNMENT,1);
+    glTexImage2D(GL_TEXTURE_RECTANGLE_EXT,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);
+    delete buf;
+
+    glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
+    glTexParameterf(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+    glTexParameterf(GL_TEXTURE_RECTANGLE_EXT,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+    glDisable(GL_TEXTURE_RECTANGLE_EXT);
+    checkGLError();
+  
+    textureName = (jint)tex;
+}
+
+void OpenGL$RectGLPicture::draw(jint dx1, jint dy1, jint dx2, jint dy2,jint sx1, jint sy1, jint sx2, jint sy2) { 
+    glColor4f(1.0f,1.0f,1.0f,1.0f);
+    glEnable(GL_TEXTURE_RECTANGLE_EXT);
+    glBindTexture(GL_TEXTURE_RECTANGLE_EXT, textureName);    
+    glBegin(GL_QUADS); 
+        glTexCoord2i (sx1, sy1   );
+        glVertex3i   (dx1, dy1, 0);   
+        glTexCoord2i (sx2, sy1   ); 
+        glVertex3i   (dx2, dy1, 0);
+        glTexCoord2i (sx2, sy2   ); 
+        glVertex3i   (dx2, dy2, 0);
+        glTexCoord2i (sx1, sy2   );
+        glVertex3i   (dx1, dy2, 0);
+    glEnd();
+    glDisable(GL_TEXTURE_RECTANGLE_EXT);
+    checkGLError();
+
+}
+
+void OpenGL::natDeleteTexture(jint tex_) {
+    activateSharedContext();
+    GLuint tex = (GLuint) tex_;
+    glDeleteTextures(1,&tex);
+}
+
+void OpenGL$SquareGLPicture::natInit(JArray<jint> *data_) {
+    unsigned char *buf;
+    int row,col,p;
+    jint *data = elements(data_);
+    buf = new unsigned char[texWidth*texHeight*4];
+    GLuint tex;
+
+    p=0;
+    for(row=0;row<height;row++) {
+        for(col=0;col<width;col++) {
+            jint pixel = data[row*width+col];
+            buf[p+0] = (pixel >> 16) & 0xff;
+            buf[p+1] = (pixel >>  8) & 0xff;
+            buf[p+2] = (pixel >>  0) & 0xff;
+            buf[p+3] = (pixel >> 24) & 0xff;
+            p+=4;
+        }
+        for(;col < texWidth;col++,p+=4)
+            buf[p+0] = buf[p+1] = buf[p+2] = buf[p+3] = 0;
+    }
+    for(;row < texHeight;row++) 
+        for(col=0;col<texWidth;col++,p+=4)
+            buf[p+0] = buf[p+1] = buf[p+2] = buf[p+3] = 0;
+    
+    gl->activateSharedContext();
+    glEnable(GL_TEXTURE_2D);
+    glGenTextures(1,&tex);
+    glBindTexture(GL_TEXTURE_2D,tex);
+    glPixelStorei(GL_PACK_ALIGNMENT,1);
+    glTexImage2D(GL_TEXTURE_2D,0,4,texWidth,texHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);
+    checkGLError();    
+    delete buf;
+
+    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
+    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+    glDisable(GL_TEXTURE_2D);
+    checkGLError();
+  
+    textureName = (jint)tex;
+}
+
+void OpenGL$SquareGLPicture::draw(jint dx1, jint dy1, jint dx2, jint dy2,jint sx1, jint sy1, jint sx2, jint sy2) {
+    float tx1,ty1,tx2,ty2; // normalized texture coords
+    tx1 = (float) sx1 / (float) texWidth;
+    ty1 = (float) sy1 / (float) texHeight;
+    tx2 = (float) sx2 / (float) texWidth;
+    ty2 = (float) sy2 / (float) texHeight;
+
+    glColor4f(1.0f,1.0f,1.0f,1.0f);
+    glEnable(GL_TEXTURE_2D);
+    glBindTexture(GL_TEXTURE_2D, textureName);    
+    checkGLError();
+    glBegin(GL_QUADS); 
+        glTexCoord2f (tx1, ty1   );
+        glVertex3i   (dx1, dy1, 0);
+        glTexCoord2f (tx2, ty1   ); 
+        glVertex3i   (dx2, dy1, 0);
+        glTexCoord2f (tx2, ty2   ); 
+        glVertex3i   (dx2, dy2, 0);
+        glTexCoord2f (tx1, ty2   );
+        glVertex3i   (dx1, dy2, 0);
+    glEnd();
+    glDisable(GL_TEXTURE_2D);
+    checkGLError();
+}
+
+} } } // end org::xwt::plat