Sign in to follow this  
coderWalker

OpenGL File not reading correctly?

Recommended Posts

EDIT: Problem changed, check most recent post.

I have the following code in my program:
[code]
static const GLfloat g_vertex_buffer_data[] =
{
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f
};
static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 };
[/code]

I am getting the following errors:
[quote]
Error 1 error C2059: syntax error : '{'
Error 2 error C2334: unexpected token(s) preceding '{'; skipping apparent function body
Error 3 error C2059: syntax error : '{'
Error 4 error C2334: unexpected token(s) preceding '{'; skipping apparent function body
[/quote]

Basically I am trying to move from OpenGL 1.0 to OpenGL 3.1.
So I need these two items to specify the coordinates of the vertices and the vertex array.

What is the problem with this code?

I got it strait from a tutorial here:
[url="http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Chapter-2.1:-Buffers-and-Textures.html"]Joe's Blog[/url]

Thanks in advance,
I have been looking at this for almost 15mins.

CoderWalker

Share this post


Link to post
Share on other sites
There is no error in that code. It must be something wrong somewhere else.

Share this post


Link to post
Share on other sites
Maby its not building because it's inside a struct?

Here is the first part of the program:
[code]
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include <GL/glew.h>

#include "stdlib.h"
#include "iostream"
#include <fstream>
#include <sstream>
#include <deque>
#include "cstring"
using namespace std;

static struct
{
//Data
GLuint vertex_buffer, element_buffer;
GLuint textures[2];

static const GLfloat g_vertex_buffer_data[] =
{
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f
};
static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 };

//Shaders
GLuint vertex_shader, fragment_shader, program;
struct {
GLint fade_factor;
GLint textures[2];
} uniforms;

struct {
GLint position;
} attributes;

GLfloat fade_factor;
} resources;
[/code]

I assumed it was the lines posted earlier, because if I comment them out the error goes away.
So there must be something conflicting?

Share this post


Link to post
Share on other sites
I found out that the array was not suppose to be inside the struct, just the tutorial was misleading.

I think that the tutorial is using C opposed to C++.

It contains the following line:
[code]file_contents(filename, &length);[/code]

I didn't have a library included with that command.
So I tried to replace it with a fstream command.

Here is what I have:
[code]
ifstream file (filename,ios::in);
if (file.good() ==false)
{
return 0;
}
file.seekg(0, ios::end);
int size = file.tellg();
source = new GLchar[size];
file.seekg(0,ios::beg);
file.read(source,size);
file.close();
[/code]

The problem is that after reading the file it has garbage appended to the end. What am I doing wrong?

variable source contains:
[code]
#version 110

attribute vec2 position;

varying vec2 texcoord;

void main()
{
gl_Position = vec4(position, 0.0, 1.0);
texcoord = position * vec2(0.5) + vec2(0.5);
}?????????????????????????
[/code]

Share this post


Link to post
Share on other sites
How do you print the data in source? If you just print it as a string remember that a string ends with a null character '\0'. source will probably not contain a null character.

This will [b]not[/b] work:
[code]std::cout << source;[/code]

Share this post


Link to post
Share on other sites
If you want to read text out of a file, and treat it as a c-string:[code] source = new GLchar[size + 1];//extra byte for the null terminator
...
file.read(source,size);
...
source[size] = '\0';//add the null terminator[/code]

Share this post


Link to post
Share on other sites

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