openGL, text changes when I load textures - c++

I'm developing a C++ application and I need to use an interface made in openGL. I have a grey background, some squared textures over the background and some text written next to the textures. The text is yellow but when I click with mouse on the window or I change the textures, the text turns grey (a different grey from the background) and I don't know why! Here is the code and screenshots:
int main(int argc, char** argv){
[...]
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_FLAT);
glEnable(GL_BLEND);
glEnable(GL_POINT_SMOOTH);
// Render the scene
glutDisplayFunc(display);
glutReshapeFunc(reshape);
// Turn the flow of control over to GLUT
glutMainLoop();
[...]
}
void display(void){
glClearColor(0.2f, 0.2f, 0.2f, 0.5f); //Background color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up viewing transformation, looking down -Z axis
glLoadIdentity();
gluLookAt(posx, 0, -g_fViewDistance, 0, 0, -1, 0, 1, 0);
// Render the scene
glMatrixMode(GL_COLOR);
glColor3f(1.0f, 1.0f, 0.0f); // Textcolor
drawOnScreen(); // Draw text on screen
glMatrixMode(GL_MODELVIEW);
loadSquare(); // Load texture
glutSwapBuffers();
}
void reshape(GLint width, GLint height){
interface->setWindowWidth(width);
interface->setWindowHeight(height);
glViewport(0, 0, g_Width, g_Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (float)g_Width / g_Height, g_nearPlane, g_farPlane);
glMatrixMode(GL_MODELVIEW);
}
void printStringOnVideo(void* font, char* s){
if (s && strlen(s)) {
while (*s) {
glutBitmapCharacter(font, *s);
s++;
}
}
}
void drawOnScreen(){
GLfloat x, y;
// Draw the strings, according to the current mode and font.
x = 2.90f;
y = 0.80f;
glRasterPos2f(x, y);
int len;
char s[] = { 'H', 'E', 'L', 'L', 'O', '\0' };
len = (int)strlen(s);
printStringOnVideo(GLUT_BITMAP_TIMES_ROMAN_24, s);
}
The loadSquare() function applies texture to squares. Its main feature is calling the class method for loading textures on squares:
void P300Square::setTexture(bool func){
// Se func = 0 allora carico la texture, viceversa il flash
string tmp = "";
if (func == false)
tmp = texturePath;
else
tmp = textureFlashPath;
GLuint texture = SOIL_load_OGL_texture
(
tmp.c_str(),
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glColor3ub(50, 50, 50);
glEnable(GL_BLEND);
glBegin(GL_QUADS);
glColor4f(0.5f, 0.0, 0.0f, 0.8f);
// Top Left
glTexCoord2f(0, 1);
glVertex2f(u, v);
// Top Right
glTexCoord2f(1, 1);
glVertex2f(u + size, v);
// Bottom Right
glTexCoord2f(1, 0);
glVertex2f(u + size, v + size);
// Bottom Left
glTexCoord2f(0, 0);
glVertex2f(u, v + size);
glEnd();
glDisable(GL_BLEND);
}
Can please someone help me? Thank you!

You should disable GL_TEXTURE_2D before you draw the bitmap font.
Internally, glutBitmapCharacter() uses glBitmap. This function maps the bitmap to a rectangle to the screen and generates a fragment for every framebuffer pixel in that rectangle where the bitmap is 1. Those fragments get the associated data from the values associated with the current raster position.
The generated fragments are processed like every other fragment (i.e. the ones generated by drawing primitives like lines or triangles). So stuff like texturing, lighting, fog, blending and so on will also affect everything drawn by glBitmap. Due to the fact that all fragments generated by a single bitmap get the same associated data, they all will get the same texture coordinate. Since you left texturing on, the GL textures all of these fragments with the very same sample from the texture, so that they all appear in the same color.

Related

Using glTexImage2D to render to texture, blank result. (Dreamcast GLdc)

I am trying to create a quick render to texture example using GLdc, a OpenGL implementation for the Sega Dreamcast. I have verified that both my Texture and Framebuffer Object are complete, yet the texture resulting from the framebuffer only has 1 white dot in it.
First, I generate an empty texture and prepare it to be written to.
func genTextures(){
glGenTextures(1, &renderedTexture[0]);
glBindTexture(GL_TEXTURE_2D, renderedTexture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smaller than texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
}
Next, I generate an FBO and bind the new texture we just created to it.
func genFBO() {
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, renderedTexture[0], 0);
}
At this point the FBO and the Texture should both be considered complete. The main loop is structured something like this:
int main(int argc, char **argv)
{
glKosInit();
InitGL(640, 480);
ReSizeGLScene(640, 480);
genTextures();
genFBO();
while(1) {
if(check_start())
break;
// I checked here for FBO and Texture completeness, both return True.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); // bind to the FBO
DrawGLScene(); // Draw our cube to the FBO
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // back to default
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
ReSizeGLScene(640,480);
DrawGLUI(); //Draw the quad with the framebuffers texture
}
return 0;
}
Here are the two functions that draw geometry:
void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-5.0f); // move 5 units into the screen.
glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate On The X Axis
glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate On The Y Axis
glRotatef(zrot,0.0f,0.0f,1.0f); // Rotate On The Z Axis
glBindTexture(GL_TEXTURE_2D, texture[0]); // choose the texture to use.
glBegin(GL_QUADS); // begin drawing a cube
// Draw my textured cube, works fine.
glEnd(); // done with the polygon.
xrot+=1.5f; // X Axis Rotation
yrot+=1.5f; // Y Axis Rotation
zrot+=1.5f; // Z Axis Rotation
glKosSwapBuffers();
}
void DrawGLUI(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, renderedTexture[0]);
glBegin(GL_QUADS);
//glColor3f(1.0f, 0.0f, 0.0);
glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex2f(1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0);
glEnd();
glEnable(GL_DEPTH_TEST);
ReSizeGLScene(640,480);
glFlush();
}
The result is
Where I would like to have the cube rendered to a texture then that texture applied to the quad in the upper right corner...
The size of the viewport must be adjusted to the size of the framebuffer with glViewport when the framebuffer is switched:
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glViewport(0, 0, 128, 128);
// [...]
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glViewport(0, 0, 640, 480);
// [...]

