Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Always use pbuffers. Add text files for shaders.

git-svn-id: svn+ssh://truffle/home/andrew/repo/memview@64 6d4121a9-3ab8-48c6-8db9-97dca464e206
  • Loading branch information...
commit d51dca272776509a584a22aa609d6110a85baf51 1 parent 9962496
@ajclinto authored
Showing with 57 additions and 36 deletions.
  1. +38 −33 Window.C
  2. +2 −1  Window.h
  3. +2 −2 memview.pro
  4. +8 −0 shader.frag
  5. +7 −0 shader.vert
View
71 Window.C
@@ -1,11 +1,9 @@
-#define GL_GLEXT_PROTOTYPES
-#include <GL/gl.h>
-
#include "Window.h"
#include "MemoryState.h"
+#include <fstream>
-// This define causes rendering to use textures, PBOs, and to render a
-// full-screen quad. When disabled, glDrawPixels is used.
+// This define causes rendering to use a textured full-screen quad rather
+// than glDrawPixels().
#define USE_SHADERS
static const QSize theDefaultSize(800, 600);
@@ -107,9 +105,35 @@ MemViewWidget::hilbert()
update();
}
+// Load a file into a buffer. The buffer is owned by the caller, and
+// should be freed with delete[].
+static char *
+loadTextFile(const char *filename)
+{
+ std::ifstream is(filename);
+ if (!is.good())
+ return 0;
+
+ is.seekg(0, std::ios::end);
+ long length = is.tellg();
+ is.seekg(0, std::ios::beg);
+
+ if (!length)
+ return 0;
+
+ char *buffer = new char[length+1];
+
+ is.read(buffer, length);
+ buffer[length] = '\0';
+
+ return buffer;
+}
+
void
MemViewWidget::initializeGL()
{
+ glGenBuffers(1, &myPixelBuffer);
+
#if defined(USE_SHADERS)
glGenTextures(1, &myTexture);
glBindTexture(GL_TEXTURE_2D, myTexture);
@@ -117,8 +141,6 @@ MemViewWidget::initializeGL()
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glGenBuffers(1, &myPixelBuffer);
-
myList = glGenLists(1);
glNewList(myList, GL_COMPILE);
@@ -132,25 +154,14 @@ MemViewWidget::initializeGL()
glEndList();
QGLShader *vshader = new QGLShader(QGLShader::Vertex, this);
- const char *vsrc =
- "varying mediump vec2 texc;\n"
- "void main(void)\n"
- "{\n"
- " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
- " texc = vec2(gl_MultiTexCoord0);\n"
- "}\n";
+ const char *vsrc = loadTextFile("shader.vert");
vshader->compileSourceCode(vsrc);
+ delete [] vsrc;
QGLShader *fshader = new QGLShader(QGLShader::Fragment, this);
- const char *fsrc =
- "uniform sampler2D texture;\n"
- "varying mediump vec2 texc;\n"
- "void main(void)\n"
- "{\n"
- " gl_FragColor = texture2D(texture, texc);\n"
- //" gl_FragColor = vec4(texc.x, texc.y, 0, 1);\n"
- "}\n";
+ const char *fsrc = loadTextFile("shader.frag");
fshader->compileSourceCode(fsrc);
+ delete [] fsrc;
myProgram = new QGLShaderProgram(this);
myProgram->addShader(vshader);
@@ -174,15 +185,11 @@ MemViewWidget::resizeGL(int width, int height)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
-#if defined(USE_SHADERS)
myImage.setSize(width, height);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, myPixelBuffer);
glBufferData(GL_PIXEL_UNPACK_BUFFER, myImage.bytes(), 0, GL_STREAM_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
-#else
- myImage.resize(width, height);
-#endif
}
void
@@ -195,12 +202,6 @@ MemViewWidget::paintGL()
myAnchor.myAbsoluteOffset;
myAnchor.myColumn += myHScrollBar->value() - myAnchor.myColumn;
-#if !defined(USE_SHADERS)
- myState->fillImage(myImage, myAnchor);
-
- glDrawPixels(myImage.width(), myImage.height(), GL_BGRA,
- GL_UNSIGNED_BYTE, myImage.data());
-#else
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, myPixelBuffer);
myImage.setData((uint32 *)
@@ -208,9 +209,13 @@ MemViewWidget::paintGL()
myState->fillImage(myImage, myAnchor);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
+#if !defined(USE_SHADERS)
+ glDrawPixels(myImage.width(), myImage.height(), GL_BGRA,
+ GL_UNSIGNED_BYTE, 0 /* offset in PBO */);
+#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
myImage.width(), myImage.height(), 0, GL_BGRA,
- GL_UNSIGNED_BYTE, 0);
+ GL_UNSIGNED_BYTE, 0 /* offset in PBO */);
glCallList(myList);
#endif
View
3  Window.h
@@ -1,4 +1,5 @@
-#ifndef NOTEPAD_H
+#ifndef Window_H
+#define Window_H
#include <QtGui>
#include <QGLWidget>
View
4 memview.pro
@@ -9,8 +9,8 @@ TARGET =
DEPENDPATH += .
INCLUDEPATH += .
-QMAKE_CFLAGS_RELEASE = -O3
-QMAKE_CXXFLAGS_RELEASE = -O3
+QMAKE_CFLAGS_RELEASE = -DGL_GLEXT_PROTOTYPES -O3
+QMAKE_CXXFLAGS_RELEASE = -DGL_GLEXT_PROTOTYPES -O3
# Input
HEADERS += Window.h MemoryState.h Loader.h
View
8 shader.frag
@@ -0,0 +1,8 @@
+uniform sampler2D texture;
+varying mediump vec2 texc;
+
+void main(void)
+{
+ gl_FragColor = texture2D(texture, texc);
+ //gl_FragColor = vec4(texc.x, texc.y, 0, 1);
+}
View
7 shader.vert
@@ -0,0 +1,7 @@
+varying mediump vec2 texc;
+
+void main(void)
+{
+ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
+ texc = vec2(gl_MultiTexCoord0);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.