PDA

View Full Version : Get a blank window without displaying any images



ZXJ106
02-12-2015, 07:29 PM
I couldn't get two displayed triangles as the first example shows in the book. All I get is a blank window. Below is my entire code, could anyone help me please ? I've spent a whole day on this, but still couldn't get it right.



#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <glew.h>
#include <FreeGL\freeglut.h>

using namespace std;

GLint attribute_v=-1; // global variables
GLuint vertexbuffer;
GLuint VertexArrayID;
GLuint program;
int width=500; int height=700;



struct ShaderInfo{

GLenum vshader;
const char* vertexfile;
GLenum fshader;
const char* fragmentfile;

};


const char* getshaderprogram(const char* filename, string& shader)
{
fstream shaderFile( filename, ios::in );

if ( shaderFile.is_open() )
{
std::stringstream buffer;
buffer << shaderFile.rdbuf();
shader = buffer.str();
buffer.clear();
}
shaderFile.close();

return shader.c_str();

}

GLuint LoadShaders(ShaderInfo shaderinfo)
{
GLuint program;
//load and compile vertex shader
GLuint vertexshader=glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentshader=glCreateShader(GL_FRAGMENT_SHADER);
string shaderprogramtext;
const char* Vertexshadersource=getshaderprogram(shaderinfo.ver texfile,shaderprogramtext);
const GLint vlength=shaderprogramtext.size();
glShaderSource(vertexshader,1,&Vertexshadersource,&vlength);

glCompileShader(vertexshader);
for( unsigned int i=0;i<shaderprogramtext.size();++i)
{
cout<<Vertexshadersource[i];
}
cout<<endl;
GLint status;
glGetShaderiv( vertexshader, GL_COMPILE_STATUS, &status );

if(status!=GL_TRUE)
{
std::cerr<<"unable to compile the vertex shader..."<<endl;
}
char* infoLog=new char[100];
GLsizei buffsize=0;
glGetShaderInfoLog(vertexshader,buffsize,NULL,info Log);
for(int i=0;i<buffsize;++i)
{
cout<<infoLog[i];
}
cout<<endl;
delete[] infoLog;

//load and compile fragment shader
string fragmentshadertext;
const char* Fragmentshadersource=getshaderprogram(shaderinfo.f ragmentfile,fragmentshadertext);
const GLint flength=fragmentshadertext.size();
for(unsigned int i=0;i<fragmentshadertext.size();++i)
{
std::cout<<Fragmentshadersource[i];
}
cout<<endl;
glShaderSource(fragmentshader,1,&Fragmentshadersource,&flength);
glCompileShader(fragmentshader);

glGetShaderiv( fragmentshader, GL_COMPILE_STATUS, &status );
if ( !( status == GL_TRUE ) )
cerr << "\nFragment Shader compilation failed..." << '\n';
infoLog = new char[];
buffsize = 0;
glGetShaderInfoLog( fragmentshader, buffsize, NULL, infoLog );
for ( int i = 0; i < buffsize; ++i )
cout << infoLog[ i ] << endl;
delete [] infoLog;


// create the shader program
program = glCreateProgram();
// attach the vertex and fragment shaders to the program
glAttachShader( program, vertexshader );
glAttachShader( program, fragmentshader );

// link the objects for an executable program
glLinkProgram( program );

glGetProgramiv( program, GL_LINK_STATUS, &status );
if ( !( status == GL_TRUE ) )
cout << "Link failed..." << endl;

// return the program
return program;
}



void
init(void)
{

glGenVertexArrays(1, &VertexArrayID);

glBindVertexArray(VertexArrayID);
GLdouble vertices[6][2] = {
{ -0.90, -0.90 }, // Triangle 1
{ 0.85, -0.90 },
{ -0.90, 0.85 },
{ 0.90, -0.85 }, // Triangle 2
{ 0.90, 0.90 },
{ -0.85, 0.90 }
};
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
ShaderInfo shaders = { GL_VERTEX_SHADER, "vertex_shader.txt" , GL_FRAGMENT_SHADER, "fragment_shader.txt" };
program = LoadShaders(shaders);


glUseProgram(program);
glVertexAttribPointer(attribute_v, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attribute_v);
const GLchar* attribute_name = "attribute_v"; // this name has to be the same variable name in the vertex_shader file.
attribute_v = glGetAttribLocation(program, attribute_name); // check attribute activated.
if (attribute_v == -1) {
cout<<"could not bind attribute for vertex..."<<endl;
}
}

void
display(void)
{
//glClearColor(1.0f,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glBindVertexArray(VertexArrayID);
glDrawArrays(GL_TRIANGLES, 0, 6);
glFlush();
}



int
main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(200,200);
glutInitContextVersion(4, 5);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("Triangle Visualization");

glewExperimental = GL_TRUE;
if (glewInit()) {
cerr << "Unable to initialize GLEW ... exiting" << endl;
exit(EXIT_FAILURE);
}
cout<< "The version of opengl is : "<<glGetString(GL_VERSION)<<endl;

init();
glutDisplayFunc(display);
glutMainLoop();
}

Spec-Chum
02-13-2015, 07:02 AM
getshaderprogram is returning a pointer to a local std::stringstream, make std::stringstream buffer global or, better yet, return a std::string and get the address of it (&) in the LoadShaders function.