Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jmfel1926

macrumors member
Original poster
Jun 22, 2012
74
0
Hello to everyone,

i am having a problem with my OpenGL GLSL project in Xcode.
I am implementing a simple Bump Mapping with Blinn Phong's Lighting equations using GLSL shaders. I have a good result right now but the problem is that i cannot run my project for over 20 seconds and thats because when i exceed this time limit the Ram is full (i have 8gb's of Ram) and then the Hard Drive gets full and what i get is a stack machine saying i have to force quit Xcode and the rest of the apps. If anyone has any suggestions please respond. I have the Macbook Air 2012 11'' i5 1.7/8/128.
 
This is known as a memory leak.

It's probably coming from your display loop, are you able to post some code?
 
i will display only the main opengl code and not the shader's code. if you think it's a shader problem let me know to post it.

Code:
#include <stdarg.h>
#include <algorithm>

#include <math.h>
#include <cmath>

#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "imageloader.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>

using namespace std;

GLuint _textureId,_textureId2;
GLfloat xRot = 0;
GLfloat yRot = 0;
GLfloat xRotOld = 0;
GLfloat yRotOld = 0;
int mouseState = 0;
int xCenter = 0;
int yCenter = 0;
#define M_ROTATE_XY     1


GLfloat light_ambient[] = { 1.0,1.0,1.0,1.0};
GLfloat light_diffuse[] = {   1.0,1.0,1.0,1.0};
GLfloat light_specular[] = {   1.0,1.0,1.0,1.0};

void handleKeypress(unsigned char key, int x, int y)
{


}



GLuint programID;
GLuint vertexbuffer;

GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){
    
    const GLubyte *errString;
    // Create the shaders
    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    
    // Read the Vertex Shader code from the file
    std::string VertexShaderCode;
    std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
    if(VertexShaderStream.is_open())
    {
        std::string Line = "";
        while(getline(VertexShaderStream, Line))
            VertexShaderCode += "\n" + Line;
        VertexShaderStream.close();
    }
    
    // Read the Fragment Shader code from the file
    std::string FragmentShaderCode;
    std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
    if(FragmentShaderStream.is_open()){
        std::string Line = "";
        while(getline(FragmentShaderStream, Line))
            FragmentShaderCode += "\n" + Line;
        FragmentShaderStream.close();
    }
    
    GLint Result = GL_FALSE;
    int InfoLogLength;
    
    // Compile Vertex Shader
    printf("Compiling shader : %s\n", vertex_file_path);
    printf("Compiling shader : %s\n", VertexShaderCode.c_str());
    char const * VertexSourcePointer = VertexShaderCode.c_str();
    glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
    glCompileShader(VertexShaderID);
    
    // Check Vertex Shader
    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    if(InfoLogLength > 0) {
        std::vector<char> VertexShaderErrorMessage(InfoLogLength);
        glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
        fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
    }
    else {
        printf("Vertex shader compiled!\n\n\n");
    }
    
    // Compile Fragment Shader
    printf("Compiling shader : %s\n", fragment_file_path);
    printf("Compiling shader : %s\n", FragmentShaderCode.c_str());
    char const * FragmentSourcePointer = FragmentShaderCode.c_str();
    glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
    glCompileShader(FragmentShaderID);
    
    // Check Fragment Shader
    glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    if(InfoLogLength > 0) {
        std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
        glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
        fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
    }
    else {
        printf("Fragment shader compiled!\n\n\n");
    }
    
    // Link the program
    fprintf(stdout, "Linking program\n");
    GLuint ProgramID = glCreateProgram();
    glAttachShader(ProgramID, VertexShaderID);
    glAttachShader(ProgramID, FragmentShaderID);
    glLinkProgram(ProgramID);
    
    // Check the program
    glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
    glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> ProgramErrorMessage( std::max(InfoLogLength, int(1)) );
    glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
    fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
    
    glDeleteShader(VertexShaderID);
    glDeleteShader(FragmentShaderID);
    
    return ProgramID;
}


//Makes the image into a texture, and returns the id of the texture
GLuint loadTexture(Image* image) {
    
	GLuint textureId;
	glGenTextures(1, &textureId); //Make room for our texture
	glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
	//Map the image to the texture
	glTexImage2D(GL_TEXTURE_2D,                //Always GL_TEXTURE_2D
				 0,                            //0 for now
				 GL_RGB,                       //Format OpenGL uses for image
				 image->width, image->height,  //Width and height
				 0,                            //The border of the image
				 GL_RGB, //GL_RGB, because pixels are stored in RGB format
				 GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
                 //as unsigned numbers
				 image->pixels);               //The actual pixel data
    
	return textureId; //Returns the id of the texture
    
}


void initRendering() {
    glClearColor(0,0,0,0);
	glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    //gluLookAt(0.5,0.5,0.5,0,0,0,0,1,0);

}


