Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions nb15110谭锦志/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
##作业说明:
- 环境 windows ; VS2015
- 底层框架使用win32 API封装,支持OpenGL 4.3
- 框架详情 : https://github.com/apanoo/ApanooEngine
- GLWindow/ 提供窗口
- System/ 提供系统工具:系统时间等
- Tool/ 封装各类工具:ShaderLoader TextureLoader等

######作业1:地月系绘制
######作业1:地月系1
- 地球绕太阳转,月球绕地球转
- 启用灯光
- 截图保存路径 : \Bin\Debug\screenshot\
- 截图保存路径 : \Bin\Debug\screenshot\

######作业2:地月系2
- 在作业1基础上添加太阳中心光源
- 绘制太阳光晕
- 启用雾
- 绘制旋转星空
- 效果图:
![pic-2](ogl-framework/pic/pic2.gif)
Binary file modified nb15110谭锦志/ogl-framework/.vs/ogl-framewprk/v14/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 6 additions & 7 deletions nb15110谭锦志/ogl-framework/Src/GLWindow/GLWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ BOOL GLWindow::CreateGlWnd(const char* title, int x, int y, int width, int heigh
// ȫ������ - Ԥ��

// ������ʽ
windowStyle = WS_POPUP;
windowExStyle |= WS_EX_TOPMOST;
//windowStyle = WS_POPUP;
//windowExStyle |= WS_EX_TOPMOST;

::AdjustWindowRectEx(&windowRect, windowStyle, 0, windowExStyle); // ��������

Expand Down Expand Up @@ -128,9 +128,10 @@ BOOL GLWindow::CreateGlWnd(const char* title, int x, int y, int width, int heigh
{ // ��֧��opengl4.X ��ԭΪopengl 2.1
m_hRc = tempContext;
}
RECT rect; // �ͻ�����С
::GetClientRect(m_hWnd, &rect);
ResizeGLScene(rect.right - rect.left, rect.bottom - rect.top); // ����GL��Ļ (ע�⣬����ֻʹ�ÿͻ�������)
//RECT rect; // �ͻ�����С
//::GetClientRect(m_hWnd, &rect);
//ResizeGLScene(rect.right - rect.left, rect.bottom - rect.top); // ����GL��Ļ (ע�⣬����ֻʹ�ÿͻ�������)
ResizeGLScene(width, height);

if (!initGL()) // ��ʼ��opengl
{
Expand Down Expand Up @@ -269,5 +270,3 @@ HRESULT GLWindow::OnPaint(WPARAM wParam, LPARAM lParam)
::ValidateRect(m_hWnd, NULL); // ʹ������Ч
return TRUE;
}


31 changes: 31 additions & 0 deletions nb15110谭锦志/ogl-framework/Src/Tools/FileTool/FileUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef __FILEUTILS_H__
#define __FILEUTILS_H__

#include <iostream>
#include <fstream>

// ��ȡ�ı��ļ�
class FileUtils
{
public:
static std::string ReadFile(const char* filepath)
{
FILE * file = fopen(filepath, "rt");
fseek(file, 0, SEEK_END);
unsigned long length = ftell(file); // ���������

char* data = new char[length + 1]; // �����ڴ�

memset(data, 0, length + 1); // ����
fseek(file, 0, SEEK_SET); // �ص��ļ�ͷ
fread(data, 1, length, file); // ��ȡ�ļ�����

fclose(file);
std::string result(data);

delete[] data;
return result;
}
};

#endif // ifn
131 changes: 131 additions & 0 deletions nb15110谭锦志/ogl-framework/Src/Tools/ShaderLoader/shader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include <cstdlib>
#include <iostream>

#define GLEW_STATIC
#include "GL/glew.h"

#include "shader.h"
#include <vector>

//////////////////////////////////////////////////////////////////////////

Shader::~Shader()
{
glDeleteProgram(m_ShaderID);
}

Shader::Shader(const char* vertPath, const char* fragPath)
: m_VertPath(vertPath), m_FragPath(fragPath)
{
m_ShaderID = 0;
}

void Shader::enable()
{
if (0 == m_ShaderID)
{
init(); // ���Ľ���Ǩ�Ƶ����췽�����ִ���
}
glUseProgram(m_ShaderID);
}

void Shader::diable() const
{
glUseProgram(0);
}

///////////////////////////////set uniform///////////////////////////////////////////
void Shader::setUniform1f(const GLchar* name, float value)
{
glUniform1f(getUniformLocation(name), value);
}

void Shader::setUniform1i(const GLchar* name, int value)
{
glUniform1i(getUniformLocation(name), value);
}

void Shader::setUniform2f(const GLchar* name, const vec2& vector)
{
glUniform2f(getUniformLocation(name), vector.x, vector.y);
}

void Shader::setUniform3f(const GLchar* name, const vec3& vector)
{
glUniform3f(getUniformLocation(name), vector.x, vector.y, vector.z);
}

void Shader::setUniform4f(const GLchar* name, const vec4& vector)
{
glUniform4f(getUniformLocation(name), vector.x, vector.y, vector.z, vector.w);
}

void Shader::setUniformMat4(const GLchar* name, const mat4 &matrix)
{
glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, matrix.elements);
}

GLuint Shader::getUniformLocation(const GLchar* name)
{
return glGetUniformLocation(m_ShaderID, name);
}

//////////////////////////////////init////////////////////////////////////////
void Shader::init()
{
GLuint program = glCreateProgram();

GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);

std::string vertSourceString = FileUtils::ReadFile(m_VertPath);
std::string fragSourceString = FileUtils::ReadFile(m_FragPath);

const char* vertSource = vertSourceString.c_str();
const char* fragSource = fragSourceString.c_str();

glShaderSource(vertex, 1, &vertSource, NULL);
glCompileShader(vertex); // ����
GLint result;
glGetShaderiv(vertex, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE) // ������������Ϣ
{
GLint length;
glGetShaderiv(vertex, GL_INFO_LOG_LENGTH, &length);
std::vector<char> error(length);
glGetShaderInfoLog(vertex, length, &length, &error[0]);
// log("error compile vertex shader");
// log(&error[0]);
glDeleteShader(vertex);
return;
}

glShaderSource(fragment, 1, &fragSource, NULL);
glCompileShader(fragment); // ����
glGetShaderiv(fragment, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE) // ������������Ϣ
{
GLint length;
glGetShaderiv(fragment, GL_INFO_LOG_LENGTH, &length);
std::vector<char> error(length);
glGetShaderInfoLog(fragment, length, &length, &error[0]);
// log("error compile fragment shader");
// log(&error[0]);
glDeleteShader(fragment);
return;
}

// attach
glAttachShader(program, vertex);
glAttachShader(program, fragment);

// link
glLinkProgram(program);
glValidateProgram(program);

// delete
glDeleteShader(vertex);
glDeleteShader(fragment);

m_ShaderID = program;
}
36 changes: 36 additions & 0 deletions nb15110谭锦志/ogl-framework/Src/Tools/ShaderLoader/shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef __SHADER_H__
#define __SHADER_H__

#include "../FileTool/FileUtils.h"
#include "../maths/maths.h"

class Shader
{
public:
~Shader();

Shader(const char* vertPath, const char* fragPath);

void init(); // ���� shader
void enable(); // ���� shader
void diable() const; // ���� shader

///////////////////////////////////drt uniform///////////////////////////////////////

void setUniform1f(const GLchar* name, float value);
void setUniform1i(const GLchar* name, int value);
void setUniform2f(const GLchar* name, const vec2& vector);
void setUniform3f(const GLchar* name, const vec3& vector);
void setUniform4f(const GLchar* name, const vec4& vector);
void setUniformMat4(const GLchar* name, const mat4 &matrix);

private:
GLuint getUniformLocation(const GLchar* name);

private:
GLuint m_ShaderID;
const char* m_VertPath;
const char* m_FragPath;
};

#endif
10 changes: 10 additions & 0 deletions nb15110谭锦志/ogl-framework/Src/Tools/maths/MathUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#define _USE_MATH_DEFINES
#include <math.h>

// �Ƕ�ת����
inline float toRadians(float degrees)
{
return (float)(degrees * (M_PI / 180.0f));
}
Loading