Jump to content
  • Advertisement
Sign in to follow this  
coderWalker

OpenGL File not reading correctly?

This topic is 2613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

EDIT: Problem changed, check most recent post.

I have the following code in my program:

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 };


I am getting the following errors:

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:
Joe's Blog

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

CoderWalker

Share this post


Link to post
Share on other sites
Advertisement
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:

#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;


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:
file_contents(filename, &length);

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:

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();


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

variable source contains:

#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);
}?????????????????????????

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 not work:
std::cout << source;

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: source = new GLchar[size + 1];//extra byte for the null terminator
...
file.read(source,size);
...
source[size] = '\0';//add the null terminator

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!