Render FFmpeg AVFrame as OpenGL texture?

I'm attempting to to render a jpeg image (1024x1024 pixels) in the form of an FFmpeg AVFrame as a texture in OpenGL. What I get instead is something that appears as a 1024x1024 dark green quad:
The code to render the AVFrame data in OpenGL is shown below. I have convinced myself that the raw RGB data held within the FFmpeg AVFrame data is not solely dark green.
GLuint g_texture = {};
//////////////////////////////////////////////////////////////////////////
void display()
{
// Clear color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); // Operate on model-view matrix
glEnable(GL_TEXTURE_2D);
GLuint texture = g_texture;
glBindTexture(GL_TEXTURE_2D, texture);
// Draw a quad
glBegin(GL_QUADS);
glVertex2i(0, 0); // top left
glVertex2i(1024, 0); // top right
glVertex2i(1024, 1024); // bottom right
glVertex2i(0, 1024); // bottom left
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glFlush();
}
/* Initialize OpenGL Graphics */
void initGL(int w, int h)
{
glViewport(0, 0, w, h); // use a screen size of WIDTH x HEIGHT
glEnable(GL_TEXTURE_2D); // Enable 2D texturing
glMatrixMode(GL_PROJECTION); // Make a simple 2D projection on the entire window
glOrtho(0.0, w, h, 0.0, 0.0, 100.0);
glMatrixMode(GL_MODELVIEW); // Set the matrix mode to object modeling
//glTranslatef( 0, 0, -15 );
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the window
}
//////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
std::shared_ptr<AVFrame> apAVFrame;
if (!load_image_to_AVFrame(apAVFrame, "marble.jpg"))
{
assert(false);
return 1;
}
// From here on out, the AVFrame is RGB interleaved
// and is sized to 1,024 x 1,024 (power of 2).
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(1060, 1060);
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL - Creating a texture");
glGenTextures(1, &g_texture);
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, g_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, apAVFrame->width,
apAVFrame->height, 0, GL_RGB, GL_UNSIGNED_BYTE,
apAVFrame->data[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* We will use linear interpolation for magnification filter */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* We will use linear interpolation for minifying filter */
initGL(1060, 1060);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Environment:
Ubuntu 18.04
GCC v8.2
EDIT: As per #immibis' suggestion below, it all works when I change the rendering of the quad to:
// Draw a quad
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(0, 0); // top left
glTexCoord2f(1, 0);
glVertex2i(1024, 0); // top right
glTexCoord2f(1, 1);
glVertex2i(1024, 1024); // bottom right
glTexCoord2f(0, 1);
glVertex2i(0, 1024); // bottom left
glEnd();
You forgot to give your vertices texture coordinates, so all the pixels on your screen are reading the same pixel from the texture. (The top-left, or wherever the default texture coordinates are)
Use glTexCoord2f before glVertex2i to set the texture coordinates for the vertex. They go from 0 on the top/left of the texture, to 1 on the bottom/right, so the corners of the texture are 0,0, 1,0, 1,1 and 0,1.

Rendering a second pass yields a different result

Currently I'm trying to render multiple passes with different shaders in a simple OpenGL application. Here's my (simplified) code:
void InitScene()
{
glViewport(0, 0, mWindowWidth, mWindowHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, mWindowWidth, mWindowHeight, 0, -1, 1);
mFramebufferName = CreateFrameBuffer(mWindowWidth, mWindowHeight);
}
void DrawScene()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
if(drawDirectlyToScreen)
{
// This works fine, image will fill the whole screen
// Directly draw to the screen
DrawFullScreenQuad();
}
else
{
// This does not work. The image from the first pass will only be a small quadrat
// Draw to frame buffer instead of screen
glBindFramebuffer(GL_FRAMEBUFFER, mFramebufferName);
DrawFullScreenQuad();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Get ready for second pass
BindFrameBufferTextureAndActivateAnotherShader();
// Now draw to the screen
DrawFullScreenQuad();
}
glPopMatrix();
}
void DrawFullScreenQuad()
{
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, mWindowHeight, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(mWindowWidth, mWindowHeight, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(mWindowWidth, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
}
void CreateFrameBuffer(int width, int height)
{
// Generate and bind the frame buffer
mFramebufferName = 0;
glGenFramebuffers(1, &mFramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, mFramebufferName);
// Create and bind the render texture
glGenTextures(1, &mSecondPassRenderTexture);
glBindTexture(GL_TEXTURE_2D, mSecondPassRenderTexture);
// Give an empty image to OpenGL ( the last "0" )
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
// Set "mSecondPassRenderTexture" as colour attachement #0
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mSecondPassRenderTexture, 0);
// Set the list of draw buffers.
GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
}
When rendering only one pass everything is fine. The image covers the whole screen. When rendering with two passes, the resulting image will only cover a small square area in the top left corner of the screen (see the attached images).
The problem seems to come from the first pass. The texture created in that pass is already wrong (i.e. the image is only in the corner, the rest of the texture is black). The second pass then works correctly (i.e. the broken texture is drawn correctly to the whole screen).
So my question is: why does my call to DrawFullScreenQuad() yield different results when
Rendering to the screen directly
Rendering to a frame buffer (which has the same size as the window)

OpenGL textures not loading with SOIL

Hey I can't get my texture to show up and I have no idea what's wrong. Tutorials haven't helped. Here's my code:
Player p();
//The glutDisplayFunc();
void display() {
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
//Load png with SOIL
playerTex = loadTex("Data/Sprites/player.png");
p.draw();
glFlush();
glPopMatrix(); }
//Load texture
GLuint loadTex(const char* c) {
GLuint temp = SOIL_load_OGL_texture(
c,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
if (0 == temp)
{
printf("SOIL loading error: '%s'\n", SOIL_last_result());
//return 0;
}
glBindTexture(GL_TEXTURE_2D, temp);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return temp; }
//Player object's draw function
void player::draw() {
glEnable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLES);
glColor3f(0, 0, 0);
glTexCoord2f(0.0, 0.0);
glVertex2f(x, y);
glTexCoord2f(0.0, 1.0);
glVertex2f(x + spriteW, y);
glTexCoord2f(1.0, 1.0);
glVertex2f(x + spriteW, y + spriteL);
glEnd();}
spriteX = 0, spriteY = 0, spriteW = 400, spriteL = 400, x = 0, y = 0
This is the output I get:
Output Window
As you can see the BLACK triangle shows up fine, but with no texture
Texture coordinates should be in the range 0..1, they are not texel coordinates. Divide by the texture width and height to convert from texel coordinates to texture coordinates.
You also have the color set to black with the following line:
glColor3f(0, 0, 0);
Don't do that. Use white (or change TEXTURE_ENV_MODE, but that doesn't seem necessary).
glColor3f(1, 1, 1);
The color is multiplied by the texture color. You might as well move it outside the glBegin() glEnd() block:
glColor(1, 1, 1);
glBegin(...);
...
glEnd();

