其他分享
首页 > 其他分享> > openGL 调用glewInit()失败

openGL 调用glewInit()失败

作者:互联网

openGL系列文章目录

`

文章目录

前言

OpenGL Extension Wrangler Library (GLEW) 是一个跨平台的开源 C/C++ 扩展加载库。GLEW 提供了高效的运行时机制,用于确定目标平台上支持哪些 OpenGL 扩展。OpenGL 核心和扩展功能在单个头文件中公开。GLEW已经在各种操作系统上进行了测试,包括Windows,Linux,Mac OS X,FreeBSD,Irix和Solaris。

在这里插入图片描述

一、glew官网

glew官网地址
在这里插入图片描述

二、glew库初始化调用失败

1.引入库

#include "glew/glew.h"
#include "glfw/glfw3.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "camera.h"
#include "Utils.h"
#include "Sphere.h"
#include "SOIL2/SOIL2.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

static const int screenWidth = 1920;
static const int screenHeight = 1080;

static const float pai = 3.1415926f;
float toRadians(float degrees) { return degrees * 2.f * pai / (float)360.f; }

static const int numVAOs = 1;
static const int numVBOs = 4;

float cameraX = 0, cameraY = 0, cameraZ = 0;
float sphereX = 0, sphereY = 0, sphereZ = 0;
float lightLocX = 0.f, lightLocY = 0.f, lightLocZ = 0.f;	//全局光照光源位置

GLuint renderingProgram = 1;
GLuint vao[numVAOs] = { 0 };
GLuint vbo[numVBOs] = { 0 };

// variable allocation for display
GLuint mvLoc = 0, projLoc = 0, nLoc = 0;
//phong 光照
GLuint globalAmbLoc = 0, ambLoc = 0, diffLoc = 0, specLoc = 0, posLoc = 0, mAmbLoc = 0, mDiffLoc = 0, mSpecLoc = 0, mShinLoc = 0;
int width = 0, height = 0;
float aspect = 0.f;
//MVP 矩阵
glm::mat4 mMat(1.f), vMat(1.f), pMat(1.f), mvMat(1.f), invTrMat(1.f);
glm::vec3 currentLightPos(0.f, 0.f, 0.f);

float lightPos[3] = { 0.f };
float rotAmt = -2.5f;

Sphere mySphere(48);
int numSphereVertices = 0;
int numSphereIndices = 0;

GLuint moonNormalMap = 0;
GLuint moonTexture = 0;

// white light
float globalAmbient[4] = { 0.1f, 0.1f, 0.1f, 1.f };
float lightAmbient[4] = { 0.f, 0.f, 0.f, 1.f };
float lightDiffuse[4] = { 1.f, 1.f, 1.f, 1.f };
float lightSpecular[4] = { 1.f, 1.f, 1.f, 1.f };

// silver material
float* matAmb = Utils::silverAmbient();
float* matDiff = Utils::silverDiffuse();
float* matSpec = Utils::silverSpecular();
float matShin = Utils::silverShininess();

void setupVertices(void)
{
	numSphereIndices = mySphere.getNumIndices();

	vector<int> ind = mySphere.getIndices();

	vector<glm::vec3> vert = mySphere.getVertices();
	vector<glm::vec2> text = mySphere.getTexCoords();
	vector<glm::vec3> norm = mySphere.getNormals();
	vector<glm::vec3> tang = mySphere.getTangents();

	vector<float> pValues;
	vector<float> tValues;
	vector<float> nValues;
	vector<float> tanValues;

	for (int i = 0; i < numSphereIndices; i++)
	{
		pValues.push_back(vert[ind[i]].x);  //gl_element_array_buffer,顶点对应成索引
		pValues.push_back(vert[ind[i]].y);
		pValues.push_back(vert[ind[i]].z);

		tValues.push_back(text[ind[i]].s);
		tValues.push_back(text[ind[i]].t);

		nValues.push_back(norm[ind[i]].x);
		nValues.push_back(norm[ind[i]].y);
		nValues.push_back(norm[ind[i]].z);

		tanValues.push_back(tang[ind[i]].x);
		tanValues.push_back(tang[ind[i]].y);
		tanValues.push_back(tang[ind[i]].z);
	}

	glGenVertexArrays(numVAOs, vao);
	glBindVertexArray(vao[0]);

	glGenBuffers(numVBOs, vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
	glBufferData(GL_ARRAY_BUFFER, pValues.size() * sizeof(float), &pValues[0], GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
	glBufferData(GL_ARRAY_BUFFER, tValues.size() * sizeof(float), &tValues[0], GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
	glBufferData(GL_ARRAY_BUFFER, nValues.size() * sizeof(float), &nValues[0], GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, vbo[3]);
	glBufferData(GL_ARRAY_BUFFER, tanValues.size() * sizeof(float), &tanValues[0], GL_STATIC_DRAW);
}

void initallLights(glm::mat4 vMatrix)
{
	glm::vec3 transformed = glm::vec3(vMatrix * glm::vec4(currentLightPos, 1.f));
	lightPos[0] = transformed.x;
	lightPos[1] = transformed.y;
	lightPos[2] = transformed.z;

	// get the locations of the light and material fields in the shader
	globalAmbLoc = glGetUniformLocation(renderingProgram, "globalAmbient");
	ambLoc = glGetUniformLocation(renderingProgram, "light.ambient");
	diffLoc = glGetUniformLocation(renderingProgram, "light.diffuse");
	specLoc = glGetUniformLocation(renderingProgram, "light.specular");
	posLoc = glGetUniformLocation(renderingProgram, "light.position");
	mAmbLoc = glGetUniformLocation(renderingProgram, "material.ambient");
	mDiffLoc = glGetUniformLocation(renderingProgram, "material.diffuse");
	mSpecLoc = glGetUniformLocation(renderingProgram, "material.specular");
	mShinLoc = glGetUniformLocation(renderingProgram, "material.shininess");

	//  set the uniform light and material values in the shader
	glProgramUniform4fv(renderingProgram, globalAmbLoc, 1, globalAmbient);
	glProgramUniform4fv(renderingProgram, ambLoc, 1, lightAmbient);
	glProgramUniform4fv(renderingProgram, diffLoc, 1, lightDiffuse);
	glProgramUniform4fv(renderingProgram, specLoc, 1, lightSpecular);
	glProgramUniform3fv(renderingProgram, posLoc, 1, lightPos);
	glProgramUniform4fv(renderingProgram, mAmbLoc, 1, matAmb);
	glProgramUniform4fv(renderingProgram, mDiffLoc, 1, matDiff);
	glProgramUniform4fv(renderingProgram, mSpecLoc, 1, matSpec);
	glProgramUniform1f(renderingProgram, mShinLoc, matShin);
}

void init(GLFWwindow* window)
{
	renderingProgram = Utils::createShaderProgram("vertShader.vert", "fragShader.frag");

	cameraX = 0.f, cameraY = 0.f, cameraZ = 2.f;
	sphereX = 0.f, sphereY = 0.f, sphereZ = -1.f;
	lightLocX = 3.f, lightLocY = 2.f, lightLocZ = 3.f;

	glfwGetFramebufferSize(window, &width, &height);
	aspect = (float)width / (float)height;
	pMat = glm::perspective(toRadians(45.f), aspect, 0.1f, 1000.f);

	setupVertices();

	moonTexture = Utils::loadTexture("moon.jpg");
	moonNormalMap = Utils::loadTexture("moonNORMAL.jpg");
}

void display(GLFWwindow* window, double currentTime)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor(0.f, 0.2f, 0.8f, 1.f);

	glUseProgram(renderingProgram);

	mvLoc = glGetUniformLocation(renderingProgram, "mv_matrix");
	projLoc = glGetUniformLocation(renderingProgram, "proj_matrix");
	nLoc = glGetUniformLocation(renderingProgram, "norm_matrix");

	vMat = glm::translate(glm::mat4(1.f), glm::vec3(-cameraX, -cameraY, -cameraZ));  //注意相机的Z方向,否则看不到物体
	mMat = glm::translate(glm::mat4(1.f), glm::vec3(sphereX, sphereY, sphereZ));
	mMat = glm::rotate(mMat, toRadians(45.f), glm::vec3(1.f, 0.f, 0.f));
	mMat = glm::rotate(mMat, rotAmt, glm::vec3(0.f, 1.f, 0.f));
	rotAmt += 0.002f;
	mvMat = vMat * mMat;

	invTrMat = glm::transpose(glm::inverse(mvMat));

	currentLightPos = glm::vec3(lightLocX, lightLocY, lightLocZ);
	initallLights(vMat);

	glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mvMat));
	glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(pMat));
	glUniformMatrix4fv(nLoc, 1, GL_FALSE, glm::value_ptr(invTrMat));

	glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(0);

	glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(1);

	glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
	glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(2);

	glBindBuffer(GL_ARRAY_BUFFER, vbo[3]);
	glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(3);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, moonTexture);

	glEnable(GL_CULL_FACE);
	glFrontFace(GL_CCW);

	glDrawArrays(GL_TRIANGLES, 0, numSphereVertices);
}

void window_size_callback(GLFWwindow* window, int newWidth, int newHeight)
{
	glfwGetFramebufferSize(window, &width, &height);
	glViewport(0, 0, width, height);
	aspect = (float)width / (float)height;
	pMat = glm::perspective(toRadians(45.f), aspect, 0.1f, 1000.f);
}

int main(int argc, char** argv)
{
	int glfwState = glfwInit();
	if (glfwState == GLFW_FALSE)
	{
		cout << "Initialize GLFW failed,invoke glfwInit()......Error file:" << __FILE__ << "......Error line:" << __LINE__ << endl;
		glfwTerminate();
		exit(EXIT_FAILURE);
	}

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
	glfwWindowHint(GLFW_OPENGL_CORE_PROFILE, GLFW_OPENGL_PROFILE);
	glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

	GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "Texture plus normal", nullptr, nullptr);
	if (!window)
	{
		cout << "GLFW create window failed,invoke glfwCreateWindow()......Error file:" << __FILE__ << "......Error line:" << __LINE__ << endl;
		glfwTerminate();
		exit(EXIT_FAILURE);
	}

	glfwMakeContextCurrent(window);

	glfwSetWindowSizeCallback(window, window_size_callback);

	int glewState = glewInit();
	if (GLEW_OK != glewState)
	{
		cout << "GLEW initialize failed, invoke glewInit()......Error file:" << __FILE__ << "......Error line:" << __LINE__ << endl;
		glfwTerminate();
		exit(EXIT_FAILURE);
	}

	glfwSwapInterval(1);

	init(window);

	while (!glfwWindowShouldClose(window))
	{
		display(window, glfwGetTime());
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	glfwDestroyWindow(window);
	glfwTerminate();
	exit(EXIT_SUCCESS);

	return 0;
}

2.glew调用失败原因

确实glfw上下文:
glfwMakeContextCurrent(window);
在这里插入图片描述

着色器

1.顶点着色器

#version 460 core

layout(location = 0) in vec3 vertPos;
layout(location = 1) in vec2 texCoords;
layout(location = 2) in vec3 vertNormal;
layout(location = 3) in vec3 vertTangent;

out vec3 varyingLightDir;
out vec3 varyingLightPos;
out vec3 varyingVertPos;
out vec3 varyingNormal;
out vec3 varyingTangent;
out vec3 originalVertex;
out vec2 tc;

layout(binding = 0) uniform sampler2D s;
layout(binding = 1) uniform sampler2D t;

struct PositionalLight
{
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	vec3 position;
};

struct Material
{
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	float shininess;
};

uniform vec4 globalAmbient;
uniform PositionalLight light;
uniform Material material;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
uniform mat4 norm_matrix;

void main(void)
{
	varyingVertPos = vec3(mv_matrix * vec4(vertPos, 1.f)).xyz;
	varyingLightDir = light.position - varyingVertPos;
	tc = texCoords;

	originalVertex = vertPos;

	varyingNormal = vec3(norm_matrix * vec4(vertNormal, 1.f)).xyz;
	varyingTangent = vec3(norm_matrix * vec4(vertTangent, 1.f)).xyz;

	gl_Position = proj_matrix * mv_matrix * vec4(vertPos, 1.f);
}

2.片元着色器

#version 460 core

in vec3 varyingLightDir;
in vec3 varyingVertPos;
in vec3 varyingNormal;
in vec3 varyingTangent;
in vec3 originalVertex;
in vec2 tc;

out vec4 fragColor;

layout(binding = 0) uniform sampler2D s;
layout(binding = 1) uniform sampler2D t;

struct PositionalLight
{
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	vec3 position;
};

struct Material
{
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	float shininess;
};

uniform vec4 globalAmbient;
uniform PositionalLight light;
uniform Material material;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
uniform mat4 norm_matrix;

vec3 calcNewNormal()
{
	vec3 normal = normalize(varyingNormal);
	vec3 tangent = normalize(varyingTangent);
	tangent = normalize(tangent - dot(tangent, normal) * normal);
	vec3 bitangent = cross(tangent, normal);
	mat3 tbn = mat3(tangent, bitangent, normal);
	vec3 retrivedNormal = texture(s, tc).xyz;
	retrivedNormal = retrivedNormal * 2.f - 1.f;
	vec3 newNormal = tbn * retrivedNormal;
	newNormal = normalize(newNormal);
	
	return newNormal;
}

void main(void)
{
	// normalize the light, normal, and view vectors:
	vec3 L = normalize(varyingLightDir);
	vec3 V = normalize(-varyingVertPos);
	vec3 N = calcNewNormal();

	// get the angle between the light and surface normal:
	float cosTheta = dot(L, N);

	// compute light reflection vector, with respect N:
	vec3 R = normalize(reflect(-L, N));   //以表面顶点为基准,入射光和反射光方向一致

	// angle between the view vector and reflected light:
	float cosPhi = dot(V, R);

	vec4 texC = texture(t, tc);

	// compute ADS contributions with surface texture image:
	fragColor = globalAmbient + light.ambient * texC																
				+ light.diffuse * texC * max(cosTheta, 0.f)
				+ light.specular *texC * pow(max(cosPhi, 0.f), material.shininess);
	
}

运行结果

在这里插入图片描述

标签:glewInit,glm,openGL,float,renderingProgram,vec4,调用,vec3,GL
来源: https://blog.csdn.net/web15185420056/article/details/123628110