Sign in to follow this  

Weird problem coloring objects

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys, i m using simple glMaterialfv to color my objects.i have lighting enabled and everything but when i draw the cube it just wont appear.it does seem to hide other objects behind it but it wont take the color i try to assign it.also the objects appear as if there was no light.as if in GL_FLAT shading.i have used gl_smooth shading.Involves the Cg language,FBOs and textures. here is the code The part i m talking about is in the update function just after i'm done drawing the quad. #include <stdio.h> #include <assert.h> #include <stdlib.h> //#define GLEW_STATIC 1 #include <GL/glew.h> #include <gl/glut.h> #include <cg/cgGL.h> #include "glErrorUtil.h" // forward declarations class HelloGPGPU; void reshape(int w, int h); // globals CGcontext g_cgContext; CGprofile g_cgProfile; HelloGPGPU *g_pHello; GLuint _fb; // FBO identifier GLuint _depth_rb;//depth buffer identifier // This shader performs a 9-tap Laplacian edge detection filter. static const char *edgeFragSource = "half4 edges(half2 coords : TEX0, \n" " uniform sampler2D texture) : COLOR \n" "{ \n" " static const half offset = 1.0 / 512.0; \n" " half4 c = tex2D(texture, coords); \n" " half4 bl = tex2D(texture, coords + half2(-offset, -offset)); \n" " half4 l = tex2D(texture, coords + half2(-offset, 0)); \n" " half4 tl = tex2D(texture, coords + half2(-offset, offset)); \n" " half4 t = tex2D(texture, coords + half2( 0, offset)); \n" " half4 ur = tex2D(texture, coords + half2( offset, offset)); \n" " half4 r = tex2D(texture, coords + half2( offset, 0)); \n" " half4 br = tex2D(texture, coords + half2( offset, offset)); \n" " half4 b = tex2D(texture, coords + half2( 0, -offset)); \n" " // scale by 8 to brighten the edges \n" " return 8 * (c + -0.125 * (bl + l + tl + t + ur + r + br + b)); \n" "} \n"; /** * Checks framebuffer status. * Copied directly out of the spec, modified to deliver a return value. */ bool checkFramebufferStatus() { GLenum status; status = (GLenum) glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); switch(status) { case GL_FRAMEBUFFER_COMPLETE_EXT: return true; case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: printf("Framebuffer incomplete, incomplete attachment\n"); return false; case GL_FRAMEBUFFER_UNSUPPORTED_EXT: printf("Unsupported framebuffer format\n"); return false; case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: printf("Framebuffer incomplete, missing attachment\n"); return false; case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: printf("Framebuffer incomplete, attached images must have same dimensions\n"); return false; case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: printf("Framebuffer incomplete, attached images must have same format\n"); return false; case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: printf("Framebuffer incomplete, missing draw buffer\n"); return false; case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: printf("Framebuffer incomplete, missing read buffer\n"); return false; } return false; } // This class encapsulates all of the GPGPU functionality of the example. class HelloGPGPU { public: // methods HelloGPGPU(int w, int h) : _rAngle(0), _iWidth(w), _iHeight(h) { // viewport for 1:1 pixel=texture mapping glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1, 1, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, _iWidth, _iHeight); // Create a simple 2D texture. This example does not use // render to texture -- it just copies from the framebuffer to the // texture. // GPGPU CONCEPT 1: Texture = Array. // Textures are the GPGPU equivalent of arrays in standard // computation. Here we allocate a texture large enough to fit our // data (which is arbitrary in this example). CheckErrorsGL("BEGIN : Creating textures"); glGenTextures(2, _iTexture); // create (reference to) a new texture for(int i=0;i<2;i++) { glBindTexture(GL_TEXTURE_2D, _iTexture[i]); // (set texture parameters here) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); //create the texture glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, _iWidth, _iHeight, 0, GL_RGB, GL_FLOAT, 0); } // GPGPU CONCEPT 2: Fragment Program = Computational Kernel. // A fragment program can be thought of as a small computational // kernel that is applied in parallel to many fragments // simultaneously. Here we load a kernel that performs an edge // detection filter on an image. // Create the edge detection fragment program _fragmentProgram = cgCreateProgram(g_cgContext, CG_SOURCE, edgeFragSource, g_cgProfile, "edges", NULL); // Create the texture parameter for the fragment program if(_fragmentProgram != NULL) { cgGLLoadProgram(_fragmentProgram); _textureParam = cgGetNamedParameter(_fragmentProgram, "texture"); } // attach two textures to FBO glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, _iTexture[0], 0); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, _iTexture[1], 0); //Attach render buffer to frame buffer glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, _iWidth, _iHeight); // Attach the render buffer to the frame buffer glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT,GL_RENDERBUFFER_EXT, _depth_rb); checkFramebufferStatus(); } ~HelloGPGPU() { cgDestroyProgram(_fragmentProgram); } // This method updates the texture by rendering the geometry (a teapot // and 3 rotating tori) and copying the image to a texture. // It then renders a second pass using the texture as input to an edge // detection filter. It copies the results of the filter to the texture. // The texture is used in HelloGPGPU::display() for displaying the // results. void DrawGeometry() { glPushMatrix(); glRotatef(-_rAngle, 0, 1, 0.25); float mat_red[] = {1, 0, 0, 1.0f}; float mat_white[] = {1, 1, 1, 1.0f}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_red); glutSolidTeapot(0.2); glPopMatrix(); glPushMatrix(); glRotatef(2.1 * _rAngle, 1, 0.5, 0); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_white); glutSolidTorus(0.05, 0.45, 64, 64); glPopMatrix(); /*glPushMatrix(); glRotatef(viewingAngle,1,0,0); glRotatef(-1.5 * _rAngle, 0, 1, 0.5); glutSolidTorus(0.05, 0.9, 64, 64); glPopMatrix(); glPushMatrix(); glRotatef(viewingAngle,1,0,0); glRotatef(1.78 * _rAngle, 0.5, 0, 1); glutSolidTorus(0.05, 0.9, 64, 64); glPopMatrix();*/ } void update() { CheckErrorsGL("BEGIN : HelloGPGPU::update()"); glGetIntegerv(GL_DRAW_BUFFER, &_currentDrawbuf); // Save the current Draw buffer glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fb); glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); // Draw into the first texture CheckErrorsGL("HelloGPGPU::update() : After FBO setup"); _rAngle += 0.5f; // store the window viewport dimensions so we can reset them, // and set the viewport to the dimensions of our texture int vp[4]; glGetIntegerv(GL_VIEWPORT, vp); // GPGPU CONCEPT 3a: One-to-one Pixel to Texel Mapping: A Data- // Dimensioned Viewport. // We need a one-to-one mapping of pixels to texels in order to // ensure every element of our texture is processed. By setting our // viewport to the dimensions of our destination texture and drawing // a screen-sized quad (see below), we ensure that every pixel of our // texel is generated and processed in the fragment program. glViewport(0, 0, _iWidth, _iHeight); float viewingAngle = -30; // Render a teapot and 3 tori glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //glRotatef(viewingAngle,1,0,0); glPushMatrix(); glRotatef(180,0,0,1); glRotatef(180,0,1,0); DrawGeometry(); glPopMatrix(); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // disable the frame buffer glDrawBuffer(_currentDrawbuf);//output to screen // read from the first texture glBindTexture(GL_TEXTURE_2D, _iTexture[0]); glEnable(GL_TEXTURE_2D); glPushMatrix(); glTranslated(0,-0.5,0); glRotated(45,1,0,0); glBegin(GL_QUADS); { glTexCoord2f(0, 0); glVertex3f(-0.5, -0.2, 0); glTexCoord2f(1, 0); glVertex3f( 0.5, -0.2, 0); glTexCoord2f(1, 1); glVertex3f( 0.5, 0.2, 0); glTexCoord2f(0, 1); glVertex3f(-0.5, 0.2, 0); } glEnd(); float mat_diffuse[] = {0.5f, 0.5f, 0.0f, 1.0f}; glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse); glTranslatef(0,-0.55,0); glScaled(1,0.2,1); glutSolidCube(1); glPopMatrix(); glDisable(GL_TEXTURE_2D); CheckErrorsGL("HelloGPGPU::update() : After first render pass"); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fb); glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);//Draw into the second texture glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); DrawGeometry(); //// read from the first texture //glBindTexture(GL_TEXTURE_2D, _iTexture[0]); // //// run the edge detection filter over the geometry texture //// Activate the edge detection filter program //cgGLBindProgram(_fragmentProgram); //cgGLEnableProfile(g_cgProfile); // //// bind the scene texture as input to the filter //cgGLSetTextureParameter(_textureParam, _iTexture[0]); //cgGLEnableTextureParameter(_textureParam); //CheckErrorsGL("HelloGPGPU::update() : Before second render pass"); // GPGPU CONCEPT 4: Viewport-Sized Quad = Data Stream Generator. // In order to execute fragment programs, we need to generate pixels. // Drawing a quad the size of our viewport (see above) generates a // fragment for every pixel of our destination texture. Each fragment // is processed identically by the fragment program. Notice that in // the reshape() function, below, we have set the frustum to // orthographic, and the frustum dimensions to [-1,1]. Thus, our // viewport-sized quad vertices are at [-1,-1], [1,-1], [1,1], and // [-1,1]: the corners of the viewport. /*glBegin(GL_QUADS); { glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f); glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f); glTexCoord2f(1, 1); glVertex3f( 1, 1, -0.5f); glTexCoord2f(0, 1); glVertex3f(-1, 1, -0.5f); } glEnd();*/ CheckErrorsGL("HelloGPGPU::update() : After second render pass"); // disable the filter /*cgGLDisableTextureParameter(_textureParam); cgGLDisableProfile(g_cgProfile);*/ // GPGPU CONCEPT 5: Copy To Texture (CTT) = Feedback. // We have just invoked our computation (edge detection) by applying // a fragment program to a viewport-sized quad. The results are now // in the frame buffer. To store them, we copy the data from the // frame buffer to a texture. This can then be fed back as input // for display (in this case) or more computation (see // more advanced samples.) // update the texture again, this time with the filtered scene //glBindTexture(GL_TEXTURE_2D, _iTexture); //glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, _iWidth, _iHeight); // restore the stored viewport dimensions glViewport(vp[0], vp[1], vp[2], vp[3]); } void display() { CheckErrorsGL("BEGIN : HelloGPGPU::display()"); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // disable the frame buffer glDrawBuffer(_currentDrawbuf); // Bind the filtered texture glBindTexture(GL_TEXTURE_2D, _iTexture[1]); glEnable(GL_TEXTURE_2D); CheckErrorsGL("HelloGPGPU::display() : After FBO/Texture setup"); // render a full-screen quad textured with the results of our // computation. Note that this is not part of the computation: this // is only the visualization of the results. /*glBegin(GL_QUADS); { glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f); glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f); glTexCoord2f(1, 1); glVertex3f( 1, 1, -0.5f); glTexCoord2f(0, 1); glVertex3f(-1, 1, -0.5f); } glEnd();*/ glPushMatrix(); glTranslated(0,0.2,0); glBegin(GL_QUADS); { glTexCoord2f(0, 0); glVertex3f(-0.5, -0.5, -0.5f); glTexCoord2f(1, 0); glVertex3f( 0.5, -0.5, -0.5f); glTexCoord2f(1, 1); glVertex3f( 0.5, 0.5, -0.5f); glTexCoord2f(0, 1); glVertex3f(-0.5, 0.5, -0.5f); } glEnd(); glPopMatrix(); CheckErrorsGL("HelloGPGPU::display() : After render pass"); glDisable(GL_TEXTURE_2D); CheckErrorsGL("END : HelloGPGPU::display()"); } protected: // data int _iWidth, _iHeight; // The dimensions of our array float _rAngle; // used for animation GLuint _iTexture[2]; // The texture used as a data array GLint _currentDrawbuf; CGprogram _fragmentProgram; // the fragment program used to update CGparameter _textureParam; // a parameter to the fragment program }; // GLUT idle function void idle() { glutPostRedisplay(); } // GLUT display function void display() { g_pHello->update(); // update the scene and run the edge detect filter g_pHello->display(); // display the results glutSwapBuffers(); } // GLUT reshape function void reshape(int w, int h) { if (h == 0) h = 1; glViewport(0, 0, w, h); // GPGPU CONCEPT 3b: One-to-one Pixel to Texel Mapping: An Orthographic // Projection. // This code sets the projection matrix to orthographic with a range of // [-1,1] in the X and Y dimensions. This allows a trivial mapping of // pixels to texels. glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1, 1, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } // Called when Cg detects an error void cgErrorCallback() { CGerror lastError = cgGetError(); if(lastError) { printf("%s\n\n", cgGetErrorString(lastError)); printf("%s\n", cgGetLastListing(g_cgContext)); printf("Cg error!\n"); } } // Called at startup void initialize() { float l0_dif[] = {1.0f, 1.0f, 1.0f, 1.0f}; float l0_amb[] = {0.9f, 0.9f, 0.9f, 0.9f}; float l0_pos[] = {0, 0.7, 0.7, 0.0f}; glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_dif); glLightfv(GL_LIGHT0, GL_AMBIENT, l0_amb); glDepthFunc(GL_ALWAYS); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLightfv(GL_LIGHT0, GL_POSITION, l0_pos); // Setup Cg cgSetErrorCallback(cgErrorCallback); g_cgContext = cgCreateContext(); // get the best profile for this hardware g_cgProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT); assert(g_cgProfile != CG_PROFILE_UNKNOWN); cgGLSetOptimalOptions(g_cgProfile); // Initialize FBO glewInit(); glGenFramebuffersEXT(1, &_fb); // create FBO (off-screen framebuffer) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fb); // bind offscreen framebuffer //(that is, skip the window-specific render target) glGenRenderbuffersEXT(1, &_depth_rb); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _depth_rb); ////Attach render buffer to frame buffer // glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, _iWidth, _iHeight); // // Attach the render buffer to the frame buffer // glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT,GL_RENDERBUFFER_EXT, _depth_rb); // Create the example object g_pHello = new HelloGPGPU(512, 512); } // The main function void main() { glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(512, 512); glutCreateWindow("Hello, GPGPU!"); glutIdleFunc(idle); glutDisplayFunc(display); glutReshapeFunc(reshape); initialize(); glutMainLoop(); } Thanks Jan

Share this post


Link to post
Share on other sites

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this