OpenGL offscreen rendered texture not displaying on screen

I am having difficulty getting a offscreen rendered texture to to be displayed. I have checked round the web and other questions and am clearly missing something.
I am new to OpenGL so may well be making some naive errors. It is part of a routine to add post processing using a shader to modify the firestor/secondlife viewer project to an equifisheye projection. My test code is based around the GLEW and GLUT framework using OpenGL 1.3 as the viewer is designed to run on older platforms.
The approach is:
set up and initialise the FBO, Render buffers and textures to draw to.
initilise various default settings for drawing the glutCube and glutTeapot :)
The display function
a) using the default framebuffer draws a rotating cube (works fine)
b) the texture is then drawn to
OUTPUT: all black and not the rotating cube.
My code is:
GLuint buffer_fbo;
GLuint bufferColtexture;
GLuint depthBuffer_rb;
typedef struct {
int width;
int height;
char* title;
float field_of_view_angle;
float z_near;
float z_far;
} glutWindow;
glutWindow win;
//
void ShutDown(void)
{
glDeleteFramebuffersEXT(1, &buffer_fbo);
glDeleteRenderbuffersEXT(1, &depthBuffer_rb);
glDeleteTextures(1,&bufferColtexture);
}
float g_potrotation = 0;
float g_potrotation_speed = 0.2f;
float xrot =0;
float yrot = 0;
float zrot = 0;
GLenum g_Drawbuffers[2] = {GL_COLOR_ATTACHMENT0_EXT };
void setupFBO(void)
{
// *********** set up scene FBO including dept buffers and their textures ****************
glGenFramebuffersEXT(1, &buffer_fbo);
// set up render buffers for depth info
glGenRenderbuffersEXT(1, &depthBuffer_rb);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer_rb);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, win.width, win.height);
// Genrate texture to render to
glGenTextures(1, &bufferColtexture);
glBindTexture(GL_TEXTURE_2D, bufferColtexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, win.width, win.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// attach texture to render to to fbo
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, buffer_fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, bufferColtexture, 0);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer_rb);
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); // check frame buffer
if(status != GL_FRAMEBUFFER_COMPLETE_EXT)
{
printf("FRAMEBUFFER status error %i \n",status);
exit(1);
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // bind back to default
}
void initialise()
{
setupFBO();
// select projection matrix
glMatrixMode(GL_PROJECTION);
// set the viewport
glViewport(0, 0, win.width, win.height);
// set matrix mode
glMatrixMode(GL_PROJECTION);
// reset projection matrix
glLoadIdentity();
GLfloat aspect = (GLfloat) win.width / win.height;
// set up a perspective projection matrix
gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far);
// specify which matrix is the current matrix
glMatrixMode(GL_MODELVIEW);
glShadeModel( GL_SMOOTH );
// specify the clear value for the depth buffer
glClearDepth( 1.0f );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
// specify implementation-specific hints
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
GLfloat amb_light[] = { 0.1, 0.1, 0.1, 1.0 };
GLfloat diffuse[] = { 0.6, 0.6, 0.6, 1 };
GLfloat specular[] = { 0.7, 0.7, 0.3, 1 };
glLightModelfv( GL_LIGHT_MODEL_AMBIENT, amb_light );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glEnable( GL_LIGHT0 );
glEnable( GL_COLOR_MATERIAL );
glShadeModel( GL_SMOOTH );
glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
glDepthFunc( GL_LEQUAL );
glEnable( GL_DEPTH_TEST );
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glClearColor(0.5, 0.5, 0.5, 1.0);
}
void drawquad(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glDisable(GL_LIGHTING); // This was the cause of the problem, disable lighting worked!
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-5.0f);
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(zrot,0.0f,0.0f,1.0f);
glBindTexture(GL_TEXTURE_2D, bufferColtexture);
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
glEnd();
xrot+=0.3f;
yrot+=0.2f;
zrot+=0.4f;
glEnable(GL_LIGHTING); // now reenabling the lighting. after adding to fix code.
}
void display(void)
{
// set red background
// draw to fbo
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
// Clear Screen and Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
// set drawing a rotating cube as per default
glLoadIdentity();
// Define a viewing transformation
gluLookAt( 3,1,0, 0,0,0, 0,1,0);
// Push and pop the current matrix stack.
// This causes that translations and rotations on this matrix wont influence others.
glPushMatrix();
glColor3f(0,1,0);
glTranslatef(0,0,0);
glRotatef(g_potrotation,0,1,0);
glRotatef(90,0,1,0);
// Draw the solid cube
glutSolidCube(1);
glPopMatrix();
// draw teapot to fbo
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, buffer_fbo);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,depthBuffer_rb);
// Clear Screen and Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Define a viewing transformation
gluLookAt( 4,2,0, 0,0,0, 0,1,0);
// Push and pop the current matrix stack.
// This causes that translations and rotations on this matrix wont influence others.
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(0,0,0);
glRotatef(g_potrotation,0,1,0);
glRotatef(90,0,1,0);
// Draw the teapot
glutSolidTeapot(1);
glPopMatrix();
g_potrotation += g_potrotation_speed;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0); // reset to default Frame and Render buffer
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,0);
drawquad();
glutSwapBuffers();
}
// informs OpenGL that window size has changed.
void reshape(int width, int height)
{
}
void idle(void)
{
glutPostRedisplay();
}
int checkglewOK()
{
if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
printf("Ready for GLSL\n");
else {
printf("Not totally ready :( \n");
exit(1);
};
}
void keyboard (unsigned char key, int mousePositionX, int mousePositionY)
{
switch (key)
{
case KEY_ESCAPE:
exit (0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
// set window values
win.width = 640;
win.height = 480;
win.title = "Shader Test";
win.field_of_view_angle = 45;
win.z_near = 1.0f;
win.z_far = 500.0f;
////////////////////////////////////////////////////////
// Initialise and Create Window
////////////////////////////////////////////////////////
glutInit(&argc, argv);
// set modes RGB-Alpha, doble bufferedd (prevent flickering)
// has a depth buffer to ensure that 3D object overlp OK
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); // set display mode
// define window size
glutInitWindowSize(win.width, win.height);
glutCreateWindow(win.title); // create the window
glewInit(); // Glew Init occurs after Create Window !!!
checkglewOK();
glutDisplayFunc(display); // register display function
//glutReshapeFunc(reshape); // register resize function
glutIdleFunc(idle); // register idel function
glutKeyboardFunc(keyboard);
initialise();
glutMainLoop(); // run glut main loop
ShutDown();
return EXIT_SUCCESS;
}