void handleResize(int w, int h) {
    
	glViewport(0, 0, (GLsizei)w,(GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(95.0, (GLdouble)w / (GLdouble)h, 0.1, 100);
    glMatrixMode(GL_MODELVIEW);

}

GLdouble angle1=0.0,angle2=0.0;

GLfloat xpos=1.0f,ypos=0.0f,zpos=0.7f;

void anim(){
    
    angle2-=0.01;
    if(angle2>360) angle2 -=360;
    
  /*  xpos -=0.01;
    if(xpos<-1.0)
        xpos=-1.0;
   */
    xpos =sin(angle2);
    ypos =cos(angle2);
   // zpos = 0.5;
    glutPostRedisplay();

}

void setLighting(void) {
  
    
   // glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
    GLfloat LightPosition[] = {xpos,ypos,zpos}; //set the LightPosition to
    
    glLightfv (GL_LIGHT0, GL_POSITION, LightPosition);
  //  glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  //  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  //  glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

    }


void drawScene() {
    
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
    
    glLoadIdentity();
    glTranslatef(0.0,0.0,-1.3);
	//glTranslatef(-0.5,0.0,-3.0);
    //glRotatef(40,0.03,0.2,0.0);
    glRotatef(xRot, 1, 0, 0);
    glRotatef(yRot, 0, 1, 0);

    
    setLighting();

    GLuint width =512;
    GLuint height=512;
    
    Image *colorimage = loadBMP("normal2.bmp");
    Image *normalimage= loadBMP("normal1.bmp");
    
    
    GLuint texture = 1;
    glGenTextures(1,&texture);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D,texture);
    
    glTexImage2D(GL_TEXTURE_2D,0 ,3, width,height, 0 , GL_RGB,GL_UNSIGNED_BYTE,colorimage-> pixels);
    // How texture will look
    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_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_SMOOTH);
   
    GLuint texture2 = 1;
    glGenTextures(1,&texture2);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D,texture2);
    
    glTexImage2D(GL_TEXTURE_2D,0 ,3, width,height, 0 , GL_RGB,GL_UNSIGNED_BYTE,normalimage-> pixels);
    // How texture will look
    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_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_SMOOTH);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture); // general texture bind call we all know and love.
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, texture2);
    
    glUseProgram(programID); //program is the shader program you create using glCreateProgram() call.
    GLint h0 = glGetUniformLocation(programID, "normalmap");
    GLint h1 = glGetUniformLocation(programID, "colormap");
    glUniform1i(h0, 0); //first texture sampler
    glUniform1i(h1, 1); //second texture sampler.
    // Texture Coordinates
    
    float shine[] = {1.0f, 1.0f, 1.0f, 1.0f};
    glMaterialfv(GL_FRONT, GL_SHININESS, shine);
    glBegin(GL_QUADS);
    glNormal3f(0.0, 0.0f, 1.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-1.0f, 1.0f,0.0);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(1.0f, 1.0f,0.0);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(1.0f, -1.0f,0.0);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(-1.0f, -1.0f,0.0);
    glEnd();
    
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);
    
    glUseProgram(0);
    
    glPushMatrix();
    glTranslatef(xpos,ypos,zpos);
    glColor3f(1.0,1.0,1.0);
    glutSolidSphere(0.05, 20, 20);
    glPopMatrix();
    glColor3f(1.0,1.0,1.0);
    
    glFlush();
	glutSwapBuffers();
}


void mouse(int button, int state, int x, int y) {
    xCenter = x;
    yCenter = y;
    
    if (state == GLUT_DOWN) {
        if (button == GLUT_LEFT_BUTTON) {
            mouseState = M_ROTATE_XY;
            xRotOld = xRot;
            yRotOld = yRot;
        }
    } else {
        mouseState = 0;
    }
}

void motion(int x, int y) {
    if (mouseState == M_ROTATE_XY) {
        xRot = xRotOld + (float)(y - yCenter) / 4.0;
        yRot = yRotOld + (float)(x - xCenter) / 4.0;
    }
}


int main(int argc, char** argv) {
    
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(1000, 800);
    glutInitWindowPosition(200,200);
	glutCreateWindow("Bling Phong Bump Mapping");
    
    const GLubyte* version = glGetString(GL_SHADING_LANGUAGE_VERSION);
    fprintf (stderr, "GLSL Version: %s\n", version);
    
    version = glGetString(GL_VERSION);
    fprintf (stderr, "openGL Version: %s\n", version);
    
    version = glGetString(GL_EXTENSIONS);
    fprintf (stderr, "openGL Extenstions: %s\n", version);
    
	initRendering();
    //glutKeyboardFunc(handleKeypress);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutIdleFunc(anim);
    glutDisplayFunc(drawScene);

    programID = LoadShaders( "spotlight.vert", "spotlight.frag" );
    glUseProgram(programID);

	glutReshapeFunc(handleResize);
	glutMainLoop();
	return 0;
    
}
 
Last edited by a moderator:
Code:
    Image *colorimage = loadBMP("normal2.bmp");
    Image *normalimage= loadBMP("normal1.bmp");

My first guess would be that these pointers (properly, the memory they point to) need to be deleted before you exit the drawScene() function. You should be able to delete them after you have uploaded their data to OpenGL with glTexImage2D(...)

If I'm wrong then you'll get a segfault!
 
well finally i found the mistake. Well it was something close to what you pointed. I did not delete the texture after i used them. so i had to call glDelteTextures for both textures i used before the end of the drawscene function. Thanks a lot for your time !
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.