master
sigonasr2 1 year ago
parent 49038b9a88
commit 836b249bc6
  1. 2
      Models/Models.vcxproj
  2. 6
      Models/Models.vcxproj.filters
  3. BIN
      Models/cupyay4.png
  4. 11
      Models/fragShader2.glsl
  5. 111
      Models/main.cpp
  6. 11
      Models/utils.h
  7. 16
      Models/vertShader.glsl
  8. 68
      Models/vertShader2.glsl
  9. 5
      TextureMapping/fragShader.glsl

@ -147,7 +147,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="fragShader.glsl" /> <None Include="fragShader.glsl" />
<None Include="fragShader2.glsl" />
<None Include="vertShader.glsl" /> <None Include="vertShader.glsl" />
<None Include="vertShader2.glsl" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="ShaderError.h" /> <ClInclude Include="ShaderError.h" />

@ -26,6 +26,12 @@
<None Include="vertShader.glsl"> <None Include="vertShader.glsl">
<Filter>Resource Files</Filter> <Filter>Resource Files</Filter>
</None> </None>
<None Include="vertShader2.glsl">
<Filter>Resource Files</Filter>
</None>
<None Include="fragShader2.glsl">
<Filter>Resource Files</Filter>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="ShaderError.h"> <ClInclude Include="ShaderError.h">

Binary file not shown.

After

Width:  |  Height:  |  Size: 253 KiB

@ -0,0 +1,11 @@
#version 430
layout (binding=0) uniform sampler2D samp;
out vec4 color;
in vec2 uv;
void main(void)
{
color=texture(samp,uv);
if(color.a<0.1){
discard;
}
}

