PDA

View Full Version : Redbook 8th sample code?



kantaki
12-13-2012, 08:08 PM
Hello,

I recently bought the new redbook 8th and it is really good so far. I also know its not done yet but their website is now down since 2 weeks and I am not able to get the sample code.

http://www.opengl-redbook.com/

Its a little bit annoying because they use their own libraries in the samples. like loadshaders();
I mean it's not a huge problem but it would definitely help to have to sample code.

Do you have any idea if there is another place where I can get those sample files?

thokra
12-14-2012, 03:22 AM
What actual problem do you have? I think we can help.

kantaki
12-15-2012, 07:37 AM
What actual problem do you have? I think we can help.
I think the helper library that they use are only for loading shaders. They probably just didn't want to overcomplicate the hello world programm. Shaders will be introduced in unit 2.

I think I will be fine.

thanks

Aleksandar
12-15-2012, 08:43 AM
Where did you buy the book, and what's the price?
The book has not been officialy released yet. That's why I'm asking those questions. :confused:

kantaki
12-15-2012, 08:47 AM
Where did you buy the book, and what's the price?
The book has not been officialy released yet. That's why I'm asking those questions. :confused:
you can get it here
http://my.safaribooksonline.com/book/programming/opengl/9780132748445

I just can't tell you if it is good or bad because I only read the first 2 chapters.
But my first impression is that the book has a good structure.

Also it got it for ~$45 and it is the rough cuts edition obviously. But you always have access to the newest edition online. Also the downloadable pdf version is really really bad formatted, you better read it online.

Qoheleth
12-16-2012, 04:47 PM
kantaki, I also registered with Safari Online. It's a decent book so far. The best resource I've found yet. Everything on the web is either outdated, or not well explained.

Check back in a few days or private message me your e-mail. Based on the section "Compiling Shaders" under Chapter 2, I think I can create a LoadShaders.h that simulates the author's version. All those functions you see in the Compiling Shaders section is basically, I believe, the LoadShaders.h file. When I get it working I'll e-mail you a copy of my version of the LoadShaders.h and LoadShaders.cpp file. I'll also post a comment at SafariOnline for that book.

chuatroi123
12-17-2012, 12:26 AM
uppppppppppppppppppppppppppppppppppppppppp

Qoheleth
12-17-2012, 01:55 AM
@chuatroi123? Huh?

Anyways, I was able to make my own LoadShaders.h and it works! Just like in the book, I was able to get the triangles colored blue.
You can download the code here: http://www.mediafire.com/?b7znikjm54itau8


#pragma once
// ---------------------------------------------------------------------------
// LoadShader.h
// Quick and dirty LoadShader function for the OpenGL Programming Guide 4.3
// Red Book.
//
// Author: Qoheleth
// http://www.opengl.org/discussion_boards/showthread.php/180175-Redbook-8th-sample-code?p=1245842#post1245842
// ---------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#include "glew.h"
#include "freeglut.h"

struct ShaderInfo {
GLenum vTarget;
const char *vShaderFile;
GLenum fTarget;
const char *fShaderFile;
};

GLuint LoadShaders( ShaderInfo shaderInfo );
const char* getShaderProgram( const char *filePath, string &shaderProgramText );


// ---------------------------------------------------------------------------
// LoadShader.cpp
// Quick and dirty LoadShader function for the OpenGL Programming Guide 4.3
// Red Book.
//
// Author: Qoheleth
// http://www.opengl.org/discussion_boards/showthread.php/180175-Redbook-8th-sample-code?p=1245842#post1245842
// ---------------------------------------------------------------------------
#include "LoadShader.h"

GLuint LoadShaders( ShaderInfo shaderInfo )
{
GLuint program;
GLuint vertexShader;
GLuint fragmentShader;
vertexShader = glCreateShader( GL_VERTEX_SHADER ); // create a vertex shader object
fragmentShader = glCreateShader( GL_FRAGMENT_SHADER ); // create a fragment shader object

// load and compile vertex shader
string shaderProgramText;
const char* text = getShaderProgram( shaderInfo.vShaderFile, shaderProgramText );
GLint length = shaderProgramText.size();
glShaderSource( vertexShader, 1, &text, NULL );
glCompileShader( vertexShader );

for ( int i = 0; i < length; i++ )
{
cout << text[ i ];
}

GLint status;
glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &status );

if ( !( status == GL_TRUE ) )
cerr << "\nVertex Shader compilation failed..." << '\n';

char *infoLog = new char[ 100 ];
GLsizei bufSize = 100;
glGetShaderInfoLog( vertexShader, bufSize, NULL, infoLog );
for ( int i = 0; i < bufSize; i++ )
cout << infoLog[ i ];
delete [] infoLog;

// load and compile fragment shader
shaderProgramText = "";
text = getShaderProgram( shaderInfo.fShaderFile, shaderProgramText );
glShaderSource( fragmentShader, 1, &text, NULL );
glCompileShader( fragmentShader );

glGetShaderiv( fragmentShader, GL_COMPILE_STATUS, &status );

if ( !( status == GL_TRUE ) )
cerr << "\nFragment Shader compilation failed..." << '\n';

infoLog = new char[];
bufSize = 0;
glGetShaderInfoLog( fragmentShader, bufSize, NULL, infoLog );
for ( int i = 0; i < bufSize; 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;
}

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

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

return shader.c_str();
}

V-man
12-18-2012, 12:43 PM
There is also the Tutorial pages in the Wiki and they have code for reading your shader file.
http://www.opengl.org/wiki/Tutorials

tulong231
12-18-2012, 03:18 PM
b*i n*y hay.dúng l* mình dang c?n thank bác nha