OpenGL Programming Guide (Red Book eighth edition) sample code configuration issues Summary

Source: Internet
Author: User

Code download Environment configuration issues glut and Glew1 Basic header file and library file configuration 2 The following is the body content the first renderer Triangles3 Chapter three 03CH03_DRAWCOMMANDS4

This article is the author himself in the configuration of the problem and solve the original data and their own experience combined with the code to download

can search for environment configuration issues on CSDN (glut and Glew) 1

Refer to my other blog post, "process for compiling and configuring Freeglut and Glew source code using CMake and Visual Studio," or refer to the problem solution below.

Provide csdn download link (to 1 points: P)
http://download.csdn.net/detail/iceteaset/9740023
http://download.csdn.net/detail/iceteaset/9740024 Basic header file and library file configuration 2

If you find large tracts of red lines, you need to add include and Lib:

Click on the Item Right button property, tap VC + + Directory

Include directory – Edit, add an include folder in the Red Book source directory

Library Directory – Edit, add the Lib folder in the Red Book source directory






The following is the body content





First Render program Triangles3

First, create a new Win32 Console application

The complete code after the modification is as follows:

//triangles.cpp///////////////////////

#include <iostream> using namespace std;
#include "vgl.h" #include "LoadShaders.h" enum Vao_ids {triangles, numvaos};
Enum Buffer_ids {ArrayBuffer, numbuffers};
Enum Attrib_ids {vposition = 0};
Gluint Vaos[numvaos];
Gluint Buffers[numbuffers];
Const Gluint numvertices = 6; ---------------------------------------------------------------------////init//void init (void) {Glgenvertexarr
    Ays (Numvaos, Vaos);
    Glbindvertexarray (Vaos[triangles]); Glfloat Vertices[numvertices][2] = {{ -0.90, -0.90},//Triangle 1 {0.85,-0.90}, {-0.90, 0.8
    5}, {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); 
        Shaderinfo shaders[] = {{Gl_vertex_shader, "Triangles.vert"}, {gl_fragment_shader, "Triangles.frag"},
    {Gl_none, NULL}};
    Gluint program = loadshaders (shaders);
    Gluseprogram (program);
    Glvertexattribpointer (Vposition, 2, Gl_float, Gl_false, 0, Buffer_offset (0));
Glenablevertexattribarray (vposition); }//---------------------------------------------------------------------////Display//void display (void) {Glclea
    R (Gl_color_buffer_bit);
    Glbindvertexarray (Vaos[triangles]);
    Gldrawarrays (gl_triangles, 0, numvertices);
Glflush (); 
    }//---------------------------------------------------------------------////main//int main (int argc, char** argv) {
    Glutinit (&AMP;ARGC, argv);
    Glutinitdisplaymode (Glut_rgba);
    Glutinitwindowsize (512, 512);
    Glutinitcontextversion (4, 3);
    Glutinitcontextprofile (Glut_core_profile);

    Glutcreatewindow (Argv[0]); GlEwexperimental = Gl_true;
        if (Glewinit ()) {Cerr << "Unable to initialize Glew ... exiting" << Endl;
    Exit (Exit_failure);
    } init ();
    Glutdisplayfunc (display);
Glutmainloop ();
 }

Second, frequently asked questions

(1) Error about "stdafx.h" error

Code header Delete #include "stdafx.h"

(2) Error "unresolved external command" associated with Loadshaders

This could be because LoadShaders.cpp can't be found.
In the Red Book source code directory, there is a Lib folder, there are LoadShaders.cpp
Right-click on the project's source file to add the existing item and add it in

(3) Error LIBCMTD.lib Library has conflicts with other libraries:
Click the Project right-property – linker – Enter to add it in the Ignore specific default library

(4) Error fopen Unsafe:
Change to a secure fopen_s or add _crt_secure_no_warnings to the precompiled macro as follows:

(5) Error LNK2019 an unresolved external symbol _sscanf, which is referenced in the function _glutinit

This is because Freeglut is not configured successfully, reconfigured, or manually added as follows:

Put Freeglut and Glew's static library (note that the static library or error) in the previously defined C + + library directory, and then add the two libraries in the linker (do not directly copy the image, according to the name of your static library to write. )