@ -10,19 +10,26 @@
#include <stack> #include <stack>
#define numVAOs 1 #define numVAOs 1
#define numVBOs 2 #define numVBOs 3
GLuint renderingProgram; GLuint renderingProgram,expandingPlaneProgram;
GLuint vao[numVAOs]; GLuint vao[numVAOs];
GLuint vbo[numVBOs]; GLuint vbo[numVBOs];
glm::vec3 camera; glm::vec3 camera;
GLuint mvLoc,projLoc; GLuint vLoc,projLoc;
int width, height; int width, height;
float aspect; float aspect;
glm::mat4 mMat, pMat, vMat, mvMat; glm::mat4 mMat, pMat, vMat, mvMat;
std::stack<glm::mat4>transforms; std::stack<glm::mat4>transforms;
GLuint cupYayTex;
void setupTextures(void){
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,cupYayTex);
}
void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed at origin void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed at origin
float cubePositions[108] = { float cubePositions[108] = {
-1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
@ -46,6 +53,14 @@ void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed
-1.0f, -1.0f, - 1.0f, 1.0f, - 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, - 1.0f, 1.0f, - 1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f
}; };
float plane[6*3]={
-1,1,0,1,1,0,-1,-1,0,
-1,-1,0,1,1,0,1,-1,0
};
float planeUVs[6*2]={
0,1,1,1,0,0,
0,0,1,1,1,0
};
glGenVertexArrays(1, vao); glGenVertexArrays(1, vao);
glBindVertexArray(vao[0]); glBindVertexArray(vao[0]);
glGenBuffers(numVBOs, vbo); glGenBuffers(numVBOs, vbo);
@ -54,32 +69,43 @@ void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed
glBufferData(GL_ARRAY_BUFFER, sizeof(cubePositions), cubePositions, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(cubePositions), cubePositions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(pyramidPositions), pyramidPositions, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(plane), plane, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(planeUVs), planeUVs, GL_STATIC_DRAW);
} }
void init(GLFWwindow* window) { void init(GLFWwindow* window) {
renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl"); renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl");
camera={0.0f,0.0f,-12.0f}; expandingPlaneProgram=utils::createShaderProgram("vertShader2.glsl","fragShader2.glsl");
camera={0.0f,0.0f,-10.0f};
setupVertices(); setupVertices();
cupYayTex=utils::loadTexture("cupyay4.png");
setupTextures();
} }
double lastTime=0; double lastTime=0;
void DrawCube(){ void DrawCube(){
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mMat)); glUniformMatrix4fv(vLoc,1,GL_FALSE,glm::value_ptr(mMat));
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
glBindBuffer(GL_ARRAY_BUFFER,vbo[0]); glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glDrawArrays(GL_TRIANGLES,0,36); glDrawArrays(GL_TRIANGLES,0,36);
} }
void DrawSquarePyramid(){ void DrawSquarePyramid(){
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mMat)); glUniformMatrix4fv(vLoc,1,GL_FALSE,glm::value_ptr(mMat));
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]); glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glDrawArrays(GL_TRIANGLES,0,18); glDrawArrays(GL_TRIANGLES,0,18);
} }
GLuint timeLoc;
GLuint mvLoc;
float size=0;
void display(GLFWwindow* window, double currentTime) { void display(GLFWwindow* window, double currentTime) {
double elapsedTime=currentTime-lastTime; double elapsedTime=currentTime-lastTime;
lastTime=currentTime; lastTime=currentTime;
@ -92,9 +118,11 @@ void display(GLFWwindow* window, double currentTime) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix"); vLoc=glGetUniformLocation(renderingProgram,"v_matrix");
projLoc=glGetUniformLocation(renderingProgram,"proj_matrix"); projLoc=glGetUniformLocation(renderingProgram,"proj_matrix");
timeLoc=glGetUniformLocation(renderingProgram,"time");
glfwGetFramebufferSize(window,&width,&height); glfwGetFramebufferSize(window,&width,&height);
aspect=(float)width/(float)height; aspect=(float)width/(float)height;
@ -104,34 +132,45 @@ void display(GLFWwindow* window, double currentTime) {
transforms.push(vMat); transforms.push(vMat);
mMat=glm::translate(transforms.top(),{0.f,0.f,0.f}); mMat=glm::translate(transforms.top(),{0.f,0.f,0.f});
transforms.push(mMat); glUniformMatrix4fv(vLoc,1,GL_FALSE,glm::value_ptr(vMat));
mMat=glm::rotate(mMat,float(utils::degToRad(90)*currentTime),{0.f,1.f,0.f}); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
mMat=glm::rotate(mMat,float(utils::degToRad(12)),{0.f,0.f,1.f}); glUniform1f(timeLoc,currentTime);
DrawSquarePyramid(); glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);
mMat=glm::translate(transforms.top(),{0.f,0.f,0.f}); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
mMat=glm::rotate(mMat,float(utils::degToRad(90)*currentTime),{0.f,1.f,0.f}); glDrawArraysInstanced(GL_TRIANGLES,0,36,100000);
mMat=glm::rotate(mMat,float(utils::degToRad(192)),{0.f,0.f,1.f});
mMat=glm::translate(mMat,{0.f,2.f,0.f}); size+=currentTime/1000;
mMat=glm::rotate(mMat,float(utils::degToRad(270)),{0.f,1.f,0.f}); glUseProgram(expandingPlaneProgram);
DrawSquarePyramid(); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
mMat=glm::translate(transforms.top(),{sin(currentTime*0.5)*6,sin(currentTime*0.5)*0.8-1,cos(currentTime*0.5)*6}); glEnableVertexAttribArray(0);
transforms.push(mMat); glEnableVertexAttribArray(1);
mMat=glm::rotate(mMat,float(utils::degToRad(24)-28),{0,0,1}); glfwGetFramebufferSize(window,&width,&height);
mMat=glm::rotate(mMat,float(utils::degToRad(24)*currentTime+28),{0,1,0}); aspect=(float)width/(float)height;
DrawCube(); pMat=glm::perspective(utils::degToRad(60),aspect,0.1f,1000.f);
vMat=glm::translate(glm::mat4(1.0f),glm::vec3{camera.x,camera.y,camera.z});
mMat=glm::rotate(transforms.top(),utils::degToRad(90),{1,0,0});
mMat=glm::translate(mMat,{sin(currentTime*3)*3,0,cos(currentTime*3)*3}); mMat=glm::translate(glm::mat4{1.0},{0,0,0});
mMat=glm::scale(mMat,{0.3,0.3,0.3}); mMat=glm::scale(mMat,{size,size,size});
mMat=glm::rotate(mMat,float(-utils::degToRad(220)*currentTime+4),{0,1,0});
DrawCube(); mvMat=vMat*mMat;
mMat=glm::rotate(transforms.top(),utils::degToRad(90),{1,1,0}); mvLoc=glGetUniformLocation(expandingPlaneProgram,"mv_matrix");
mMat=glm::translate(mMat,{sin(currentTime*3)*3,0,cos(currentTime*3)*3}); projLoc=glGetUniformLocation(expandingPlaneProgram,"proj_matrix");
mMat=glm::scale(mMat,{0.3,0.3,0.3}); glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat));
mMat=glm::rotate(mMat,float(utils::degToRad(120)*currentTime),{0,1,0}); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
DrawCube(); glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[2]);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0);
glActiveTexture(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,cupYayTex);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDrawArrays(GL_TRIANGLES,0,6);
} }
int main(void) { int main(void) {

@ -3,6 +3,7 @@
#include <fstream> #include <fstream>
#include <GL/glew.h> #include <GL/glew.h>
#include "ShaderError.h" #include "ShaderError.h"
#include <SOIL2/SOIL2.h>
#define PI 3.14159 #define PI 3.14159
@ -80,4 +81,14 @@ public:
inline static float radToDeg(float rad){ inline static float radToDeg(float rad){
return rad*57.2957795130823208767; return rad*57.2957795130823208767;
} }
inline static GLuint loadTexture(const char*file){
GLuint textureID;
textureID=SOIL_load_OGL_texture(file,SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID,SOIL_FLAG_INVERT_Y);
if(!textureID){
std::cout<<"Could not load texture file "<<file<<std::endl;
throw;
}
return textureID;
}
}; };

