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 10 of 10

Thread: Redbook 8th sample code?

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    11

    Redbook 8th sample code?

    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?

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    What actual problem do you have? I think we can help.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    11
    Quote Originally Posted by thokra View Post
    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

  4. #4
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,145
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    11
    Quote Originally Posted by Aleksandar View Post
    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.
    you can get it here
    http://my.safaribooksonline.com/book.../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.

  6. #6
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    9
    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.
    Last edited by Qoheleth; 12-16-2012 at 05:02 PM. Reason: clarification

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    1
    uppppppppppppppppppppppppppppppppppppppppp

  8. #8
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    9
    @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

    Code :
    #pragma once
    // ---------------------------------------------------------------------------
    // LoadShader.h
    // Quick and dirty LoadShader function for the OpenGL Programming Guide 4.3
    // Red Book.
    //
    // Author: Qoheleth
    // [url]http://www.opengl.org/discussion_boards/showthread.php/180175-Redbook-8th-sample-code?p=1245842#post1245842[/url]
    // ---------------------------------------------------------------------------
    #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 );

    Code :
    // ---------------------------------------------------------------------------
    // 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();
    }

  9. #9
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    There is also the Tutorial pages in the Wiki and they have code for reading your shader file.
    http://www.opengl.org/wiki/Tutorials
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  10. #10
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    1
    b*i n*y hay.dúng l* mình dang c?n thank bác nha

Posting Permissions

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