Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: application not using shaders :((((

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    9

    application not using shaders :((((

    Hii. I am trying to render a half completed cube, but for some reason my application isn't using the shaders I load. I am guessing it has something to do with my shader loading function, because I am only half confident that it actually works. Could someone please look at my code and help me out?

    CHeers.


    Main.cpp:

    Code :
    #include "main.h"	 //g++ *.cpp -lGLEW -lGL -lglfw -lGLU -lglut -DGL_GLEXT_PROTOTYPES -o triangle
    #define BUFFER_OFFSET(offset) ((void *)(offset))
    using namespace std;
     
    enum VAO_IDs	{Triangles, NumVAOs};
    enum Buffer_IDs	{ArrayBuffer, cBuffer, NumBuffers};
    enum Attrib_IDs {vPosition = 0, colorPosition = 1};
    GLuint VAOs[NumVAOs];
    GLuint ColourObjects[NumVAOs];
    GLuint Buffers[NumBuffers];
    const GLuint NumVertices = 12;
    GLuint ProgramID;
    program program;
    Shader vertex;
    Shader fragment;
    //assigning vertex data to buffer objects and preparing to send to 
    void init()	 //Vertex Shaders.
    {
    	glewInit();
     
     
    	program.createProgram();
    	vertex.Load(GL_VERTEX_SHADER,"triangle.vert");		//compiling and linking vertex shader
    	program.attach(vertex.ShaderID);
    	program.link();
    														//compiling and linking fragment shader
     
    	fragment.Load(GL_FRAGMENT_SHADER, "triangle.frag");
    	program.attach(fragment.ShaderID);
    	program.link();
     
     
    	glGenVertexArrays(NumVAOs, VAOs);
    	glBindVertexArray(VAOs[Triangles]);
     
    	GLfloat vertices [NumVertices] [3] =
    	{
    		{	0.30,  0.00, -0.30	},	//Triangle 1
    		{	0.30,  0.00,  0.30	},
    		{  -0.30,  0.00, -0.30	},
    		{  -0.30,  0.00, -0.30	},	//Triangle 2
    		{  -0.30,  0.00,  0.30	},
    		{   0.30,  0.00, -0.30	},
    		{  -0.30,  0.00,  0.30	},	//Triangle 3
    		{	0.30,  0.00,  0.30	},
    		{	0.30,  0.60,  0.30	},
    		{  -0.30,  0.60,  0.30	},	//Triangle 4
    		{	0.30,  0.60,  0.30	},
    		{  -0.30,  0.00,  0.30	},
    	};
     
    	glGenBuffers(NumBuffers, Buffers);
    	glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
     
     
     
    	glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
    	glEnableVertexAttribArray(vPosition );
     
     
    	GLfloat g_color_buffer_data[NumVertices][3] = 
    	{
    	    {	0.583,  0.771,  0.014	},
    	    {	0.609,  0.115,  0.436	},
    	    {	0.327,  0.483,  0.844	},
    	    {	0.822,  0.569,  0.201	},
    	    {	0.435,  0.602,  0.223	},
    	    {	0.310,  0.747,  0.185	},
    	    {	0.597,  0.770,  0.761	},
    	    {	0.559,  0.436,  0.730	},
    	    {	0.359,  0.583,  0.152	},
    	    {	0.483,  0.596,  0.789	},
    	    {	0.559,  0.861,  0.639	},
    	    {	0.195,  0.548,  0.859	},
    	};
    	glBindBuffer(GL_ARRAY_BUFFER, Buffers[cBuffer]);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
     
    	glVertexAttribPointer(colorPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
    	glEnableVertexAttribArray(colorPosition);	
     
     
    }
     
     
     
     
     
    void display()
    {
    	program.use();
    	glClearColor(1,0.5f,0,0);
    	glClear(GL_COLOR_BUFFER_BIT);
     
    	glUseProgram(ProgramID);
     
    	glBindVertexArray(VAOs[Triangles]); //?
    	glDrawArrays(GL_TRIANGLES, 0, NumVertices);
     
    	glFlush();
    }
     
    int main(int argc, char** argv)
    {
    	const int width = 400, height = 400;
     
    	glutInit(&argc, argv);
    	glutInitWindowSize(width, height);
    	glutInitDisplayMode(GLUT_RGBA);
    	glutInitContextVersion(3, 3);
    	glutInitContextProfile(GLUT_CORE_PROFILE);
    	glutCreateWindow(argv[0]);
     
    	init();
    	glutDisplayFunc(display);
    	glutMainLoop();
    	return 0;
    }

    main.h:

    Code :
    #include"GL/glew.h"
    #include"GL/gl.h"
    #include"GL/glut.h"
    #include <GL/freeglut.h>
    #include"GL/glu.h"
    #include"GLFW/glfw3.h"
    #include<iostream>
    #include <fstream>
    #include <vector>
    #include "LoadMyShaders.h"
    #include <string>

    triangle.frag:

    Code :
    #version 330 core
    in vec3 fragmentColor;
    out vec4 color;
     
    void main()
    {
    	color = fragmentColor;
    }

    triangle.vert:

    Code :
    #version 330 core
     
    layout(location = 0) in vec3 vPostion;
    layout(location = 1) in vec4 vertexColor;
    out vec4 fragmentColor;
    void main()
    {
    	gl_Position = vPosition;
    	fragmentColor = vertexColor;
    }

    LoadMyShaders.h:

    Code :
    #include<string>
    #include<GL/gl.h>
    class Shader{
    public:
    	void Load(GLenum Type, std::string fileName);
    	GLuint ShaderID;	//shader object	
    };
     
     
    class program{
    private:
    	GLuint ProgramID;
    public:
    	void createProgram();
    	void attach(GLuint ShaderID);
    	void link();
    	void use();
    };

    LoadMyShaders.cpp:

    Code :
    #include"LoadMyShaders.h"
    #include<iostream>
    #include<fstream>
     
    using namespace std;
     
    void Shader::Load(GLenum Type, std::string fileName)
    {
    	GLenum type = Type;
    	//Loads code
    	 ShaderID = glCreateShader(type);	//type = GL_VERTEX_SHADER or GL_FRAGMENT_SHADER etc.
     
    	 string data = "";
    	 ifstream ShaderStream(fileName.c_str());	//c_str() = reads contents of string and put it in to a constant char * / c_str
    	 if (ShaderStream.is_open())
    	 {
     
    	 	string line = "";
    	 	while(getline(ShaderStream, line)){		//first argument = file, second argument puts stuff in to line.
    	 		data += '\n' + line;
    	 		}
    	 	ShaderStream.close();
    	 }
     
    	 //Compliles the shader.
    	 const char * codeSource = data.c_str();
    	 glShaderSource(ShaderID, 1, &codeSource, NULL);
    	 glCompileShader(ShaderID);
    }

    linkShaders.cpp:

    Code :
    #include"LoadMyShaders.h"
     
    using namespace std;
     
    void program::createProgram()
    {
    	ProgramID = glCreateProgram();
    }
     
    void program::attach(GLuint ShaderID)
    {
    	 glAttachShader(ProgramID, ShaderID);
    }
     
    void program::link()
    {
    	glLinkProgram(ProgramID);
    }
     
    void program::use()
    {
    	glUseProgram(ProgramID);
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,109
    First - put some error checking in your code. When compile shaders
    Code :
    glCompileShader( p_ID );
    GLint compiled = 0;
    glGetShaderiv( p_ID, GL_COMPILE_STATUS, &compiled);
    if (!compiled)
    {
      GLint l;
      GLchar compilerSpew[1024];
      glGetShaderInfoLog(p_ID, 1024, &l, compilerSpew);
    }

    and
    Code :
    glGetProgramiv( ProgramID, GL_LINK_STATUS, &linked);
    afer linking

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •