The first example of Opengl red book ﹣ vs2017 ﹣ glew2.1 ﹣ recording successful code snippets

After a busy afternoon, I was finally able to run:

In the Opengl programming guide, the code in the book is not complete, so it's hard to find the original code on the Internet

https://blog.csdn.net/candycat1992/article/details/39676669

I don't know why, before downloading the glew2.1 compression package, there will be errors, and it can't be decompressed. Today, it's good

Use [manage NuGet package] to download the next glut package. This package is also available in the environment of glew32.lib x64

LNK2001 error occurred before, I don't know why

The function glutInitContextVersion is useful in the book. I can't use it here. Fortunately, it's not necessary

 

2018-05-05 research new version of glew2.1.cpp:

// 2018-05-05 development environment retest.cpp: define the entry point of the console application.
//

#include "stdafx.h"

#include <GL/glew.h>
#include <GL/glut.h>  
#include "LoadShaders.h"

enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };
#define BUFFER_OFFSET(offset) ((void*)(offset))


GLuint  VAOs[NumVAOs];
GLuint  Buffers[NumBuffers];
const GLuint NumVertices = 6;

// Book p21
void display()
{
	glClear(GL_COLOR_BUFFER_BIT);

	glBindVertexArray(VAOs[Triangles]);
	glDrawArrays(GL_TRIANGLES, 0, NumVertices);

	glFlush();
}

// Book P12
void init(void)
{
	glGenVertexArrays(NumVAOs, VAOs);
	glBindVertexArray(VAOs[Triangles]);

	GLfloat vertices[NumVertices][2] = {

		{ -0.90, -0.90 },  // Triangle 1  
	{ 0.85, -0.90 },
	{ -0.90,  0.85 },
	{ 0.90, -0.85 },  // Triangle 2  
	{ 0.90,  0.90 },
	{ -0.85,  0.90 }
	};

	
	glGenBuffers(NumBuffers, Buffers);
	glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	// In fact, it can run without shaders, but the patches are white. Without these two shaders, there will be no error, but the patches are white
	ShaderInfo shaders[] = {
	//	{ GL_VERTEX_SHADER,"triangles.vert" },
	//{ GL_FRAGMENT_SHADER,"triangles.frag" },
	{ GL_NONE,NULL }
	};

	GLuint program = LoadShaders(shaders);
	glUseProgram(program);
	// END Shader

	glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
	glEnableVertexAttribArray(vPosition);
}

// Book P11
int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA);
	glutInitWindowPosition(960, 100);
	glutInitWindowSize(512, 512);
	//glutInitContextVersion(4, 3);
	glutCreateWindow("First OpenGL program");

	if (glewInit())
	{
		cerr << "Unable to initalize GLEW ... exiting" << endl;
		exit(EXIT_FAILURE);
	}

	init();

	glutDisplayFunc(&display);
	glutMainLoop();
	return 0;
}

stdafx.h:

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>



// TODO: reference other header files required by the program here
#include<iostream>
using namespace std;

 

Only when the source code is found can the LoadShader files be obtained:

(OpenGL Programming Guide (Eighth Edition) pdf + source code) http://down.51cto.com/data/2193129

 

LoadShaders.h:

//////////////////////////////////////////////////////////////////////////////
//
//  --- LoadShaders.h ---
//
//////////////////////////////////////////////////////////////////////////////

#ifndef __LOAD_SHADERS_H__
#define __LOAD_SHADERS_H__

#include <GL/gl.h>

#ifdef __cplusplus
extern "C" {
#endif  // __cplusplus

//----------------------------------------------------------------------------
//
//  LoadShaders() takes an array of ShaderFile structures, each of which
//    contains the type of the shader, and a pointer a C-style character
//    string (i.e., a NULL-terminated array of characters) containing the
//    entire shader source.
//
//  The array of structures is terminated by a final Shader with the
//    "type" field set to GL_NONE.
//
//  LoadShaders() returns the shader program value (as returned by
//    glCreateProgram()) on success, or zero on failure. 
//

typedef struct {
    GLenum       type;
    const char*  filename;
    GLuint       shader;
} ShaderInfo;

GLuint LoadShaders( ShaderInfo* );

//----------------------------------------------------------------------------

#ifdef __cplusplus
};
#endif // __cplusplus

#endif // __LOAD_SHADERS_H__

 LoadShaders.cpp:

//////////////////////////////////////////////////////////////////////////////
//
//  --- LoadShaders.cxx ---
//
//////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <cstdlib>
#include <iostream>

//#define GLEW_STATIC
#include <GL/glew.h>
#include "LoadShaders.h"

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

//----------------------------------------------------------------------------

static const GLchar*
ReadShader( const char* filename )
{
#ifdef WIN32
	FILE* infile;
	fopen_s( &infile, filename, "rb" );
#else
	FILE* infile;
	fopen_s(&infile,filename, "rb");
#endif // WIN32

    if ( !infile ) {
#ifdef _DEBUG
        std::cerr << "Unable to open file '" << filename << "'" << std::endl;
#endif /* DEBUG */
        return NULL;
    }

    fseek( infile, 0, SEEK_END );
    int len = ftell( infile );
    fseek( infile, 0, SEEK_SET );

    GLchar* source = new GLchar[len+1];

    fread( source, 1, len, infile );
    fclose( infile );

    source[len] = 0;

    return const_cast<const GLchar*>(source);
}

//----------------------------------------------------------------------------

GLuint
LoadShaders( ShaderInfo* shaders )
{
    if ( shaders == NULL ) { return 0; }

    GLuint program = glCreateProgram();

    ShaderInfo* entry = shaders;
    while ( entry->type != GL_NONE ) {
        GLuint shader = glCreateShader( entry->type );

        entry->shader = shader;

        const GLchar* source = ReadShader( entry->filename );
        if ( source == NULL ) {
            for ( entry = shaders; entry->type != GL_NONE; ++entry ) {
                glDeleteShader( entry->shader );
                entry->shader = 0;
            }

            return 0;
        }

        glShaderSource( shader, 1, &source, NULL );
        delete [] source;

        glCompileShader( shader );

        GLint compiled;
        glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
        if ( !compiled ) {
#ifdef _DEBUG
            GLsizei len;
            glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &len );

            GLchar* log = new GLchar[len+1];
            glGetShaderInfoLog( shader, len, &len, log );
            std::cerr << "Shader compilation failed: " << log << std::endl;
            delete [] log;
#endif /* DEBUG */

            return 0;
        }

        glAttachShader( program, shader );
        
        ++entry;
    }

#ifdef GL_VERSION_4_1
    if ( GLEW_VERSION_4_1 ) {
        // glProgramParameteri( program, GL_PROGRAM_SEPARABLE, GL_TRUE );
    }
#endif /* GL_VERSION_4_1 */
    
    glLinkProgram( program );

    GLint linked;
    glGetProgramiv( program, GL_LINK_STATUS, &linked );
    if ( !linked ) {
#ifdef _DEBUG
        GLsizei len;
        glGetProgramiv( program, GL_INFO_LOG_LENGTH, &len );

        GLchar* log = new GLchar[len+1];
        glGetProgramInfoLog( program, len, &len, log );
        std::cerr << "Shader linking failed: " << log << std::endl;
        delete [] log;
#endif /* DEBUG */

        for ( entry = shaders; entry->type != GL_NONE; ++entry ) {
            glDeleteShader( entry->shader );
            entry->shader = 0;
        }
        
        return 0;
    }

    return program;
}

//----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif // __cplusplus


Tags: Programming

Posted on Fri, 20 Mar 2020 12:38:20 -0700 by SmokyBarnable