1 // OpenGL implementation in Qt
2 // Parts of this are blantantly ripped off from BSNES (thanks Byuu!)
5 // (C) 2010 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 01/14/2010 Created this file
18 GLWidget::GLWidget(QWidget * parent/*= 0*/): QGLWidget(parent), texture(0),
19 textureWidth(0), textureHeight(0), buffer(0), rasterWidth(64), rasterHeight(64)
20 // textureWidth(0), textureHeight(0), buffer(0), rasterWidth(256), rasterHeight(256)
28 void GLWidget::initializeGL()
30 format().setDoubleBuffer(true);
31 resizeGL(rasterWidth, rasterHeight);
33 glDisable(GL_ALPHA_TEST);
35 glDisable(GL_DEPTH_TEST);
36 glDisable(GL_POLYGON_SMOOTH);
37 glDisable(GL_STENCIL_TEST);
39 glEnable(GL_TEXTURE_2D);
40 glClearColor(0.0, 0.0, 0.0, 0.0);
43 void GLWidget::paintGL()
45 unsigned outputWidth = width();
46 unsigned outputHeight = height();
48 glMatrixMode(GL_PROJECTION);
50 glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0);
51 glViewport(0, 0, outputWidth, outputHeight);
53 glMatrixMode(GL_MODELVIEW);
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (smoothGLOutput ? GL_LINEAR : GL_NEAREST));
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (smoothGLOutput ? GL_LINEAR : GL_NEAREST));
58 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST /*GL_LINEAR*/);
59 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST /*GL_LINEAR*/);
60 // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
61 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
63 double w = (double)rasterWidth / (double)textureWidth;
64 double h = (double)rasterHeight / (double)textureHeight;
65 unsigned u = outputWidth;
66 unsigned v = outputHeight;
68 glBegin(GL_TRIANGLE_STRIP);
69 glTexCoord2f(0, 0); glVertex3i(0, v, 0);
70 glTexCoord2f(w, 0); glVertex3i(u, v, 0);
71 glTexCoord2f(0, h); glVertex3i(0, 0, 0);
72 glTexCoord2f(w, h); glVertex3i(u, 0, 0);
76 void GLWidget::resizeGL(int width, int height)
78 if (width > textureWidth || height > textureHeight)
80 // textureWidth = max(width, textureWidth);
81 // textureHeight = max(height, textureHeight);
82 // Seems that power of 2 sizes are still mandatory...
83 textureWidth = 1024;//(width > textureWidth ? width : textureWidth);
84 textureHeight = 512;//(height > textureHeight ? height : textureHeight);
85 // textureWidth = (width > textureWidth ? width : textureWidth);
86 // textureHeight = (height > textureHeight ? height : textureHeight);
88 printf("Resizing: new texture width/height = %i x %i\n", textureWidth, textureHeight);
89 printf("Resizing: new raster width/height = %i x %i\n", rasterWidth, rasterHeight);
95 glDeleteTextures(1, &texture);
98 buffer = new uint32_t[textureWidth * textureHeight];
99 glGenTextures(1, &texture);
100 glBindTexture(GL_TEXTURE_2D, texture);
101 glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth);
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
104 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
105 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
110 class RubyGLWidget: public QGLWidget
114 unsigned textureWidth, textureHeight;
117 unsigned rasterWidth, rasterHeight;
122 void updateSynchronization() {
125 CGLContextObj context = CGLGetCurrentContext();
126 GLint value = synchronize; //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
127 CGLSetParameter(context, kCGLCPSwapInterval, &value);