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, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
58 // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
59 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
61 double w = (double)rasterWidth / (double)textureWidth;
62 double h = (double)rasterHeight / (double)textureHeight;
63 unsigned u = outputWidth;
64 unsigned v = outputHeight;
66 glBegin(GL_TRIANGLE_STRIP);
67 glTexCoord2f(0, 0); glVertex3i(0, v, 0);
68 glTexCoord2f(w, 0); glVertex3i(u, v, 0);
69 glTexCoord2f(0, h); glVertex3i(0, 0, 0);
70 glTexCoord2f(w, h); glVertex3i(u, 0, 0);
74 void GLWidget::resizeGL(int width, int height)
76 if (width > textureWidth || height > textureHeight)
78 // textureWidth = max(width, textureWidth);
79 // textureHeight = max(height, textureHeight);
80 // Seems that power of 2 sizes are still mandatory...
81 textureWidth = 1024;//(width > textureWidth ? width : textureWidth);
82 textureHeight = 512;//(height > textureHeight ? height : textureHeight);
83 // textureWidth = (width > textureWidth ? width : textureWidth);
84 // textureHeight = (height > textureHeight ? height : textureHeight);
86 printf("Resizing: new texture width/height = %i x %i\n", textureWidth, textureHeight);
87 printf("Resizing: new raster width/height = %i x %i\n", rasterWidth, rasterHeight);
93 glDeleteTextures(1, &texture);
96 buffer = new uint32_t[textureWidth * textureHeight];
97 glGenTextures(1, &texture);
98 glBindTexture(GL_TEXTURE_2D, texture);
99 glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth);
100 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
102 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
103 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
108 class RubyGLWidget: public QGLWidget
112 unsigned textureWidth, textureHeight;
115 unsigned rasterWidth, rasterHeight;
120 void updateSynchronization() {
123 CGLContextObj context = CGLGetCurrentContext();
124 GLint value = synchronize; //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
125 CGLSetParameter(context, kCGLCPSwapInterval, &value);