Permalink
Browse files

Merge branch 'master' of github.com:jchilln92/CS123-Final-Project

  • Loading branch information...
2 parents e0b5676 + 6112bd9 commit da7d3e12bc6814dde9001abb021dc9be415b0718 @ejcaruso ejcaruso committed Dec 7, 2011
Showing with 52 additions and 32 deletions.
  1. +14 −3 geom/Planet.cpp
  2. +8 −0 geom/Planet.h
  3. +9 −0 geom/Shape.cpp
  4. +3 −1 geom/Shape.h
  5. +6 −9 geom/Sphere.cpp
  6. +1 −1 geom/Sphere.h
  7. +6 −14 lab/glwidget.cpp
  8. +5 −3 shaders/terrain.frag
  9. +0 −1 support/resourceloader.cpp
View
@@ -1,5 +1,6 @@
#include "Planet.h"
#include "Sphere.h"
+#include "support/resourceloader.h"
#include <qgl.h>
#define PARAM_VERY_LOW 15
@@ -45,7 +46,19 @@ Planet::Planet(Vector3 center, float radius) {
m_center = center;
}
-Planet::~Planet() {}
+Planet::~Planet() {
+}
+
+void Planet::setTexture(const char *filename, int i) {
+ if (i < MAX_PLANET_TEXTURES) {
+ glDeleteTextures(1, &textures[i]);
+ textures[i] = ResourceLoader::loadTextureImage(filename);
+ }
+}
+
+GLuint Planet::getTexture(int i) {
+ return textures[i];
+}
void Planet::setDetail(MeshDetail detail) {
m_renderDetail = detail;
@@ -54,8 +67,6 @@ void Planet::setDetail(MeshDetail detail) {
void Planet::render() {
glPushMatrix();
glTranslatef(m_center.x, m_center.y, m_center.z);
- // float scale = m_radius / .5;
- // glScalef(scale, scale, scale);
switch (m_renderDetail) {
case VERY_LOW:
View
@@ -3,8 +3,11 @@
#include "math/vector.h"
#include "MeshDetail.h"
+#include <qgl.h>
class Sphere;
+#define MAX_PLANET_TEXTURES 4
+
class Planet {
public:
static void initStaticResources();
@@ -16,13 +19,18 @@ class Planet {
void setDetail(MeshDetail detail);
void render();
+ void setTexture(const char *filename, int i);
+ GLuint getTexture(int i);
+
Vector3 getCenter() { return m_center; }
float getRadius() { return m_radius; }
private:
Vector3 m_center;
float m_radius;
MeshDetail m_renderDetail;
+ GLuint textures[MAX_PLANET_TEXTURES];
+
// sphere meshes at different detail levels
// these are used as the basis for every planet
static bool staticInitialized;
View
@@ -9,12 +9,14 @@ Shape::Shape(int param1, int param2, float param3) {
m_vertices = NULL;
m_vertexNormals = NULL;
m_triangles = NULL;
+ m_textureCoordinates = NULL;
}
Shape::~Shape() {
delete[] m_vertices;
delete[] m_vertexNormals;
delete[] m_triangles;
+ delete[] m_textureCoordinates;
}
/* Parameters */
@@ -66,17 +68,24 @@ void Shape::initializeContainers() {
delete[] m_triangles;
delete[] m_vertices;
delete[] m_vertexNormals;
+ delete[] m_textureCoordinates;
m_triangles = new int[3 * getTriangleCount()];
m_vertices = new float[4 * getVertexCount()];
m_vertexNormals = new float[4 * getVertexCount()];
+ m_textureCoordinates = new float[2 * getVertexCount()];
}
void Shape::setVertex(int v_idx, float x, float y, float z) {
m_vertices[v_idx * 4 + 0] = x;
m_vertices[v_idx * 4 + 1] = y;
m_vertices[v_idx * 4 + 2] = z;
m_vertices[v_idx * 4 + 3] = 1;
+
+ float u, v;
+ computeTextureCoordinates(&m_vertices[v_idx * 4], &u, &v);
+ m_textureCoordinates[v_idx * 2 + 0] = u;
+ m_textureCoordinates[v_idx * 2 + 1] = v;
}
void Shape::setVertexNormal(int v_idx, float x, float y, float z) {
View
@@ -33,10 +33,12 @@ class Shape {
// geometry information
float *m_vertices; // 1d array, 4 entries per vertex
float *m_vertexNormals; // 1d array, 4 entries per normal, indices match m_vertices
- int *m_triangles; // 3 entries per triangle, indices into m_points
+ float *m_textureCoordinates; // 1d array, 2 entries per tex coord, indices match m_vertices
+ int *m_triangles; // 3 entries per triangle, indices into m_vertices
virtual int getTriangleCount() = 0;
virtual int getVertexCount() = 0;
+ virtual void computeTextureCoordinates(float *pos, float *u, float *v) = 0;
void initializeContainers();
void setVertex(int v_idx, float x, float y, float z);
View
@@ -29,22 +29,19 @@ void Sphere::render() {
float *n1 = &m_vertexNormals[triangle[0] * 4];
float *n2 = &m_vertexNormals[triangle[1] * 4];
float *n3 = &m_vertexNormals[triangle[2] * 4];
+ float *t1 = &m_textureCoordinates[triangle[0] * 2];
+ float *t2 = &m_textureCoordinates[triangle[1] * 2];
+ float *t3 = &m_textureCoordinates[triangle[2] * 2];
- // compute texture coordinates
- float u1, u2, u3, vv1, vv2, vv3;
- computeTextureCoordinates(v1, &u1, &vv1);
- computeTextureCoordinates(v2, &u2, &vv2);
- computeTextureCoordinates(v3, &u3, &vv3);
-
- glTexCoord2f(u1, vv1);
+ glTexCoord2f(t1[0], t1[1]);
glNormal3f(n1[0], n1[1], n1[2]);
glVertex3f(v1[0], v1[1], v1[2]);
- glTexCoord2f(u2, vv2);
+ glTexCoord2f(t2[0], t2[1]);
glNormal3f(n2[0], n2[1], n2[2]);
glVertex3f(v2[0], v2[1], v2[2]);
- glTexCoord2f(u3, vv3);
+ glTexCoord2f(t3[0], t3[1]);
glNormal3f(n3[0], n3[1], n3[2]);
glVertex3f(v3[0], v3[1], v3[2]);
}
View
@@ -13,7 +13,7 @@ class Sphere : public Shape {
protected:
int getTriangleCount();
int getVertexCount();
- void computeTextureCoordinates(float *vertex, float *u, float *v);
+ virtual void computeTextureCoordinates(float *pos, float *u, float *v);
int validateParam1(int param1) { return param1 < 2 ? 2 : param1; }
int validateParam2(int param2) { return param2 < 3 ? 3 : param2; }
View
@@ -97,8 +97,10 @@ void GLWidget::initializeResources()
m_planet = new Planet();
m_planet->setDetail(LOW);
-
- noise = new PerlinNoise(.5, .5);
+ m_planet->setTexture("/course/cs123/data/image/terrain/grass.JPG", 0);
+ m_planet->setTexture("/course/cs123/data/image/terrain/dirt.JPG", 1);
+ m_planet->setTexture("/course/cs123/data/image/terrain/snow.JPG", 2);
+ m_planet->setTexture("/course/cs123/data/image/terrain/rock.JPG", 3);
loadCubeMap();
cout << "Loaded cube map..." << endl;
@@ -147,15 +149,6 @@ void GLWidget::createShaderPrograms()
/**
Allocate framebuffer objects.
-Using OpenGL Version 4.1.0 NVIDIA 270.41.19
-
---- Loading Resources ---
-Loaded skybox...
-Loaded cube map...
-Loaded shader programs...
-Loaded framebuffer objects...
- --- Finish Loading Resources ---
-Unrecognised OpenGL version
@param width: the viewport width
@param height: the viewport height
**/
@@ -210,11 +203,10 @@ void GLWidget::paintGL()
int height = this->height();
// load a test texture
- glBindTexture(GL_TEXTURE_2D, ResourceLoader::loadTextureImage("/course/cs123/data/image/terrain/grass.JPG"));
+ glBindTexture(GL_TEXTURE_2D, m_planet->getTexture(0));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
- m_shaderPrograms["terrain"]->setUniformValue("testTexture", 0);
+ m_shaderPrograms["terrain"]->setUniformValue("tex1", 0);
// draw scene
applyPerspectiveCamera(width, height);
View
@@ -1,9 +1,11 @@
varying float height;
varying float intensity;
-uniform sampler2D testTexture;
+uniform sampler2D tex1;
+uniform sampler2D tex2;
+uniform sampler2D tex3;
+uniform sampler2D tex4;
void main() {
- //gl_FragColor = texture2D(testTexture, gl_TexCoord[0].st) * intensity;
- gl_FragColor = (height+.1)/.2 * vec4(1.0, 1.0, 1.0, 1.0);
+ gl_FragColor = texture2D(tex1, gl_TexCoord[0].st) * intensity;
}
@@ -21,7 +21,6 @@ GLuint ResourceLoader::loadTextureImage(const char *filename) {
QImage formattedImage = QGLWidget::convertToGLFormat(img);
// bind image to gl texture
- glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, formattedImage.width(),
formattedImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, formattedImage.bits());

0 comments on commit da7d3e1

Please sign in to comment.