/*
* glviewer.cpp
* eternity-opengl
*
* Created by Chris Healer on 4/19/08.
* Copyright 2008 The Molecule. All rights reserved.
*
*/
#include "glviewer.h"
void printProgramInfoLog(GLuint obj);
void printShaderInfoLog(GLuint obj);
static GLint cubeArray[][3] = {
{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0},
{0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1},
{0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1},
{0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0},
{0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1},
{1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1}
};
static GLint cubeTextureArray[][2] = {
{0, 0}, {1, 0}, {1, 1}, {0, 1},
{0, 0}, {0, 1}, {1, 1}, {1, 0},
{0, 0}, {1, 0}, {1, 1}, {0, 1},
{1, 0}, {0, 0}, {0, 1}, {1, 1},
{0, 0}, {1, 0}, {1, 1}, {0, 1},
{1, 0}, {0, 0}, {0, 1}, {1, 1}
};
static GLint faceArray[][2] = {
{1, -1}, {1, 1}, {-1, 1}, {-1, -1}
};
static GLfloat texCoordArray[][2] = {
{0.0, 0.0}, {1.0,0.0}, {1.0,1.0}, {0.0,1.0}
};
/*
static GLubyte colorArray[][4] = {
{170, 202, 0, 255},
{120, 143, 0, 255},
{83, 102, 0, 255},
{120, 143, 0, 255}
};
*/
//glviewer::glviewer(QWidget *parent) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
shader = 0;
printf("Has framebuffer objects.\n");
}
void glviewer
::createEternityShader(QString vertexShaderPath,
QString fragmentShaderPath
) {
GLint compiled;
QFile vf
(vertexShaderPath
);
QFile ff
(fragmentShaderPath
);
if (!vf.
open(QFile::ReadOnly) ||
!ff.
open(QFile::ReadOnly)) {
printf("ERROR! Current path is:\n");
system("pwd");
}
const char * vd = qv.data();
const char * fd = qf.data();
printf("%s\n%s\n",vd,fd);
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
g = 0; // no geometry shader
glShaderSource(f, 1, &fd,NULL);
glShaderSource(v, 1, &vd,NULL);
glCompileShader(v);
glGetShaderiv(v, GL_COMPILE_STATUS, &compiled);
if (!compiled) printShaderInfoLog(v);
glCompileShader(f);
glGetShaderiv(f, GL_COMPILE_STATUS, &compiled);
if (!compiled) printShaderInfoLog(f);
shader = glCreateProgram();
glAttachShader(shader,v);
glAttachShader(shader,f);
glLinkProgram(shader);
printProgramInfoLog(shader);
glUseProgram(shader);
defaultTextureSampler = glGetUniformLocation(shader,"Tex");
glUniform1i(defaultTextureSampler,defaultTexture);
GL_CHECK_ERROR();
printf("Shaders were successfully created.\n");
}
void glviewer::initializeEternityShader()
{
printf("would populate here.\n");
}
void glviewer::initializeGL()
{
printf("initialize!\n");
makeCurrent();
// QImage im(":res/images/default1.png");
QImage im
(":res/images/cubelogo.png");
defaultTexture = bindTexture(im);
printf("Default texture number: %d\n",(int)defaultTexture);
createEternityShader(VERTEX_SHADER_PATH,FRAGMENT_SHADER_PATH);
glMatrixMode(GL_MODELVIEW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_INT, 0, cubeTextureArray);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
defaultTextureSampler = glGetUniformLocation(shader,"Tex");
// glUniform1i(defaultTextureSampler,fbo->texture());
glUniform1i(defaultTextureSampler,defaultTexture);
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
pbufferList = glGenLists(1);
glNewList(pbufferList, GL_COMPILE);
{
glClear(GL_COLOR_BUFFER_BIT);
// draw shaded square
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// glEnable(GL_TEXTURE_2D);
glLoadIdentity();
glTranslatef(0.5f, 0.5f, -2.0f);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glTexCoordPointer(2, GL_FLOAT, 0, texCoordArray);
glVertexPointer(2, GL_INT, 0, faceArray);
glDrawArrays(GL_QUADS, 0, 4);
glPopMatrix();
// draw cube
glTranslatef(0.5f, 0.5f, 0.5f);
glRotatef(3.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(-0.5f, -0.5f, -0.5f);
glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
glVertexPointer(3, GL_INT, 0, cubeArray);
glDrawArrays(GL_QUADS, 0, 24);
}
glEndList();
}
void glviewer::paintGL()
{
printf("paint!\n");
glClear(GL_COLOR_BUFFER_BIT); //clear whole widget with clearColor
// glPopMatrix(); // pop the matrix pushed in the pbuffer list
// push the projection matrix and the entire GL state before
// doing any rendering into our framebuffer object
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glViewport(0, 0, fbo->size().width(), fbo->size().height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -99, 99);
glTranslatef(-0.5f, -0.5f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
// render to the framebuffer object
fbo->bind();
glCallList(pbufferList);
fbo->release();
// pop the projection matrix and GL state back for rendering
// to the actual widget
glPopAttrib();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
GL_CHECK_ERROR();
printf("pt2\n");
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, fbo->texture());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw the background
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// glRotatef(180,0,0,1);
// glRotatef(45,45,0,1);
glVertexPointer(2, GL_INT, 0, faceArray);
// glColor4f(0.5, 0.5, 0.5, 1.0);
glDrawArrays(GL_QUADS, 0, 4);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
//glFlush();
GL_CHECK_ERROR();
}
void glviewer::resizeGL(int w, int h)
{
printf("resize!\n");
glViewport(0, 0, (GLint)w, (GLint)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = w/(float)(h ? h : 1);
glFrustum(-aspect, aspect, -1, 1, 10, 100);
glTranslatef(-0.5f, -0.5f, -0.5f);
glTranslatef(0.0f, 0.0f, -15.0f);
}
/*!
internal function used by constructor, to print out the compiling log. Critical errors are reported with popups.
*/
void printShaderInfoLog(GLuint obj)
{
GLint infologLength = 0;
GLsizei charsWritten = 0;
char *infoLog;
glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 0)
{
infoLog = (char *)malloc(infologLength);
glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
free(infoLog);
}
}
void printProgramInfoLog(GLuint obj)
{
GLint infologLength = 0;
GLsizei charsWritten = 0;
char *infoLog;
glGetProgramiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 0)
{
infoLog = (char *)malloc(infologLength);
glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
free(infoLog);
}
}
/*
* glviewer.cpp
* eternity-opengl
*
* Created by Chris Healer on 4/19/08.
* Copyright 2008 The Molecule. All rights reserved.
*
*/
#include "glviewer.h"
void printProgramInfoLog(GLuint obj);
void printShaderInfoLog(GLuint obj);
static GLint cubeArray[][3] = {
{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0},
{0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1},
{0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1},
{0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0},
{0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1},
{1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1}
};
static GLint cubeTextureArray[][2] = {
{0, 0}, {1, 0}, {1, 1}, {0, 1},
{0, 0}, {0, 1}, {1, 1}, {1, 0},
{0, 0}, {1, 0}, {1, 1}, {0, 1},
{1, 0}, {0, 0}, {0, 1}, {1, 1},
{0, 0}, {1, 0}, {1, 1}, {0, 1},
{1, 0}, {0, 0}, {0, 1}, {1, 1}
};
static GLint faceArray[][2] = {
{1, -1}, {1, 1}, {-1, 1}, {-1, -1}
};
static GLfloat texCoordArray[][2] = {
{0.0, 0.0}, {1.0,0.0}, {1.0,1.0}, {0.0,1.0}
};
/*
static GLubyte colorArray[][4] = {
{170, 202, 0, 255},
{120, 143, 0, 255},
{83, 102, 0, 255},
{120, 143, 0, 255}
};
*/
//glviewer::glviewer(QWidget *parent) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
glviewer::glviewer(QWidget *parent) : QGLWidget(parent)
{
shader = 0;
if (QGLFramebufferObject::hasOpenGLFramebufferObjects())
printf("Has framebuffer objects.\n");
}
void glviewer::createEternityShader(QString vertexShaderPath, QString fragmentShaderPath)
{
GLint compiled;
QFile vf(vertexShaderPath);
QFile ff(fragmentShaderPath);
if (!vf.open(QFile::ReadOnly) || !ff.open(QFile::ReadOnly))
{
printf("ERROR! Current path is:\n");
system("pwd");
}
QByteArray qv = vf.readAll();
QByteArray qf = ff.readAll();
const char * vd = qv.data();
const char * fd = qf.data();
printf("%s\n%s\n",vd,fd);
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
g = 0; // no geometry shader
glShaderSource(f, 1, &fd,NULL);
glShaderSource(v, 1, &vd,NULL);
glCompileShader(v);
glGetShaderiv(v, GL_COMPILE_STATUS, &compiled);
if (!compiled) printShaderInfoLog(v);
glCompileShader(f);
glGetShaderiv(f, GL_COMPILE_STATUS, &compiled);
if (!compiled) printShaderInfoLog(f);
shader = glCreateProgram();
glAttachShader(shader,v);
glAttachShader(shader,f);
glLinkProgram(shader);
printProgramInfoLog(shader);
glUseProgram(shader);
defaultTextureSampler = glGetUniformLocation(shader,"Tex");
glUniform1i(defaultTextureSampler,defaultTexture);
GL_CHECK_ERROR();
printf("Shaders were successfully created.\n");
}
void glviewer::initializeEternityShader()
{
printf("would populate here.\n");
}
void glviewer::initializeGL()
{
printf("initialize!\n");
makeCurrent();
fbo = new QGLFramebufferObject(160, 160);
// QImage im(":res/images/default1.png");
QImage im(":res/images/cubelogo.png");
defaultTexture = bindTexture(im);
printf("Default texture number: %d\n",(int)defaultTexture);
createEternityShader(VERTEX_SHADER_PATH,FRAGMENT_SHADER_PATH);
glMatrixMode(GL_MODELVIEW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_INT, 0, cubeTextureArray);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
defaultTextureSampler = glGetUniformLocation(shader,"Tex");
// glUniform1i(defaultTextureSampler,fbo->texture());
glUniform1i(defaultTextureSampler,defaultTexture);
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
pbufferList = glGenLists(1);
glNewList(pbufferList, GL_COMPILE);
{
glClear(GL_COLOR_BUFFER_BIT);
// draw shaded square
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// glEnable(GL_TEXTURE_2D);
glLoadIdentity();
glTranslatef(0.5f, 0.5f, -2.0f);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glTexCoordPointer(2, GL_FLOAT, 0, texCoordArray);
glVertexPointer(2, GL_INT, 0, faceArray);
glDrawArrays(GL_QUADS, 0, 4);
glPopMatrix();
// draw cube
glTranslatef(0.5f, 0.5f, 0.5f);
glRotatef(3.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(-0.5f, -0.5f, -0.5f);
glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
glVertexPointer(3, GL_INT, 0, cubeArray);
glDrawArrays(GL_QUADS, 0, 24);
}
glEndList();
}
void glviewer::paintGL()
{
printf("paint!\n");
glClear(GL_COLOR_BUFFER_BIT); //clear whole widget with clearColor
// glPopMatrix(); // pop the matrix pushed in the pbuffer list
// push the projection matrix and the entire GL state before
// doing any rendering into our framebuffer object
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glViewport(0, 0, fbo->size().width(), fbo->size().height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -99, 99);
glTranslatef(-0.5f, -0.5f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
// render to the framebuffer object
fbo->bind();
glCallList(pbufferList);
fbo->release();
// pop the projection matrix and GL state back for rendering
// to the actual widget
glPopAttrib();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
GL_CHECK_ERROR();
printf("pt2\n");
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, fbo->texture());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw the background
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// glRotatef(180,0,0,1);
// glRotatef(45,45,0,1);
glVertexPointer(2, GL_INT, 0, faceArray);
// glColor4f(0.5, 0.5, 0.5, 1.0);
glDrawArrays(GL_QUADS, 0, 4);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
//glFlush();
GL_CHECK_ERROR();
}
void glviewer::resizeGL(int w, int h)
{
printf("resize!\n");
glViewport(0, 0, (GLint)w, (GLint)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = w/(float)(h ? h : 1);
glFrustum(-aspect, aspect, -1, 1, 10, 100);
glTranslatef(-0.5f, -0.5f, -0.5f);
glTranslatef(0.0f, 0.0f, -15.0f);
}
/*!
internal function used by constructor, to print out the compiling log. Critical errors are reported with popups.
*/
void printShaderInfoLog(GLuint obj)
{
GLint infologLength = 0;
GLsizei charsWritten = 0;
char *infoLog;
glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 0)
{
infoLog = (char *)malloc(infologLength);
glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
free(infoLog);
}
}
void printProgramInfoLog(GLuint obj)
{
GLint infologLength = 0;
GLsizei charsWritten = 0;
char *infoLog;
glGetProgramiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 0)
{
infoLog = (char *)malloc(infologLength);
glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
free(infoLog);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks