gpt4 book ai didi

c++ - OpenGL 光照不着色

转载 作者:行者123 更新时间:2023-11-30 01:49:04 27 4
gpt4 key购买 nike

我也在尝试为我的场景添加基本照明。到目前为止,即使没有 GLSL。看来我的照明不起作用。我的意思是一切都没有阴影(一切都亮了)。

enter image description here

我知道这张照片不是最好的,但部分原因是图像中的纹理有阴影。

这是我的初始化代码:

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);                            // Clear to black with full alpha
glEnable(GL_DEPTH_TEST); // Enable depth testing
glDepthFunc(GL_LEQUAL); // Specify depth testing function
glClearDepth(1.0); // Clear the full extent of the depth buffer (default)
glEnable(GL_CULL_FACE); // Enable face culling
glCullFace(GL_BACK); // Cull back faces of polygons
glFrontFace(GL_CCW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
glEnable ( GL_COLOR_MATERIAL );

GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 0.2f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position[] = {0, 100.0f, 0, 1.0f };

glLightModelfv(GL_AMBIENT, ambientLight);
glLightModelfv(GL_DIFFUSE, ambientLight);
glLightModelfv(GL_SPECULAR, specularLight);
glLightModelfv(GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);

绘制代码:

 glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);

//For loop of the lights...
glLightfv(GL_LIGHT0+m_lights.at(i)->id, GL_POSITION, glm_to_array(m_lights.at(i)->position));
glEnable(GL_LIGHT0+m_lights.at(i)->id);

//Draw geometry here...

是的,我正在从网格文件中检索法线并插入每个面的 glNormal3f。 (GL_TRIANGLES) 即使我将法线值完全更改为随机值,即使模型所在的平面也不会受到影响。

平面示例:

 if(m_shader_programme){
glUseProgram(m_shader_programme);
}

if(m_texture_id){
glBindTexture(GL_TEXTURE_2D, m_texture_id);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}


glBegin(GL_TRIANGLES);

glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, m_sizey/10);
glVertex3f(m_sizex, m_height, m_sizey);

glTexCoord2f(m_sizex/10, m_sizey/10);
glVertex3f(m_sizex, m_height, -m_sizey);

glTexCoord2f(m_sizex/10, 0.0f);
glVertex3f(-m_sizex, m_height, -m_sizey);

glNormal3f(0.0f, 1.0f, 0.0f);

glTexCoord2f(m_sizex/10, 0.0f);
glVertex3f(-m_sizex, m_height, -m_sizey);

glTexCoord2f(0.0f, 0.0f);
glVertex3f(-m_sizex, m_height, m_sizey);

glTexCoord2f(0.0f, m_sizey/10);
glVertex3f(m_sizex, m_height, m_sizey);
glEnd();

if(m_shader_programme){
glUseProgram(0);
}

if(m_texture_id){
glBindTexture(GL_TEXTURE_2D, 0);
}

GLSL 片段着色器:

#version 120

uniform sampler2D tex;

void main()
{
vec4 color = texture2D(tex,gl_TexCoord[0].st);
gl_FragColor = color;
}

顶点着色器:

#version 120

void main() {

vec3 normal, lightDir;
vec4 diffuse, ambient, globalAmbient;
float NdotL;


gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
normal = normalize(gl_NormalMatrix * gl_Normal);
lightDir = normalize(vec3(gl_LightSource[0].position));
NdotL = max(dot(normal, lightDir), 0.0);
diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
/* Compute the ambient and globalAmbient terms */

ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;
gl_FrontColor = NdotL * diffuse + globalAmbient + ambient;


gl_Position = ftransform();
}

系统规范

  • OS X(优胜美地)10.10.13(64 位)
  • 渲染器:NVIDIA GeForce GTX 780M
  • OpenGL 引擎 OpenGL 版本支持 2.1 NVIDIA-10.2.7 310.41.25f01

只要有人想知道为什么我不使用 OpenGL 3.x 是因为 GL 3.x 在我的机器上似乎运行不佳。

最佳答案

你的片段着色器:

vec4 color = texture2D(tex,gl_TexCoord[0].st);
gl_FragColor = color;

简单地丢弃您在顶点着色器中所做的所有计算并对纹理颜色进行采样。您需要将纹理样本乘以插值颜色值:

 vec4 color = texture2D(tex, gl_TexCoord[0].st) * gl_Color;
^~~~~~
gl_FragColor = color;

关于c++ - OpenGL 光照不着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29616739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com