前言:O3D程序基本结构

  1. 创建一个O3D对象
  2. 设置全局变量初始化通用库
  3. 创建一个O3D包管理对象
  4. 创建一个渲染图
  5. 设置绘图上下文(包括投影矩阵和观察转化矩阵)
  6. 创建材质效果对象,加载shader 文件或内容,付给材质效果对象
  7. 创建材质对象和形状对象,设置材质 draw list(透明或不透明)设置材质对象其他参数
  8. j加载一个矩阵变换(transforms)和形状对象(shapes)到转换图对象(transform graph).
  9. 为图元对象(primitives)创建一个draw elements.
  10. 设置渲染回调操作函数, 3D图形每渲染一次都会执行渲染回调函数。

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
Hello Cube: Getting started with O3D
</title>

<!--连接O3D 的js文件-->
<script type="text/javascript" src="o3djs/base.js"></script>
<script type="text/javascript">

//加载相关库
o3djs
.require('o3djs.util');
o3djs
.require('o3djs.math');
o3djs
.require('o3djs.rendergraph');

// javascript事件
// Run the init() function once the page has finished loading.
// Run the uninit() function when the page has is unloaded.
window
.onload = init;
window
.onunload = uninit;

// 创建全局变量
var g_o3d;
var g_math;
var g_client;
var g_pack;
var g_clock = 0;
var g_timeMult = 1;
var g_cubeTransform;
var g_finished = false; // for selenium testing


/**
* 渲染回调函数
* 每渲染一次形状的位置就变换一次.
* @param {o3d.RenderEvent} renderEvent The render event object that gives
* us the elapsed time since the last time a frame was rendered.
*/

function renderCallback(renderEvent) {
g_clock
+= renderEvent.elapsedTime * g_timeMult;
// Rotate the cube around the Y axis.
g_cubeTransform
.identity();
g_cubeTransform
.rotateY(2.0 * g_clock);
}


/**
* Creates the client area.
*/

function init() {
o3djs
.util.makeClients(initStep2);
}

/**
* Initializes O3D, creates the cube and sets up the transform and
* render graphs.
* @param {Array} clientElements Array of o3d object elements.
*/

function initStep2(clientElements) {
// 初始化全局变量和库.
var o3dElement = clientElements[0];
g_client
= o3dElement.client;
g_o3d
= o3dElement.o3d;
g_math
= o3djs.math;

// Initialize O3D sample libraries.
o3djs
.base.init(o3dElement);

// 创建包对象,材质、纹理效果等对象都是由包创建、管理和销毁.
g_pack
= g_client.createPack();

// 创建渲染图的视图窗口对象.
var viewInfo = o3djs.rendergraph.createBasicView(
g_pack
,
g_client
.root,
g_client
.renderGraphRoot);

// 设置视图窗口的投影矩阵(平行投影-orthographic或透视投影-perspective).

viewInfo
.drawContext.projection = g_math.matrix4.perspective(
g_math
.degToRad(30), // 30 度视野角度.
g_client
.width / g_client.height,//比例
1, // z轴近点剪切平面.
5000); // z轴远点剪切平面.

// 设置观察点的位置方向角度
// cube is located.
viewInfo
.drawContext.view = g_math.matrix4.lookAt([0, 1, 5], // eye
[0, 0, 0], // target
[0, 1, 0]); // up

//创建效果对象
var redEffect = g_pack.createObject('Effect');

//加载shader内容
var shaderString = document.getElementById('effect').value;
redEffect
.loadFromFXString(shaderString);

// 创建材质对象.
var redMaterial = g_pack.createObject('Material');

// 设置材质对象drawList的方式(透明-zOrderedDrawList或不透明-performanceDrawList).
redMaterial
.drawList = viewInfo.performanceDrawList;

// 应用效果给材质对象,效果对象提交shader文件给3D硬件处理
redMaterial
.effect = redEffect;

// 创建一个立方体,应用材质效果.
var cubeShape = o3djs.primitives.createCube(g_pack,redMaterial,1);

// 创建转换图节点,把立方体加入到转换节点
g_cubeTransform = g_pack.createObject('Transform');
g_cubeTransform
.addShape(cubeShape);

//将转换节点加入到根节点下
g_cubeTransform.parent = g_client.root;

// 开始对立方体进行渲染
cubeShape
.createDrawElements(g_pack, null);

// 执行渲染回调函数
g_client
.setRenderCallback(renderCallback);

g_finished
= true; // for selenium testing.
}

/**
* 清除所有对象.
*/

function uninit() {
if (g_client) {
g_client
.cleanup();
}
}

</script>
</head>
<body>
<h1>Hello Cube</h1>
This example shows how to display a spinning red cube in O3D.
<br/>


<!-- Start of O3D plugin 必须以O3D开头-->
<div id="o3d" style="width: 600px; height: 600px;"></div>
<!-- End of O3D plugin -->