(6)
The compilation passed, but Vaos an access violation occurred:

The reason is that glewinit () does not complete all initialization, so in the front row of Glewinit () add:

Glewexperimental = Gl_true;

has been added in the above code.

Glewexperimental is quite a general switch, and if it is set to gl_true it will allow Glew to support all the extensions, and Glewinit () will be able to do all the initialization successfully.

(7)
The compilation passed, but it is the white triangle is not blue (or the error can not find the xxx file)

Need to create a new two text in the same directory of the project named Triangles.vert and Triangles.frag

The code is as follows (note that the first line cannot be saved, and if there are errors you can try hand rather than copy):

Triangles.vert

#version 430 Core
layout (location = 0) in Vec4 vposition;  
void Main ()  
{  
  gl_position = vposition;  
}  

Triangles.frag

#version 430 core out
vec4 Fcolor;  
void Main ()  
{  
  Fcolor = vec4 (0.0, 0.0, 1.0, 1.0);  
}  

And then compile and run, and a blue triangle appears.





Chapter III 03\CH03_DRAWCOMMANDS4

(1) The complete code after the modification:

/* $URL $ $REV $ $Author $ $Date $ $Id $ */#include "vapp.h" #include "vutils.h" #include "vmath.h" #include "L

OadShaders.h "#include <stdio.h> using namespace Vmath; Define Use_primitive_restart to 0 to use separate draw commands #define Use_primitive_restart 1 Begin_app_declarat
    ION (drawcommandexample)//Override functions from base class virtual void Initialize (const char * title);
    virtual void Display (bool auto_redraw);
    virtual void Finalize (void);

    virtual void reshape (int width, int height);
    Member variables float aspect;
    Gluint Render_prog;
    Gluint Vao[1];
    Gluint Vbo[1];

    Gluint Ebo[1];
    Glint Render_model_matrix_loc;