@ -1,7 +1,8 @@
#version 430 #version 430
layout (location=0) in vec3 position; layout (location=0) in vec3 position;
uniform mat4 mv_matrix; uniform mat4 v_matrix;
uniform mat4 proj_matrix; uniform mat4 proj_matrix;
uniform float time;
out vec4 varyingColor; out vec4 varyingColor;
mat4 buildTranslate(float x, float y, float z); mat4 buildTranslate(float x, float y, float z);
@ -11,6 +12,19 @@ mat4 buildRotateZ(float rad);
void main(void) void main(void)
{ {
float i=time+gl_InstanceID;
float a = sin(203.0 * i/8000.0) * 403.0;
float b = cos(301.0 * i/4001.0) * 401.0;
float c = sin(400.0 * i/6003.0) * 405.0;
mat4 localRotX = buildRotateX(i);
mat4 localRotY = buildRotateY(i);
mat4 localRotZ = buildRotateZ(i);
mat4 localTrans = buildTranslate(a,b,c);
// build the model matrix and then the model-view matrix
mat4 newM_matrix = localTrans * localRotX * localRotY * localRotZ;
mat4 mv_matrix = v_matrix * newM_matrix;
gl_Position=proj_matrix*mv_matrix*vec4(position,1.0); gl_Position=proj_matrix*mv_matrix*vec4(position,1.0);
varyingColor=vec4(position,1.0)*0.5+vec4(0.5,0.5,0.5,0.5); varyingColor=vec4(position,1.0)*0.5+vec4(0.5,0.5,0.5,0.5);
} }

@ -0,0 +1,68 @@
#version 430
layout (location=0) in vec3 position;
layout (location=1) in vec2 texCoords;
uniform mat4 proj_matrix;
uniform mat4 mv_matrix;
out vec2 uv;
mat4 buildTranslate(float x, float y, float z);
mat4 buildRotateX(float rad);
mat4 buildRotateY(float rad);
mat4 buildRotateZ(float rad);
void main(void)
{
gl_Position=proj_matrix*mv_matrix*vec4(position,1.0);
uv=texCoords;
}
// builds and returns a translation matrix
mat4 buildTranslate(float x, float y, float z)
{
mat4 trans = mat4(1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
x, y, z, 1.0 );
return trans;
}
// builds and returns a matrix that performs a rotation around the X axis
mat4 buildRotateX(float rad)
{
mat4 xrot = mat4(1.0, 0.0, 0.0, 0.0,
0.0, cos(rad), -sin(rad), 0.0,
0.0, sin(rad), cos(rad), 0.0,
0.0, 0.0, 0.0, 1.0 );
return xrot;
}
// builds and returns a matrix that performs a rotation around the Y axis
mat4 buildRotateY(float rad)
{
mat4 yrot = mat4(cos(rad), 0.0, sin(rad), 0.0,
0.0, 1.0, 0.0, 0.0,
-sin(rad), 0.0, cos(rad), 0.0,
0.0, 0.0, 0.0, 1.0 );
return yrot;
}
// builds and returns a matrix that performs a rotation around the Z axis
mat4 buildRotateZ(float rad)
{
mat4 zrot = mat4(cos(rad), -sin(rad), 0.0, 0.0,
sin(rad), cos(rad), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 );
return zrot;
}
// builds and returns a scale matrix
mat4 buildScale(float x, float y, float z)
{
mat4 scale = mat4(x, 0.0, 0.0, 0.0,
0.0, y, 0.0, 0.0,
0.0, 0.0, z, 0.0,
0.0, 0.0, 0.0, 1.0 );
return scale;
}

@ -1,8 +1,7 @@
#version 430 #version 430
layout (binding=0) uniform sampler2D samp;
out vec4 color; out vec4 color;
in vec2 uv; in vec4 varyingColor;
void main(void) void main(void)
{ {
color=texture(samp,uv); color=varyingColor;
} }
Loading…
Cancel
Save