<!-- shader内容,告诉3D硬件,如何渲染的内容-->
<div style="display:none">
<!-- Start of effect -->
<textarea id="effect">
// 世界、视口、投影矩阵 ,这三个矩阵和顶点坐标矩阵计算,得出顶点的屏幕坐标
float4x4 worldViewProjection : WorldViewProjection;

// 顶点数据结构,
struct VertexShaderInput {
float4 position : POSITION;
};

//像素数据结构
struct PixelShaderInput {
float4 position : POSITION;
};

/**
* 此函数输入顶点的相关数据,返回给像素数据结构*/
PixelShaderInput vertexShaderFunction(VertexShaderInput input) {
PixelShaderInput output;

// 计算顶点位置=顶点坐标*世界坐标*视口坐标*投影坐标
output.position = mul(input.position, worldViewProjection);
return output;
}

/**
* 返回像素着色颜色,都是红色。.
*/
float4 pixelShaderFunction(PixelShaderInput input): COLOR {
return float4(1, 0, 0, 1); // Red.
}

//以下三行是必须的留的,不是注释,告诉3D硬件-顶点着色程序的入口点、像素着色程序的入口点
// #o3d VertexShaderEntryPoint vertexShaderFunction
// #o3d PixelShaderEntryPoint pixelShaderFunction
// #o3d MatrixLoadOrder RowMajor
</textarea>
<!-- End of effect -->
</div>
</body>
</html>

以下是代码要求和代码,代码要求:在命令行界面提示用户输入正整数n(2<=n<=1000),找出不超过n的所有素数的集合,并按照10个一行进行格式化输出,输出的素数之间用逗号隔开,每一个素数占位4字符,不够4字符的用空格补齐。代码:/* Copyright© 2025 Shenzhen TP-LINK Technologies Co.Ltd. file work2.c brief This is a sample of coding criterion for prime checking. author Lin Zihao version 1.0.1 date 25Jul31 history \arg 1.0.1, 25Jul31, Lin Zihao, Create the file. */ #include <stdio.h> // /* DEFINES */ // #define MAX_RANGE 1000 /* 定义用户输入的最大范围 / #define MIN_RANGE 2 / 定义用户输入的最小范围 */ // /* TYPES */ // // /* EXTERN_PROTOTYPES */ // // /* LOCAL_PROTOTYPES */ // static int is_prime(int x); // /* VARIABLES */ // // /* LOCAL_FUNCTIONS */ // /* Function: is_prime Purpose: Checks whether a given integer is a prime number。 Parameters: n - The integer to be checked Return value: Returns 1 if the number is prime, 0 otherwise. / int is_prime(int x) { if (x <= 1) return 0; / Numbers less than 2 are not prime / for (int i = 2; ii<=x; i++) { /* Check only odd numbers up to n-1 / if (0 == x % i) return 0; / If divisible by any odd number, it’s not prime / } return 1; / If no divisors found, the number is prime */ } // /* PUBLIC_FUNCTIONS */ // // /* GLOBAL_FUNCTIONS */ // /* Function: main Purpose: Entry point of the program. Demonstrates the use of the is_prime function. Parameters: n - Represents the upper limit of the range within which the program identifies. dentifies. count - An integer counter used to manage the formatting of the output. Return value: 0 on successful execution, non-zero otherwise. Notes: This function is for demonstration purposes only. */ int main() { int n, count = 0; // Define variables: n for user input limit, count for tracking output formatting printf(“Enter a positive integer n: “); scanf(”%d”, &n); if(n <MIN_RANGE || n>MAX_RANGE){ /* Validate input range */ printf("Error!Please enter a number between 2 and 1000."); }else{ printf("Prime numbers up to %d are:\n", n); for (int i = 2; i <= n; i++) { if (is_prime(i)) { if (0 == count % 10 && 0 != count) { /* Start a new line after every 10 primes */ printf("\n"); } if (0 != count % 10) { /* Add comma separator except before the first number in a line */ printf(", "); } printf("%4d", i); /* Print prime number with 4-character width formatting */ count++; } } printf("\n"); } return 0; } 现在要写一个程序设计概要,目前已有初稿,现在请你修改润色下,写的更加详细深入些,并在概要设计中给出以下几个问题的回答:1、如何让用户使用起来更友好?2、如果n的输入范围【2,1000】发生变化,那任务性质会随之发生什么变化?3、考虑嵌入式环境,如何提高效率?程序效率和通用性的关系是怎样?——以下是初稿:2. 素数输出 2.1. 前言 2.1.1. 项目简要说明 实现素数筛选和格式化输出功能,输入正整数n(2≤n≤1000),输出所有不超过n的素数。 2.1.2. 任务概述 数学计算模块,需优化素数判断算法。 2.2. 需求分析 2.2.1. 功能需求 输入验证:2≤n≤1000 素数判断:识别不超过n的所有素数 格式化输出: 每行10个素数 逗号分隔 固定4字符宽度(右对齐) 2.3. 代码设计
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值