I am doing a 3rd year at University in Computer Games Programming. We have just started using OpenGL and I need to say I don't have any experience in using it. I am trying to open up a shader from a text file, but I keep getting the same error. Any clues what am I doing wrong? Here is the part of the code as well as the error that I am getting:
EDIT:
I have tried to change the ifstream file to the following, but it just gave me dozens of unresolved external errors:
std::ifstream file(filename.c_str());
ERROR:
c:\users\adrian\desktop\graphics programming\lesson29\lesson29\shader.cpp(10) : error C2664: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'const std::string' to 'const char *'
shader.h
#pragma once #include <string> #include <glew.h> GLuint loadShader(const std::string& filename, GLenum type); GLuint createProgram(GLuint* shaders, int count);
shader.cpp
#include "shader.h"
#include <iostream>
#include <fstream>
#include <sstream>
//Read the file
std::string readFile(const std::string& filename)
{
//Open the file
std::ifstream file(filename.c_str());
//Create a string stream to act as buffer to read the file into
std::stringstream buffer;
//Read the file into the buffer
buffer << file.rdbuf();
//Get the internal string of the buffer and use it as content
std::string content = buffer.str();
return content;
}
//Load and compile a shader
GLuint loadShader(const std::string& filename, GLenum type)
{
//Read the file of name filename
std::string fileContent = readFile(filename);
//Create a shader
GLuint shader = glCreateShader(type);
//Get the underlying character array from the read-in file
const char* source = fileContent.c_str();
//Set the shader source to the character array
glShaderSource(shader, 1, &source, 0);
//Compile the shader
glCompileShader(shader);
//Check if it is compiled
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
//If it is not compiled get the shader compiler log
if(!compiled)
{
GLsizei length;
char log[1024];
glGetShaderInfoLog(shader, 1024, &length, log);
std::cout << "Could not compile shader: " << shader << ": " << filename << std::endl;
std::cout << log << std::endl;
glDeleteShader(shader);
return 0;
}
//Return to the shader object
return shader;
}
//Take an array of shader objects and attach them to a program object
GLuint createProgram(GLuint* shaders, int count)
{
//Create the program object
GLuint program = glCreateProgram();
//Attach all shader objects to the program
for(int i = 0; i < count; i++)
glAttachShader(program, shaders[i]);
//Link the program
glLinkProgram(program);
//Check if it is linked
GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
//If it is not linked, get the program link log
if(!linked)
{
GLsizei length;
char log[1024];
glGetProgramInfoLog(program, 1024, &length, log);
std::cout << "Error linking program. " << std::endl;
std::cout << log << std::endl;
for(int i=0; i<count; i++)
glDetachShader(program, shaders[i]);
glDeleteProgram(program);
return 0;
}
//Return to the program object
return program;
}
main.cpp
#pragma comment(lib, "GLFWDLL")
#pragma comment(lib, "OpenGL32")
#pragma comment(lib, "glew32")
#define GLFW_DLL
#include <glew.h>
#include <GL\glfw.h>
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\gtx\quaternion.hpp>
#include <glm\gtc\type_ptr.hpp>
#include <glm\gtx\constants.hpp>
#include <cstdlib>
#include <iostream>
#include "geometry.h"
#include "shader.h"
bool running = true;
//Declare an array of 2 shader objects
GLuint shaders[2];
//Declare a program object
GLuint program;
//Store the location of the uniform colour
GLint colourUniform;
//Declare a render object for geometry
render_object object;
void initialise()
{
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 projection = glm::perspective(
glm::degrees(glm::quarter_pi<float>()),
800.0f/600.0f,
0.1f,
10000.0f);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(projection));
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_VERTEX_ARRAY);
//Load vertex shader from colour.vert text file
shaders[0] = loadShader("colour.vert", GL_VERTEX_SHADER);
//Load fragment shader from colour.frag text file
shaders[1] = loadShader("colour.frag", GL_FRAGMENT_SHADER);
//Check if both shaders loaded in correctly
if(shaders[0] && shaders[1])
{
//Create a program using the loaded shaders
program = createProgram(shaders, 2);
//If program is not loaded, exit application
if(!program)
exit(EXIT_FAILURE);
}
else
exit(EXIT_FAILURE);
//Get the uniform location
colourUniform = glGetUniformLocation(program, "colour");
//Create a box
geometry* geom = createBox();
object.geometry = geom;
//Set the colour of the box to green
object.colour = glm::vec4(0.0f, 1.0f, 0.0f, 1.0f);
}
void update(double deltaTime)
{
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
}
void render()
{
//Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Set the camera view
glm::mat4 view = glm::lookAt(glm::vec3(10.0f, 10.0f, 10.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
glMatrixMode(GL_MODELVIEW);
//Use the created program
glUseProgram(program);
//Set the colour iniform to be the colour of the object
glUniform4fv(colourUniform, 1, glm::value_ptr(object.colour));
//Render the view
object.render(view);
//Stop using the program
glUseProgram(0);
glfwSwapBuffers();
}
//Free any resources that are not used by the application anymore
void cleanup()
{
//If the objects are created, delete them from memory
if(program)
glDeleteProgram(program);
if(shaders[0])
glDeleteShader(shaders[0]);
if(shaders[1])
glDeleteShader(shaders[1]);
if(object.geometry)
delete object.geometry;
}
int main()
{
if(!glfwInit())
exit(EXIT_FAILURE);
if(!glfwOpenWindow(800, 600, 0, 0, 0, 0, 0, 0, GLFW_WINDOW))
{
glfwTerminate();
exit(EXIT_FAILURE);
}
GLenum error = glewInit();
if(error != GLEW_OK)
{
std::cout << "Error: " << glewGetErrorString(error) << std::endl;
exit(EXIT_FAILURE);
}
initialise();
double prevTimeStamp = glfwGetTime();
double currentTimeStamp;
while(running)
{
currentTimeStamp = glfwGetTime();
update(currentTimeStamp - prevTimeStamp);
render();
prevTimeStamp = currentTimeStamp;
}
cleanup();
glfwTerminate();
exit(EXIT_SUCCESS);
}
Thank you very much for your help and sorry for the trouble!
speed-e
This post has been edited by speed-e: 03 October 2012 - 11:24 AM

New Topic/Question
Reply




MultiQuote


|