Glint Render_projection_matrix_loc; End_app_declaration () Define_app (Drawcommandexample, "Drawing Commands Example") void Drawcommandexample::initialize
(const char * title)

    {base::initialize (title); Static Shaderinfo shader_info[] = {{Gl_vertex_shader, "... /../03/CH03_PRIMITIVE_RESTART/PRIMITIVE_RESTART.VS.GLSL "}, {Gl_fragment_shader," ... /..

    /03/CH03_PRIMITIVE_RESTART/PRIMITIVE_RESTART.FS.GLSL "}, {Gl_none, NULL}};

    Render_prog = Loadshaders (Shader_info);

    Gluseprogram (Render_prog); "Model_matrix" is actually an array of 4 matrices Render_model_matrix_loc = Glgetuniformlocation (Render_prog, "mode
    L_matrix ");

    Render_projection_matrix_loc = Glgetuniformlocation (Render_prog, "Projection_matrix");  A single triangle static const glfloat vertex_positions[] = { -1.0f, -1.0f, 0.0f, 1.0f, 1.0f,

    -1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 0.0f, 1.0f,}; Color for each vertex static const glfloat vertex_colors[] = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.

    0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f}; Indices for the triangle strips static const glushort vertex_indices[] = {0, 1, 2};
    Set up the element array buffer glgenbuffers (1, Ebo);
    Glbindbuffer (Gl_element_array_buffer, ebo[0]);

    Glbufferdata (Gl_element_array_buffer, sizeof (vertex_indices), vertex_indices, Gl_static_draw);
    Set up the vertex attributes Glgenvertexarrays (1, Vao);

    Glbindvertexarray (Vao[0]);
    Glgenbuffers (1, VBO);
    Glbindbuffer (Gl_array_buffer, vbo[0]);
    Glbufferdata (Gl_array_buffer, sizeof (vertex_positions) + sizeof (vertex_colors), NULL, Gl_static_draw);
    Glbuffersubdata (gl_array_buffer, 0, sizeof (vertex_positions), vertex_positions);

    Glbuffersubdata (Gl_array_buffer, sizeof (vertex_positions), sizeof (vertex_colors), vertex_colors);
    Glvertexattribpointer (0, 4, gl_float, Gl_false, 0, NULL);
    Glvertexattribpointer (1, 4, gl_float, Gl_false, 0, (const glvoid *) sizeof (vertex_positions));
    Glenablevertexattribarray (0);

    Glenablevertexattribarray (1);
Glclearcolor (0.0f, 0.0f, 0.0f, 1.0f); } void Drawcommandexample::D isplay (bool auto_redraw) {Float T = float (GetTickCount () & 0x1fff)/float (0X1FFF);
    static float q = 0.0f;
    static const VMATH::VEC3 X (1.0f, 0.0f, 0.0f);
    static const VMATH::VEC3 Y (0.0f, 1.0f, 0.0f);

    static const VMATH::VEC3 Z (0.0f, 0.0f, 1.0f);

    VMATH::MAT4 Model_matrix;
    Setup glenable (gl_cull_face);

    Gldisable (gl_depth_test); Glclear (Gl_color_buffer_bit |

    Gl_depth_buffer_bit);

    Activate Simple Shading Program Gluseprogram (Render_prog); Set up the model and projection matrix VMATH::MAT4 Projection_matrix (Vmath::frustum ( -1.0f, 1.0f,-aspect, aspect, 1
    .0f, 500.0f));

    GLUNIFORMMATRIX4FV (Render_projection_matrix_loc, 1, Gl_false, Projection_matrix);
    Set up for a gldrawelements call Glbindvertexarray (Vao[0]);

    Glbindbuffer (Gl_element_array_buffer, ebo[0]);
    Draw Arrays ... model_matrix = vmath::translate ( -3.0f, 0.0f, -5.0f); GLUNIFORMMATRIX4FV (Render_model_matrix_loc, 1, Gl_false, Model_matrix);

    Gldrawarrays (gl_triangles, 0, 3);
    Drawelements Model_matrix = vmath::translate ( -1.0f, 0.0f, -5.0f);
    GLUNIFORMMATRIX4FV (Render_model_matrix_loc, 1, Gl_false, Model_matrix);

    Gldrawelements (Gl_triangles, 3, Gl_unsigned_short, NULL);
    Drawelementsbasevertex Model_matrix = vmath::translate (1.0f, 0.0f, -5.0f);
    GLUNIFORMMATRIX4FV (Render_model_matrix_loc, 1, Gl_false, Model_matrix);

    Gldrawelementsbasevertex (Gl_triangles, 3, Gl_unsigned_short, NULL, 1);
    drawarraysinstanced Model_matrix = vmath::translate (3.0f, 0.0f, -5.0f);
    GLUNIFORMMATRIX4FV (Render_model_matrix_loc, 1, Gl_false, Model_matrix);

    gldrawarraysinstanced (gl_triangles, 0, 3, 1);
Base::D isplay ();
    } void Drawcommandexample::finalize (void) {Gluseprogram (0);
    Gldeleteprogram (Render_prog);
    Gldeletevertexarrays (1, Vao);
Gldeletebuffers (1, VBO); } void Drawcommandexample::reshape (int width, int height) {glviewport (0, 0, WIdth, height);
aspect = float (height)/float (width);
 }


FAQ:

Line 46:static const Shaderinfo shader_info[] = changed to static shaderinfo shader_info[] =
Line 116:MAT4 Model_matrix; Change into VMATH::MAT4 Model_matrix;
Line 136/141/146/151:translation converted to translate
Line 137/142/147/152: Change the parameter 4 to 1, i.e.
GLUNIFORMMATRIX4FV (Render_model_matrix_loc, 4, Gl_false, Model_matrix); Changed to GLUNIFORMMATRIX4FV (Render_model_matrix_loc, 1, Gl_false, Model_matrix);

The results are as follows:



The following is the original address or reference address: Basic header file and library file configuration:
Http://blog.csdn.net/iceteaset/article/details/50359559↩ Basic Configuration:
Http://blog.csdn.net/qq821869798/article/details/45247241↩ the first render program:
http://blog.csdn.net/qq821869798/article/details/45247241
Http://www.cnblogs.com/caster99/p/4752354.html↩ Chapter III Ch03_drawcommands:
Http://blog.csdn.net/fanbird2008/article/details/17538243↩

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.